Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
TagNameDialog.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 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.event.WindowAdapter;
23 import java.awt.event.WindowEvent;
24 import javax.swing.JFrame;
25 import javax.swing.JOptionPane;
26 import javax.swing.event.DocumentEvent;
27 import javax.swing.event.DocumentListener;
28 import org.openide.util.NbBundle;
29 import org.openide.util.NbBundle.Messages;
30 import org.openide.windows.WindowManager;
31 import org.sleuthkit.datamodel.TskData;
32 
33 @Messages({"TagNameDialog.descriptionLabel.text=Description:",
34  "TagNameDialog.notableCheckbox.text=Tag indicates item is notable."})
35 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
36 final class TagNameDialog extends javax.swing.JDialog {
37 
38  private static final long serialVersionUID = 1L;
39  private String userTagDisplayName;
40  private String userTagDescription;
41  private boolean userTagIsNotable;
42  private BUTTON_PRESSED result;
43 
44  enum BUTTON_PRESSED {
45  OK, CANCEL;
46  }
47 
51  TagNameDialog() {
52  super(new JFrame(NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.title.text")),
53  NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.title.text"), true);
54  initComponents();
55  this.display();
56  }
57 
58  @Messages({"TagNameDialog.editTitle.text=Edit Tag"})
59  TagNameDialog(TagNameDefinition tagNameToEdit) {
60  super(new JFrame(NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.editTitle.text")),
61  NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.editTitle.text"), true);
62  initComponents();
63  tagNameTextField.setText(tagNameToEdit.getDisplayName());
64  descriptionTextArea.setText(tagNameToEdit.getDescription());
65  notableCheckbox.setSelected(tagNameToEdit.getKnownStatus() == TskData.FileKnown.BAD);
66  tagNameTextField.setEnabled(false);
67  this.display();
68  }
69 
73  private void display() {
74  setLayout(new BorderLayout());
75 
76  /*
77  * Center the dialog
78  */
79  setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
80 
81  /*
82  * Add a handler for when the dialog window is closed directly.
83  */
84  this.addWindowListener(new WindowAdapter() {
85  @Override
86  public void windowClosing(WindowEvent e) {
87  doButtonAction(false);
88  }
89  });
90 
91  /*
92  * Add a listener to enable the OK button when the text field changes.
93  */
94  tagNameTextField.getDocument().addDocumentListener(new DocumentListener() {
95  @Override
96  public void changedUpdate(DocumentEvent e) {
97  fire();
98  }
99 
100  @Override
101  public void removeUpdate(DocumentEvent e) {
102  fire();
103  }
104 
105  @Override
106  public void insertUpdate(DocumentEvent e) {
107  fire();
108  }
109 
110  private void fire() {
111  enableOkButton();
112  }
113  });
114 
115  enableOkButton();
116 
117  /*
118  * Used to show the dialog.
119  */
120  setResizable(false);
121  setVisible(true);
122  }
123 
129  @Messages({"TagNameDialog.JOptionPane.tagDescriptionIllegalCharacters.message=Tag descriptions may not contain commas (,) or semicolons (;)",
130  "TagNameDialog.JOptionPane.tagDescriptionIllegalCharacters.title=Invalid character in tag description"})
131  private void doButtonAction(boolean okPressed) {
132  if (okPressed) {
133  String newTagDisplayName = tagNameTextField.getText().trim();
134  String descriptionText = descriptionTextArea.getText();
135  if (newTagDisplayName.isEmpty()) {
136  JOptionPane.showMessageDialog(this,
137  NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.JOptionPane.tagNameEmpty.message"),
138  NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.JOptionPane.tagNameEmpty.title"),
139  JOptionPane.ERROR_MESSAGE);
140  return;
141  }
142  //if a tag name contains illegal characters and is not the name of one of the standard tags
143  if (TagsManager.containsIllegalCharacters(newTagDisplayName) && !TagNameDefinition.getStandardTagNames().contains(newTagDisplayName)) {
144  JOptionPane.showMessageDialog(this,
145  NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.JOptionPane.tagDescriptionIllegalCharacters.message"),
146  NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.JOptionPane.tagDescriptionIllegalCharacters.title"),
147  JOptionPane.ERROR_MESSAGE);
148  return;
149  } else if (descriptionText.contains(",")
150  || descriptionText.contains(";")) {
151  JOptionPane.showMessageDialog(this,
152  NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.JOptionPane.tagDescriptionIllegalCharacters.message"),
153  NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.JOptionPane.tagDescriptionIllegalCharacters.title"),
154  JOptionPane.ERROR_MESSAGE);
155  return;
156  }
157 
158  userTagDescription = descriptionTextArea.getText();
159  userTagDisplayName = newTagDisplayName;
160  userTagIsNotable = notableCheckbox.isSelected();
161  result = BUTTON_PRESSED.OK;
162  } else {
163  result = BUTTON_PRESSED.CANCEL;
164  }
165  setVisible(false);
166  }
167 
173  String getTagName() {
174  return userTagDisplayName;
175  }
176 
177  String getTagDesciption() {
178  return userTagDescription;
179  }
180 
181  boolean isTagNotable() {
182  return userTagIsNotable;
183  }
184 
190  BUTTON_PRESSED getResult() {
191  return result;
192  }
193 
199  private void enableOkButton() {
200  okButton.setEnabled(!tagNameTextField.getText().isEmpty());
201  getRootPane().setDefaultButton(okButton);
202  }
203 
209  @SuppressWarnings("unchecked")
210  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
211  private void initComponents() {
212 
213  newTagNameLabel = new javax.swing.JLabel();
214  tagNameTextField = new javax.swing.JTextField();
215  cancelButton = new javax.swing.JButton();
216  okButton = new javax.swing.JButton();
217  descriptionScrollPane = new javax.swing.JScrollPane();
218  descriptionTextArea = new javax.swing.JTextArea();
219  descriptionLabel = new javax.swing.JLabel();
220  notableCheckbox = new javax.swing.JCheckBox();
221 
222  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
223 
224  org.openide.awt.Mnemonics.setLocalizedText(newTagNameLabel, org.openide.util.NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.newTagNameLabel.text")); // NOI18N
225 
226  tagNameTextField.setText(org.openide.util.NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.tagNameTextField.text")); // NOI18N
227 
228  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.cancelButton.text")); // NOI18N
229  cancelButton.addActionListener(new java.awt.event.ActionListener() {
230  public void actionPerformed(java.awt.event.ActionEvent evt) {
231  cancelButtonActionPerformed(evt);
232  }
233  });
234 
235  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.okButton.text")); // NOI18N
236  okButton.addActionListener(new java.awt.event.ActionListener() {
237  public void actionPerformed(java.awt.event.ActionEvent evt) {
238  okButtonActionPerformed(evt);
239  }
240  });
241 
242  descriptionTextArea.setColumns(20);
243  descriptionTextArea.setRows(3);
244  descriptionScrollPane.setViewportView(descriptionTextArea);
245 
246  org.openide.awt.Mnemonics.setLocalizedText(descriptionLabel, org.openide.util.NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.descriptionLabel.text")); // NOI18N
247 
248  org.openide.awt.Mnemonics.setLocalizedText(notableCheckbox, org.openide.util.NbBundle.getMessage(TagNameDialog.class, "TagNameDialog.notableCheckbox.text")); // NOI18N
249 
250  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
251  getContentPane().setLayout(layout);
252  layout.setHorizontalGroup(
253  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
254  .addGroup(layout.createSequentialGroup()
255  .addContainerGap()
256  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
257  .addComponent(tagNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
258  .addGroup(layout.createSequentialGroup()
259  .addGap(0, 0, Short.MAX_VALUE)
260  .addComponent(okButton)
261  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
262  .addComponent(cancelButton))
263  .addComponent(newTagNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
264  .addComponent(descriptionScrollPane, javax.swing.GroupLayout.Alignment.TRAILING)
265  .addGroup(layout.createSequentialGroup()
266  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
267  .addComponent(notableCheckbox)
268  .addComponent(descriptionLabel))
269  .addGap(0, 0, Short.MAX_VALUE)))
270  .addContainerGap())
271  );
272  layout.setVerticalGroup(
273  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
274  .addGroup(layout.createSequentialGroup()
275  .addContainerGap()
276  .addComponent(newTagNameLabel)
277  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
278  .addComponent(tagNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
279  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
280  .addComponent(descriptionLabel)
281  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
282  .addComponent(descriptionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
283  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
284  .addComponent(notableCheckbox)
285  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
286  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
287  .addComponent(cancelButton)
288  .addComponent(okButton)))
289  );
290 
291  pack();
292  }// </editor-fold>//GEN-END:initComponents
293 
294  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
295  doButtonAction(true);
296  }//GEN-LAST:event_okButtonActionPerformed
297 
298  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
299  doButtonAction(false);
300  }//GEN-LAST:event_cancelButtonActionPerformed
301 
302 
303  // Variables declaration - do not modify//GEN-BEGIN:variables
304  private javax.swing.JButton cancelButton;
305  private javax.swing.JLabel descriptionLabel;
306  private javax.swing.JScrollPane descriptionScrollPane;
307  private javax.swing.JTextArea descriptionTextArea;
308  private javax.swing.JLabel newTagNameLabel;
309  private javax.swing.JCheckBox notableCheckbox;
310  private javax.swing.JButton okButton;
311  private javax.swing.JTextField tagNameTextField;
312  // End of variables declaration//GEN-END:variables
313 }

Copyright © 2012-2022 Basis Technology. Generated on: Wed Oct 5 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.