Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddFileTypeDialog.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2020 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.modules.filetypeid;
20 
21 import java.awt.BorderLayout;
22 import java.awt.Dimension;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.awt.event.WindowAdapter;
26 import java.awt.event.WindowEvent;
27 import java.beans.PropertyChangeEvent;
28 import java.beans.PropertyChangeListener;
29 import javax.swing.BoxLayout;
30 import javax.swing.JButton;
31 import javax.swing.JDialog;
32 import javax.swing.JPanel;
33 import org.openide.util.NbBundle;
34 import org.openide.util.NbBundle.Messages;
35 import org.openide.windows.WindowManager;
36 
40 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
41 class AddFileTypeDialog extends JDialog {
42 
47  enum BUTTON_PRESSED {
48 
49  OK, CANCEL;
50  }
51 
52  private static final long serialVersionUID = 1L;
53  private static final Dimension BUTTON_SIZE = new Dimension(85, 23);
54  private FileType fileType;
55  final private AddFileTypePanel addMimeTypePanel;
56  private BUTTON_PRESSED result;
57  private JButton okButton;
58  private JButton cancelButton;
59 
63  @Messages({"AddMimeTypedialog.title=File Type"})
64  AddFileTypeDialog() {
65  super(WindowManager.getDefault().getMainWindow(), Bundle.AddMimeTypedialog_title(), true);
66  addMimeTypePanel = new AddFileTypePanel();
67  init();
68  }
69 
75  AddFileTypeDialog(FileType fileType) {
76  super(WindowManager.getDefault().getMainWindow(), Bundle.AddMimeTypedialog_title(), true);
77  addMimeTypePanel = new AddFileTypePanel(fileType);
78  init();
79  }
80 
84  @NbBundle.Messages({
85  "AddMimeTypeDialog.addButton.title=OK",
86  "AddMimeTypeDialog.cancelButton.title=Cancel"})
87  private void init() {
88  setLayout(new BorderLayout());
89 
94  add(this.addMimeTypePanel, BorderLayout.PAGE_START);
95 
96  // Add the OK button
97  okButton = new JButton(Bundle.AddMimeTypeDialog_addButton_title());
98  okButton.addActionListener(new ActionListener() {
99  @Override
100  public void actionPerformed(ActionEvent e) {
101  doButtonAction(true);
102  }
103  });
104  //setting both max and preffered size appears to be necessary to change the button size
105  okButton.setMaximumSize(BUTTON_SIZE);
106  okButton.setPreferredSize(BUTTON_SIZE);
107 
108  // Add a close button.
109  cancelButton = new JButton(Bundle.AddMimeTypeDialog_cancelButton_title());
110  cancelButton.addActionListener(new ActionListener() {
111  @Override
112  public void actionPerformed(ActionEvent e) {
113  doButtonAction(false);
114  }
115  });
116  //setting both max and preffered size appears to be necessary to change the button size
117  cancelButton.setMaximumSize(BUTTON_SIZE);
118  cancelButton.setPreferredSize(BUTTON_SIZE);
119 
120  // Put the buttons in their own panel, under the settings panel.
121  JPanel buttonPanel = new JPanel();
122  buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
123  buttonPanel.add(okButton);
124  buttonPanel.add(new javax.swing.Box.Filler(new Dimension(10, 35), new Dimension(10, 35), new Dimension(10, 35)));
125  buttonPanel.add(cancelButton);
126  buttonPanel.add(new javax.swing.Box.Filler(new Dimension(10, 35), new Dimension(10, 35), new Dimension(10, 35)));
127  buttonPanel.validate();
128  add(buttonPanel, BorderLayout.LINE_END);
129 
134  this.addWindowListener(new WindowAdapter() {
135  @Override
136  public void windowClosing(WindowEvent e) {
137  doButtonAction(false);
138  }
139  });
140  this.addMimeTypePanel.addPropertyChangeListener(new PropertyChangeListener() {
141  @Override
142  public void propertyChange(PropertyChangeEvent evt) {
143  if (evt.getPropertyName().equals(AddFileTypePanel.EVENT.SIG_LIST_CHANGED.toString())) {
144  enableOkButton();
145  }
146  }
147  });
148  enableOkButton();
149  setResizable(false);
150  pack();
151  }
152 
157  void display() {
161  setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
165  setVisible(true);
166  }
167 
174  private void doButtonAction(boolean okPressed) {
175  if (okPressed) {
176  FileType fType = addMimeTypePanel.getFileType();
177  if (fType != null) {
178  this.fileType = fType;
179  this.result = BUTTON_PRESSED.OK;
180  setVisible(false);
181  }
182  } else {
183  this.fileType = null;
184  this.result = BUTTON_PRESSED.CANCEL;
185  setVisible(false);
186  }
187  }
188 
194  FileType getFileType() {
195  return fileType;
196  }
197 
203  BUTTON_PRESSED getResult() {
204  return result;
205  }
206 
207  private void enableOkButton() {
208  this.okButton.setEnabled(addMimeTypePanel.hasSignature());
209  }
210 
211 }

Copyright © 2012-2022 Basis Technology. Generated on: Wed Oct 5 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.