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

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