Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileExtMismatchSettingsPanel.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.modules.fileextmismatch;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Set;
26 import java.util.logging.Level;
27 import javax.swing.JOptionPane;
28 import javax.swing.ListSelectionModel;
29 import javax.swing.event.ListSelectionEvent;
30 import javax.swing.event.ListSelectionListener;
31 import javax.swing.table.AbstractTableModel;
32 import org.netbeans.spi.options.OptionsPanelController;
33 import org.openide.util.NbBundle;
38 
43 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
44 final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel implements OptionsPanel {
45 
46  private static final Logger logger = Logger.getLogger(FileExtMismatchSettingsPanel.class.getName());
47  private HashMap<String, Set<String>> editableMap = new HashMap<>();
48  private ArrayList<String> mimeList = null;
49  private ArrayList<String> currentExtensions = null;
50  private MimeTableModel mimeTableModel;
51  private ExtTableModel extTableModel;
52  private final String EXT_HEADER_LABEL = NbBundle.getMessage(FileExtMismatchSettingsPanel.class,
53  "AddFileExtensionAction.extHeaderLbl.text");
54  private String selectedMime = "";
55  private String selectedExt = "";
56  ListSelectionModel lsm = null;
57  private FileTypeDetector fileTypeDetector;
58 
59  public FileExtMismatchSettingsPanel() {
60  mimeTableModel = new MimeTableModel();
61  extTableModel = new ExtTableModel();
62 
63  try {
64  fileTypeDetector = new FileTypeDetector();
65  } catch (FileTypeDetector.FileTypeDetectorInitException ex) {
66  logger.log(Level.SEVERE, "Failed to create file type detector", ex); //NON-NLS
67  fileTypeDetector = null;
68  }
69 
70  initComponents();
71  customizeComponents();
72  }
73 
74  @NbBundle.Messages({"FileExtMismatchSettingsPanel.Title=Global File Extension Mismatch Identification Settings"})
75  private void customizeComponents() {
76  setName(Bundle.FileExtMismatchSettingsPanel_Title());
77 
78  // Handle selections on the left table
79  lsm = mimeTable.getSelectionModel();
80  lsm.addListSelectionListener(new ListSelectionListener() {
81  @Override
82  public void valueChanged(ListSelectionEvent e) {
83  ListSelectionModel listSelectionModel = (ListSelectionModel) e.getSource();
84  if (!listSelectionModel.isSelectionEmpty()) {
85  int index = listSelectionModel.getMinSelectionIndex();
86  listSelectionModel.setSelectionInterval(index, index);
87 
88  selectedMime = mimeList.get(index);
89  String labelStr = EXT_HEADER_LABEL + selectedMime + ":";
90  if (labelStr.length() > 80) {
91  labelStr = labelStr.substring(0, 80);
92  }
93  extHeaderLabel.setText(labelStr);
94  updateExtList();
95 
96  extTableModel.resync();
97  removeTypeButton.setEnabled(true);
98  newExtButton.setEnabled(true);
99  //initButtons();
100  } else {
101  selectedMime = "";
102  currentExtensions = null;
103  removeTypeButton.setEnabled(false);
104  newExtButton.setEnabled(false);
105  extTableModel.resync();
106  }
107  }
108  });
109 
110  // Handle selections on the right table
111  ListSelectionModel extLsm = extTable.getSelectionModel();
112  extLsm.addListSelectionListener(new ListSelectionListener() {
113  @Override
114  public void valueChanged(ListSelectionEvent e) {
115  ListSelectionModel listSelectionModel = (ListSelectionModel) e.getSource();
116  if (!listSelectionModel.isSelectionEmpty()) {
117  int index = listSelectionModel.getMinSelectionIndex();
118  listSelectionModel.setSelectionInterval(index, index);
119 
120  selectedExt = currentExtensions.get(index);
121  removeExtButton.setEnabled(true);
122  } else {
123  selectedExt = "";
124  removeExtButton.setEnabled(false);
125  }
126 
127  }
128  });
129  removeExtButton.setEnabled(false);
130  removeTypeButton.setEnabled(false);
131  newExtButton.setEnabled(false);
132  }
133 
139  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
140  private void initComponents() {
141 
142  jPanel1 = new javax.swing.JPanel();
143  jScrollPane1 = new javax.swing.JScrollPane();
144  jSplitPane1 = new javax.swing.JSplitPane();
145  mimePanel = new javax.swing.JPanel();
146  jLabel1 = new javax.swing.JLabel();
147  jScrollPane2 = new javax.swing.JScrollPane();
148  mimeTable = new javax.swing.JTable();
149  newTypeButton = new javax.swing.JButton();
150  removeTypeButton = new javax.swing.JButton();
151  extensionPanel = new javax.swing.JPanel();
152  newExtButton = new javax.swing.JButton();
153  jScrollPane3 = new javax.swing.JScrollPane();
154  extTable = new javax.swing.JTable();
155  removeExtButton = new javax.swing.JButton();
156  extHeaderLabel = new javax.swing.JLabel();
157 
158  setPreferredSize(new java.awt.Dimension(718, 430));
159 
160  jPanel1.setPreferredSize(new java.awt.Dimension(718, 430));
161 
162  jScrollPane1.setRequestFocusEnabled(false);
163 
164  jSplitPane1.setDividerLocation(365);
165  jSplitPane1.setDividerSize(1);
166 
167  mimePanel.setPreferredSize(new java.awt.Dimension(369, 424));
168  mimePanel.setRequestFocusEnabled(false);
169 
170  jLabel1.setText(org.openide.util.NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.jLabel1.text")); // NOI18N
171 
172  mimeTable.setModel(mimeTableModel);
173  jScrollPane2.setViewportView(mimeTable);
174 
175  newTypeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/add16.png"))); // NOI18N
176  newTypeButton.setText(org.openide.util.NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newTypeButton.text")); // NOI18N
177  newTypeButton.setMaximumSize(new java.awt.Dimension(111, 25));
178  newTypeButton.setMinimumSize(new java.awt.Dimension(111, 25));
179  newTypeButton.addActionListener(new java.awt.event.ActionListener() {
180  public void actionPerformed(java.awt.event.ActionEvent evt) {
181  newTypeButtonActionPerformed(evt);
182  }
183  });
184 
185  removeTypeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/delete16.png"))); // NOI18N
186  removeTypeButton.setText(org.openide.util.NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.removeTypeButton.text")); // NOI18N
187  removeTypeButton.setToolTipText(org.openide.util.NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.removeTypeButton.toolTipText")); // NOI18N
188  removeTypeButton.addActionListener(new java.awt.event.ActionListener() {
189  public void actionPerformed(java.awt.event.ActionEvent evt) {
190  removeTypeButtonActionPerformed(evt);
191  }
192  });
193 
194  javax.swing.GroupLayout mimePanelLayout = new javax.swing.GroupLayout(mimePanel);
195  mimePanel.setLayout(mimePanelLayout);
196  mimePanelLayout.setHorizontalGroup(
197  mimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
198  .addGroup(mimePanelLayout.createSequentialGroup()
199  .addContainerGap()
200  .addGroup(mimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
201  .addGroup(mimePanelLayout.createSequentialGroup()
202  .addGap(0, 0, Short.MAX_VALUE)
203  .addComponent(jLabel1)
204  .addGap(286, 286, 286))
205  .addGroup(mimePanelLayout.createSequentialGroup()
206  .addGroup(mimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
207  .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE)
208  .addGroup(mimePanelLayout.createSequentialGroup()
209  .addComponent(newTypeButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
210  .addGap(10, 10, 10)
211  .addComponent(removeTypeButton)))
212  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
213  );
214  mimePanelLayout.setVerticalGroup(
215  mimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
216  .addGroup(mimePanelLayout.createSequentialGroup()
217  .addContainerGap()
218  .addComponent(jLabel1)
219  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
220  .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
221  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
222  .addGroup(mimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
223  .addComponent(newTypeButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
224  .addComponent(removeTypeButton))
225  .addContainerGap())
226  );
227 
228  jSplitPane1.setLeftComponent(mimePanel);
229 
230  extensionPanel.setPreferredSize(new java.awt.Dimension(344, 424));
231 
232  newExtButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/add16.png"))); // NOI18N
233  newExtButton.setText(org.openide.util.NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newExtButton.text")); // NOI18N
234  newExtButton.setMaximumSize(new java.awt.Dimension(111, 25));
235  newExtButton.setMinimumSize(new java.awt.Dimension(111, 25));
236  newExtButton.addActionListener(new java.awt.event.ActionListener() {
237  public void actionPerformed(java.awt.event.ActionEvent evt) {
238  newExtButtonActionPerformed(evt);
239  }
240  });
241 
242  extTable.setModel(extTableModel);
243  jScrollPane3.setViewportView(extTable);
244 
245  removeExtButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/delete16.png"))); // NOI18N
246  removeExtButton.setText(org.openide.util.NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.removeExtButton.text")); // NOI18N
247  removeExtButton.addActionListener(new java.awt.event.ActionListener() {
248  public void actionPerformed(java.awt.event.ActionEvent evt) {
249  removeExtButtonActionPerformed(evt);
250  }
251  });
252 
253  extHeaderLabel.setText(org.openide.util.NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.extHeaderLabel.text")); // NOI18N
254 
255  javax.swing.GroupLayout extensionPanelLayout = new javax.swing.GroupLayout(extensionPanel);
256  extensionPanel.setLayout(extensionPanelLayout);
257  extensionPanelLayout.setHorizontalGroup(
258  extensionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
259  .addGroup(extensionPanelLayout.createSequentialGroup()
260  .addContainerGap()
261  .addGroup(extensionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
262  .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
263  .addGroup(extensionPanelLayout.createSequentialGroup()
264  .addGroup(extensionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
265  .addComponent(extHeaderLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE)
266  .addGroup(extensionPanelLayout.createSequentialGroup()
267  .addComponent(newExtButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
268  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
269  .addComponent(removeExtButton)))
270  .addGap(0, 0, Short.MAX_VALUE)))
271  .addContainerGap())
272  );
273  extensionPanelLayout.setVerticalGroup(
274  extensionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
275  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, extensionPanelLayout.createSequentialGroup()
276  .addContainerGap()
277  .addComponent(extHeaderLabel)
278  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
279  .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
280  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
281  .addGroup(extensionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
282  .addComponent(newExtButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
283  .addComponent(removeExtButton))
284  .addContainerGap())
285  );
286 
287  jSplitPane1.setRightComponent(extensionPanel);
288 
289  jScrollPane1.setViewportView(jSplitPane1);
290 
291  javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
292  jPanel1.setLayout(jPanel1Layout);
293  jPanel1Layout.setHorizontalGroup(
294  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
295  .addGroup(jPanel1Layout.createSequentialGroup()
296  .addGap(0, 0, 0)
297  .addComponent(jScrollPane1)
298  .addGap(0, 0, 0))
299  );
300  jPanel1Layout.setVerticalGroup(
301  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
302  .addGroup(jPanel1Layout.createSequentialGroup()
303  .addGap(0, 0, 0)
304  .addComponent(jScrollPane1)
305  .addGap(0, 0, 0))
306  );
307 
308  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
309  this.setLayout(layout);
310  layout.setHorizontalGroup(
311  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
312  .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
313  );
314  layout.setVerticalGroup(
315  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
316  .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
317  );
318  }// </editor-fold>//GEN-END:initComponents
319 
320  // Add a user-provided filename extension string to the selecte mimetype
321  private void newExtButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newExtButtonActionPerformed
322  String newExt = (String) JOptionPane.showInputDialog(this, NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newExtPrompt.message"),
323  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newExtPrompt.title"), JOptionPane.PLAIN_MESSAGE, null, null, "");
324 
325  if (newExt == null) {
326  return;
327  }
328  if (newExt.isEmpty()) {
329  JOptionPane.showMessageDialog(this,
330  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newExtPrompt.empty.message"),
331  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newExtPrompt.empty.title"),
332  JOptionPane.ERROR_MESSAGE);
333  return;
334  }
335  if (selectedMime.isEmpty()) {
336  JOptionPane.showMessageDialog(this,
337  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newExtPrompt.noMimeType.message"),
338  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newExtPrompt.noMimeType.title"),
339  JOptionPane.ERROR_MESSAGE);
340  return;
341  }
342  if (currentExtensions.contains(newExt)) {
343  JOptionPane.showMessageDialog(this,
344  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newExtPrompt.extExists.message"),
345  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newExtPrompt.extExists.title"),
346  JOptionPane.ERROR_MESSAGE);
347  return;
348  }
349 
350  Set<String> editedExtensions = editableMap.get(selectedMime);
351  editedExtensions.add(newExt);
352 
353  // Old array will be replaced by new array for this key
354  editableMap.put(selectedMime, editedExtensions);
355 
356  // Refresh table
357  updateExtList();
358  extTableModel.resync();
359  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
360  }//GEN-LAST:event_newExtButtonActionPerformed
361 
362  private void removeExtButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeExtButtonActionPerformed
363  if (selectedExt.isEmpty()) {
364  JOptionPane.showMessageDialog(this,
365  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.removeExtButton.noneSelected.message"),
366  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.removeExtButton.noneSelected.title"),
367  JOptionPane.ERROR_MESSAGE);
368  return;
369  }
370 
371  if (selectedMime.isEmpty()) {
372  JOptionPane.showMessageDialog(this,
373  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.removeExtButton.noMimeTypeSelected.message"),
374  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.removeExtButton.noMimeTypeSelected.title"),
375  JOptionPane.ERROR_MESSAGE);
376  return;
377  }
378 
379  Set<String> editedExtensions = editableMap.get(selectedMime);
380  editedExtensions.remove(selectedExt);
381  String deadExt = selectedExt;
382 
383  // Old array will be replaced by new array for this key
384  editableMap.put(selectedMime, editedExtensions);
385 
386  // Refresh tables
387  updateExtList();
388  extTableModel.resync();
389  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
390  }//GEN-LAST:event_removeExtButtonActionPerformed
391 
392  private void removeTypeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeTypeButtonActionPerformed
393  if (selectedMime.isEmpty()) {
394  JOptionPane.showMessageDialog(this,
395  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.removeTypeButton.noneSelected.message"),
396  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.removeTypeButton.noneSelected.title"),
397  JOptionPane.ERROR_MESSAGE);
398  return;
399  }
400 
401  editableMap.remove(selectedMime);
402 
403  // Refresh table
404  updateMimeList();
405  mimeTableModel.resync();
406  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
407  }//GEN-LAST:event_removeTypeButtonActionPerformed
408 
409  private void newTypeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newTypeButtonActionPerformed
410  String newMime = (String) JOptionPane.showInputDialog(this, NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newMimePrompt.message"),
411  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newMimePrompt.title"), JOptionPane.PLAIN_MESSAGE, null, null, "");
412 
413  if (newMime == null) {
414  return;
415  }
416  if (newMime.isEmpty()) {
417  JOptionPane.showMessageDialog(this,
418  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newMimePrompt.emptyMime.message"),
419  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newMimePrompt.emptyMime.title"),
420  JOptionPane.ERROR_MESSAGE);
421  return;
422  }
423  if (newMime.equals("application/octet-stream")) { //NON-NLS
424  JOptionPane.showMessageDialog(this,
425  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newMimePrompt.mimeTypeNotSupported.message"),
426  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newMimePrompt.mimeTypeNotSupported.title"),
427  JOptionPane.ERROR_MESSAGE);
428  return;
429  }
430  if (mimeList.contains(newMime)) {
431  JOptionPane.showMessageDialog(this,
432  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newMimePrompt.mimeTypeExists.message"),
433  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newMimePrompt.mimeTypeExists.title"),
434  JOptionPane.ERROR_MESSAGE);
435  return;
436  }
437 
438  FileTypeDetector detector;
439  try {
440  detector = new FileTypeDetector();
441  } catch (FileTypeDetector.FileTypeDetectorInitException ex) {
442  logger.log(Level.WARNING, "Couldn't create file type detector for file ext mismatch settings.", ex);
443  return;
444  }
445  boolean mimeTypeDetectable = (null != detector) ? detector.isDetectable(newMime) : false;
446  if (!mimeTypeDetectable) {
447  JOptionPane.showMessageDialog(this,
448  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newMimePrompt.mimeTypeNotDetectable.message"),
449  NbBundle.getMessage(FileExtMismatchSettingsPanel.class, "FileExtMismatchSettingsPanel.newMimePrompt.mimeTypeNotDetectable.title"),
450  JOptionPane.ERROR_MESSAGE);
451  return;
452  }
453 
454  editableMap.put(newMime, new HashSet<String>());
455 
456  // Refresh table
457  updateMimeList();
458  mimeTableModel.resync();
459  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
460  }//GEN-LAST:event_newTypeButtonActionPerformed
461 
462  private void updateMimeList() {
463  mimeList = new ArrayList<>(editableMap.keySet());
464  if (mimeList.size() > 0) {
465  Collections.sort(mimeList);
466  }
467  }
468 
469  private void updateExtList() {
470  Set<String> temp = editableMap.get(selectedMime);
471  if (temp != null) {
472  currentExtensions = new ArrayList<>(temp);
473  if (temp.size() > 0) {
474  Collections.sort(currentExtensions);
475  }
476  } else {
477  currentExtensions = null;
478  }
479  }
480 
481  @Override
482  public void saveSettings() {
483  try {
484  FileExtMismatchSettings.writeSettings(new FileExtMismatchSettings(editableMap));
485  } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) {
486  //error
487  JOptionPane.showMessageDialog(this,
488  NbBundle.getMessage(this.getClass(),
489  "FileExtMismatchConfigPanel.store.msgDlg.msg"),
490  NbBundle.getMessage(this.getClass(),
491  "FileExtMismatchConfigPanel.save.msgDlg.title"),
492  JOptionPane.ERROR_MESSAGE);
493  }
494  }
495 
496  @Override
497  public void load() {
498  try {
499  // Load the configuration into a buffer that the user can modify. They can choose
500  // to save it back to the file after making changes.
501  editableMap = FileExtMismatchSettings.readSettings().getMimeTypeToExtsMap();
502 
503  } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) {
504  //error
505  JOptionPane.showMessageDialog(this,
506  NbBundle.getMessage(this.getClass(),
507  "AddFileExtensionAction.msgDlg.msg2"),
508  NbBundle.getMessage(this.getClass(),
509  "FileExtMismatchConfigPanel.save.msgDlg.title"),
510  JOptionPane.ERROR_MESSAGE);
511  }
512  updateMimeList();
513  updateExtList();
514  }
515 
516  @Override
517  public void store() {
518  saveSettings();
519  }
520 
521  public void cancel() {
522  load(); // The next time this panel is opened, we want it to be fresh
523  }
524 
525  public void ok() {
526  store();
527  load(); // The next time this panel is opened, we want it to be fresh
528  }
529 
530  boolean valid() {
531  return true;
532  }
533  // Variables declaration - do not modify//GEN-BEGIN:variables
534  private javax.swing.JLabel extHeaderLabel;
535  private javax.swing.JTable extTable;
536  private javax.swing.JPanel extensionPanel;
537  private javax.swing.JLabel jLabel1;
538  private javax.swing.JPanel jPanel1;
539  private javax.swing.JScrollPane jScrollPane1;
540  private javax.swing.JScrollPane jScrollPane2;
541  private javax.swing.JScrollPane jScrollPane3;
542  private javax.swing.JSplitPane jSplitPane1;
543  private javax.swing.JPanel mimePanel;
544  private javax.swing.JTable mimeTable;
545  private javax.swing.JButton newExtButton;
546  private javax.swing.JButton newTypeButton;
547  private javax.swing.JButton removeExtButton;
548  private javax.swing.JButton removeTypeButton;
549  // End of variables declaration//GEN-END:variables
550 
551  private class MimeTableModel extends AbstractTableModel {
552 
553  @Override
554  public int getColumnCount() {
555  return 1;
556  }
557 
558  @Override
559  public int getRowCount() {
560  return editableMap == null ? 0 : editableMap.size();
561  }
562 
563  @Override
564  public String getColumnName(int column) {
565  String colName = null;
566  return colName;
567  }
568 
569  @Override
570  public Object getValueAt(int rowIndex, int columnIndex) {
571  Object ret = null;
572  if ((mimeList == null) || (rowIndex > mimeList.size())) {
573  return "";
574  }
575  String word = mimeList.get(rowIndex);
576  switch (columnIndex) {
577  case 0:
578  ret = (Object) word;
579  break;
580  default:
581  logger.log(Level.SEVERE, "Invalid table column index: {0}", columnIndex); //NON-NLS
582  break;
583  }
584  return ret;
585  }
586 
587  @Override
588  public boolean isCellEditable(int rowIndex, int columnIndex) {
589  return false;
590  }
591 
592  @Override
593  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
594  }
595 
596  @Override
597  public Class<?> getColumnClass(int c) {
598  return getValueAt(0, c).getClass();
599  }
600 
601  void resync() {
602  fireTableDataChanged();
603  }
604  }
605 
606  private class ExtTableModel extends AbstractTableModel {
607 
608  @Override
609  public int getColumnCount() {
610  return 1;
611  }
612 
613  @Override
614  public int getRowCount() {
615  return currentExtensions == null ? 0 : currentExtensions.size();
616  }
617 
618  @Override
619  public String getColumnName(int column) {
620  String colName = null;
621  return colName;
622  }
623 
624  @Override
625  public Object getValueAt(int rowIndex, int columnIndex) {
626  Object ret = null;
627 
628  if ((currentExtensions == null) || (currentExtensions.isEmpty()) || (rowIndex > currentExtensions.size())) {
629  return "";
630  }
631  String word = currentExtensions.get(rowIndex);
632  switch (columnIndex) {
633  case 0:
634  ret = (Object) word;
635  break;
636  default:
637  logger.log(Level.SEVERE, "Invalid table column index: {0}", columnIndex); //NON-NLS
638  break;
639  }
640  return ret;
641  }
642 
643  @Override
644  public boolean isCellEditable(int rowIndex, int columnIndex) {
645  return false;
646  }
647 
648  @Override
649  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
650  }
651 
652  @Override
653  public Class<?> getColumnClass(int c) {
654  return getValueAt(0, c).getClass();
655  }
656 
657  void resync() {
658  fireTableDataChanged();
659  }
660  }
661 }

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.