KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > statusbar > StatusBar


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.core.gui.statusbar;
18
19 import java.awt.Component JavaDoc;
20 import java.awt.Insets JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23
24 import javax.swing.BorderFactory JavaDoc;
25 import javax.swing.Icon JavaDoc;
26 import javax.swing.JButton JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JProgressBar JavaDoc;
29 import javax.swing.Timer JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32
33 import org.columba.api.command.IWorkerStatusController;
34 import org.columba.api.statusbar.IStatusBar;
35 import org.columba.api.statusbar.IStatusBarExtension;
36 import org.columba.core.command.TaskManager;
37 import org.columba.core.command.TaskManagerEvent;
38 import org.columba.core.command.TaskManagerListener;
39 import org.columba.core.command.Worker;
40 import org.columba.core.connectionstate.ConnectionStateImpl;
41 import org.columba.core.gui.base.JStatusBar;
42 import org.columba.core.resourceloader.IconKeys;
43 import org.columba.core.resourceloader.ImageLoader;
44
45 /**
46  * A status bar intended to be displayed at the bottom of each window.
47  * <p>
48  * Implementation notes:
49  * <p>
50  * An update timer is used to only update the statusbar, every xx seconds. As a
51  * nice side-effect, all swing method calls happen in the awt-event dispatcher
52  * thread automatically. If we don't do this, we have to wrap every swing method
53  * in a Runnable interface and execute it using SwingUtilities.invokeLater().
54  * <p>
55  * Note, that without an update timer, the statusbar text and most importantly
56  * the progressbar are updated very frequently, using very small updates. But,
57  * because these are called using invokeLater(), all have to be placed in the
58  * awt-event dispatcher queue. This makes things very slow. We discovered, when
59  * moving around 1000 messages and updating the progressbar for every message,
60  * it will take more time to update the statusbar than actually moving the
61  * messages.
62  * <p>
63  * There's another Timer, addWorkerTimer which makes sure that only Workers who
64  * are alive for at most 2000 ms will appear in the statusbar. This prevents the
65  * statusbar from flicker, caused by many smaller tasks which usually tend to
66  * hide the parent task. For example when downloaded POP3 messages and using
67  * filters which move message to different folders. Without the addWorkerTimer
68  * you would see all those little move tasks. Instead now, you only see the POP3
69  * tasks which is much more comfortable for the user.
70  * <p>
71  * There's yet another Timer ;-), clearTextTimer which automatically clears the
72  * statusbar after a delay of 2000 ms.
73  */

