Autopsy  4.1
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-2016 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.awt.event.ActionListener;
23 import java.beans.PropertyChangeEvent;
24 import java.beans.PropertyChangeListener;
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.regex.Pattern;
31 import java.util.regex.PatternSyntaxException;
32 import javax.swing.JFileChooser;
33 import javax.swing.JTable;
34 import javax.swing.ListSelectionModel;
35 import javax.swing.event.ListSelectionEvent;
36 import javax.swing.event.ListSelectionListener;
37 import javax.swing.filechooser.FileNameExtensionFilter;
38 import javax.swing.table.AbstractTableModel;
39 import javax.swing.table.TableColumn;
40 import org.netbeans.spi.options.OptionsPanelController;
41 import org.openide.util.NbBundle;
45 
49 class GlobalEditListPanel extends javax.swing.JPanel implements ListSelectionListener, OptionsPanel {
50 
51  private static final Logger logger = Logger.getLogger(GlobalEditListPanel.class.getName());
52  private static final long serialVersionUID = 1L;
53  private final KeywordTableModel tableModel;
54  private KeywordList currentKeywordList;
55 
59  GlobalEditListPanel() {
60  tableModel = new KeywordTableModel();
61  initComponents();
62  customizeComponents();
63  }
64 
65  private void customizeComponents() {
66  newKeywordsButton.setToolTipText((NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.customizeComponents.addWordToolTip")));
67  exportButton.setToolTipText(NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.customizeComponents.exportToFile"));
68  saveListButton.setToolTipText(NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.customizeComponents.saveCurrentWIthNewNameToolTip"));
69  deleteWordButton.setToolTipText(NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.customizeComponents.removeSelectedMsg"));
70 
71  keywordTable.getParent().setBackground(keywordTable.getBackground());
72  final int width = jScrollPane1.getPreferredSize().width;
73  keywordTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
74  TableColumn column;
75  for (int i = 0; i < keywordTable.getColumnCount(); i++) {
76  column = keywordTable.getColumnModel().getColumn(i);
77  if (i == 0) {
78  column.setPreferredWidth(((int) (width * 0.90)));
79  } else {
80  column.setPreferredWidth(((int) (width * 0.10)));
81  }
82  }
83  keywordTable.setCellSelectionEnabled(false);
84  keywordTable.setRowSelectionAllowed(true);
85 
86  final ListSelectionModel lsm = keywordTable.getSelectionModel();
87  lsm.addListSelectionListener(new ListSelectionListener() {
88  @Override
89  public void valueChanged(ListSelectionEvent e) {
90  if (lsm.isSelectionEmpty() || currentKeywordList.isEditable() || IngestManager.getInstance().isIngestRunning()) {
91  deleteWordButton.setEnabled(false);
92  } else {
93  deleteWordButton.setEnabled(true);
94  }
95  }
96  });
97 
98  setButtonStates();
99 
100  IngestManager.getInstance().addIngestJobEventListener(new PropertyChangeListener() {
101  @Override
102  public void propertyChange(PropertyChangeEvent evt) {
103  Object source = evt.getSource();
104  if (source instanceof String && ((String) source).equals("LOCAL")) { //NON-NLS
105  EventQueue.invokeLater(() -> {
106  setButtonStates();
107  });
108  }
109  }
110  });
111  }
112 
113  void setButtonStates() {
114  boolean isIngestRunning = IngestManager.getInstance().isIngestRunning();
115  boolean isListSelected = currentKeywordList != null;
116 
117  // items that only need a selected list
118  boolean canEditList = ((isListSelected == true) && (isIngestRunning == false));
119  ingestMessagesCheckbox.setEnabled(canEditList);
120  ingestMessagesCheckbox.setSelected(currentKeywordList != null && currentKeywordList.getIngestMessages());
121  listOptionsLabel.setEnabled(canEditList);
122  listOptionsSeparator.setEnabled(canEditList);
123 
124  // items that need an unlocked list w/out ingest running
125  boolean isListLocked = ((isListSelected == false) || (currentKeywordList.isEditable()));
126  boolean canAddWord = isListSelected && !isIngestRunning && !isListLocked;
127  newKeywordsButton.setEnabled(canAddWord);
128  keywordOptionsLabel.setEnabled(canAddWord);
129  keywordOptionsSeparator.setEnabled(canAddWord);
130  deleteListButton.setEnabled(canAddWord);
131 
132  // items that need a non-empty list
133  if ((currentKeywordList == null) || (currentKeywordList.getKeywords().isEmpty())) {
134  saveListButton.setEnabled(false);
135  exportButton.setEnabled(false);
136  deleteWordButton.setEnabled(false);
137  } else {
138  saveListButton.setEnabled(true);
139  exportButton.setEnabled(true);
140  // We do not set deleteWordButton because it will be set by the list select model code when a word is selected.
141  }
142  }
143 
149  @SuppressWarnings("unchecked")
150  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
151  private void initComponents() {
152 
153  listEditorPanel = new javax.swing.JPanel();
154  jScrollPane1 = new javax.swing.JScrollPane();
155  keywordTable = new javax.swing.JTable();
156  addKeywordPanel = new javax.swing.JPanel();
157  deleteWordButton = new javax.swing.JButton();
158  newKeywordsButton = new javax.swing.JButton();
159  ingestMessagesCheckbox = new javax.swing.JCheckBox();
160  keywordsLabel = new javax.swing.JLabel();
161  keywordOptionsLabel = new javax.swing.JLabel();
162  listOptionsLabel = new javax.swing.JLabel();
163  keywordOptionsSeparator = new javax.swing.JSeparator();
164  listOptionsSeparator = new javax.swing.JSeparator();
165  deleteListButton = new javax.swing.JButton();
166  saveListButton = new javax.swing.JButton();
167  exportButton = new javax.swing.JButton();
168 
169  setMinimumSize(new java.awt.Dimension(0, 0));
170 
171  listEditorPanel.setMinimumSize(new java.awt.Dimension(0, 0));
172 
173  jScrollPane1.setPreferredSize(new java.awt.Dimension(340, 300));
174 
175  keywordTable.setModel(tableModel);
176  keywordTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
177  keywordTable.setGridColor(new java.awt.Color(153, 153, 153));
178  keywordTable.setMaximumSize(new java.awt.Dimension(30000, 30000));
179  keywordTable.getTableHeader().setReorderingAllowed(false);
180  jScrollPane1.setViewportView(keywordTable);
181 
182  deleteWordButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/delete16.png"))); // NOI18N
183  deleteWordButton.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.deleteWordButton.text")); // NOI18N
184  deleteWordButton.addActionListener(new java.awt.event.ActionListener() {
185  public void actionPerformed(java.awt.event.ActionEvent evt) {
186  deleteWordButtonActionPerformed(evt);
187  }
188  });
189 
190  newKeywordsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/new16.png"))); // NOI18N
191  newKeywordsButton.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.newKeywordsButton.text")); // NOI18N
192  newKeywordsButton.addActionListener(new java.awt.event.ActionListener() {
193  public void actionPerformed(java.awt.event.ActionEvent evt) {
194  newKeywordsButtonActionPerformed(evt);
195  }
196  });
197 
198  javax.swing.GroupLayout addKeywordPanelLayout = new javax.swing.GroupLayout(addKeywordPanel);
199  addKeywordPanel.setLayout(addKeywordPanelLayout);
200  addKeywordPanelLayout.setHorizontalGroup(
201  addKeywordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
202  .addGroup(addKeywordPanelLayout.createSequentialGroup()
203  .addComponent(newKeywordsButton)
204  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
205  .addComponent(deleteWordButton)
206  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
207  );
208  addKeywordPanelLayout.setVerticalGroup(
209  addKeywordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
210  .addGroup(addKeywordPanelLayout.createSequentialGroup()
211  .addGap(0, 0, 0)
212  .addGroup(addKeywordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
213  .addComponent(deleteWordButton)
214  .addComponent(newKeywordsButton))
215  .addGap(72, 72, 72))
216  );
217 
218  ingestMessagesCheckbox.setSelected(true);
219  ingestMessagesCheckbox.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.ingestMessagesCheckbox.text")); // NOI18N
220  ingestMessagesCheckbox.setToolTipText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.ingestMessagesCheckbox.toolTipText")); // NOI18N
221  ingestMessagesCheckbox.addActionListener(new java.awt.event.ActionListener() {
222  public void actionPerformed(java.awt.event.ActionEvent evt) {
223  ingestMessagesCheckboxActionPerformed(evt);
224  }
225  });
226 
227  keywordsLabel.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.keywordsLabel.text")); // NOI18N
228 
229  keywordOptionsLabel.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.keywordOptionsLabel.text")); // NOI18N
230 
231  listOptionsLabel.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.listOptionsLabel.text")); // NOI18N
232 
233  deleteListButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/delete16.png"))); // NOI18N
234  deleteListButton.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.deleteListButton.text")); // NOI18N
235  deleteListButton.addActionListener(new java.awt.event.ActionListener() {
236  public void actionPerformed(java.awt.event.ActionEvent evt) {
237  deleteListButtonActionPerformed(evt);
238  }
239  });
240 
241  saveListButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/save16.png"))); // NOI18N
242  saveListButton.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.saveListButton.text")); // NOI18N
243 
244  exportButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/export16.png"))); // NOI18N
245  exportButton.setText(org.openide.util.NbBundle.getMessage(GlobalEditListPanel.class, "KeywordSearchEditListPanel.exportButton.text")); // NOI18N
246  exportButton.addActionListener(new java.awt.event.ActionListener() {
247  public void actionPerformed(java.awt.event.ActionEvent evt) {
248  exportButtonActionPerformed(evt);
249  }
250  });
251 
252  javax.swing.GroupLayout listEditorPanelLayout = new javax.swing.GroupLayout(listEditorPanel);
253  listEditorPanel.setLayout(listEditorPanelLayout);
254  listEditorPanelLayout.setHorizontalGroup(
255  listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
256  .addGroup(listEditorPanelLayout.createSequentialGroup()
257  .addContainerGap()
258  .addGroup(listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
259  .addGroup(listEditorPanelLayout.createSequentialGroup()
260  .addGap(10, 10, 10)
261  .addComponent(addKeywordPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
262  .addGroup(listEditorPanelLayout.createSequentialGroup()
263  .addGroup(listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
264  .addGroup(listEditorPanelLayout.createSequentialGroup()
265  .addComponent(listOptionsLabel)
266  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
267  .addComponent(listOptionsSeparator))
268  .addGroup(listEditorPanelLayout.createSequentialGroup()
269  .addComponent(keywordOptionsLabel)
270  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
271  .addComponent(keywordOptionsSeparator))
272  .addGroup(listEditorPanelLayout.createSequentialGroup()
273  .addGap(10, 10, 10)
274  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 482, Short.MAX_VALUE))
275  .addGroup(listEditorPanelLayout.createSequentialGroup()
276  .addGroup(listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
277  .addComponent(keywordsLabel)
278  .addGroup(listEditorPanelLayout.createSequentialGroup()
279  .addGap(10, 10, 10)
280  .addGroup(listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
281  .addComponent(ingestMessagesCheckbox)
282  .addGroup(listEditorPanelLayout.createSequentialGroup()
283  .addComponent(exportButton)
284  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
285  .addComponent(saveListButton)
286  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
287  .addComponent(deleteListButton)))))
288  .addGap(0, 0, Short.MAX_VALUE)))
289  .addContainerGap())))
290  );
291  listEditorPanelLayout.setVerticalGroup(
292  listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
293  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, listEditorPanelLayout.createSequentialGroup()
294  .addContainerGap()
295  .addComponent(keywordsLabel)
296  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
297  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
298  .addGap(10, 10, 10)
299  .addGroup(listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
300  .addGroup(listEditorPanelLayout.createSequentialGroup()
301  .addGroup(listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
302  .addComponent(keywordOptionsSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 7, javax.swing.GroupLayout.PREFERRED_SIZE)
303  .addComponent(keywordOptionsLabel))
304  .addGap(7, 7, 7)
305  .addComponent(addKeywordPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
306  .addGap(0, 0, 0)
307  .addComponent(listOptionsLabel))
308  .addGroup(listEditorPanelLayout.createSequentialGroup()
309  .addGap(123, 123, 123)
310  .addComponent(listOptionsSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE)))
311  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
312  .addComponent(ingestMessagesCheckbox)
313  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
314  .addGroup(listEditorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
315  .addComponent(exportButton)
316  .addComponent(saveListButton)
317  .addComponent(deleteListButton))
318  .addContainerGap())
319  );
320 
321  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
322  this.setLayout(layout);
323  layout.setHorizontalGroup(
324  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
325  .addComponent(listEditorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
326  );
327  layout.setVerticalGroup(
328  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
329  .addGroup(layout.createSequentialGroup()
330  .addComponent(listEditorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
331  .addGap(5, 5, 5))
332  );
333  }// </editor-fold>//GEN-END:initComponents
334 
335  private void deleteWordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteWordButtonActionPerformed
336  if (KeywordSearchUtil.displayConfirmDialog(NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.removeKwMsg"), NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.deleteWordButtonActionPerformed.delConfirmMsg"), KeywordSearchUtil.DIALOG_MESSAGE_TYPE.WARN)) {
337 
338  tableModel.deleteSelected(keywordTable.getSelectedRows());
339  XmlKeywordSearchList.getCurrent().addList(currentKeywordList);
340  setButtonStates();
341  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
342  }
343  }//GEN-LAST:event_deleteWordButtonActionPerformed
344 
345  private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed
346 
347  final String FEATURE_NAME = NbBundle.getMessage(this.getClass(),
348  "KeywordSearchEditListPanel.exportButtonAction.featureName.text");
349 
350  JFileChooser chooser = new JFileChooser();
351  final String EXTENSION = "xml"; //NON-NLS
352  FileNameExtensionFilter filter = new FileNameExtensionFilter(
353  NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.exportButtonActionPerformed.fileFilterLabel"), EXTENSION);
354  chooser.setFileFilter(filter);
355  chooser.setSelectedFile(new File(currentKeywordList.getName()));
356  chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
357 
358  int returnVal = chooser.showSaveDialog(this);
359  if (returnVal == JFileChooser.APPROVE_OPTION) {
360  File selFile = chooser.getSelectedFile();
361  if (selFile == null) {
362  return;
363  }
364 
365  //force append extension if not given
366  String fileAbs = selFile.getAbsolutePath();
367  if (!fileAbs.endsWith("." + EXTENSION)) {
368  fileAbs = fileAbs + "." + EXTENSION;
369  selFile = new File(fileAbs);
370  }
371 
372  boolean shouldWrite = true;
373  if (selFile.exists()) {
374  shouldWrite = KeywordSearchUtil.displayConfirmDialog(FEATURE_NAME,
375  NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.exportButtonActionPerformed.fileExistPrompt",
376  selFile.getName()), KeywordSearchUtil.DIALOG_MESSAGE_TYPE.WARN);
377  }
378  if (!shouldWrite) {
379  return;
380  }
381 
382  XmlKeywordSearchList reader = XmlKeywordSearchList.getCurrent();
383 
384  List<KeywordList> toWrite = new ArrayList<>();
385  toWrite.add(reader.getList(currentKeywordList.getName()));
386  final XmlKeywordSearchList exporter = new XmlKeywordSearchList(fileAbs);
387  boolean written = exporter.saveLists(toWrite);
388  if (written) {
389  KeywordSearchUtil.displayDialog(FEATURE_NAME,
390  NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.exportButtonActionPerformed.kwListExportedMsg"),
391  KeywordSearchUtil.DIALOG_MESSAGE_TYPE.INFO);
392  }
393  }
394  }//GEN-LAST:event_exportButtonActionPerformed
395 
396  private void ingestMessagesCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ingestMessagesCheckboxActionPerformed
397  currentKeywordList.setIngestMessages(ingestMessagesCheckbox.isSelected());
398  XmlKeywordSearchList updater = XmlKeywordSearchList.getCurrent();
399  updater.addList(currentKeywordList);
400  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
401  }//GEN-LAST:event_ingestMessagesCheckboxActionPerformed
402 
403  private void deleteListButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteListButtonActionPerformed
404  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
405  }//GEN-LAST:event_deleteListButtonActionPerformed
406 
407  private void newKeywordsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newKeywordsButtonActionPerformed
408  String keywordsToRedisplay = "";
409  AddKeywordsDialog dialog = new AddKeywordsDialog();
410 
411  int goodCount;
412  int dupeCount;
413  int badCount = 1; // Default to 1 so we enter the loop the first time
414 
415  while(badCount > 0){
416  dialog.setInitialKeywordList(keywordsToRedisplay);
417  dialog.display();
418 
419  goodCount = 0;
420  dupeCount = 0;
421  badCount = 0;
422  keywordsToRedisplay = "";
423 
424  if(!dialog.getKeywords().isEmpty()){
425 
426 
427  for(String newWord:dialog.getKeywords()){
428  if (newWord.isEmpty()) {
429  continue;
430  }
431 
432  final Keyword keyword = new Keyword(newWord, !dialog.isKeywordRegex(), dialog.isKeywordExact());
433  if (currentKeywordList.hasKeyword(keyword)) {
434  dupeCount++;
435  continue;
436  }
437 
438  //check if valid
439  boolean valid = true;
440  try {
441  Pattern.compile(newWord);
442  } catch (PatternSyntaxException ex1) {
443  valid = false;
444  } catch (IllegalArgumentException ex2) {
445  valid = false;
446  }
447  if (!valid) {
448 
449  // Invalid keywords will reappear in the UI
450  keywordsToRedisplay += newWord + "\n";
451  badCount++;
452  continue;
453  }
454 
455  // Add the new keyword
456  tableModel.addKeyword(keyword);
457  goodCount++;
458  }
459  XmlKeywordSearchList.getCurrent().addList(currentKeywordList);
460  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
461 
462  if((badCount > 0) || (dupeCount > 0)){
463  // Display the error counts to the user
464  // The add keywords dialog will pop up again if any were invalid with any
465  // invalid entries (valid entries and dupes will disappear)
466 
467  String summary = "";
468  KeywordSearchUtil.DIALOG_MESSAGE_TYPE level = KeywordSearchUtil.DIALOG_MESSAGE_TYPE.INFO;
469  if(goodCount > 0){
470  if(goodCount > 1){
471  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordsAddedPlural.text", goodCount) + "\n";
472  } else {
473  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordsAdded.text", goodCount) + "\n";
474  }
475  }
476  if(dupeCount > 0){
477  if(dupeCount > 1){
478  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordDupesSkippedPlural.text", dupeCount) + "\n";
479  } else {
480  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordDupesSkipped.text", dupeCount) + "\n";
481  }
482  level = KeywordSearchUtil.DIALOG_MESSAGE_TYPE.WARN;
483  }
484  if(badCount > 0){
485  if(badCount > 1){
486  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordErrorsPlural.text", badCount) + "\n";
487  } else {
488  summary += NbBundle.getMessage(GlobalEditListPanel.class, "GlobalEditListPanel.keywordErrors.text", badCount) + "\n";
489  }
490  level = KeywordSearchUtil.DIALOG_MESSAGE_TYPE.ERROR;
491  }
492  KeywordSearchUtil.displayDialog(NbBundle.getMessage(this.getClass(), "GlobalEditListPanel.addKeywordResults.text"),
493  summary, level);
494  }
495  }
496  }
497  setFocusOnKeywordTextBox();
498  setButtonStates();
499  }//GEN-LAST:event_newKeywordsButtonActionPerformed
500 
501  // Variables declaration - do not modify//GEN-BEGIN:variables
502  private javax.swing.JPanel addKeywordPanel;
503  private javax.swing.JButton deleteListButton;
504  private javax.swing.JButton deleteWordButton;
505  private javax.swing.JButton exportButton;
506  private javax.swing.JCheckBox ingestMessagesCheckbox;
507  private javax.swing.JScrollPane jScrollPane1;
508  private javax.swing.JLabel keywordOptionsLabel;
509  private javax.swing.JSeparator keywordOptionsSeparator;
510  private javax.swing.JTable keywordTable;
511  private javax.swing.JLabel keywordsLabel;
512  private javax.swing.JPanel listEditorPanel;
513  private javax.swing.JLabel listOptionsLabel;
514  private javax.swing.JSeparator listOptionsSeparator;
515  private javax.swing.JButton newKeywordsButton;
516  private javax.swing.JButton saveListButton;
517  // End of variables declaration//GEN-END:variables
518 
519  @Override
520  public void valueChanged(ListSelectionEvent e) {
521  //respond to list selection changes in KeywordSearchListManagementPanel
522  ListSelectionModel listSelectionModel = (ListSelectionModel) e.getSource();
523  if (!listSelectionModel.isSelectionEmpty()) {
524  int index = listSelectionModel.getMinSelectionIndex();
525 
526  listSelectionModel.setSelectionInterval(index, index);
527  XmlKeywordSearchList loader = XmlKeywordSearchList.getCurrent();
528 
529  currentKeywordList = loader.getListsL(false).get(index);
530  tableModel.resync();
531  setButtonStates();
532  } else {
533  currentKeywordList = null;
534  tableModel.resync();
535  setButtonStates();
536  }
537  }
538 
539  @Override
540  public void store() {
541  // Implemented by parent panel
542  }
543 
544  @Override
545  public void load() {
546  // Implemented by parent panel
547  }
548 
549  KeywordList getCurrentKeywordList() {
550  return currentKeywordList;
551  }
552 
553  void setCurrentKeywordList(KeywordList list) {
554  currentKeywordList = list;
555  }
556 
557  void addDeleteButtonActionPerformed(ActionListener l) {
558  deleteListButton.addActionListener(l);
559  }
560 
561  void addSaveButtonActionPerformed(ActionListener l) {
562  saveListButton.addActionListener(l);
563  }
564 
565  private class KeywordTableModel extends AbstractTableModel {
566 
567  @Override
568  public int getColumnCount() {
569  return 2;
570  }
571 
572  @Override
573  public int getRowCount() {
574  return currentKeywordList == null ? 0 : currentKeywordList.getKeywords().size();
575  }
576 
577  @Override
578  public String getColumnName(int column) {
579  String colName = null;
580 
581  switch (column) {
582  case 0:
583  colName = NbBundle.getMessage(this.getClass(), "KeywordSearchEditListPanel.kwColName");
584  break;
585  case 1:
586  colName = NbBundle.getMessage(this.getClass(), "KeywordSearch.typeColLbl");
587  break;
588  default:
589  ;
590  }
591  return colName;
592  }
593 
594  @Override
595  public Object getValueAt(int rowIndex, int columnIndex) {
596  Object ret = null;
597  if (currentKeywordList == null) {
598  return "";
599  }
600  Keyword word = currentKeywordList.getKeywords().get(rowIndex);
601  switch (columnIndex) {
602  case 0:
603  ret = word.getSearchTerm();
604  break;
605  case 1:
606  ret = word.getSearchTermType();
607  break;
608  default:
609  logger.log(Level.SEVERE, "Invalid table column index: {0}", columnIndex); //NON-NLS
610  break;
611  }
612  return ret;
613  }
614 
615  @Override
616  public boolean isCellEditable(int rowIndex, int columnIndex) {
617  return false;
618  }
619 
620  @Override
621  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
622  }
623 
624  @Override
625  public Class<?> getColumnClass(int c) {
626  return getValueAt(0, c).getClass();
627  }
628 
629  void addKeyword(Keyword keyword) {
630  if (!currentKeywordList.hasKeyword(keyword)) {
631  currentKeywordList.getKeywords().add(keyword);
632  }
633  fireTableDataChanged();
634  }
635 
636  void resync() {
637  fireTableDataChanged();
638  }
639 
640  //delete selected from handle, events are fired from the handle
641  void deleteSelected(int[] selected) {
642  List<Keyword> words = currentKeywordList.getKeywords();
643  Arrays.sort(selected);
644  for (int arrayi = selected.length - 1; arrayi >= 0; arrayi--) {
645  words.remove(selected[arrayi]);
646  }
647  resync();
648  }
649  }
650 
654  void setFocusOnKeywordTextBox() {
655  newKeywordsButton.requestFocus();
656  }
657 }

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