19 package org.sleuthkit.autopsy.directorytree;
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;
34 @SuppressWarnings(
"PMD.SingularField")
35 class AddExternalViewerRulePanel extends javax.swing.JPanel {
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);
48 AddExternalViewerRulePanel() {
50 fc.setDragEnabled(
false);
51 fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
52 fc.setMultiSelectionEnabled(
false);
53 fc.setFileFilter(exeFilter);
63 AddExternalViewerRulePanel(ExternalViewerRule rule) {
65 nameTextField.setText(rule.getName());
66 exePathTextField.setText(rule.getExePath());
67 if (rule.getRuleType() == ExternalViewerRule.RuleType.EXT) {
68 extRadioButton.setSelected(
true);
77 private void customize() {
78 mimeRadioButton.setActionCommand(
"mime");
79 extRadioButton.setActionCommand(
"ext");
80 nameTextField.getDocument().addDocumentListener(
new DocumentListener() {
82 public void changedUpdate(DocumentEvent e) {
86 public void removeUpdate(DocumentEvent e) {
90 public void insertUpdate(DocumentEvent e) {
94 firePropertyChange(EVENT.CHANGED.toString(), null, null);
97 exePathTextField.getDocument().addDocumentListener(
new DocumentListener() {
99 public void changedUpdate(DocumentEvent e) {
103 public void removeUpdate(DocumentEvent e) {
107 public void insertUpdate(DocumentEvent e) {
110 private void fire() {
111 firePropertyChange(EVENT.CHANGED.toString(), null, null);
122 boolean hasFields() {
123 return !exePathTextField.getText().isEmpty() && !nameTextField.getText().isEmpty() &&
124 (mimeRadioButton.isSelected() || extRadioButton.isSelected());
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);
145 String name = nameTextField.getText();
146 if (mimeRadioButton.isSelected()) {
147 FileTypeDetector detector;
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);
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);
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);
170 if (name.charAt(0) !=
'.') {
173 return new ExternalViewerRule(name.toLowerCase(), exePath, ExternalViewerRule.RuleType.EXT);
183 @SuppressWarnings(
"unchecked")
185 private
void initComponents() {
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();
196 org.openide.awt.Mnemonics.setLocalizedText(nameLabel,
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.nameLabel.text"));
198 nameTextField.setText(
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.nameTextField.text"));
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"));
204 buttonGroup.add(extRadioButton);
205 org.openide.awt.Mnemonics.setLocalizedText(extRadioButton,
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.extRadioButton.text"));
207 org.openide.awt.Mnemonics.setLocalizedText(exePathLabel,
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.exePathLabel.text"));
209 exePathTextField.setEditable(
false);
210 exePathTextField.setText(
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.exePathTextField.text"));
212 org.openide.awt.Mnemonics.setLocalizedText(browseButton,
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.browseButton.text"));
213 browseButton.addActionListener(
new java.awt.event.ActionListener() {
214 public void actionPerformed(java.awt.event.ActionEvent evt) {
215 browseButtonActionPerformed(evt);
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()
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)))
242 layout.setVerticalGroup(
243 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
244 .addGroup(layout.createSequentialGroup()
246 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
247 .addComponent(nameLabel)
248 .addComponent(mimeRadioButton)
249 .addComponent(extRadioButton))
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))
262 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {
263 int returnState = fc.showOpenDialog(
this);
264 if (returnState == JFileChooser.APPROVE_OPTION) {
265 String path = fc.getSelectedFile().getPath();
266 exePathTextField.setText(path);
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;