Autopsy  4.4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ModalDialogProgressIndicator.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.progress;
20 
21 import java.awt.Dialog;
22 import java.awt.Frame;
23 import java.awt.event.ActionListener;
24 import javax.annotation.concurrent.GuardedBy;
25 import javax.annotation.concurrent.ThreadSafe;
26 import javax.swing.JDialog;
27 import javax.swing.SwingUtilities;
28 import org.openide.DialogDescriptor;
29 import org.openide.DialogDisplayer;
30 import org.openide.util.HelpCtx;
31 
38 @ThreadSafe
39 public final class ModalDialogProgressIndicator implements ProgressIndicator {
40 
41  private final Frame parent;
42  private final String title;
43  private final ProgressPanel progressPanel;
44  private final Object[] buttonLabels;
45  private final Object focusedButtonLabel;
46  private final ActionListener buttonListener;
47  private Dialog dialog;
48  @GuardedBy("this")
49  private boolean cancelling;
50 
63  public ModalDialogProgressIndicator(Frame parent, String title, Object[] buttonLabels, Object focusedButtonLabel, ActionListener buttonListener) {
64  this.parent = parent;
65  this.title = title;
66  progressPanel = new ProgressPanel();
67  progressPanel.setIndeterminate(true);
68  this.buttonLabels = buttonLabels;
69  this.focusedButtonLabel = focusedButtonLabel;
70  this.buttonListener = buttonListener;
71  }
72 
80  public ModalDialogProgressIndicator(Frame parent, String title) {
81  this.parent = parent;
82  this.title = title;
83  progressPanel = new ProgressPanel();
84  progressPanel.setIndeterminate(true);
85  this.buttonLabels = null;
86  this.focusedButtonLabel = null;
87  this.buttonListener = null;
88  }
89 
97  @Override
98  public synchronized void start(String message, int totalWorkUnits) {
99  cancelling = false;
100  SwingUtilities.invokeLater(() -> {
101  progressPanel.setIndeterminate(false);
102  progressPanel.setMessage(message);
103  progressPanel.setMaximum(totalWorkUnits);
104  displayDialog();
105  });
106  }
107 
114  @Override
115  public synchronized void start(String message) {
116  cancelling = false;
117  SwingUtilities.invokeLater(() -> {
118  progressPanel.setIndeterminate(true);
119  progressPanel.setMessage(message);
120  displayDialog();
121  });
122  }
123 
131  public synchronized void setCancelling(String cancellingMessage) {
132  cancelling = true;
133  SwingUtilities.invokeLater(() -> {
134  progressPanel.setIndeterminate(false);
135  progressPanel.setMessage(cancellingMessage);
136  });
137  }
138 
145  @Override
146  public synchronized void switchToIndeterminate(String message) {
147  if (!cancelling) {
148  SwingUtilities.invokeLater(() -> {
149  progressPanel.setIndeterminate(true);
150  progressPanel.setMessage(message);
151  });
152  }
153  }
154 
163  @Override
164  public synchronized void switchToDeterminate(String message, int workUnitsCompleted, int totalWorkUnits) {
165  if (!cancelling) {
166  SwingUtilities.invokeLater(() -> {
167  progressPanel.setIndeterminate(false);
168  progressPanel.setMessage(message);
169  progressPanel.setMaximum(totalWorkUnits);
170  progressPanel.setCurrent(workUnitsCompleted);
171  });
172  }
173  }
174 
180  @Override
181  public synchronized void progress(String message) {
182  if (!cancelling) {
183  SwingUtilities.invokeLater(() -> {
184  progressPanel.setMessage(message);
185  });
186  }
187  }
188 
196  @Override
197  public synchronized void progress(int workUnitsCompleted) {
198  if (!cancelling) {
199  SwingUtilities.invokeLater(() -> {
200  progressPanel.setCurrent(workUnitsCompleted);
201  });
202  }
203  }
204 
213  @Override
214  public synchronized void progress(String message, int workUnitsCompleted) {
215  if (!cancelling) {
216  SwingUtilities.invokeLater(() -> {
217  progressPanel.setMessage(message);
218  progressPanel.setCurrent(workUnitsCompleted);
219  });
220  }
221  }
222 
226  @Override
227  public synchronized void finish() {
228  SwingUtilities.invokeLater(() -> {
229  this.dialog.setVisible(false);
230  });
231  }
232 
236  private void displayDialog() {
237  if (null != buttonLabels && null != focusedButtonLabel && null != buttonListener) {
238  /*
239  * Dialog with buttons.
240  */
241  DialogDescriptor dialogDescriptor = new DialogDescriptor(
242  progressPanel,
243  title,
244  true,
245  buttonLabels,
246  focusedButtonLabel,
247  DialogDescriptor.BOTTOM_ALIGN,
248  HelpCtx.DEFAULT_HELP,
249  buttonListener);
250  dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
251  } else {
252  /*
253  * Dialog without buttons.
254  */
255  dialog = new JDialog(parent, title, true);
256  dialog.add(progressPanel);
257  dialog.pack();
258  }
259  dialog.setLocationRelativeTo(parent);
260  this.dialog.setVisible(true);
261  }
262 }
synchronized void progress(String message, int workUnitsCompleted)
synchronized void start(String message, int totalWorkUnits)
synchronized void switchToDeterminate(String message, int workUnitsCompleted, int totalWorkUnits)

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.