Source code: javatools/swing/StatusLabelSync.java
1 /*
2 * StatusLabelSync.java
3 *
4 * Created on 23 ottobre 2002, 12.09
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.swing;
26
27 import java.util.*;
28 import java.text.MessageFormat;
29 import javatools.util.RunningRunnable;
30
31 /** It is a thread to control messages displayed into a status label. Use this
32 * sequence of commands:
33 * <CODE>StatusLabelSync sync = new StatusLabelSync();
34 * sync.setStatusLabel(myOwnJLabel);
35 * sync.start();
36 * -- repeat
37 * sync.setMessage(theMessageGroup, theMessageToBeDisplayed);
38 * // make what you need
39 * sync.removeMessage(theMessageGroup);
40 * -- return to repeat while you need it.
41 * sync.stopAll();</CODE>
42 *
43 * @author Antonio Petrelli
44 * @version 0.1.8
45 */
46 public class StatusLabelSync extends java.lang.Thread implements RunningRunnable {
47
48 /** Creates a new instance of StatusLabelSync */
49 public StatusLabelSync() {
50 javatoolsBundle = java.util.ResourceBundle.getBundle("res/JavatoolsBundle");
51 name2message = new HashMap();
52 formatter = new MessageFormat("");
53 stoppedExternally = false;
54 }
55
56 /** Thread's run method.
57 */
58 public void run() {
59 statusLabel.setText(javatoolsBundle.getString("Ready"));
60 doShowLabel();
61 }
62
63 /** Stops the thread.
64 */
65 public synchronized void stopAll() {
66 stoppedExternally = true;
67 notifyAll();
68 }
69
70 /** Sets the status label in which messages will be displayed.
71 * @param pStatusLabel The label to use.
72 */
73 public void setStatusLabel(javax.swing.JLabel pStatusLabel) {
74 statusLabel = pStatusLabel;
75 }
76
77 /** Sets the message to be displayed. It will be displayed only if the message is
78 * lonely, otherwise a message of the type "X concurrent operations under execution"
79 * will be displayed.
80 * @param threadName The message group.
81 * @param message The message to be displayed.
82 */
83 public synchronized void setMessage(String threadName, String message) {
84 name2message.put(threadName, message);
85 notifyAll();
86 }
87
88 /** Removes the message previously set.
89 * @param threadName The message group.
90 */
91 public synchronized void removeMessage(String threadName) {
92 name2message.remove(threadName);
93 notifyAll();
94 }
95
96
97 /** Effectively shows the string of the status label.
98 */
99 public synchronized void wakeUp() {
100 int mapSize;
101 Iterator mapIt;
102 String pattern, output;
103
104 mapSize = name2message.size();
105 switch (mapSize) {
106 case 0:
107 statusLabel.setText(javatoolsBundle.getString("Ready"));
108 break;
109 case 1:
110 mapIt = name2message.values().iterator();
111 statusLabel.setText((String) mapIt.next());
112 break;
113 default:
114 pattern = javatoolsBundle.getString("ConcurrentOperations");
115 formatter.applyPattern(pattern);
116 tempArray[0] = Integer.toString(mapSize);
117 output = formatter.format(tempArray);
118 statusLabel.setText(output);
119 }
120 }
121
122 private javax.swing.JLabel statusLabel;
123 private HashMap name2message;
124 private boolean stoppedExternally;
125 private MessageFormat formatter;
126 private java.util.ResourceBundle javatoolsBundle;
127 private Object[] tempArray;
128
129 private synchronized void doShowLabel() {
130
131 tempArray = new Object[1];
132
133 while (!stoppedExternally) {
134 try {
135 wait();
136 }
137 catch (InterruptedException e) {
138 System.out.println(e.getMessage());
139 }
140
141 if (stoppedExternally)
142 return;
143
144 MoreSwingUtilities.invokeWakingUp(this);
145 }
146 }
147 }