74 public class StatusBar extends JStatusBar implements TaskManagerListener,
75         ActionListener JavaDoc, ChangeListener JavaDoc, IStatusBar {
76
77     /**
78      * update status every 10 ms
79      */

80     private static final int UPDATE_TIMER_INTERVAL = 10;
81
82     /**
83      * Constant definining the delay used when using
84      * clearDisplayTextWithDelay(). Defined to be 2000 millisec.
85      */

86     private static final int CLEAR_TIMER_DELAY = 2000;
87
88     /**
89      * time to wait until statusbar will show a tasks progress
90      */

91     private static final int ADDWORKER_TIMER_INTERVAL = 2000;
92
93     protected static Icon JavaDoc onlineIcon = ImageLoader.getSmallIcon(IconKeys.ONLINE);
94
95     protected static Icon JavaDoc offlineIcon = ImageLoader.getSmallIcon(IconKeys.OFFLINE);
96
97     /**
98      * showing status messages
99      */

100     private JLabel JavaDoc label;
101
102     /**
103      * showing progress info
104      */

105     private JProgressBar JavaDoc progressBar;
106
107     
108
109     /**
110      * button opening task manager dialog
111      */

112     //private JButton taskButton;
113

114     /**
115      * Currently displayed worker
116      */

117     private Worker displayedWorker;
118
119     /**
120      * manager of all running tasks
121      */

122     private TaskManager taskManager;
123
124     /**
125      * connection state button
126      */

127     private JButton JavaDoc onlineButton;
128
129     /** Timer to use when clearing status bar text after a certain timeout */
130     private Timer JavaDoc clearTextTimer;
131
132     /**
133      * Timer makes sure that statusbar is only updated every xx ms, to make that
134      * its not getting flooded with too many update notifications
135      */

136     private Timer JavaDoc updateTimer;
137
138     private Timer JavaDoc addWorkerTimer;
139
140     /**
141      * last displayed message
142      */

143     private String JavaDoc lastMessage;
144
145     private TaskManagerEvent currentEvent;
146
147     public StatusBar(TaskManager tm) {
148         super();
149
150         taskManager = tm;
151         tm.addTaskManagerListener(this);
152         ConnectionStateImpl.getInstance().addChangeListener(this);
153
154         // setBorder(BorderFactory.createEmptyBorder(1, 2, 1, 2));
155

156         initComponents();
157
158         // layoutComponents();
159

160         // update connection state
161
stateChanged(null);
162
163         clearTextTimer = new Timer JavaDoc(CLEAR_TIMER_DELAY, this);
164
165         // init update timer
166
updateTimer = new Timer JavaDoc(UPDATE_TIMER_INTERVAL, this);
167         // updateTimer.start();
168

169         addWorkerTimer = new Timer JavaDoc(ADDWORKER_TIMER_INTERVAL, this);
170
171     }
172
173     /**
174      * init components
175      */

176     private void initComponents() {
177
178         label = new JLabel JavaDoc("");
179         label.setAlignmentX(Component.LEFT_ALIGNMENT);
180
181         onlineButton = new JButton JavaDoc();
182         onlineButton.setMargin(new Insets JavaDoc(0,0,0,0));
183         onlineButton.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
184         onlineButton.setRolloverEnabled(true);
185         onlineButton.setActionCommand("ONLINE");
186         onlineButton.addActionListener(this);
187
188         progressBar = new JProgressBar JavaDoc(0, 100);
189
190         progressBar.setStringPainted(false);
191         progressBar.setBorderPainted(false);
192         progressBar.setValue(0);
193
194 // taskButton = new JButton();
195
// taskButton.setIcon(ImageLoader.getImageIcon("group_small.png"));
196
// taskButton.setToolTipText("Show list of running tasks");
197
// taskButton.setRolloverEnabled(true);
198
// taskButton.setActionCommand("TASKMANAGER");
199
// taskButton.addActionListener(this);
200
//
201
// taskButton.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
202

203         setMainLeftComponent(label);
204
205         addRightComponent(progressBar, 100);
206         //addRightComponent(taskButton, 30);
207
addRightComponent(onlineButton, 30);
208     }
209
210     public void addComponent(IStatusBarExtension ext) {
211         if (ext == null)
212             throw new IllegalArgumentException JavaDoc("extension == null");
213
214         addRightComponent(ext.getView());
215     }
216
217     /**
218      * layout components
219      */

220     // protected void layoutComponents() {
221
// setLayout(new BorderLayout());
222
//
223
// leftMainPanel = new JPanel();
224
// leftMainPanel.setLayout(new BorderLayout());
225
//
226
// JPanel taskPanel = new JPanel();
227
// taskPanel.setLayout(new BorderLayout());
228
//
229
// // Border border = getDefaultBorder();
230
// //Border margin = new EmptyBorder(0, 0, 0, 2);
231
//
232
// // taskPanel.setBorder(new CompoundBorder(border, margin));
233
//
234
// taskPanel.add(taskButton, BorderLayout.CENTER);
235
//
236
// leftMainPanel.add(taskPanel, BorderLayout.WEST);
237
// JPanel labelPanel = new JPanel();
238
// labelPanel.setLayout(new BorderLayout());
239
// // margin = new EmptyBorder(0, 10, 0, 10);
240
// // labelPanel.setBorder(new CompoundBorder(border, margin));
241
//
242
// // margin = new EmptyBorder(0, 0, 0, 2);
243
// labelPanel.add(label, BorderLayout.CENTER);
244
//
245
// leftMainPanel.add(labelPanel, BorderLayout.CENTER);
246
//
247
// add(leftMainPanel, BorderLayout.CENTER);
248
//
249
// mainRightPanel = new JPanel();
250
// mainRightPanel.setLayout(new BorderLayout());
251
//
252
// JPanel progressPanel = new JPanel();
253
// progressPanel.setLayout(new BorderLayout());
254
// // progressPanel.setBorder(new CompoundBorder(border, margin));
255
//
256
// progressPanel.add(progressBar, BorderLayout.CENTER);
257
//
258
// JPanel rightPanel = new JPanel();
259
// rightPanel.setLayout(new BorderLayout());
260
//
261
// rightPanel.add(progressPanel, BorderLayout.CENTER);
262
//
263
// JPanel onlinePanel = new JPanel();
264
// onlinePanel.setLayout(new BorderLayout());
265
// // onlinePanel.setBorder(new CompoundBorder(border, margin));
266
//
267
// onlinePanel.add(onlineButton, BorderLayout.CENTER);
268
//
269
// rightPanel.add(onlinePanel, BorderLayout.EAST);
270
// add(rightPanel, BorderLayout.EAST);
271
// }
272
// public Border getDefaultBorder() {
273
// return UIManager.getBorder("TableHeader.cellBorder");
274
// }
275
/**
276      * @see org.columba.api.statusbar.IStatusBar#displayTooltipMessage(java.lang.String)
277      */

278     public void displayTooltipMessage(String JavaDoc message) {
279         label.setText(message);
280     }
281
282     public void workerAdded(TaskManagerEvent e) {
283
284         if (getDisplayedWorker() == null) {
285
286             currentEvent = e;
287
288             if (taskManager.getWorkers().length == 1) {
289                 setDisplayedWorker(currentEvent.getWorker());
290
291                 // update text and progress bar
292
updateTimer.restart();
293
294                 addWorkerTimer.stop();
295             } else {
296
297                 addWorkerTimer.restart();
298             }
299
300         }
301     }
302
303     public void workerRemoved(TaskManagerEvent e) {
304
305         if (e.getWorker() == displayedWorker) {
306
307             // remember last message
308
lastMessage = e.getWorker().getDisplayText();
309
310             // immediately update text and progress bar
311
// updateGui();
312

313             Worker[] workers = taskManager.getWorkers();
314             setDisplayedWorker(workers.length > 0 ? workers[0] : null);
315         }
316
317         // if only one task left
318
if (taskManager.getWorkers().length == 0) {
319
320             // stop update timer
321
updateTimer.stop();
322
323             // set text
324
label.setText(lastMessage);
325
326             // clear text with delay
327
clearTextTimer.restart();
328         }
329
330     }
331
332     public void actionPerformed(ActionEvent JavaDoc e) {
333         if (e.getSource() == updateTimer) {
334             // update timer event
335
updateGui();
336             return;
337         }
338
339         if (e.getSource() == clearTextTimer) {
340
341             // clear label
342
label.setText("");
343
344             // stop clear timer
345
clearTextTimer.stop();
346
347             return;
348         }
349
350         if (e.getSource() == addWorkerTimer) {
351
352             if (taskManager.exists(currentEvent.getWorker())) {
353
354                 setDisplayedWorker(currentEvent.getWorker());
355
356                 // update text and progress bar
357
updateTimer.restart();
358
359                 addWorkerTimer.stop();
360
361                 return;
362             } else {
363
364                 addWorkerTimer.stop();
365                 return;
366             }
367         }
368
369         String JavaDoc command = e.getActionCommand();
370
371         if (command.equals("ONLINE")) {
372             ConnectionStateImpl.getInstance().setOnline(
373                     !ConnectionStateImpl.getInstance().isOnline());
374         } else if (command.equals("TASKMANAGER")) {
375             TaskManagerDialog.createInstance();
376         } else if (command.equals("CANCEL_ACTION")) {
377             displayedWorker.cancel();
378         }
379     }
380
381     /**
382      * Update statusbar with currently selected worker status.
383      * <p>
384      * Runs in awt-event dispatcher thread
385      */

386     private void updateGui() {
387         // System.out.println("update-gui");
388

389         if (displayedWorker != null) {
390             label.setText(displayedWorker.getDisplayText());
391             progressBar.setValue(displayedWorker.getProgressBarValue());
392             progressBar.setMaximum(displayedWorker.getProgessBarMaximum());
393         }
394
395     }
396
397     /**
398      * Sets the worker to be displayed.
399      */

400     protected void setDisplayedWorker(Worker w) {
401         displayedWorker = w;
402
403     }
404
405     /**
406      * Returns the worker currently displayed.
407      */

408     public IWorkerStatusController getDisplayedWorker() {
409         return displayedWorker;
410     }
411
412     /**
413      * Returns the task manager this status bar is attached to.
414      */

415     public TaskManager getTaskManager() {
416         return taskManager;
417     }
418
419     public void stateChanged(ChangeEvent JavaDoc e) {
420         if (ConnectionStateImpl.getInstance().isOnline()) {
421             onlineButton.setIcon(onlineIcon);
422             // TODO (@author fdietz): i18n
423
onlineButton.setToolTipText("You are in ONLINE state");
424         } else {
425             onlineButton.setIcon(offlineIcon);
426             // TODO (@author fdietz): i18n
427
onlineButton.setToolTipText("You are in OFFLINE state");
428         }
429     }
430 }
Popular Tags