Debian Codegraph @

freehep-graphicsio/src/main/java/org/freehep/graphicsio/exportchooser/ProgressDialog.java

raw
1 // Copyright 2001 freehep
2 package org.freehep.graphicsio.exportchooser;
3
4 import java.awt.BorderLayout;
5 import java.awt.Component;
6 import java.awt.FlowLayout;
7 import java.awt.GridLayout;
8 import java.awt.Point;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.io.IOException;
12
13 import javax.swing.JButton;
14 import javax.swing.JDialog;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.JProgressBar;
18
19 /**
20 * A dialog showing a progress bar and a label. Pressing the cancel button will
21 * call the cancel method on an
22 * <tt>ExportGraphicsFileTypeAdaptor.CancelThread</tt>. Exceptions occuring
23 * in another thread can be reported to this class and at the end of the thread
24 * queried.
25 *
26 * @author Simon Fischer
27 * @version $Id: freehep-graphicsio/src/main/java/org/freehep/graphicsio/exportchooser/ProgressDialog.java 5641ca92a537 2005/11/26 00:15:35 duns $
28 */
29 public class ProgressDialog extends JDialog {
30
31 private JProgressBar progressBar;
32
33 private JLabel label;
34
35 private int progress;
36
37 private IOException exception;
38
39 private AbstractExportFileType.CancelThread thread;
40
41 public ProgressDialog(Component parent, int steps, String string) {
42 setTitle("Exporting file...");
43 setModal(true);
44 this.exception = null;
45 this.progress = 0;
46 getContentPane().setLayout(new BorderLayout());
47 JPanel center = new JPanel(new GridLayout(2, 1));
48 progressBar = new JProgressBar(0, steps);
49 center.add(progressBar);
50 label = new JLabel(string);
51 center.add(label);
52 getContentPane().add(center, BorderLayout.CENTER);
53 JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
54 JButton cancel = new JButton("Cancel");
55 cancel.addActionListener(new ActionListener() {
56 public void actionPerformed(ActionEvent e) {
57 cancel();
58 }
59 });
60 buttons.add(cancel);
61 getContentPane().add(buttons, BorderLayout.SOUTH);
62 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
63 pack();
64
65 if (parent != null) {
66 Point pLoc = parent.getLocation();
67 setLocation(pLoc.x + parent.getWidth() / 2 - getWidth() / 2, pLoc.y
68 + parent.getHeight() / 2 - getHeight() / 2);
69 }
70 }
71
72 public void step(String string) {
73 progressBar.setValue(++progress);
74 label.setText(string);
75 repaint();
76 }
77
78 public void exceptionOccured(IOException e) {
79 this.exception = e;
80 }
81
82 public IOException getException() {
83 return exception;
84 }
85
86 public void interruptOnCancel(AbstractExportFileType.CancelThread thread) {
87 this.thread = thread;
88 }
89
90 public void cancel() {
91 if (thread != null)
92 thread.cancel();
93 try {
94 thread.join();
95 } catch (InterruptedException e) {
96 e.printStackTrace();
97 }
98 dispose();
99 }
100 }