Autopsy  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-2016 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;
31 import org.openide.windows.WindowManager;
32 import java.awt.Cursor;
33 
37 class OpenRecentCasePanel extends javax.swing.JPanel {
38 
39  private static final long serialVersionUID = 1L;
40  private static final Logger logger = Logger.getLogger(OpenRecentCasePanel.class.getName());
41  private static OpenRecentCasePanel instance;
42  private static String[] caseNames;
43  private static String[] casePaths;
44  private RecentCasesTableModel model;
45 
50  private OpenRecentCasePanel() {
51  initComponents();
52  }
53 
54  /*
55  * Gets the singleton instance of the panel used by the the open recent case
56  * option of the start window.
57  */
58  static OpenRecentCasePanel getInstance() {
59  if (instance == null) {
60  instance = new OpenRecentCasePanel();
61  }
62  instance.refreshRecentCasesTable();
63  return instance;
64  }
65 
71  void setCloseButtonActionListener(ActionListener listener) {
72  this.cancelButton.addActionListener(listener);
73  }
74 
78  private void refreshRecentCasesTable() {
79  caseNames = RecentCases.getInstance().getRecentCaseNames();
80  casePaths = RecentCases.getInstance().getRecentCasePaths();
81  model = new RecentCasesTableModel();
82  imagesTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
83  imagesTable.setModel(model);
84 
85  int width = tableScrollPane.getPreferredSize().width;
86  imagesTable.getColumnModel().getColumn(0).setPreferredWidth((int) (.30 * width));
87  imagesTable.getColumnModel().getColumn(1).setPreferredWidth((int) (.70 * width));
88  // If there are any images, let's select the first one
89  if (imagesTable.getRowCount() > 0) {
90  imagesTable.setRowSelectionInterval(0, 0);
91  openButton.setEnabled(true);
92  } else {
93  openButton.setEnabled(false);
94  }
95  }
96 
97  /*
98  * Opens the selected case.
99  */
100  private void openCase() {
101  if (casePaths.length < 1) {
102  return;
103  }
104  final String casePath = casePaths[imagesTable.getSelectedRow()];
105  final String caseName = caseNames[imagesTable.getSelectedRow()];
106  if (!casePath.equals("")) {
107  try {
108  StartupWindowProvider.getInstance().close();
109  CueBannerPanel.closeOpenRecentCasesWindow();
110  } catch (Exception ex) {
111  logger.log(Level.SEVERE, "Error closing start up window", ex); //NON-NLS
112  }
113 
114  /*
115  * Open the case.
116  */
117  if (caseName.equals("") || casePath.equals("") || (!new File(casePath).exists())) {
118  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
119  NbBundle.getMessage(this.getClass(), "RecentItems.openRecentCase.msgDlg.text", caseName),
120  NbBundle.getMessage(this.getClass(), "CaseOpenAction.msgDlg.cantOpenCase.title"),
121  JOptionPane.ERROR_MESSAGE);
122  RecentCases.getInstance().removeRecentCase(caseName, casePath); // remove the recent case if it doesn't exist anymore
123  if (Case.isCaseOpen() == false) {
124  StartupWindowProvider.getInstance().open();
125  }
126  } else {
127  SwingUtilities.invokeLater(() -> {
128  WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
129  });
130  new Thread(() -> {
131  try {
132  Case.open(casePath);
133  } catch (CaseActionException ex) {
134  SwingUtilities.invokeLater(() -> {
135  logger.log(Level.SEVERE, String.format("Error opening case with metadata file path %s", casePath), ex); //NON-NLS
136  WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
137  JOptionPane.showMessageDialog(
138  WindowManager.getDefault().getMainWindow(),
139  ex.getMessage(), // Should be user-friendly
140  NbBundle.getMessage(this.getClass(), "CaseOpenAction.msgDlg.cantOpenCase.title"), //NON-NLS
141  JOptionPane.ERROR_MESSAGE);
142  if (!Case.isCaseOpen()) {
143  StartupWindowProvider.getInstance().open();
144  }
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.equals("")) {
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-2016 Basis Technology. Generated on: Tue Oct 25 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.