Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
NewTagNameDialog.java
Go to the documentation of this file.
1 /*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2011-2016 Basis Technology Corp.
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.casemodule.services;
20 
21 import java.awt.BorderLayout;
22 import java.awt.Dimension;
23 import java.awt.Toolkit;
24 import java.awt.event.WindowAdapter;
25 import java.awt.event.WindowEvent;
26 import javax.swing.JFrame;
27 import javax.swing.JOptionPane;
28 import javax.swing.event.DocumentEvent;
29 import javax.swing.event.DocumentListener;
30 import org.openide.util.NbBundle;
31 
32 final class NewTagNameDialog extends javax.swing.JDialog {
33 
34  private static final long serialVersionUID = 1L;
35  private String userTagDisplayName;
36  private BUTTON_PRESSED result;
37 
38  enum BUTTON_PRESSED {
39  OK, CANCEL;
40  }
41 
45  NewTagNameDialog() {
46  super(new JFrame(NbBundle.getMessage(NewTagNameDialog.class, "NewTagNameDialog.title.text")),
47  NbBundle.getMessage(NewTagNameDialog.class, "NewTagNameDialog.title.text"), true);
48  initComponents();
49  this.display();
50  }
51 
55  private void display() {
56  setLayout(new BorderLayout());
57 
58  /*
59  * Center the dialog
60  */
61  Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
62  int width = this.getSize().width;
63  int height = this.getSize().height;
64  setLocation((screenDimension.width - width) / 2, (screenDimension.height - height) / 2);
65 
66  /*
67  * Add a handler for when the dialog window is closed directly.
68  */
69  this.addWindowListener(new WindowAdapter() {
70  @Override
71  public void windowClosing(WindowEvent e) {
72  doButtonAction(false);
73  }
74  });
75 
76  /*
77  * Add a listener to enable the OK button when the text field changes.
78  */
79  tagNameTextField.getDocument().addDocumentListener(new DocumentListener() {
80  @Override
81  public void changedUpdate(DocumentEvent e) {
82  fire();
83  }
84  @Override
85  public void removeUpdate(DocumentEvent e) {
86  fire();
87  }
88  @Override
89  public void insertUpdate(DocumentEvent e) {
90  fire();
91  }
92  private void fire() {
93  enableOkButton();
94  }
95  });
96 
97  enableOkButton();
98 
99  /*
100  * Used to show the dialog.
101  */
102  setResizable(false);
103  setVisible(true);
104  }
105 
110  private void doButtonAction(boolean okPressed) {
111  if (okPressed) {
112  String newTagDisplayName = tagNameTextField.getText().trim();
113  if (newTagDisplayName.isEmpty()) {
114  JOptionPane.showMessageDialog(null,
115  NbBundle.getMessage(NewTagNameDialog.class, "NewTagNameDialog.JOptionPane.tagNameEmpty.message"),
116  NbBundle.getMessage(NewTagNameDialog.class, "NewTagNameDialog.JOptionPane.tagNameEmpty.title"),
117  JOptionPane.ERROR_MESSAGE);
118  return;
119  }
120  if (TagsManager.containsIllegalCharacters(newTagDisplayName)) {
121  JOptionPane.showMessageDialog(null,
122  NbBundle.getMessage(NewTagNameDialog.class, "NewTagNameDialog.JOptionPane.tagNameIllegalCharacters.message"),
123  NbBundle.getMessage(NewTagNameDialog.class, "NewTagNameDialog.JOptionPane.tagNameIllegalCharacters.title"),
124  JOptionPane.ERROR_MESSAGE);
125  return;
126  }
127  userTagDisplayName = newTagDisplayName;
128  result = BUTTON_PRESSED.OK;
129  } else {
130  result = BUTTON_PRESSED.CANCEL;
131  }
132  setVisible(false);
133  }
134 
140  String getTagName() {
141  return userTagDisplayName;
142  }
143 
149  BUTTON_PRESSED getResult() {
150  return result;
151  }
152 
158  private void enableOkButton() {
159  okButton.setEnabled(!tagNameTextField.getText().isEmpty());
160  getRootPane().setDefaultButton(okButton);
161  }
162 
168  @SuppressWarnings("unchecked")
169  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
170  private void initComponents() {
171 
172  newTagNameLabel = new javax.swing.JLabel();
173  tagNameTextField = new javax.swing.JTextField();
174  cancelButton = new javax.swing.JButton();
175  okButton = new javax.swing.JButton();
176 
177  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
178 
179  org.openide.awt.Mnemonics.setLocalizedText(newTagNameLabel, org.openide.util.NbBundle.getMessage(NewTagNameDialog.class, "NewTagNameDialog.newTagNameLabel.text")); // NOI18N
180 
181  tagNameTextField.setText(org.openide.util.NbBundle.getMessage(NewTagNameDialog.class, "NewTagNameDialog.tagNameTextField.text")); // NOI18N
182 
183  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(NewTagNameDialog.class, "NewTagNameDialog.cancelButton.text")); // NOI18N
184  cancelButton.addActionListener(new java.awt.event.ActionListener() {
185  public void actionPerformed(java.awt.event.ActionEvent evt) {
186  cancelButtonActionPerformed(evt);
187  }
188  });
189 
190  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(NewTagNameDialog.class, "NewTagNameDialog.okButton.text")); // NOI18N
191  okButton.addActionListener(new java.awt.event.ActionListener() {
192  public void actionPerformed(java.awt.event.ActionEvent evt) {
193  okButtonActionPerformed(evt);
194  }
195  });
196 
197  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
198  getContentPane().setLayout(layout);
199  layout.setHorizontalGroup(
200  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
201  .addGroup(layout.createSequentialGroup()
202  .addContainerGap()
203  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
204  .addComponent(tagNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
205  .addGroup(layout.createSequentialGroup()
206  .addGap(0, 0, Short.MAX_VALUE)
207  .addComponent(okButton)
208  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
209  .addComponent(cancelButton))
210  .addComponent(newTagNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
211  .addContainerGap())
212  );
213  layout.setVerticalGroup(
214  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
215  .addGroup(layout.createSequentialGroup()
216  .addContainerGap()
217  .addComponent(newTagNameLabel)
218  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
219  .addComponent(tagNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
220  .addContainerGap(50, Short.MAX_VALUE))
221  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
222  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
223  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
224  .addComponent(cancelButton)
225  .addComponent(okButton))
226  .addContainerGap())
227  );
228 
229  pack();
230  }// </editor-fold>//GEN-END:initComponents
231 
232  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
233  doButtonAction(true);
234  }//GEN-LAST:event_okButtonActionPerformed
235 
236  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
237  doButtonAction(false);
238  }//GEN-LAST:event_cancelButtonActionPerformed
239 
240 
241  // Variables declaration - do not modify//GEN-BEGIN:variables
242  private javax.swing.JButton cancelButton;
243  private javax.swing.JLabel newTagNameLabel;
244  private javax.swing.JButton okButton;
245  private javax.swing.JTextField tagNameTextField;
246  // End of variables declaration//GEN-END:variables
247 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon Apr 24 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.