19 package org.sleuthkit.autopsy.modules.filetypeid;
21 import java.util.ArrayList;
22 import java.util.List;
23 import javax.swing.DefaultListModel;
24 import javax.swing.JOptionPane;
25 import javax.swing.event.ListSelectionEvent;
26 import javax.swing.event.ListSelectionListener;
27 import org.apache.commons.lang.ArrayUtils;
28 import org.apache.commons.lang3.StringUtils;
29 import org.openide.util.NbBundle;
30 import org.openide.util.NbBundle.Messages;
38 @Messages(
"AddFileTypePanel.mimeFormatLabel.text=Form of MIME type should be: media type/media subtype")
39 @SuppressWarnings(
"PMD.SingularField")
40 class AddFileTypePanel extends javax.swing.JPanel {
42 private static final long serialVersionUID = 1L;
44 private AddFileTypeSignatureDialog addSigDialog;
45 private DefaultListModel<FileType.Signature> signaturesListModel;
52 this.signaturesListModel =
new DefaultListModel<>();
53 this.signatureList.setModel(signaturesListModel);
54 this.addTypeListSelectionListener();
67 AddFileTypePanel(FileType toEdit) {
69 this.setComponentValues(toEdit);
77 private void setComponentValues(FileType toEdit) {
78 this.mimeTypeTextField.setText(toEdit.getMimeType());
79 for (Signature sig : toEdit.getSignatures()) {
80 this.signaturesListModel.addElement(sig);
82 this.postHitCheckBox.setSelected(toEdit.shouldCreateInterestingFileHit());
83 this.setNameTextField.setEnabled(toEdit.shouldCreateInterestingFileHit());
84 this.setNameTextField.setText(toEdit.getInterestingFilesSetName());
95 @Messages({
"AddMimeTypePanel.emptySigList.message=Must have at least one signature.",
96 "AddMimeTypePanel.emptySigList.title=Invalid Signature List",
97 "AddMimeTypePanel.emptySetName.message=Interesting files set name is required if alert is requested.",
98 "AddMimeTypePanel.emptySetName.title=Missing Interesting Files Set Name",
99 "# {0} - media subtype",
100 "AddFileTypePanel.nonStandardMIMEType.message="
101 +
"MIME type must be of form: media type/media subtype. Custom/{0} has been suggested instead.",
103 "AddFileTypePanel.containsIllegalCharacter.message=Invalid character in MIME type, {0} has been suggested instead",
104 "AddFileTypePanel.containsIllegalCharacter.title=Invalid Character in MIME Type",
105 "AddFileTypePanel.nonStandardMIMEType.title=Non-standard MIME Type"})
107 FileType getFileType() {
108 String typeName = mimeTypeTextField.getText();
111 if (typeName.isEmpty()) {
112 JOptionPane.showMessageDialog(
this,
113 NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class,
"FileTypeIdGlobalSettingsPanel.JOptionPane.invalidMIMEType.message"),
114 NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class,
"FileTypeIdGlobalSettingsPanel.JOptionPane.invalidMIMEType.title"),
115 JOptionPane.ERROR_MESSAGE);
119 if (typeName.contains(
"\'")) {
120 typeName = typeName.replaceAll(
"[\\']",
"");
121 JOptionPane.showMessageDialog(
this,
122 NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class,
"AddFileTypePanel.containsIllegalCharacter.message", typeName),
123 NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class,
"AddFileTypePanel.containsIllegalCharacter.title"),
124 JOptionPane.WARNING_MESSAGE);
125 mimeTypeTextField.setText(typeName);
130 String[] splitName = typeName.split(
"/");
131 if (splitName.length < 2 || splitName[0].isEmpty()) {
132 JOptionPane.showMessageDialog(
this,
133 NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class,
"AddFileTypePanel.nonStandardMIMEType.message", typeName),
134 NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class,
"AddFileTypePanel.nonStandardMIMEType.title"),
135 JOptionPane.WARNING_MESSAGE);
136 mimeTypeTextField.setText(
"custom/" + typeName);
142 if (!StringUtils.join(ArrayUtils.subarray(splitName, 0, splitName.length),
"/").equals(typeName)) {
143 String rejoinedMimeType = StringUtils.join(ArrayUtils.subarray(splitName, 0, splitName.length),
"/");
144 JOptionPane.showMessageDialog(
this,
145 NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class,
"AddFileTypePanel.nonStandardMIMEType.message", rejoinedMimeType),
146 NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class,
"AddFileTypePanel.nonStandardMIMEType.title"),
147 JOptionPane.WARNING_MESSAGE);
148 mimeTypeTextField.setText(rejoinedMimeType);
151 if (this.signaturesListModel.isEmpty()) {
152 JOptionPane.showMessageDialog(
this,
153 Bundle.AddMimeTypePanel_emptySigList_message(),
154 Bundle.AddMimeTypePanel_emptySigList_title(),
155 JOptionPane.ERROR_MESSAGE);
158 List<Signature> sigList =
new ArrayList<>();
159 for (
int i = 0; i < this.signaturesListModel.getSize(); i++) {
160 sigList.add(this.signaturesListModel.elementAt(i));
164 if (this.postHitCheckBox.isSelected()) {
165 if (this.setNameTextField.getText().isEmpty()) {
166 JOptionPane.showMessageDialog(
this,
167 Bundle.AddMimeTypePanel_emptySetName_message(),
168 Bundle.AddMimeTypePanel_emptySetName_title(),
169 JOptionPane.ERROR_MESSAGE);
173 setName = this.setNameTextField.getText();
175 return new FileType(typeName, sigList, this.postHitCheckBox.isSelected(), setName);
182 private void addTypeListSelectionListener() {
183 this.signatureList.addListSelectionListener(
new ListSelectionListener() {
185 public void valueChanged(ListSelectionEvent e) {
186 if (e.getValueIsAdjusting() ==
false) {
198 private void enableButtons() {
199 if (signatureList.getSelectedIndex() == -1) {
200 editSigButton.setEnabled(
false);
201 deleteSigButton.setEnabled(
false);
203 editSigButton.setEnabled(
true);
204 deleteSigButton.setEnabled(
true);
208 boolean hasSignature() {
209 return !this.signaturesListModel.isEmpty();
217 @SuppressWarnings(
"unchecked")
219 private
void initComponents() {
221 editSigButton =
new javax.swing.JButton();
222 deleteSigButton =
new javax.swing.JButton();
223 mimeTypeLabel =
new javax.swing.JLabel();
224 jScrollPane1 =
new javax.swing.JScrollPane();
225 signatureList =
new javax.swing.JList<>();
226 mimeTypeTextField =
new javax.swing.JTextField();
227 addSigButton =
new javax.swing.JButton();
228 jLabel1 =
new javax.swing.JLabel();
229 postHitCheckBox =
new javax.swing.JCheckBox();
230 setNameLabel =
new javax.swing.JLabel();
231 setNameTextField =
new javax.swing.JTextField();
232 mimeFormatLabel =
new javax.swing.JLabel();
234 editSigButton.setIcon(
new javax.swing.ImageIcon(getClass().getResource(
"/org/sleuthkit/autopsy/images/edit16.png")));
235 org.openide.awt.Mnemonics.setLocalizedText(editSigButton,
org.openide.util.NbBundle.getMessage(AddFileTypePanel.class,
"AddFileTypePanel.editSigButton.text"));
236 editSigButton.addActionListener(
new java.awt.event.ActionListener() {
237 public void actionPerformed(java.awt.event.ActionEvent evt) {
238 editSigButtonActionPerformed(evt);
242 deleteSigButton.setIcon(
new javax.swing.ImageIcon(getClass().getResource(
"/org/sleuthkit/autopsy/images/delete16.png")));
243 org.openide.awt.Mnemonics.setLocalizedText(deleteSigButton,
org.openide.util.NbBundle.getMessage(AddFileTypePanel.class,
"AddFileTypePanel.deleteSigButton.text"));
244 deleteSigButton.addActionListener(
new java.awt.event.ActionListener() {
245 public void actionPerformed(java.awt.event.ActionEvent evt) {
246 deleteSigButtonActionPerformed(evt);
250 mimeTypeLabel.setFont(mimeTypeLabel.getFont().deriveFont(mimeTypeLabel.getFont().getStyle() & ~java.awt.Font.BOLD, 11));
251 org.openide.awt.Mnemonics.setLocalizedText(mimeTypeLabel,
org.openide.util.NbBundle.getMessage(AddFileTypePanel.class,
"AddFileTypePanel.mimeTypeLabel.text"));
253 signatureList.setModel(
new javax.swing.AbstractListModel<FileType.Signature>() {
254 Signature[] signatures = {};
255 public int getSize() {
return signatures.length; }
256 public Signature getElementAt(
int i) {
return signatures[i]; }
258 jScrollPane1.setViewportView(signatureList);
260 mimeTypeTextField.setFont(mimeTypeTextField.getFont().deriveFont(mimeTypeTextField.getFont().getStyle() & ~java.awt.Font.BOLD, 11));
261 mimeTypeTextField.setText(
org.openide.util.NbBundle.getMessage(AddFileTypePanel.class,
"AddFileTypePanel.mimeTypeTextField.text"));
263 addSigButton.setIcon(
new javax.swing.ImageIcon(getClass().getResource(
"/org/sleuthkit/autopsy/images/add16.png")));
264 org.openide.awt.Mnemonics.setLocalizedText(addSigButton,
org.openide.util.NbBundle.getMessage(AddFileTypePanel.class,
"AddFileTypePanel.addSigButton.text"));
265 addSigButton.addActionListener(
new java.awt.event.ActionListener() {
266 public void actionPerformed(java.awt.event.ActionEvent evt) {
267 addSigButtonActionPerformed(evt);
271 org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
org.openide.util.NbBundle.getMessage(AddFileTypePanel.class,
"AddFileTypePanel.jLabel1.text"));
273 org.openide.awt.Mnemonics.setLocalizedText(postHitCheckBox,
org.openide.util.NbBundle.getMessage(AddFileTypePanel.class,
"AddFileTypePanel.postHitCheckBox.text"));
274 postHitCheckBox.addActionListener(
new java.awt.event.ActionListener() {
275 public void actionPerformed(java.awt.event.ActionEvent evt) {
276 postHitCheckBoxActionPerformed(evt);
280 org.openide.awt.Mnemonics.setLocalizedText(setNameLabel,
org.openide.util.NbBundle.getMessage(AddFileTypePanel.class,
"AddFileTypePanel.setNameLabel.text"));
281 setNameLabel.setEnabled(postHitCheckBox.isSelected());
283 setNameTextField.setText(
org.openide.util.NbBundle.getMessage(AddFileTypePanel.class,
"AddFileTypePanel.setNameTextField.text"));
284 setNameTextField.setEnabled(postHitCheckBox.isSelected());
286 org.openide.awt.Mnemonics.setLocalizedText(mimeFormatLabel,
org.openide.util.NbBundle.getMessage(AddFileTypePanel.class,
"AddFileTypePanel.mimeFormatLabel.text"));
288 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(
this);
289 this.setLayout(layout);
290 layout.setHorizontalGroup(
291 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
292 .addGroup(layout.createSequentialGroup()
294 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
295 .addGroup(layout.createSequentialGroup()
296 .addComponent(mimeTypeLabel)
298 .addComponent(mimeTypeTextField))
299 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
300 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
301 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
302 .addComponent(addSigButton)
303 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
304 .addComponent(editSigButton)
305 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
306 .addComponent(deleteSigButton))
307 .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE))
308 .addGroup(layout.createSequentialGroup()
310 .addComponent(setNameLabel)
311 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
312 .addComponent(setNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
313 .addGroup(layout.createSequentialGroup()
314 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
315 .addComponent(jLabel1)
316 .addComponent(postHitCheckBox))
317 .addGap(0, 0, Short.MAX_VALUE))
318 .addGroup(layout.createSequentialGroup()
320 .addComponent(mimeFormatLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
323 layout.setVerticalGroup(
324 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
325 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
327 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
328 .addComponent(mimeTypeLabel)
329 .addComponent(mimeTypeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
330 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
331 .addComponent(mimeFormatLabel)
332 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
333 .addComponent(jLabel1)
335 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
336 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
337 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
338 .addComponent(addSigButton)
339 .addComponent(editSigButton)
340 .addComponent(deleteSigButton))
341 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
342 .addComponent(postHitCheckBox)
343 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
344 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
345 .addComponent(setNameLabel)
346 .addComponent(setNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
347 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
351 private void editSigButtonActionPerformed(java.awt.event.ActionEvent evt) {
352 if (evt.getSource().equals(this.editSigButton) && this.signatureList.getSelectedValue() != null) {
353 int selected = this.signatureList.getSelectedIndex();
354 this.addSigDialog =
new AddFileTypeSignatureDialog(this.signatureList.getSelectedValue());
355 if (addSigDialog.getResult() == BUTTON_PRESSED.OK) {
356 signaturesListModel.removeElementAt(selected);
357 this.signaturesListModel.add(selected, this.addSigDialog.getSignature());
362 private void deleteSigButtonActionPerformed(java.awt.event.ActionEvent evt) {
363 if (this.signatureList.getSelectedIndex() != -1) {
364 signaturesListModel.removeElementAt(this.signatureList.getSelectedIndex());
365 if (!this.signaturesListModel.isEmpty()) {
366 signatureList.setSelectedIndex(0);
368 firePropertyChange(SIG_LIST_CHANGED.toString(), null, null);
372 private void addSigButtonActionPerformed(java.awt.event.ActionEvent evt) {
373 if (evt.getSource().equals(this.addSigButton)) {
374 this.addSigDialog =
new AddFileTypeSignatureDialog();
375 if (addSigDialog.getResult() == AddFileTypeSignatureDialog.BUTTON_PRESSED.OK) {
376 signaturesListModel.addElement(this.addSigDialog.getSignature());
378 firePropertyChange(SIG_LIST_CHANGED.toString(), null, null);
382 private void postHitCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
383 if (evt.getSource().equals(postHitCheckBox)) {
384 this.setNameLabel.setEnabled(postHitCheckBox.isSelected());
385 this.setNameTextField.setEnabled(postHitCheckBox.isSelected());
391 private javax.swing.JButton addSigButton;
392 private javax.swing.JButton deleteSigButton;
393 private javax.swing.JButton editSigButton;
394 private javax.swing.JLabel jLabel1;
395 private javax.swing.JScrollPane jScrollPane1;
396 private javax.swing.JLabel mimeFormatLabel;
397 private javax.swing.JLabel mimeTypeLabel;
398 private javax.swing.JTextField mimeTypeTextField;
399 private javax.swing.JCheckBox postHitCheckBox;
400 private javax.swing.JLabel setNameLabel;
401 private javax.swing.JTextField setNameTextField;
402 private javax.swing.JList<FileType.Signature> signatureList;