Autopsy  4.10.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddExternalViewerRulePanel.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.directorytree;
20 
21 import java.util.logging.Level;
22 import javax.swing.JFileChooser;
23 import javax.swing.JOptionPane;
24 import javax.swing.event.DocumentEvent;
25 import javax.swing.event.DocumentListener;
26 import org.openide.util.NbBundle;
30 
34 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
35 class AddExternalViewerRulePanel extends javax.swing.JPanel {
36 
37  private static final Logger logger = Logger.getLogger(AddExternalViewerRulePanel.class.getName());
38  private final JFileChooser fc = new JFileChooser();
39  private static final GeneralFilter exeFilter = new GeneralFilter(GeneralFilter.EXECUTABLE_EXTS, GeneralFilter.EXECUTABLE_DESC);
40 
41  enum EVENT {
42  CHANGED
43  }
44 
48  AddExternalViewerRulePanel() {
49  initComponents();
50  fc.setDragEnabled(false);
51  fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
52  fc.setMultiSelectionEnabled(false);
53  fc.setFileFilter(exeFilter);
54  customize();
55  }
56 
63  AddExternalViewerRulePanel(ExternalViewerRule rule) {
64  this();
65  nameTextField.setText(rule.getName());
66  exePathTextField.setText(rule.getExePath());
67  if (rule.getRuleType() == ExternalViewerRule.RuleType.EXT) {
68  extRadioButton.setSelected(true);
69  }
70  customize();
71  }
72 
77  private void customize() {
78  mimeRadioButton.setActionCommand("mime");
79  extRadioButton.setActionCommand("ext");
80  nameTextField.getDocument().addDocumentListener(new DocumentListener() {
81  @Override
82  public void changedUpdate(DocumentEvent e) {
83  fire();
84  }
85  @Override
86  public void removeUpdate(DocumentEvent e) {
87  fire();
88  }
89  @Override
90  public void insertUpdate(DocumentEvent e) {
91  fire();
92  }
93  private void fire() {
94  firePropertyChange(EVENT.CHANGED.toString(), null, null);
95  }
96  });
97  exePathTextField.getDocument().addDocumentListener(new DocumentListener() {
98  @Override
99  public void changedUpdate(DocumentEvent e) {
100  fire();
101  }
102  @Override
103  public void removeUpdate(DocumentEvent e) {
104  fire();
105  }
106  @Override
107  public void insertUpdate(DocumentEvent e) {
108  fire();
109  }
110  private void fire() {
111  firePropertyChange(EVENT.CHANGED.toString(), null, null);
112  }
113  });
114  }
115 
122  boolean hasFields() {
123  return !exePathTextField.getText().isEmpty() && !nameTextField.getText().isEmpty() &&
124  (mimeRadioButton.isSelected() || extRadioButton.isSelected());
125  }
126 
135  ExternalViewerRule getRule() {
136  String exePath = exePathTextField.getText();
137  if (exePath.isEmpty()) {
138  JOptionPane.showMessageDialog(this,
139  NbBundle.getMessage(ExternalViewerGlobalSettingsPanel.class, "ExternalViewerGlobalSettingsPanel.JOptionPane.invalidExePath.message"),
140  NbBundle.getMessage(ExternalViewerGlobalSettingsPanel.class, "ExternalViewerGlobalSettingsPanel.JOptionPane.invalidExePath.title"),
141  JOptionPane.ERROR_MESSAGE);
142  return null;
143  }
144 
145  String name = nameTextField.getText();
146  if (mimeRadioButton.isSelected()) {
147  FileTypeDetector detector;
148  try {
149  detector = new FileTypeDetector();
150  } catch (FileTypeDetector.FileTypeDetectorInitException ex) {
151  logger.log(Level.WARNING, "Couldn't create file type detector for file ext mismatch settings.", ex);
152  return null;
153  }
154  if (name.isEmpty() || !detector.isDetectable(name)) {
155  JOptionPane.showMessageDialog(this,
156  NbBundle.getMessage(ExternalViewerGlobalSettingsPanel.class, "ExternalViewerGlobalSettingsPanel.JOptionPane.invalidMime.message"),
157  NbBundle.getMessage(ExternalViewerGlobalSettingsPanel.class, "ExternalViewerGlobalSettingsPanel.JOptionPane.invalidMime.title"),
158  JOptionPane.ERROR_MESSAGE);
159  return null;
160  }
161  return new ExternalViewerRule(name, exePath, ExternalViewerRule.RuleType.MIME);
162  } else if (extRadioButton.isSelected()) {
163  if (name.isEmpty() || !name.matches("^\\.?\\w+$")) {
164  JOptionPane.showMessageDialog(this,
165  NbBundle.getMessage(ExternalViewerGlobalSettingsPanel.class, "ExternalViewerGlobalSettingsPanel.JOptionPane.invalidExt.message"),
166  NbBundle.getMessage(ExternalViewerGlobalSettingsPanel.class, "ExternalViewerGlobalSettingsPanel.JOptionPane.invalidExt.title"),
167  JOptionPane.ERROR_MESSAGE);
168  return null;
169  }
170  if (name.charAt(0) != '.') {
171  name = "." + name;
172  }
173  return new ExternalViewerRule(name.toLowerCase(), exePath, ExternalViewerRule.RuleType.EXT);
174  }
175  return null;
176  }
177 
183  @SuppressWarnings("unchecked")
184  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
185  private void initComponents() {
186 
187  buttonGroup = new javax.swing.ButtonGroup();
188  nameLabel = new javax.swing.JLabel();
189  nameTextField = new javax.swing.JTextField();
190  mimeRadioButton = new javax.swing.JRadioButton();
191  extRadioButton = new javax.swing.JRadioButton();
192  exePathLabel = new javax.swing.JLabel();
193  exePathTextField = new javax.swing.JTextField();
194  browseButton = new javax.swing.JButton();
195 
196  org.openide.awt.Mnemonics.setLocalizedText(nameLabel, org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class, "AddExternalViewerRulePanel.nameLabel.text")); // NOI18N
197 
198  nameTextField.setText(org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class, "AddExternalViewerRulePanel.nameTextField.text")); // NOI18N
199 
200  buttonGroup.add(mimeRadioButton);
201  mimeRadioButton.setSelected(true);
202  org.openide.awt.Mnemonics.setLocalizedText(mimeRadioButton, org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class, "AddExternalViewerRulePanel.mimeRadioButton.text")); // NOI18N
203 
204  buttonGroup.add(extRadioButton);
205  org.openide.awt.Mnemonics.setLocalizedText(extRadioButton, org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class, "AddExternalViewerRulePanel.extRadioButton.text")); // NOI18N
206 
207  org.openide.awt.Mnemonics.setLocalizedText(exePathLabel, org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class, "AddExternalViewerRulePanel.exePathLabel.text")); // NOI18N
208 
209  exePathTextField.setEditable(false);
210  exePathTextField.setText(org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class, "AddExternalViewerRulePanel.exePathTextField.text")); // NOI18N
211 
212  org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class, "AddExternalViewerRulePanel.browseButton.text")); // NOI18N
213  browseButton.addActionListener(new java.awt.event.ActionListener() {
214  public void actionPerformed(java.awt.event.ActionEvent evt) {
215  browseButtonActionPerformed(evt);
216  }
217  });
218 
219  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
220  this.setLayout(layout);
221  layout.setHorizontalGroup(
222  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
223  .addGroup(layout.createSequentialGroup()
224  .addContainerGap()
225  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
226  .addComponent(nameTextField)
227  .addGroup(layout.createSequentialGroup()
228  .addComponent(exePathTextField)
229  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
230  .addComponent(browseButton))
231  .addGroup(layout.createSequentialGroup()
232  .addComponent(exePathLabel)
233  .addGap(0, 0, Short.MAX_VALUE))
234  .addGroup(layout.createSequentialGroup()
235  .addComponent(nameLabel)
236  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
237  .addComponent(mimeRadioButton)
238  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
239  .addComponent(extRadioButton)))
240  .addContainerGap())
241  );
242  layout.setVerticalGroup(
243  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
244  .addGroup(layout.createSequentialGroup()
245  .addContainerGap()
246  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
247  .addComponent(nameLabel)
248  .addComponent(mimeRadioButton)
249  .addComponent(extRadioButton))
250  .addGap(2, 2, 2)
251  .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
252  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
253  .addComponent(exePathLabel)
254  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
255  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
256  .addComponent(exePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
257  .addComponent(browseButton))
258  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
259  );
260  }// </editor-fold>//GEN-END:initComponents
261 
262  private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
263  int returnState = fc.showOpenDialog(this);
264  if (returnState == JFileChooser.APPROVE_OPTION) {
265  String path = fc.getSelectedFile().getPath();
266  exePathTextField.setText(path);
267  }
268  }//GEN-LAST:event_browseButtonActionPerformed
269 
270 
271  // Variables declaration - do not modify//GEN-BEGIN:variables
272  private javax.swing.JButton browseButton;
273  private javax.swing.ButtonGroup buttonGroup;
274  private javax.swing.JLabel exePathLabel;
275  private javax.swing.JTextField exePathTextField;
276  private javax.swing.JRadioButton extRadioButton;
277  private javax.swing.JRadioButton mimeRadioButton;
278  private javax.swing.JLabel nameLabel;
279  private javax.swing.JTextField nameTextField;
280  // End of variables declaration//GEN-END:variables
281 }

Copyright © 2012-2018 Basis Technology. Generated on: Fri Mar 22 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.