001: package org.swingml.component;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.util.*;
006: import javax.swing.*;
007:
008: import org.swingml.event.*;
009: import org.swingml.icons.*;
010: import org.swingml.task.*;
011: import org.swingml.task.monitoring.*;
012:
013: /**
014: * @author CrossLogic
015: */
016: public class StatusBar implements ITaskMonitor, IEventListener {
017:
018: private static Map registry = new HashMap();
019:
020: public static StatusBar findStatusBarFor(Container container) {
021: StatusBar result = null;
022: if (container != null) {
023: if (registry.containsKey(container)) {
024: result = (StatusBar) registry.get(container);
025: } else {
026: // try parentage
027: Container parent = container.getParent();
028: while (parent != null && result == null) {
029: if (registry.containsKey(parent)) {
030: result = (StatusBar) registry.get(parent);
031: break;
032: }
033: parent = parent.getParent();
034: }
035: }
036: }
037: return result;
038: }
039:
040: public static void register(StatusBar statusBar, Container container) {
041: registry.put(container, statusBar);
042: }
043:
044: public static void unRegister(Container container) {
045: if (container != null && registry.containsKey(container)) {
046: registry.remove(container);
047: }
048: }
049:
050: private Container bar;
051: private ITask currentTask;
052: private JProgressBar progressBar;
053: private JPanel progressPanel;
054: private JButton showDialogButton;
055: private JPanel statusPanel;
056: private JLabel statusText;
057:
058: public StatusBar(Container container) {
059: // register this status bar to this container
060: register(this , container);
061: ThreadedEventHandler.addListener(this );
062: bar = new JPanel();
063: GridBagLayout layout = new GridBagLayout();
064: GridBagConstraints constraints = new GridBagConstraints();
065: constraints.fill = GridBagConstraints.BOTH;
066: bar.setLayout(layout);
067:
068: statusPanel = new JPanel();
069:
070: // keep from resizing.
071: statusPanel.setMinimumSize(new Dimension(350, 20));
072: statusPanel.setMaximumSize(new Dimension(350, 20));
073: statusPanel.setPreferredSize(new Dimension(350, 20));
074: statusPanel.setBorder(BorderFactory.createLoweredBevelBorder());
075: statusPanel.setLayout(new BorderLayout());
076: statusText = new JLabel(" ");
077: statusPanel.add(statusText, BorderLayout.CENTER);
078: constraints.weightx = .8;
079: constraints.weighty = 1.0;
080: constraints.gridx = 0;
081: constraints.gridy = 0;
082: layout.setConstraints(statusPanel, constraints);
083: bar.add(statusPanel);
084:
085: progressPanel = new JPanel();
086:
087: // keep from resizing.
088: progressPanel.setMinimumSize(new Dimension(120, 20));
089: progressPanel.setMaximumSize(new Dimension(120, 20));
090: progressPanel.setPreferredSize(new Dimension(120, 20));
091: progressPanel.setLayout(new BorderLayout());
092: progressPanel.setBorder(BorderFactory
093: .createLoweredBevelBorder());
094:
095: // icon
096: Icon icon = IconLoader.getIcon(IconLoader.ICON_PROGRESS_DIALOG);
097: showDialogButton = new JButton(icon);
098: if (icon == null) {
099: // No icons. Default to the letter "T" for "Tasks".
100: showDialogButton.setText("T");
101: } else {
102: // Try to set rollover icon, too.
103: Icon rolloverIcon = IconLoader
104: .getIcon(IconLoader.ICON_PROGRESS_DIALOG_OVER);
105: if (rolloverIcon != null) {
106: showDialogButton.setRolloverIcon(rolloverIcon);
107: }
108: }
109: showDialogButton.setSize(10, 10);
110: showDialogButton.setBorder(BorderFactory.createEmptyBorder());
111: showDialogButton.addMouseListener(new MouseListener() {
112:
113: public void mouseClicked(MouseEvent e) {
114: TaskMonitorDialog.getInstance().show(true);
115: }
116:
117: public void mouseEntered(MouseEvent e) {
118: }
119:
120: public void mouseExited(MouseEvent e) {
121: }
122:
123: public void mousePressed(MouseEvent e) {
124: }
125:
126: public void mouseReleased(MouseEvent e) {
127: }
128: });
129: progressPanel.add(showDialogButton, BorderLayout.EAST);
130: constraints.weightx = .2;
131: constraints.weighty = 1.0;
132: constraints.gridx = 1;
133: constraints.gridy = 0;
134: layout.setConstraints(progressPanel, constraints);
135: bar.add(progressPanel);
136: }
137:
138: /**
139: * Implemented to only track one task at a time (currentTask).
140: */
141: public void beginTask(ITask task) {
142: if (currentTask == null) {
143: currentTask = task;
144: String name = currentTask.getName();
145: int totalTicks = currentTask.getTotalStepCount();
146: setMessage(task, "Starting " + name + "... ");
147: // set initial values
148: createProgressBar();
149: getProgressBar().setMinimum(0);
150: getProgressBar().setValue(0);
151: if (totalTicks > 0) {
152: getProgressBar().setIndeterminate(false);
153: getProgressBar().setMaximum(totalTicks);
154: } else {
155: getProgressBar().setIndeterminate(true);
156: getProgressBar().setMaximum(0);
157: getProgressBar().setString("");
158: }
159: // show it
160: getProgressBar().setVisible(true);
161: }
162: }
163:
164: private void createProgressBar() {
165: JProgressBar aBar = new JProgressBar();
166: aBar.setString("");
167: aBar.setIndeterminate(true);
168: progressPanel.add(aBar, BorderLayout.WEST);
169: progressBar = aBar;
170: }
171:
172: private void destroyProgressBar() {
173: getProgressBar().setVisible(false);
174: this .progressPanel.removeAll();
175: setProgressBar(null);
176: }
177:
178: public void disposeOfYourself() {
179: ThreadedEventHandler.removeListener(this );
180: }
181:
182: public void endTask(ITask task) {
183: if (currentTask == task) {
184: setMessage(task, " ");
185: destroyProgressBar();
186: currentTask = null;
187: }
188: }
189:
190: public void eventCompleted(ISwingMLEvent event, int remainingCount) {
191: if (remainingCount == 0) {
192: if (showDialogButton != null) {
193: showDialogButton.setVisible(false);
194: }
195: }
196: }
197:
198: public void eventStarting(ISwingMLEvent event, int runningCount) {
199: if (runningCount > 1) {
200: if (showDialogButton != null) {
201: showDialogButton.setVisible(true);
202: }
203: }
204: }
205:
206: public Container getBar() {
207: return bar;
208: }
209:
210: protected JProgressBar getProgressBar() {
211: if (this .progressBar == null) {
212: createProgressBar();
213: }
214: return this .progressBar;
215: }
216:
217: public JLabel getStatusText() {
218: return statusText;
219: }
220:
221: public void progressMade(ITask task) {
222: progressMade(task, 1);
223: }
224:
225: public void progressMade(ITask task, int ticks) {
226: if (currentTask == task) {
227: int currentTicks = getProgressBar().getValue();
228: currentTicks += ticks;
229: getProgressBar().setValue(currentTicks);
230: }
231: }
232:
233: public void progressMade(ITask task, int ticks, String aMessage) {
234: progressMade(task, ticks);
235: setMessage(task, aMessage);
236: }
237:
238: public void progressMade(ITask task, String aMessage) {
239: progressMade(task, 1);
240: setMessage(task, aMessage);
241: }
242:
243: public void setMessage(ITask task, String text) {
244: if (currentTask == task) {
245: if (text == null) {
246: text = "";
247: }
248: getStatusText().setText(text);
249: }
250: }
251:
252: public void setProgressBar(JProgressBar aBar) {
253: progressBar = aBar;
254: }
255: }
|