1 /***
2 * MyProgressBar.java
3 *
4 * Project: Dependency Tool
5 *
6 * WHEN WHO WHAT
7 * 06.06.2003 pko initial public release
8 * 31.01.2002 ctr creation
9 *
10 * Copyright 2003 ELCA Informatique SA
11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland
12 * www.elca.ch
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public License
16 * as published by the Free Software Foundation; either version 2.1 of
17 * the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * USA
28 */
29
30 package ch.elca.dependency.gui;
31
32 import java.awt.BorderLayout;
33 import java.awt.Cursor;
34 import java.awt.Frame;
35 import java.awt.event.*;
36 import javax.swing.*;
37
38 import org.apache.log4j.Category;
39
40 /***
41 * Customized progress bar which does not need to know the time of a task.
42 * It's just for showing the user that some regular operations are performed.
43 * With some animation and a corresponding message string which describes the
44 * task which is performed at this moment.
45 * This feature will be included in a frther release.
46 *
47 * @see ch.elca.dependency.gui.MainFrame
48 *
49 * @author Christoph Trutmann
50 * @version 1.0-beta
51 */
52 public class MyProgressBar extends JDialog {
53
54 /***
55 * <code>JProgressBar</code> displayed on the dialog.
56 */
57 private JProgressBar m_progressBar;
58
59 /***
60 * Button to cancel the whole application in case of a "hanging"
61 * application.
62 */
63 private JButton m_cancelButton;
64
65 /***
66 * Specifies the name of the current performing task.
67 */
68 private JLabel m_taskNameLabel;
69
70 /***
71 * Log4j category to invoke the log statements.
72 */
73 private static final Category CAT
74 = Category.getInstance(MyProgressBar.class);
75
76 private Thread m_runner;
77
78 private boolean m_isRunning;
79
80
81 /***
82 * Constructor - Creates a non modal progress bar with no owner.
83 */
84 public MyProgressBar() {
85 super();
86 init();
87 m_runner = new ProgressBarRunner();
88 }
89
90
91 /***
92 * Constructor - Creates a non modal progress bar with the specified owner.
93 *
94 * @param owner The owner frame of this progress bar.
95 */
96 public MyProgressBar(Frame owner) {
97 super(owner);
98 init();
99 m_runner = new ProgressBarRunner();
100 }
101
102
103 /***
104 * Initializes the gui components of the progress bar dialog.
105 */
106 public void init() {
107
108 m_taskNameLabel = new JLabel();
109 m_progressBar = new JProgressBar(0, 100);
110
111 // enable the string in the progress bar for displaying detailed infos
112 m_progressBar.setStringPainted(true);
113 m_progressBar.setString("");
114
115 m_cancelButton = new JButton("Cancel");
116 m_cancelButton.setActionCommand("cancel");
117 m_cancelButton.addActionListener(new ButtonListener());
118
119 JPanel panel1 = new JPanel();
120 panel1.add(m_cancelButton);
121
122 // panel containing all the gui components
123 JPanel contentPane = new JPanel();
124 contentPane.setLayout(new BorderLayout());
125 contentPane.add(m_taskNameLabel, BorderLayout.NORTH);
126 JPanel panel2 = new JPanel();
127 panel2.setBorder(BorderFactory.createEmptyBorder(5, 0, 15, 0));
128 panel2.add(m_progressBar);
129 contentPane.add(panel2, BorderLayout.CENTER);
130 contentPane.add(panel1, BorderLayout.SOUTH);
131 contentPane.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
132 setContentPane(contentPane);
133
134 pack();
135 setResizable(false);
136 setLocation(500, 400);
137
138 this.addWindowListener(new WindowAdapter() {
139 public void windowClosing(WindowEvent e) {
140 System.exit(0);
141 }
142 });
143 }
144
145
146 /***
147 * Displays the progress bar at the start of the task.
148 */
149 public void start() {
150 getOwner().setCursor(new Cursor(Cursor.WAIT_CURSOR));
151 setVisible(true);
152 m_isRunning = true;
153 m_runner.start();
154 }
155
156
157 /***
158 * Hides the progress bar at the end of the task.
159 */
160 public void stop() {
161 setVisible(false);
162 m_isRunning = false;
163
164 try{
165 m_runner.interrupt();
166 m_runner.join();
167 }
168 catch ( Exception e) {
169 CAT.error("The stop method of the progress bar failed.");
170 }
171 getOwner().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
172 }
173
174
175 /***
176 * Sets the message string above the progress bar.
177 * This task name is displayed abovethe progress bar.
178 *
179 * @param msg Name of the task.
180 */
181 public void setTaskName(String msg) {
182 m_taskNameLabel.setText(msg);
183 }
184
185
186 /***
187 * Set the string in the progress bar.
188 * This is useful for showing the user detailed progress informations. This
189 * message is displayed in the progress bar.
190 */
191 public void setString(String msg) {
192 m_progressBar.setString(msg);
193 }
194
195
196 /***
197 * The actionPerformed method in this class
198 * is called when the user presses the start button.
199 */
200 class ButtonListener implements ActionListener {
201
202 private int count;
203
204 /***
205 * Handle the action event of the buttons.
206 */
207 public void actionPerformed(ActionEvent evt) {
208 if ( evt.getActionCommand().equals("cancel") ) {
209 System.exit(0);
210 }
211 }
212 }
213
214
215 /***
216 * Performing the visualisation of the progress bar in a separate thread.
217 */
218 class ProgressBarRunner extends Thread {
219
220 /***
221 * Constructor - Sets the minimal priority.
222 */
223 public ProgressBarRunner() {
224 }
225
226
227 /***
228 * Runs the progress bar.
229 */
230 public void run() {
231
232 int i=0;
233 while ( true ) {
234 m_progressBar.setValue(i%110);
235
236 try {
237 if ( m_isRunning ) {
238 sleep(800);
239 }
240 yield();
241 }
242 catch ( Exception e ) {
243 CAT.error("The run method of the class ProgressBarRunner" +
244 " failed.");
245 }
246
247 i += 10;
248 }
249 }
250 }
251 }
This page was automatically generated by Maven