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

Copyright © 2012-2018 Basis Technology. Generated on: Tue Dec 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.