Autopsy  4.5.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
GlobalEditListPanel.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.EventQueue;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.logging.Level;
27 import java.util.regex.Pattern;
28 import java.util.regex.PatternSyntaxException;
29 import javax.swing.JTable;
30 import javax.swing.ListSelectionModel;
31 import javax.swing.event.ListSelectionEvent;
32 import javax.swing.event.ListSelectionListener;
33 import javax.swing.table.AbstractTableModel;
34 import javax.swing.table.TableColumn;
35 import org.netbeans.spi.options.OptionsPanelController;
36 import org.openide.util.NbBundle;
40 
44 class GlobalEditListPanel extends javax.swing.JPanel implements ListSelectionListener, OptionsPanel {
45 
46  private static final Logger logger = Logger.getLogger(GlobalEditListPanel.class.getName());
47  private static final long serialVersionUID = 1L;
48  private final KeywordTableModel tableModel;
49  private KeywordList currentKeywordList;
50 
54  GlobalEditListPanel() {
55  tableModel = new KeywordTableModel();
56  initComponents();
57  customizeComponents();
58  }
59 
60  private void customizeComponents() {
61  newKeywordsButton.setToolTipText((NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.customizeComponents.addWordToolTip")));
62  deleteWordButton.setToolTipText(NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.customizeComponents.removeSelectedMsg"));
63 
64  keywordTable.getParent().setBackground(keywordTable.getBackground());
65  final int width = jScrollPane1.getPreferredSize().width;
66  keywordTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
67  TableColumn column;
68  for (int i = 0; i < keywordTable.getColumnCount(); i++) {
69  column = keywordTable.getColumnModel().getColumn(i);
70  if (i == 0) {
71  column.setPreferredWidth(((int) (width * 0.90)));
72  } else {
73  column.setPreferredWidth(((int) (width * 0.10)));
74  }
75  }
76  keywordTable.setCellSelectionEnabled(false);
77  keywordTable.setRowSelectionAllowed(true);
78 
79  final ListSelectionModel lsm = keywordTable.getSelectionModel();
80  lsm.addListSelectionListener(new ListSelectionListener() {
81  @Override
82  public void valueChanged(ListSelectionEvent e) {
83  boolean canDelete = !(lsm.isSelectionEmpty() || currentKeywordList.isEditable() || IngestManager.getInstance().isIngestRunning());
84  boolean canEdit = canDelete && (lsm.getMaxSelectionIndex() == lsm.getMinSelectionIndex()); //edit only enabled with single selection
85  deleteWordButton.setEnabled(canDelete);
86  editWordButton.setEnabled(canEdit);
87  }
88  });
89 
90  setButtonStates();
91 
92  IngestManager.getInstance().addIngestJobEventListener(new PropertyChangeListener() {
93  @Override
94  public void propertyChange(PropertyChangeEvent evt) {
95  Object source = evt.getSource();
96  if (source instanceof String && ((String) source).equals("LOCAL")) { //NON-NLS
97  EventQueue.invokeLater(() -> {
98  setButtonStates();
99  });
100  }
101  }
102  });
103  }
104 
108  void setButtonStates() {
109  boolean isIngestRunning = IngestManager.getInstance().isIngestRunning();
110  boolean isListSelected = currentKeywordList != null;
111 
112  // items that only need a selected list
113  boolean canEditList = isListSelected && !isIngestRunning;
114  ingestMessagesCheckbox.setEnabled(canEditList);
115  ingestMessagesCheckbox.setSelected(currentKeywordList != null && currentKeywordList.getIngestMessages());
116 
117  // items that need an unlocked list w/out ingest running
118  boolean canAddWord = canEditList && !currentKeywordList.isEditable();
119  newKeywordsButton.setEnabled(canAddWord);
120 
121  // items that need a non-empty list
122  if ((currentKeywordList == null) || (currentKeywordList.getKeywords().isEmpty())) {
123  deleteWordButton.setEnabled(false);
124  editWordButton.setEnabled(false);
125  }
126  }
127 
128  @NbBundle.Messages("GlobalEditListPanel.editKeyword.title=Edit Keyword")
135  private boolean addKeywordsAction(String existingKeywords, boolean isLiteral, boolean isWholeWord) {
136  String keywordsToRedisplay = existingKeywords;
137  AddKeywordsDialog dialog = new AddKeywordsDialog();
138 
139  int goodCount = 0;
140  int dupeCount = 0;
141  int badCount = 1; // Default to 1 so we enter the loop the first time
142 
143  if (!existingKeywords.isEmpty()) { //if there is an existing keyword then this action was called by the edit button
144  dialog.setTitle(NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.editKeyword.title"));
145  }
146  while (badCount > 0) {
147  dialog.setInitialKeywordList(keywordsToRedisplay, isLiteral, isWholeWord);
148  dialog.display();
149 
150  goodCount = 0;
151  dupeCount = 0;
152  badCount = 0;
153  keywordsToRedisplay = "";
154 
155  if (!dialog.getKeywords().isEmpty()) {
156 
157  for (String newWord : dialog.getKeywords()) {
158  if (newWord.isEmpty()) {
159  continue;
160  }
161 
162  final Keyword keyword = new Keyword(newWord, !dialog.isKeywordRegex(), dialog.isKeywordExact(), currentKeywordList.getName(), newWord);
163  if (currentKeywordList.hasKeyword(keyword)) {
164  dupeCount++;
165  continue;
166  }
167 
168  //check if valid
169  boolean valid = true;
170  try {
171  Pattern.compile(newWord);
172  } catch (PatternSyntaxException ex1) {
173  valid = false;
174  } catch (IllegalArgumentException ex2) {
175  valid = false;
176  }
177  if (!valid) {
178 
179  // Invalid keywords will reappear in the UI
180  keywordsToRedisplay += newWord + "\n";
181  badCount++;
182  continue;
183  }
184 
185  // Add the new keyword
186  tableModel.addKeyword(keyword);
187  goodCount++;
188  }
189  XmlKeywordSearchList.getCurrent().addList(currentKeywordList);
190  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
191 
192  if ((badCount > 0) || (dupeCount > 0)) {
193  // Display the error counts to the user
194  // The add keywords dialog will pop up again if any were invalid with any
195  // invalid entries (valid entries and dupes will disappear)
196 
197  String summary = "";
198  KeywordSearchUtil.DIALOG_MESSAGE_TYPE level = KeywordSearchUtil.DIALOG_MESSAGE_TYPE.INFO;
199  if (goodCount > 0) {
200  if (goodCount > 1) {
201  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordsAddedPlural.text", goodCount) + "\n";
202  } else {
203  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordsAdded.text", goodCount) + "\n";
204  }
205  }
206  if (dupeCount > 0) {
207  if (dupeCount > 1) {
208  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordDupesSkippedPlural.text", dupeCount) + "\n";
209  } else {
210  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordDupesSkipped.text", dupeCount) + "\n";
211  }
212  level = KeywordSearchUtil.DIALOG_MESSAGE_TYPE.WARN;
213  }
214  if (badCount > 0) {
215  if (badCount > 1) {
216  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordErrorsPlural.text", badCount) + "\n";
217  } else {
218  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordErrors.text", badCount) + "\n";
219  }
220  level = KeywordSearchUtil.DIALOG_MESSAGE_TYPE.ERROR;
221  }
222  KeywordSearchUtil.displayDialog(NbBundle.getMessage(this.getClass(), "GlobalEditListPanel.addKeywordResults.text"),
223  summary, level);
224  }
225  }
226  }
227  setFocusOnKeywordTextBox();
228  setButtonStates();
229  return (goodCount >= 1 && dupeCount == 0);
230  }
231 
238  private void deleteKeywordAction(int[] selectedKeywords) {
239  tableModel.deleteSelected(selectedKeywords);
240  XmlKeywordSearchList.getCurrent().addList(currentKeywordList);
241  setButtonStates();
242  }
243 
249  @SuppressWarnings("unchecked")
250  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
251  private void initComponents() {
252 
253  listEditorPanel = new javax.swing.JPanel();
254  jScrollPane1 = new javax.swing.JScrollPane();
255  keywordTable = new javax.swing.JTable();
256  ingestMessagesCheckbox = new javax.swing.JCheckBox();
257  keywordsLabel = new javax.swing.JLabel();
258  newKeywordsButton = new javax.swing.JButton();
259  deleteWordButton = new javax.swing.JButton();
260  editWordButton = new javax.swing.JButton();
261 
262  setMinimumSize(new java.awt.Dimension(0, 0));
263 
264  listEditorPanel.setMinimumSize(new java.awt.Dimension(0, 0));
265 
266  jScrollPane1.setPreferredSize(new java.awt.Dimension(340, 300));
267 
268  keywordTable.setModel(tableModel);
269  keywordTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
270  keywordTable.setGridColor(new java.awt.Color(153, 153, 153));
271  keywordTable.setMaximumSize(new java.awt.Dimension(30000, 30000));
272  keywordTable.getTableHeader().setReorderingAllowed(false);
273  jScrollPane1.setViewportView(keywordTable);
274 
275  ingestMessagesCheckbox.setSelected(true);
276  ingestMessagesCheckbox.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.ingestMessagesCheckbox.text")); // NOI18N
277  ingestMessagesCheckbox.setToolTipText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.ingestMessagesCheckbox.toolTipText")); // NOI18N
278  ingestMessagesCheckbox.addActionListener(new java.awt.event.ActionListener() {
279  public void actionPerformed(java.awt.event.ActionEvent evt) {
280  ingestMessagesCheckboxActionPerformed(evt);
281  }
282  });
283 
284  keywordsLabel.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.keywordsLabel.text")); // NOI18N
285 
286  newKeywordsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/add16.png"))); // NOI18N
287  newKeywordsButton.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.newKeywordsButton.text")); // NOI18N
288  newKeywordsButton.addActionListener(new java.awt.event.ActionListener() {
289  public void actionPerformed(java.awt.event.ActionEvent evt) {
290  newKeywordsButtonActionPerformed(evt);
291  }
292  });
293 
294  deleteWordButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/delete16.png"))); // NOI18N
295  deleteWordButton.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.deleteWordButton.text")); // NOI18N
296  deleteWordButton.addActionListener(new java.awt.event.ActionListener() {
297  public void actionPerformed(java.awt.event.ActionEvent evt) {
298  deleteWordButtonActionPerformed(evt);
299  }
300  });
301 
302  editWordButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/edit16.png"))); // NOI18N
303  editWordButton.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.editWordButton.text")); // NOI18N
304  editWordButton.addActionListener(new java.awt.event.ActionListener() {
305  public void actionPerformed(java.awt.event.ActionEvent evt) {
306  editWordButtonActionPerformed(evt);
307  }
308  });
309 
310  javax.swing.GroupLayout listEditorPanelLayout = new javax.swing.GroupLayout(listEditorPanel);
311  listEditorPanel.setLayout(listEditorPanelLayout);
312  listEditorPanelLayout.setHorizontalGroup(
313  listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
314  .addGroup(listEditorPanelLayout.createSequentialGroup()
315  .addContainerGap()
316  .addGroup(listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
317  .addGroup(listEditorPanelLayout.createSequentialGroup()
318  .addComponent(keywordsLabel)
319  .addGap(0, 0, Short.MAX_VALUE))
320  .addGroup(listEditorPanelLayout.createSequentialGroup()
321  .addGap(10, 10, 10)
322  .addGroup(listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
323  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
324  .addGroup(listEditorPanelLayout.createSequentialGroup()
325  .addGroup(listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
326  .addComponent(ingestMessagesCheckbox)
327  .addGroup(listEditorPanelLayout.createSequentialGroup()
328  .addComponent(newKeywordsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
329  .addGap(14, 14, 14)
330  .addComponent(editWordButton, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
331  .addGap(14, 14, 14)
332  .addComponent(deleteWordButton, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
333  .addGap(0, 0, Short.MAX_VALUE)))))
334  .addContainerGap())
335  );
336 
337  listEditorPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deleteWordButton, editWordButton, newKeywordsButton});
338 
339  listEditorPanelLayout.setVerticalGroup(
340  listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
341  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, listEditorPanelLayout.createSequentialGroup()
342  .addContainerGap()
343  .addComponent(keywordsLabel)
344  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
345  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
346  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
347  .addGroup(listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
348  .addComponent(deleteWordButton)
349  .addComponent(newKeywordsButton)
350  .addComponent(editWordButton))
351  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
352  .addComponent(ingestMessagesCheckbox)
353  .addGap(9, 9, 9))
354  );
355 
356  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
357  this.setLayout(layout);
358  layout.setHorizontalGroup(
359  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
360  .addComponent(listEditorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
361  );
362  layout.setVerticalGroup(
363  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
364  .addGroup(layout.createSequentialGroup()
365  .addComponent(listEditorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
366  .addGap(5, 5, 5))
367  );
368  }// </editor-fold>//GEN-END:initComponents
369 
370  private void deleteWordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteWordButtonActionPerformed
371  if (KeywordSearchUtil.displayConfirmDialog(NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.removeKwMsg"),
372  NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.deleteWordButtonActionPerformed.delConfirmMsg"),
373  KeywordSearchUtil.DIALOG_MESSAGE_TYPE.WARN)) {
374  deleteKeywordAction(keywordTable.getSelectedRows());
375  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
376  }
377  }//GEN-LAST:event_deleteWordButtonActionPerformed
378 
379  private void ingestMessagesCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ingestMessagesCheckboxActionPerformed
380  currentKeywordList.setIngestMessages(ingestMessagesCheckbox.isSelected());
381  XmlKeywordSearchList updater = XmlKeywordSearchList.getCurrent();
382  updater.addList(currentKeywordList);
383  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
384  }//GEN-LAST:event_ingestMessagesCheckboxActionPerformed
385 
386  private void newKeywordsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newKeywordsButtonActionPerformed
387  addKeywordsAction("", true, true);
388  }//GEN-LAST:event_newKeywordsButtonActionPerformed
389 
390  private void editWordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editWordButtonActionPerformed
391  int[] selectedKeywords = keywordTable.getSelectedRows();
392  if (selectedKeywords.length == 1) {
393  Keyword currentKeyword = currentKeywordList.getKeywords().get(selectedKeywords[0]);
394  if (addKeywordsAction(currentKeyword.getSearchTerm(), currentKeyword.searchTermIsLiteral(), currentKeyword.searchTermIsWholeWord())) {
395  deleteKeywordAction(selectedKeywords);
396  }
397  }
398  }//GEN-LAST:event_editWordButtonActionPerformed
399 
400  // Variables declaration - do not modify//GEN-BEGIN:variables
401  private javax.swing.JButton deleteWordButton;
402  private javax.swing.JButton editWordButton;
403  private javax.swing.JCheckBox ingestMessagesCheckbox;
404  private javax.swing.JScrollPane jScrollPane1;
405  private javax.swing.JTable keywordTable;
406  private javax.swing.JLabel keywordsLabel;
407  private javax.swing.JPanel listEditorPanel;
408  private javax.swing.JButton newKeywordsButton;
409  // End of variables declaration//GEN-END:variables
410 
411  @Override
412  public void valueChanged(ListSelectionEvent e) {
413  //respond to list selection changes in KeywordSearchListManagementPanel
414  ListSelectionModel listSelectionModel = (ListSelectionModel) e.getSource();
415  currentKeywordList = null;
416  if (!listSelectionModel.isSelectionEmpty()) {
417  XmlKeywordSearchList loader = XmlKeywordSearchList.getCurrent();
418  if (listSelectionModel.getMinSelectionIndex() == listSelectionModel.getMaxSelectionIndex()) {
419  currentKeywordList = loader.getListsL(false).get(listSelectionModel.getMinSelectionIndex());
420  }
421  }
422  tableModel.resync();
423  setButtonStates();
424  }
425 
426  @Override
427  public void store() {
428  // Implemented by parent panel
429  }
430 
431  @Override
432  public void load() {
433  // Implemented by parent panel
434  }
435 
436  KeywordList getCurrentKeywordList() {
437  return currentKeywordList;
438  }
439 
440  void setCurrentKeywordList(KeywordList list) {
441  currentKeywordList = list;
442  }
443 
444  private class KeywordTableModel extends AbstractTableModel {
445 
446  @Override
447  public int getColumnCount() {
448  return 2;
449  }
450 
451  @Override
452  public int getRowCount() {
453  return currentKeywordList == null ? 0 : currentKeywordList.getKeywords().size();
454  }
455 
456  @Override
457  public String getColumnName(int column) {
458  String colName = null;
459 
460  switch (column) {
461  case 0:
462  colName = NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.kwColName");
463  break;
464  case 1:
465  colName = NbBundle.getMessage(this.getClass(), "KeywordSearch.typeColLbl");
466  break;
467  default:
468  ;
469  }
470  return colName;
471  }
472 
473  @Override
474  public Object getValueAt(int rowIndex, int columnIndex) {
475  Object ret = null;
476  if (currentKeywordList == null) {
477  return "";
478  }
479  Keyword word = currentKeywordList.getKeywords().get(rowIndex);
480  switch (columnIndex) {
481  case 0:
482  ret = word.getSearchTerm();
483  break;
484  case 1:
485  ret = word.getSearchTermType();
486  break;
487  default:
488  logger.log(Level.SEVERE, "Invalid table column index: {0}", columnIndex); //NON-NLS
489  break;
490  }
491  return ret;
492  }
493 
494  @Override
495  public boolean isCellEditable(int rowIndex, int columnIndex) {
496  return false;
497  }
498 
499  @Override
500  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
501  }
502 
503  @Override
504  public Class<?> getColumnClass(int c) {
505  return getValueAt(0, c).getClass();
506  }
507 
508  void addKeyword(Keyword keyword) {
509  if (!currentKeywordList.hasKeyword(keyword)) {
510  currentKeywordList.getKeywords().add(keyword);
511  }
512  fireTableDataChanged();
513  }
514 
515  void resync() {
516  fireTableDataChanged();
517  }
518 
519  //delete selected from handle, events are fired from the handle
520  void deleteSelected(int[] selected) {
521  List<Keyword> words = currentKeywordList.getKeywords();
522  Arrays.sort(selected);
523  for (int arrayi = selected.length - 1; arrayi >= 0; arrayi--) {
524  words.remove(selected[arrayi]);
525  }
526  resync();
527  }
528  }
529 
533  void setFocusOnKeywordTextBox() {
534  newKeywordsButton.requestFocus();
535  }
536 }

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