Autopsy  4.5.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SelectDriveDialog.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package org.sleuthkit.autopsy.livetriage;
7 
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.concurrent.CancellationException;
11 import java.util.logging.Level;
12 import java.awt.Dimension;
13 import java.awt.Point;
14 import javax.swing.SwingWorker;
15 import javax.swing.event.ListSelectionEvent;
16 import javax.swing.event.ListSelectionListener;
17 import javax.swing.event.TableModelListener;
18 import javax.swing.table.TableModel;
19 import org.openide.util.NbBundle;
23 
27 class SelectDriveDialog extends javax.swing.JDialog {
28 
29  private List<LocalDisk> disks = new ArrayList<>();
30  private final LocalDiskModel model = new LocalDiskModel();
31  private final java.awt.Frame parent;
32  private String drivePath = "";
33 
37  @NbBundle.Messages({"SelectDriveDialog.title=Create Live Triage Drive"})
38  SelectDriveDialog(java.awt.Frame parent, boolean modal) {
39  super(parent, modal);
40  initComponents();
41  this.parent = parent;
42 
43  model.loadDisks();
44  bnOk.setEnabled(false);
45  diskTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
46  @Override
47  public void valueChanged(ListSelectionEvent e) {
48  if (diskTable.getSelectedRow() >= 0 && diskTable.getSelectedRow() < disks.size()) {
49  bnOk.setEnabled(true);
50  } else { //The selection changed to nothing valid being selected, such as with ctrl+click
51  bnOk.setEnabled(false);
52  }
53  }
54  });
55  }
56 
57  void display() {
58  this.setTitle(Bundle.SelectDriveDialog_title());
59 
60  final Dimension parentSize = parent.getSize();
61  final Point parentLocationOnScreen = parent.getLocationOnScreen();
62  final Dimension childSize = this.getSize();
63  int x;
64  int y;
65  x = (parentSize.width - childSize.width) / 2;
66  y = (parentSize.height - childSize.height) / 2;
67  x += parentLocationOnScreen.x;
68  y += parentLocationOnScreen.y;
69 
70  setLocation(x, y);
71  setVisible(true);
72  }
73 
74  String getSelectedDrive() {
75  return this.drivePath;
76  }
77 
83  @SuppressWarnings("unchecked")
84  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
85  private void initComponents() {
86 
87  jScrollPane1 = new javax.swing.JScrollPane();
88  diskTable = new javax.swing.JTable();
89  jLabel1 = new javax.swing.JLabel();
90  bnRefresh = new javax.swing.JButton();
91  bnOk = new javax.swing.JButton();
92  errorLabel = new javax.swing.JLabel();
93  jSeparator1 = new javax.swing.JSeparator();
94  bnCancel = new javax.swing.JButton();
95  jScrollPane2 = new javax.swing.JScrollPane();
96  jTextArea1 = new javax.swing.JTextArea();
97 
98  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
99 
100  diskTable.setModel(model);
101  jScrollPane1.setViewportView(diskTable);
102 
103  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(SelectDriveDialog.class, "SelectDriveDialog.jLabel1.text")); // NOI18N
104 
105  org.openide.awt.Mnemonics.setLocalizedText(bnRefresh, org.openide.util.NbBundle.getMessage(SelectDriveDialog.class, "SelectDriveDialog.bnRefresh.text")); // NOI18N
106  bnRefresh.addActionListener(new java.awt.event.ActionListener() {
107  public void actionPerformed(java.awt.event.ActionEvent evt) {
108  bnRefreshActionPerformed(evt);
109  }
110  });
111 
112  org.openide.awt.Mnemonics.setLocalizedText(bnOk, org.openide.util.NbBundle.getMessage(SelectDriveDialog.class, "SelectDriveDialog.bnOk.text")); // NOI18N
113  bnOk.addActionListener(new java.awt.event.ActionListener() {
114  public void actionPerformed(java.awt.event.ActionEvent evt) {
115  bnOkActionPerformed(evt);
116  }
117  });
118 
119  org.openide.awt.Mnemonics.setLocalizedText(errorLabel, org.openide.util.NbBundle.getMessage(SelectDriveDialog.class, "SelectDriveDialog.errorLabel.text")); // NOI18N
120 
121  org.openide.awt.Mnemonics.setLocalizedText(bnCancel, org.openide.util.NbBundle.getMessage(SelectDriveDialog.class, "SelectDriveDialog.bnCancel.text")); // NOI18N
122  bnCancel.addActionListener(new java.awt.event.ActionListener() {
123  public void actionPerformed(java.awt.event.ActionEvent evt) {
124  bnCancelActionPerformed(evt);
125  }
126  });
127 
128  jScrollPane2.setBorder(null);
129 
130  jTextArea1.setBackground(new java.awt.Color(240, 240, 240));
131  jTextArea1.setColumns(20);
132  jTextArea1.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
133  jTextArea1.setRows(5);
134  jTextArea1.setText(org.openide.util.NbBundle.getMessage(SelectDriveDialog.class, "SelectDriveDialog.jTextArea1.text")); // NOI18N
135  jTextArea1.setBorder(null);
136  jScrollPane2.setViewportView(jTextArea1);
137 
138  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
139  getContentPane().setLayout(layout);
140  layout.setHorizontalGroup(
141  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
142  .addGroup(layout.createSequentialGroup()
143  .addContainerGap()
144  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
145  .addGroup(layout.createSequentialGroup()
146  .addComponent(bnRefresh, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
147  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
148  .addComponent(bnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
149  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
150  .addComponent(bnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
151  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
152  .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
153  .addComponent(jSeparator1)
154  .addComponent(errorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
155  .addComponent(jScrollPane2))
156  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
157  );
158  layout.setVerticalGroup(
159  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
160  .addGroup(layout.createSequentialGroup()
161  .addContainerGap()
162  .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
163  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
164  .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
165  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
166  .addComponent(jLabel1)
167  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
168  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
169  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
170  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
171  .addComponent(bnRefresh)
172  .addComponent(bnCancel)
173  .addComponent(bnOk))
174  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
175  .addComponent(errorLabel)
176  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
177  );
178 
179  pack();
180  }// </editor-fold>//GEN-END:initComponents
181 
182  private void bnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnRefreshActionPerformed
183  model.loadDisks();
184  }//GEN-LAST:event_bnRefreshActionPerformed
185 
186  private void bnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnOkActionPerformed
187  if (diskTable.getSelectedRow() >= 0 && diskTable.getSelectedRow() < disks.size()) {
188  LocalDisk selectedDisk = disks.get(diskTable.getSelectedRow());
189  drivePath = selectedDisk.getPath();
190  } else {
191  drivePath = "";
192  }
193  dispose();
194  }//GEN-LAST:event_bnOkActionPerformed
195 
196  private void bnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnCancelActionPerformed
197  dispose();
198  }//GEN-LAST:event_bnCancelActionPerformed
199 
200 
201  // Variables declaration - do not modify//GEN-BEGIN:variables
202  private javax.swing.JButton bnCancel;
203  private javax.swing.JButton bnOk;
204  private javax.swing.JButton bnRefresh;
205  private javax.swing.JTable diskTable;
206  private javax.swing.JLabel errorLabel;
207  private javax.swing.JLabel jLabel1;
208  private javax.swing.JScrollPane jScrollPane1;
209  private javax.swing.JScrollPane jScrollPane2;
210  private javax.swing.JSeparator jSeparator1;
211  private javax.swing.JTextArea jTextArea1;
212  // End of variables declaration//GEN-END:variables
213 
217  @NbBundle.Messages({"SelectDriveDialog.localDiskModel.loading.msg=",
218  "SelectDriveDialog.localDiskModel.nodrives.msg=Executable could not be found",
219  "SelectDriveDialog.diskTable.column1.title=Disk Name",
220  "SelectDriveDialog.diskTable.column2.title=Disk Size",
221  "SelectDriveDialog.errLabel.disksNotDetected.text=Disks were not detected. On some systems it requires admin privileges",
222  "SelectDriveDialog.errLabel.disksNotDetected.toolTipText=Disks were not detected."
223 
224  })
225  private class LocalDiskModel implements TableModel {
226 
227  private LocalDiskThread worker = null;
228  private boolean ready = false;
229  private volatile boolean loadingDisks = false;
230 
231  //private String SELECT = "Select a local disk:";
232  private final String LOADING = NbBundle.getMessage(this.getClass(), "SelectDriveDialog.localDiskModel.loading.msg");
233  private final String NO_DRIVES = NbBundle.getMessage(this.getClass(), "SelectDriveDialog.localDiskModel.nodrives.msg");
234 
235  private void loadDisks() {
236 
237  // if there is a worker already building the lists, then cancel it first.
238  if (loadingDisks && worker != null) {
239  worker.cancel(false);
240  }
241 
242  // Clear the lists
243  errorLabel.setText("");
244  diskTable.setEnabled(false);
245  ready = false;
246  loadingDisks = true;
247  worker = new LocalDiskThread();
248  worker.execute();
249  }
250 
251  @Override
252  public int getRowCount() {
253  if (disks.isEmpty()) {
254  return 0;
255  }
256  return disks.size();
257  }
258 
259  @Override
260  public int getColumnCount() {
261  return 2;
262 
263  }
264 
265  @Override
266  public String getColumnName(int columnIndex) {
267  switch (columnIndex) {
268  case 0:
269  return NbBundle.getMessage(this.getClass(), "SelectDriveDialog.diskTable.column1.title");
270  case 1:
271  return NbBundle.getMessage(this.getClass(), "SelectDriveDialog.diskTable.column2.title");
272  default:
273  return "Unnamed"; //NON-NLS
274  }
275  }
276 
277  @Override
278  public Class<?> getColumnClass(int columnIndex) {
279  return String.class;
280  }
281 
282  @Override
283  public boolean isCellEditable(int rowIndex, int columnIndex) {
284  return false;
285  }
286 
287  @Override
288  public Object getValueAt(int rowIndex, int columnIndex) {
289  if (ready) {
290  if (disks.isEmpty()) {
291  return NO_DRIVES;
292  }
293  switch (columnIndex) {
294  case 0:
295  return disks.get(rowIndex).getName();
296  case 1:
297  return disks.get(rowIndex).getReadableSize();
298  default:
299  return disks.get(rowIndex).getPath();
300  }
301  } else {
302  return LOADING;
303  }
304  }
305 
306  @Override
307  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
308  //setter does nothing they should not be able to modify table
309  }
310 
311  @Override
312  public void addTableModelListener(TableModelListener l) {
313 
314  }
315 
316  @Override
317  public void removeTableModelListener(TableModelListener l) {
318 
319  }
320 
325  class LocalDiskThread extends SwingWorker<Object, Void> {
326 
327  private final Logger logger = Logger.getLogger(LocalDiskThread.class.getName());
328  private List<LocalDisk> partitions = new ArrayList<>();
329 
330  @Override
331  protected Object doInBackground() throws Exception {
332  // Populate the lists
333  partitions = new ArrayList<>();
334  partitions = PlatformUtil.getPartitions();
335  return null;
336  }
337 
342  private void displayErrors() {
343  if (partitions.isEmpty()) {
344  if (PlatformUtil.isWindowsOS()) {
345  errorLabel.setText(
346  NbBundle.getMessage(this.getClass(), "SelectDriveDialog.errLabel.disksNotDetected.text"));
347  errorLabel.setToolTipText(NbBundle.getMessage(this.getClass(),
348  "SelectDriveDialog.errLabel.disksNotDetected.toolTipText"));
349  } else {
350  errorLabel.setText(
351  NbBundle.getMessage(this.getClass(), "SelectDriveDialog.errLabel.drivesNotDetected.text"));
352  errorLabel.setToolTipText(NbBundle.getMessage(this.getClass(),
353  "SelectDriveDialog.errLabel.drivesNotDetected.toolTipText"));
354  }
355  errorLabel.setVisible(true);
356  diskTable.setEnabled(false);
357  }
358  }
359 
360  @Override
361  protected void done() {
362  try {
363  super.get(); //block and get all exceptions thrown while doInBackground()
364  } catch (CancellationException ex) {
365  logger.log(Level.INFO, "Loading local disks was canceled."); //NON-NLS
366  } catch (InterruptedException ex) {
367  logger.log(Level.INFO, "Loading local disks was interrupted."); //NON-NLS
368  } catch (Exception ex) {
369  logger.log(Level.SEVERE, "Fatal error when loading local disks", ex); //NON-NLS
370  } finally {
371  if (!this.isCancelled()) {
372  displayErrors();
373  worker = null;
374  loadingDisks = false;
375  disks = new ArrayList<>();
376  disks.addAll(partitions);
377  if (disks.size() > 0) {
378  diskTable.setEnabled(true);
379  diskTable.clearSelection();
380  }
381  ready = true;
382  }
383  }
384  diskTable.revalidate();
385  }
386  }
387  }
388 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
void setValueAt(Object aValue, int rowIndex, int columnIndex)

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