Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ProgressWindow.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013 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.timeline;
20 
21 import java.awt.Component;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.KeyEvent;
24 import java.awt.event.WindowAdapter;
25 import java.awt.event.WindowEvent;
26 import javax.annotation.concurrent.Immutable;
27 import javax.swing.AbstractAction;
28 import javax.swing.ActionMap;
29 import javax.swing.GroupLayout;
30 import javax.swing.InputMap;
31 import javax.swing.JComponent;
32 import javax.swing.JFrame;
33 import javax.swing.JLabel;
34 import javax.swing.JOptionPane;
35 import javax.swing.JProgressBar;
36 import javax.swing.KeyStroke;
37 import javax.swing.LayoutStyle;
38 import javax.swing.SwingUtilities;
39 import javax.swing.SwingWorker;
40 import org.openide.awt.Mnemonics;
41 import org.openide.util.NbBundle;
42 import org.openide.windows.WindowManager;
43 
47 public class ProgressWindow extends JFrame {
48 
49  private final SwingWorker<?, ?> worker;
50 
54  public ProgressWindow(Component parent, boolean modal, SwingWorker<?, ?> worker) {
55  super();
57 
58  setLocationRelativeTo(parent);
59 
60  setAlwaysOnTop(modal);
61 
62  //set icon the same as main app
63  SwingUtilities.invokeLater(() -> {
64  setIconImage(WindowManager.getDefault().getMainWindow().getIconImage());
65  });
66 
67  //progressBar.setIndeterminate(true);
68  setName(NbBundle.getMessage(TimeLineTopComponent.class, "Timeline.progressWindow.name"));
69  setTitle(NbBundle.getMessage(TimeLineTopComponent.class, "Timeline.progressWindow.title"));
70  // Close the dialog when Esc is pressed
71  String cancelName = "cancel"; // NON-NLS
72  InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
73 
74  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
75  ActionMap actionMap = getRootPane().getActionMap();
76 
77  actionMap.put(cancelName, new AbstractAction() {
78  @Override
79  public void actionPerformed(ActionEvent e) {
80  cancel();
81  }
82  });
83  this.worker = worker;
84  }
85 
86  public void updateProgress(final int progress) {
87  SwingUtilities.invokeLater(() -> {
88  progressBar.setValue(progress);
89  });
90  }
91 
92  public void updateProgress(final int progress, final String message) {
93  SwingUtilities.invokeLater(() -> {
94  progressBar.setValue(progress);
95  progressBar.setString(message);
96  });
97  }
98 
99  public void updateProgress(final String message) {
100  SwingUtilities.invokeLater(() -> {
101  progressBar.setString(message);
102  });
103  }
104 
105  public void setProgressTotal(final int total) {
106  SwingUtilities.invokeLater(() -> {
107  progressBar.setIndeterminate(false);
108  progressBar.setMaximum(total);
109  progressBar.setStringPainted(true);
110  });
111  }
112 
113  public void updateHeaderMessage(final String headerMessage) {
114  SwingUtilities.invokeLater(() -> {
115  progressHeader.setText(headerMessage);
116  });
117  }
118 
119  public void setIndeterminate() {
120  SwingUtilities.invokeLater(() -> {
121  progressBar.setIndeterminate(true);
122  progressBar.setStringPainted(true);
123  });
124  }
125 
131  @SuppressWarnings("unchecked")
132  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
133  private void initComponents() {
134 
135  progressBar = new JProgressBar();
136  progressHeader = new JLabel();
137 
138  addWindowListener(new WindowAdapter() {
139  public void windowClosing(WindowEvent evt) {
140  closeDialog(evt);
141  }
142  });
143 
144  Mnemonics.setLocalizedText(progressHeader, NbBundle.getMessage(ProgressWindow.class, "ProgressWindow.progressHeader.text")); // NOI18N
145 
146  GroupLayout layout = new GroupLayout(getContentPane());
147  getContentPane().setLayout(layout);
148  layout.setHorizontalGroup(
149  layout.createParallelGroup(GroupLayout.Alignment.LEADING)
150  .addGroup(layout.createSequentialGroup()
151  .addContainerGap()
152  .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
153  .addComponent(progressBar, GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
154  .addGroup(layout.createSequentialGroup()
155  .addComponent(progressHeader)
156  .addGap(0, 0, Short.MAX_VALUE)))
157  .addContainerGap())
158  );
159  layout.setVerticalGroup(
160  layout.createParallelGroup(GroupLayout.Alignment.LEADING)
161  .addGroup(layout.createSequentialGroup()
162  .addContainerGap()
163  .addComponent(progressHeader)
164  .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
165  .addComponent(progressBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
166  .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
167  );
168 
169  pack();
170  }// </editor-fold>//GEN-END:initComponents
171 
175  private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
176  cancel();
177  }//GEN-LAST:event_closeDialog
178 
179  public void cancel() {
180  SwingUtilities.invokeLater(() -> {
181  if (isVisible()) {
182  int showConfirmDialog = JOptionPane.showConfirmDialog(ProgressWindow.this,
183  NbBundle.getMessage(TimeLineTopComponent.class,
184  "Timeline.ProgressWindow.cancel.confdlg.msg"),
185  NbBundle.getMessage(TimeLineTopComponent.class,
186  "Timeline.ProgressWindow.cancel.confdlg.detail"),
187  JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
188  if (showConfirmDialog == JOptionPane.YES_OPTION) {
189  close();
190  }
191  } else {
192  close();
193  }
194  });
195  }
196 
197  public void close() {
198  worker.cancel(false);
199  setVisible(false);
200  dispose();
201  }
202  // Variables declaration - do not modify//GEN-BEGIN:variables
203  private JProgressBar progressBar;
204  private JLabel progressHeader;
205  // End of variables declaration//GEN-END:variables
206 
207  public void update(ProgressUpdate chunk) {
209  if (chunk.getTotal() >= 0) {
210  setProgressTotal(chunk.getTotal());
211  updateProgress(chunk.getProgress(), chunk.getDetailMessage());
212  } else {
215  }
216  }
217 
219  @Immutable
220  public static class ProgressUpdate {
221 
222  private final int progress;
223  private final int total;
224  private final String headerMessage;
225  private final String detailMessage;
226 
227  public int getProgress() {
228  return progress;
229  }
230 
231  public int getTotal() {
232  return total;
233  }
234 
235  public String getHeaderMessage() {
236  return headerMessage;
237  }
238 
239  public String getDetailMessage() {
240  return detailMessage;
241  }
242 
243  public ProgressUpdate(int progress, int total, String headerMessage, String detailMessage) {
244  super();
245  this.progress = progress;
246  this.total = total;
247  this.headerMessage = headerMessage;
248  this.detailMessage = detailMessage;
249  }
250  }
251 }
ProgressUpdate(int progress, int total, String headerMessage, String detailMessage)
void updateHeaderMessage(final String headerMessage)
void closeDialog(java.awt.event.WindowEvent evt)
ProgressWindow(Component parent, boolean modal, SwingWorker<?,?> worker)
void updateProgress(final int progress, final String message)

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.