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

Copyright © 2012-2018 Basis Technology. Generated on: Fri Mar 22 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.