Autopsy  4.19.3
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-2019 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.apache.commons.lang3.SerializationUtils;
29 import org.openide.DialogDescriptor;
30 import org.openide.DialogDisplayer;
31 import org.openide.util.HelpCtx;
32 
39 @ThreadSafe
40 public final class ModalDialogProgressIndicator implements ProgressIndicator {
41 
42  private final Frame parent;
43  private final String title;
44  private final ProgressPanel progressPanel;
45  private final Object[] buttonLabels;
46  private final Object focusedButtonLabel;
47  private final ActionListener buttonListener;
48  private Dialog dialog;
49  @GuardedBy("this")
50  private boolean cancelling;
51 
64  public ModalDialogProgressIndicator(Frame parent, String title, Object[] buttonLabels, Object focusedButtonLabel, ActionListener buttonListener) {
65  this.parent = parent;
66  this.title = title;
67  progressPanel = new ProgressPanel();
68  progressPanel.setIndeterminate(true);
69  this.buttonLabels = SerializationUtils.clone(buttonLabels);
70  this.focusedButtonLabel = focusedButtonLabel;
71  this.buttonListener = buttonListener;
72  }
73 
81  public ModalDialogProgressIndicator(Frame parent, String title) {
82  this.parent = parent;
83  this.title = title;
84  progressPanel = new ProgressPanel();
85  progressPanel.setIndeterminate(true);
86  this.buttonLabels = null;
87  this.focusedButtonLabel = null;
88  this.buttonListener = null;
89  }
90 
98  @Override
99  public synchronized void start(String message, int totalWorkUnits) {
100  cancelling = false;
101  SwingUtilities.invokeLater(() -> {
102  progressPanel.setIndeterminate(false);
103  progressPanel.setMessage(message);
104  progressPanel.setMaximum(totalWorkUnits);
105  displayDialog();
106  });
107  }
108 
115  @Override
116  public synchronized void start(String message) {
117  cancelling = false;
118  SwingUtilities.invokeLater(() -> {
119  progressPanel.setIndeterminate(true);
120  progressPanel.setMessage(message);
121  displayDialog();
122  });
123  }
124 
132  @Override
133  public synchronized void setCancelling(String cancellingMessage) {
134  cancelling = true;
135  SwingUtilities.invokeLater(() -> {
136  progressPanel.setIndeterminate(false);
137  progressPanel.setMessage(cancellingMessage);
138  });
139  }
140 
147  @Override
148  public synchronized void switchToIndeterminate(String message) {
149  if (!cancelling) {
150  SwingUtilities.invokeLater(() -> {
151  progressPanel.setIndeterminate(true);
152  progressPanel.setMessage(message);
153  });
154  }
155  }
156 
165  @Override
166  public synchronized void switchToDeterminate(String message, int workUnitsCompleted, int totalWorkUnits) {
167  if (!cancelling) {
168  SwingUtilities.invokeLater(() -> {
169  progressPanel.setIndeterminate(false);
170  progressPanel.setMessage(message);
171  progressPanel.setMaximum(totalWorkUnits);
172  progressPanel.setCurrent(workUnitsCompleted);
173  });
174  }
175  }
176 
182  @Override
183  public synchronized void progress(String message) {
184  if (!cancelling) {
185  SwingUtilities.invokeLater(() -> {
186  progressPanel.setMessage(message);
187  });
188  }
189  }
190 
198  @Override
199  public synchronized void progress(int workUnitsCompleted) {
200  if (!cancelling) {
201  SwingUtilities.invokeLater(() -> {
202  progressPanel.setCurrent(workUnitsCompleted);
203  });
204  }
205  }
206 
215  @Override
216  public synchronized void progress(String message, int workUnitsCompleted) {
217  if (!cancelling) {
218  SwingUtilities.invokeLater(() -> {
219  progressPanel.setMessage(message);
220  progressPanel.setCurrent(workUnitsCompleted);
221  });
222  }
223  }
224 
228  @Override
229  public synchronized void finish() {
230  SwingUtilities.invokeLater(() -> {
231  this.dialog.setVisible(false);
232  this.dialog.dispose();
233  });
234  }
235 
239  private void displayDialog() {
240  if (null != buttonLabels && null != focusedButtonLabel && null != buttonListener) {
241  /*
242  * Dialog with buttons.
243  */
244  DialogDescriptor dialogDescriptor = new DialogDescriptor(
245  progressPanel,
246  title,
247  true,
248  buttonLabels,
249  focusedButtonLabel,
250  DialogDescriptor.BOTTOM_ALIGN,
251  HelpCtx.DEFAULT_HELP,
252  buttonListener);
253  dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor, parent);
254  } else {
255  /*
256  * Dialog without buttons.
257  */
258  dialog = new JDialog(parent, title, true);
259  dialog.add(progressPanel);
260  dialog.pack();
261  }
262  dialog.setResizable(false);
263  dialog.setLocationRelativeTo(parent);
264  this.dialog.setVisible(true);
265  }
266 }
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-2022 Basis Technology. Generated on: Sun Sep 25 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.