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;
35 @SuppressWarnings(
"PMD.SingularField")
36 class AddExternalViewerRulePanel extends javax.swing.JPanel {
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);
51 AddExternalViewerRulePanel() {
62 AddExternalViewerRulePanel(ExternalViewerRule rule) {
64 nameTextField.setText(rule.getName());
65 exePathTextField.setText(rule.getExePath());
66 if (rule.getRuleType() == ExternalViewerRule.RuleType.EXT) {
67 extRadioButton.setSelected(
true);
76 private void customize() {
77 mimeRadioButton.setActionCommand(
"mime");
78 extRadioButton.setActionCommand(
"ext");
79 nameTextField.getDocument().addDocumentListener(
new DocumentListener() {
81 public void changedUpdate(DocumentEvent e) {
85 public void removeUpdate(DocumentEvent e) {
89 public void insertUpdate(DocumentEvent e) {
93 firePropertyChange(EVENT.CHANGED.toString(), null, null);
96 exePathTextField.getDocument().addDocumentListener(
new DocumentListener() {
98 public void changedUpdate(DocumentEvent e) {
102 public void removeUpdate(DocumentEvent e) {
106 public void insertUpdate(DocumentEvent e) {
109 private void fire() {
110 firePropertyChange(EVENT.CHANGED.toString(), null, null);
121 boolean hasFields() {
122 return !exePathTextField.getText().isEmpty() && !nameTextField.getText().isEmpty() &&
123 (mimeRadioButton.isSelected() || extRadioButton.isSelected());
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);
144 String name = nameTextField.getText();
145 if (mimeRadioButton.isSelected()) {
146 FileTypeDetector detector;
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);
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);
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);
169 if (name.charAt(0) !=
'.') {
172 return new ExternalViewerRule(name.toLowerCase(), exePath, ExternalViewerRule.RuleType.EXT);
182 @SuppressWarnings(
"unchecked")
184 private
void initComponents() {
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();
195 org.openide.awt.Mnemonics.setLocalizedText(nameLabel,
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.nameLabel.text"));
197 nameTextField.setText(
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.nameTextField.text"));
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"));
203 buttonGroup.add(extRadioButton);
204 org.openide.awt.Mnemonics.setLocalizedText(extRadioButton,
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.extRadioButton.text"));
206 org.openide.awt.Mnemonics.setLocalizedText(exePathLabel,
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.exePathLabel.text"));
208 exePathTextField.setEditable(
false);
209 exePathTextField.setText(
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.exePathTextField.text"));
211 org.openide.awt.Mnemonics.setLocalizedText(browseButton,
org.openide.util.NbBundle.getMessage(AddExternalViewerRulePanel.class,
"AddExternalViewerRulePanel.browseButton.text"));
212 browseButton.addActionListener(
new java.awt.event.ActionListener() {
213 public void actionPerformed(java.awt.event.ActionEvent evt) {
214 browseButtonActionPerformed(evt);
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()
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)))
241 layout.setVerticalGroup(
242 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
243 .addGroup(layout.createSequentialGroup()
245 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
246 .addComponent(nameLabel)
247 .addComponent(mimeRadioButton)
248 .addComponent(extRadioButton))
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))
261 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {
263 fc = chooserHelper.getChooser();
264 fc.setDragEnabled(
false);
265 fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
266 fc.setMultiSelectionEnabled(
false);
267 fc.setFileFilter(exeFilter);
269 int returnState = fc.showOpenDialog(
this);
270 if (returnState == JFileChooser.APPROVE_OPTION) {
271 String path = fc.getSelectedFile().getPath();
272 exePathTextField.setText(path);
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;