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

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