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