Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddKeywordsDialog.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.keywordsearch;
20 
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.util.List;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import javax.swing.JFrame;
27 import javax.swing.JMenuItem;
28 import javax.swing.JPopupMenu;
29 import javax.swing.SwingUtilities;
30 import javax.swing.event.DocumentEvent;
31 import javax.swing.event.DocumentListener;
32 import org.openide.util.NbBundle;
33 import org.openide.windows.WindowManager;
34 
38 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
39 class AddKeywordsDialog extends javax.swing.JDialog {
40 
41  List<String> newKeywords = new ArrayList<>();
42  private javax.swing.JTextArea keywordTextArea;
50  AddKeywordsDialog() {
51  super((JFrame) WindowManager.getDefault().getMainWindow(),
52  NbBundle.getMessage(AddKeywordsDialog.class, "AddKeywordsDialog.addKeywordsTitle.text"),
53  true);
54  initComponents();
55  // Set the add button to only be active when there is text in the text area
56  addButton.setEnabled(false);
57  initKeywordTextArea();
58  }
59 
63  void display() {
64  newKeywords.clear();
65  setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
66  setVisible(true);
67  }
68 
69  private void initKeywordTextArea() {
70  keywordTextArea = new javax.swing.JTextArea() {
71  //Override the paste action for this jtext area to always append pasted text with a new line if necessary
72  @Override
73  public void paste() {
74  //if the cursor position is not at the start of a new line add the new line symbol before the pasted text
75  if (!(keywordTextArea.getDocument().getLength()==0) && !keywordTextArea.getText().endsWith("\n")) {
76  keywordTextArea.append(System.getProperty("line.separator"));
77  }
78  keywordTextArea.setCaretPosition(keywordTextArea.getDocument().getLength());
79  super.paste();
80  }
81  };
82  keywordTextArea.setColumns(
83  20);
84  keywordTextArea.setRows(
85  5);
86  keywordTextArea.addMouseListener(
87  new java.awt.event.MouseAdapter() {
88  @Override
89  public void mouseClicked(java.awt.event.MouseEvent evt
90  ) {
91  keywordTextAreaMouseClicked(evt);
92  }
93  }
94  );
95  jScrollPane1.setViewportView(keywordTextArea);
96 
97  keywordTextArea.getDocument()
98  .addDocumentListener(new DocumentListener() {
99  @Override
100  public void changedUpdate(DocumentEvent e
101  ) {
102  fire();
103  }
104 
105  @Override
106  public void removeUpdate(DocumentEvent e
107  ) {
108  fire();
109  }
110 
111  @Override
112  public void insertUpdate(DocumentEvent e
113  ) {
114  fire();
115  }
116 
117  private void fire() {
118  enableButtons();
119  }
120  }
121  );
122  }
123 
130  void setInitialKeywordList(String initialKeywords, boolean isLiteral, boolean isWholeWord) {
131  keywordTextArea.setText(initialKeywords);
132  if (!isLiteral) {
133  regexRadioButton.setSelected(true);
134  } else if (isWholeWord) {
135  exactRadioButton.setSelected(true);
136  } else {
137  substringRadioButton.setSelected(true);
138  }
139  }
140 
141  private void enableButtons() {
142  addButton.setEnabled(!keywordTextArea.getText().isEmpty());
143  }
144 
150  List<String> getKeywords() {
151  return newKeywords;
152  }
153 
159  boolean isKeywordRegex() {
160  return regexRadioButton.isSelected();
161  }
162 
168  boolean isKeywordExact() {
169  return exactRadioButton.isSelected();
170  }
171 
177  @SuppressWarnings("unchecked")
178  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
179  private void initComponents() {
180 
181  keywordTypeButtonGroup = new javax.swing.ButtonGroup();
182  exactRadioButton = new javax.swing.JRadioButton();
183  substringRadioButton = new javax.swing.JRadioButton();
184  regexRadioButton = new javax.swing.JRadioButton();
185  jScrollPane1 = new javax.swing.JScrollPane();
186  enterKeywordsLabel = new javax.swing.JLabel();
187  keywordTypeLabel = new javax.swing.JLabel();
188  addButton = new javax.swing.JButton();
189  cancelButton = new javax.swing.JButton();
190  pasteButton = new javax.swing.JButton();
191 
192  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
193 
194  keywordTypeButtonGroup.add(exactRadioButton);
195  exactRadioButton.setSelected(true);
196  org.openide.awt.Mnemonics.setLocalizedText(exactRadioButton, org.openide.util.NbBundle.getMessage(AddKeywordsDialog.class, "AddKeywordsDialog.exactRadioButton.text")); // NOI18N
197 
198  keywordTypeButtonGroup.add(substringRadioButton);
199  org.openide.awt.Mnemonics.setLocalizedText(substringRadioButton, org.openide.util.NbBundle.getMessage(AddKeywordsDialog.class, "AddKeywordsDialog.substringRadioButton.text")); // NOI18N
200 
201  keywordTypeButtonGroup.add(regexRadioButton);
202  org.openide.awt.Mnemonics.setLocalizedText(regexRadioButton, org.openide.util.NbBundle.getMessage(AddKeywordsDialog.class, "AddKeywordsDialog.regexRadioButton.text")); // NOI18N
203 
204  org.openide.awt.Mnemonics.setLocalizedText(enterKeywordsLabel, org.openide.util.NbBundle.getMessage(AddKeywordsDialog.class, "AddKeywordsDialog.enterKeywordsLabel.text")); // NOI18N
205 
206  org.openide.awt.Mnemonics.setLocalizedText(keywordTypeLabel, org.openide.util.NbBundle.getMessage(AddKeywordsDialog.class, "AddKeywordsDialog.keywordTypeLabel.text")); // NOI18N
207 
208  org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(AddKeywordsDialog.class, "AddKeywordsDialog.addButton.text")); // NOI18N
209  addButton.addActionListener(new java.awt.event.ActionListener() {
210  public void actionPerformed(java.awt.event.ActionEvent evt) {
211  addButtonActionPerformed(evt);
212  }
213  });
214 
215  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(AddKeywordsDialog.class, "AddKeywordsDialog.cancelButton.text")); // NOI18N
216  cancelButton.addActionListener(new java.awt.event.ActionListener() {
217  public void actionPerformed(java.awt.event.ActionEvent evt) {
218  cancelButtonActionPerformed(evt);
219  }
220  });
221 
222  org.openide.awt.Mnemonics.setLocalizedText(pasteButton, org.openide.util.NbBundle.getMessage(AddKeywordsDialog.class, "AddKeywordsDialog.pasteButton.text")); // NOI18N
223  pasteButton.addActionListener(new java.awt.event.ActionListener() {
224  public void actionPerformed(java.awt.event.ActionEvent evt) {
225  pasteButtonActionPerformed(evt);
226  }
227  });
228 
229  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
230  getContentPane().setLayout(layout);
231  layout.setHorizontalGroup(
232  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
233  .addGroup(layout.createSequentialGroup()
234  .addContainerGap()
235  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
236  .addComponent(enterKeywordsLabel)
237  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
238  .addComponent(pasteButton))
239  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
240  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
241  .addGroup(layout.createSequentialGroup()
242  .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
243  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
244  .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
245  .addComponent(keywordTypeLabel)
246  .addGroup(layout.createSequentialGroup()
247  .addGap(10, 10, 10)
248  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
249  .addComponent(substringRadioButton)
250  .addComponent(exactRadioButton)
251  .addComponent(regexRadioButton))))
252  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
253  );
254  layout.setVerticalGroup(
255  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
256  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
257  .addContainerGap()
258  .addComponent(enterKeywordsLabel)
259  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
260  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
261  .addGroup(layout.createSequentialGroup()
262  .addComponent(keywordTypeLabel)
263  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
264  .addComponent(exactRadioButton)
265  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
266  .addComponent(substringRadioButton)
267  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
268  .addComponent(regexRadioButton)
269  .addGap(194, 194, 194))
270  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
271  .addGap(0, 0, Short.MAX_VALUE)
272  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
273  .addGap(5, 5, 5)))
274  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
275  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
276  .addComponent(addButton)
277  .addComponent(cancelButton))
278  .addComponent(pasteButton))
279  .addContainerGap())
280  );
281 
282  pack();
283  }// </editor-fold>//GEN-END:initComponents
284 
285  private void pasteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasteButtonActionPerformed
286  keywordTextArea.paste();
287  }//GEN-LAST:event_pasteButtonActionPerformed
288 
289  private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
290  // Save the values from the list
291  newKeywords.addAll(Arrays.asList(keywordTextArea.getText().split("\\r?\\n")));
292 
293  setVisible(false);
294  dispose();
295  }//GEN-LAST:event_addButtonActionPerformed
296 
297  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
298  setVisible(false);
299  dispose();
300  }//GEN-LAST:event_cancelButtonActionPerformed
301 
302  private void keywordTextAreaMouseClicked(java.awt.event.MouseEvent evt) {
303  if (SwingUtilities.isRightMouseButton(evt)) {
304  JPopupMenu popup = new JPopupMenu();
305 
306  JMenuItem cutMenu = new JMenuItem("Cut"); // NON-NLS
307  cutMenu.addActionListener(new ActionListener() {
308  @Override
309  public void actionPerformed(ActionEvent e) {
310  keywordTextArea.cut();
311  }
312  });
313 
314  JMenuItem copyMenu = new JMenuItem("Copy"); // NON-NLS
315  copyMenu.addActionListener(new ActionListener() {
316  @Override
317  public void actionPerformed(ActionEvent e) {
318  keywordTextArea.copy();
319  }
320  });
321 
322  JMenuItem pasteMenu = new JMenuItem("Paste"); // NON-NLS
323  pasteMenu.addActionListener(new ActionListener() {
324  @Override
325  public void actionPerformed(ActionEvent e) {
326  keywordTextArea.paste();
327  }
328  });
329 
330  popup.add(cutMenu);
331  popup.add(copyMenu);
332  popup.add(pasteMenu);
333  popup.show(keywordTextArea, evt.getX(), evt.getY());
334  }
335  }
336  // Variables declaration - do not modify//GEN-BEGIN:variables
337  private javax.swing.JButton addButton;
338  private javax.swing.JButton cancelButton;
339  private javax.swing.JLabel enterKeywordsLabel;
340  private javax.swing.JRadioButton exactRadioButton;
341  private javax.swing.JScrollPane jScrollPane1;
342  private javax.swing.ButtonGroup keywordTypeButtonGroup;
343  private javax.swing.JLabel keywordTypeLabel;
344  private javax.swing.JButton pasteButton;
345  private javax.swing.JRadioButton regexRadioButton;
346  private javax.swing.JRadioButton substringRadioButton;
347  // End of variables declaration//GEN-END:variables
348 }

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.