KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > util > ProgressDialog


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.util;
9
10 import javax.swing.*;
11 import java.awt.*;
12 import java.awt.event.ComponentAdapter JavaDoc;
13 import java.awt.event.ComponentEvent JavaDoc;
14
15 /**
16     Dialog which displays indeterminate progress.
17
18     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
19     @version $Revision: 1.1 $ $Date: 2003/08/18 07:46:43 $
20 */

21 public class ProgressDialog extends JDialog {
22
23     private static final int PROGRESS_BAR_WIDTH = 200;
24
25     private Runnable JavaDoc runnable;
26
27     private JProgressBar progressBar;
28     private JLabel lblMessage;
29
30     /**
31      * Constructor.
32      * @param parent the parent frame.
33      * @param runnable the <tt>Runnable</tt> to be started on <tt>setVisible</tt>.
34      * @param message the initial status message.
35      */

36     public ProgressDialog(JFrame parent, Runnable JavaDoc runnable, String JavaDoc message) {
37         super(parent);
38         init(runnable, message);
39     }
40
41     /**
42      * Constructor.
43      * @param parent the parent dialog.
44      * @param runnable the <tt>Runnable</tt> to be started on <tt>setVisible</tt>.
45      * @param message the initial status message.
46      */

47     public ProgressDialog(JDialog parent, Runnable JavaDoc runnable, String JavaDoc message) {
48         super(parent);
49         init(runnable, message);
50     }
51
52     /**
53      * Set the current status message.
54      * @param message the message.
55      */

56     public void setMessage(String JavaDoc message) {
57         lblMessage.setText(message);
58     }
59
60     /**
61      * Set the <tt>Runnable</tt> to be started on <tt>setVisible</tt>.
62      * @param runnable the <tt>Runnable</tt>.
63      */

64     public void setRunnable(Runnable JavaDoc runnable) {
65         this.runnable = runnable;
66     }
67
68     public void setVisible(boolean visible) {
69         if (visible) {
70             progressBar.setIndeterminate(true);
71             GUIHelper.centerOnParentWindow(this, getOwner());
72         } else {
73             progressBar.setIndeterminate(false);
74         }
75         super.setVisible(visible);
76     }
77
78     private void init(Runnable JavaDoc runnable, String JavaDoc message) {
79         setupControls();
80         setupComponent();
81         setupEventHandlers();
82         setMessage(message);
83         setRunnable(runnable);
84     }
85
86     private void setupControls() {
87
88         progressBar = new JProgressBar();
89         Dimension preferredSize = progressBar.getPreferredSize();
90         preferredSize.width = PROGRESS_BAR_WIDTH;
91         progressBar.setPreferredSize(preferredSize);
92         lblMessage = new JLabel(" ");
93     }
94
95     private void setupComponent() {
96
97         JPanel contentPane = (JPanel)getContentPane();
98         contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
99         contentPane.setLayout(new GridBagLayout());
100         GridBagConstraints gc = new GridBagConstraints();
101         gc.gridx = 0;
102         gc.gridy = GridBagConstraints.RELATIVE;
103         gc.anchor = GridBagConstraints.NORTHWEST;
104         contentPane.add(lblMessage, gc);
105         gc.weightx = 1;
106         gc.fill = GridBagConstraints.HORIZONTAL;
107         contentPane.add(progressBar, gc);
108
109         setTitle(GUIHelper.MESSAGE_TITLE);
110         setModal(true);
111         pack();
112
113     }
114
115     private void setupEventHandlers() {
116
117         addComponentListener(new ComponentAdapter JavaDoc() {
118             public void componentShown(ComponentEvent JavaDoc event) {
119                 final Thread JavaDoc task = new Thread JavaDoc(runnable);
120                 task.start();
121                 new Thread JavaDoc() {
122                     public void run() {
123                         try {
124                             task.join();
125                         } catch (InterruptedException JavaDoc e) {
126                         }
127                         SwingUtilities.invokeLater(new Runnable JavaDoc() {
128                             public void run() {
129                                 setVisible(false);
130                             }
131                         });
132                     }
133                 }.start();
134             }
135         });
136     }
137
138
139 }
140
Popular Tags