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

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.