Autopsy  4.4
Graphical digital forensics platform for The Sleuth Kit and other tools.
OpenRecentCasePanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2017 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.casemodule;
20 
21 import java.awt.event.ActionListener;
22 import java.awt.event.KeyEvent;
23 import java.io.File;
24 import java.util.logging.Level;
25 import javax.swing.JOptionPane;
26 import javax.swing.JTable;
27 import javax.swing.SwingUtilities;
28 import javax.swing.table.AbstractTableModel;
29 import org.openide.util.NbBundle;
30 import org.openide.windows.WindowManager;
32 
36 class OpenRecentCasePanel extends javax.swing.JPanel {
37 
38  private static final long serialVersionUID = 1L;
39  private static final Logger logger = Logger.getLogger(OpenRecentCasePanel.class.getName());
40  private static OpenRecentCasePanel instance;
41  private static String[] caseNames;
42  private static String[] casePaths;
43  private RecentCasesTableModel model;
44 
49  private OpenRecentCasePanel() {
50  initComponents();
51  }
52 
53  /*
54  * Gets the singleton instance of the panel used by the the open recent case
55  * option of the start window.
56  */
57  static OpenRecentCasePanel getInstance() {
58  if (instance == null) {
59  instance = new OpenRecentCasePanel();
60  }
61  instance.refreshRecentCasesTable();
62  return instance;
63  }
64 
70  void setCloseButtonActionListener(ActionListener listener) {
71  this.cancelButton.addActionListener(listener);
72  }
73 
77  private void refreshRecentCasesTable() {
78  caseNames = RecentCases.getInstance().getRecentCaseNames();
79  casePaths = RecentCases.getInstance().getRecentCasePaths();
80  model = new RecentCasesTableModel();
81  imagesTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
82  imagesTable.setModel(model);
83 
84  int width = tableScrollPane.getPreferredSize().width;
85  imagesTable.getColumnModel().getColumn(0).setPreferredWidth((int) (.30 * width));
86  imagesTable.getColumnModel().getColumn(1).setPreferredWidth((int) (.70 * width));
87  // If there are any images, let's select the first one
88  if (imagesTable.getRowCount() > 0) {
89  imagesTable.setRowSelectionInterval(0, 0);
90  openButton.setEnabled(true);
91  } else {
92  openButton.setEnabled(false);
93  }
94  }
95 
96  /*
97  * Opens the selected case.
98  */
99  private void openCase() {
100  if (casePaths.length < 1) {
101  return;
102  }
103  final String caseMetadataFilePath = casePaths[imagesTable.getSelectedRow()];
104  final String caseName = caseNames[imagesTable.getSelectedRow()];
105  if (!caseMetadataFilePath.isEmpty()) {
106  try {
107  StartupWindowProvider.getInstance().close();
108  CueBannerPanel.closeOpenRecentCasesWindow();
109  } catch (Exception ex) {
110  logger.log(Level.SEVERE, "Error closing start up window", ex); //NON-NLS
111  }
112 
113  /*
114  * Open the case.
115  */
116  if (caseName.isEmpty() || caseMetadataFilePath.isEmpty() || (!new File(caseMetadataFilePath).exists())) {
117  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
118  NbBundle.getMessage(this.getClass(), "RecentItems.openRecentCase.msgDlg.text", caseName),
119  NbBundle.getMessage(this.getClass(), "CaseOpenAction.msgDlg.cantOpenCase.title"),
120  JOptionPane.ERROR_MESSAGE);
121  RecentCases.getInstance().removeRecentCase(caseName, caseMetadataFilePath); // remove the recent case if it doesn't exist anymore
122  StartupWindowProvider.getInstance().open();
123  } else {
124  new Thread(() -> {
125  try {
126  Case.openAsCurrentCase(caseMetadataFilePath);
127  } catch (CaseActionException ex) {
128  SwingUtilities.invokeLater(() -> {
129  if (!(ex instanceof CaseActionCancelledException)) {
130  logger.log(Level.SEVERE, String.format("Error opening case with metadata file path %s", caseMetadataFilePath), ex); //NON-NLS
131  JOptionPane.showMessageDialog(
132  WindowManager.getDefault().getMainWindow(),
133  ex.getMessage(),
134  NbBundle.getMessage(OpenRecentCasePanel.this.getClass(), "CaseOpenAction.msgDlg.cantOpenCase.title"), //NON-NLS
135  JOptionPane.ERROR_MESSAGE);
136  }
137  StartupWindowProvider.getInstance().open();
138  });
139  }
140  }).start();
141  }
142  }
143  }
144 
148  private class RecentCasesTableModel extends AbstractTableModel {
149 
150  private static final long serialVersionUID = 1L;
151 
152  @Override
153  public int getRowCount() {
154  int count = 0;
155  for (String s : caseNames) {
156  if (!s.isEmpty()) {
157  count++;
158  }
159  }
160  return count;
161  }
162 
163  @Override
164  public int getColumnCount() {
165  return 2;
166  }
167 
168  @Override
169  public String getColumnName(int column) {
170  String colName = null;
171  switch (column) {
172  case 0:
173  colName = NbBundle.getMessage(OpenRecentCasePanel.class, "OpenRecentCasePanel.colName.caseName");
174  break;
175  case 1:
176  colName = NbBundle.getMessage(OpenRecentCasePanel.class, "OpenRecentCasePanel.colName.path");
177  break;
178  default:
179  break;
180  }
181  return colName;
182  }
183 
184  @Override
185  public Object getValueAt(int rowIndex, int columnIndex) {
186  Object ret = null;
187  switch (columnIndex) {
188  case 0:
189  ret = caseNames[rowIndex];
190  break;
191  case 1:
192  ret = shortenPath(casePaths[rowIndex]);
193  break;
194  default:
195  logger.log(Level.SEVERE, "Invalid table column index: {0}", columnIndex); //NON-NLS
196  break;
197  }
198  return ret;
199  }
200 
201  @Override
202  public boolean isCellEditable(int rowIndex, int columnIndex) {
203  return false;
204  }
205 
206  @Override
207  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
208  }
209 
217  private String shortenPath(String path) {
218  String shortenedPath = path;
219  if (shortenedPath.length() > 50) {
220  shortenedPath = path.substring(0, 10 + path.substring(10).indexOf(File.separator) + 1) + "..."
221  + path.substring((path.length() - 20) + path.substring(path.length() - 20).indexOf(File.separator));
222  }
223  return shortenedPath;
224  }
225  }
226 
232  @SuppressWarnings("unchecked")
233  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
234  private void initComponents() {
235 
236  jLabel1 = new javax.swing.JLabel();
237  cancelButton = new javax.swing.JButton();
238  openButton = new javax.swing.JButton();
239  tableScrollPane = new javax.swing.JScrollPane();
240  imagesTable = new javax.swing.JTable();
241 
242  jLabel1.setText(org.openide.util.NbBundle.getMessage(OpenRecentCasePanel.class, "OpenRecentCasePanel.jLabel1.text")); // NOI18N
243 
244  cancelButton.setText(org.openide.util.NbBundle.getMessage(OpenRecentCasePanel.class, "OpenRecentCasePanel.cancelButton.text")); // NOI18N
245 
246  openButton.setText(org.openide.util.NbBundle.getMessage(OpenRecentCasePanel.class, "OpenRecentCasePanel.openButton.text")); // NOI18N
247  openButton.addActionListener(new java.awt.event.ActionListener() {
248  public void actionPerformed(java.awt.event.ActionEvent evt) {
249  openButtonActionPerformed(evt);
250  }
251  });
252 
253  imagesTable.setModel(new javax.swing.table.DefaultTableModel(
254  new Object [][] {
255 
256  },
257  new String [] {
258 
259  }
260  ));
261  imagesTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
262  imagesTable.setShowHorizontalLines(false);
263  imagesTable.setShowVerticalLines(false);
264  imagesTable.getTableHeader().setReorderingAllowed(false);
265  imagesTable.setUpdateSelectionOnSort(false);
266  imagesTable.addMouseListener(new java.awt.event.MouseAdapter() {
267  public void mouseClicked(java.awt.event.MouseEvent evt) {
268  imagesTableMouseClicked(evt);
269  }
270  });
271  imagesTable.addKeyListener(new java.awt.event.KeyAdapter() {
272  public void keyPressed(java.awt.event.KeyEvent evt) {
273  imagesTableKeyPressed(evt);
274  }
275  });
276  tableScrollPane.setViewportView(imagesTable);
277 
278  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
279  this.setLayout(layout);
280  layout.setHorizontalGroup(
281  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
282  .addGroup(layout.createSequentialGroup()
283  .addContainerGap()
284  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
285  .addGroup(layout.createSequentialGroup()
286  .addComponent(jLabel1)
287  .addGap(292, 414, Short.MAX_VALUE))
288  .addGroup(layout.createSequentialGroup()
289  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
290  .addComponent(tableScrollPane)
291  .addGroup(layout.createSequentialGroup()
292  .addGap(0, 0, Short.MAX_VALUE)
293  .addComponent(openButton)
294  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
295  .addComponent(cancelButton)))
296  .addContainerGap())))
297  );
298  layout.setVerticalGroup(
299  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
300  .addGroup(layout.createSequentialGroup()
301  .addContainerGap()
302  .addComponent(jLabel1)
303  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
304  .addComponent(tableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
305  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
306  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
307  .addComponent(cancelButton)
308  .addComponent(openButton))
309  .addContainerGap())
310  );
311  }// </editor-fold>//GEN-END:initComponents
312 
313  private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
314  openCase();
315  }//GEN-LAST:event_openButtonActionPerformed
316 
317  private void imagesTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imagesTableMouseClicked
318  // If it's a doubleclick
319  if (evt.getClickCount() == 2) {
320  openCase();
321  }
322  }//GEN-LAST:event_imagesTableMouseClicked
323 
324  private void imagesTableKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_imagesTableKeyPressed
325  if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
326  openCase();
327  }
328  }//GEN-LAST:event_imagesTableKeyPressed
329 
330  // Variables declaration - do not modify//GEN-BEGIN:variables
331  private javax.swing.JButton cancelButton;
332  private javax.swing.JTable imagesTable;
333  private javax.swing.JLabel jLabel1;
334  private javax.swing.JButton openButton;
335  private javax.swing.JScrollPane tableScrollPane;
336  // End of variables declaration//GEN-END:variables
337 
338 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:161

Copyright © 2012-2016 Basis Technology. Generated on: Tue Jun 13 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.