Autopsy  4.11.0
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  @Override
132  public synchronized void setCancelling(String cancellingMessage) {
133  cancelling = true;
134  SwingUtilities.invokeLater(() -> {
135  progressPanel.setIndeterminate(false);
136  progressPanel.setMessage(cancellingMessage);
137  });
138  }
139 
146  @Override
147  public synchronized void switchToIndeterminate(String message) {
148  if (!cancelling) {
149  SwingUtilities.invokeLater(() -> {
150  progressPanel.setIndeterminate(true);
151  progressPanel.setMessage(message);
152  });
153  }
154  }
155 
164  @Override
165  public synchronized void switchToDeterminate(String message, int workUnitsCompleted, int totalWorkUnits) {
166  if (!cancelling) {
167  SwingUtilities.invokeLater(() -> {
168  progressPanel.setIndeterminate(false);
169  progressPanel.setMessage(message);
170  progressPanel.setMaximum(totalWorkUnits);
171  progressPanel.setCurrent(workUnitsCompleted);
172  });
173  }
174  }
175 
181  @Override
182  public synchronized void progress(String message) {
183  if (!cancelling) {
184  SwingUtilities.invokeLater(() -> {
185  progressPanel.setMessage(message);
186  });
187  }
188  }
189 
197  @Override
198  public synchronized void progress(int workUnitsCompleted) {
199  if (!cancelling) {
200  SwingUtilities.invokeLater(() -> {
201  progressPanel.setCurrent(workUnitsCompleted);
202  });
203  }
204  }
205 
214  @Override
215  public synchronized void progress(String message, int workUnitsCompleted) {
216  if (!cancelling) {
217  SwingUtilities.invokeLater(() -> {
218  progressPanel.setMessage(message);
219  progressPanel.setCurrent(workUnitsCompleted);
220  });
221  }
222  }
223 
227  @Override
228  public synchronized void finish() {
229  SwingUtilities.invokeLater(() -> {
230  this.dialog.setVisible(false);
231  this.dialog.dispose();
232  });
233  }
234 
238  private void displayDialog() {
239  if (null != buttonLabels && null != focusedButtonLabel && null != buttonListener) {
240  /*
241  * Dialog with buttons.
242  */
243  DialogDescriptor dialogDescriptor = new DialogDescriptor(
244  progressPanel,
245  title,
246  true,
247  buttonLabels,
248  focusedButtonLabel,
249  DialogDescriptor.BOTTOM_ALIGN,
250  HelpCtx.DEFAULT_HELP,
251  buttonListener);
252  dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor, parent);
253  } else {
254  /*
255  * Dialog without buttons.
256  */
257  dialog = new JDialog(parent, title, true);
258  dialog.add(progressPanel);
259  dialog.pack();
260  }
261  dialog.setResizable(false);
262  dialog.setLocationRelativeTo(parent);
263  this.dialog.setVisible(true);
264  }
265 }
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-2018 Basis Technology. Generated on: Fri Jun 21 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.