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

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.