001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.window;
020:
021: import java.awt.*;
022:
023: import javax.swing.*;
024: import javax.swing.border.*;
025:
026: import org.openharmonise.him.context.StateHandler;
027: import org.openharmonise.vfs.context.*;
028:
029: /**
030: * Status bar which sits across the bottom of the application window. Contains
031: * its own paint thread for updating the progress bar even when the main Swing
032: * event thread is busy.
033: *
034: * @author Matthew Large
035: * @version $Revision: 1.1 $
036: *
037: */
038: public class StatusBar extends JPanel implements ContextListener,
039: Runnable {
040:
041: /**
042: * Instance following singleton pattern.
043: */
044: private static StatusBar m_instance = null;
045:
046: /**
047: * Progress bar.
048: */
049: private JProgressBar m_progressBar = null;
050:
051: /**
052: * True if the progress bar is moving.
053: */
054: private boolean m_bProgressMoving = false;
055:
056: /**
057: * True if the entire status bar needs to be repainted.
058: */
059: private boolean m_bFullPrint = false;
060:
061: /**
062: * Text to display in the status bar.
063: */
064: private JLabel m_infoText = null;
065:
066: /**
067: * Constructs a new status bar.
068: *
069: */
070: private StatusBar() {
071: super ();
072: this .setup();
073: }
074:
075: /**
076: * @param arg0
077: */
078: private StatusBar(boolean arg0) {
079: super (arg0);
080: }
081:
082: /**
083: * @param arg0
084: */
085: private StatusBar(LayoutManager arg0) {
086: super (arg0);
087: }
088:
089: /**
090: * @param arg0
091: * @param arg1
092: */
093: private StatusBar(LayoutManager arg0, boolean arg1) {
094: super (arg0, arg1);
095: }
096:
097: /**
098: * Gets the status bar component, follows the singleton pattern.
099: *
100: * @return status bar.
101: */
102: public static StatusBar getInstance() {
103: if (m_instance == null) {
104: m_instance = new StatusBar();
105: }
106:
107: return m_instance;
108: }
109:
110: /**
111: * Initialises this component.
112: *
113: */
114: private void setup() {
115: new Thread(this ).start();
116: ContextHandler.getInstance().addListener(
117: ContextType.CONTEXT_WAIT, this );
118:
119: this .setPreferredSize(new Dimension(200, 20));
120: this .setBorder(BorderFactory
121: .createBevelBorder(BevelBorder.LOWERED));
122: this .setLayout(new BorderLayout());
123:
124: this .m_infoText = new JLabel("Logging in to services...");
125: String fontName = "Dialog";
126: int fontSize = 10;
127: Font font = new Font(fontName, Font.PLAIN, fontSize);
128: this .m_infoText.setFont(font);
129: this .m_infoText.setPreferredSize(new Dimension(300, 20));
130: this .add(this .m_infoText, BorderLayout.WEST);
131:
132: this .m_progressBar = new JProgressBar(0, 200);
133: this .m_progressBar.setIndeterminate(false);
134: this .add(this .m_progressBar, BorderLayout.EAST);
135: }
136:
137: /* (non-Javadoc)
138: * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
139: */
140: public void contextMessage(ContextEvent ce) {
141: if (ce.CONTEXT_TYPE == ContextType.CONTEXT_WAIT) {
142: if (ce.getMessage().equals("ON")) {
143: if (StateHandler.getInstance().getBarText() != null) {
144: this .m_infoText.setText(StateHandler.getInstance()
145: .getBarText());
146: }
147: this .revalidate();
148: this .validate();
149: this .paint(this .getGraphics());
150: this .m_bProgressMoving = true;
151: this .m_bFullPrint = true;
152: } else {
153: this .m_infoText.setText("");
154: this .m_bProgressMoving = false;
155: this .m_progressBar.setValue(0);
156: this .revalidate();
157: this .validate();
158: this .paint(this .getGraphics());
159: }
160: }
161: }
162:
163: /* (non-Javadoc)
164: * @see java.lang.Runnable#run()
165: */
166: public void run() {
167: try {
168: while (true) {
169: if (this .m_bProgressMoving) {
170: if (this .m_bFullPrint) {
171: this .print(this .getGraphics());
172: this .m_bFullPrint = false;
173: } else {
174: if (this .m_progressBar.getValue() >= this .m_progressBar
175: .getMaximum()) {
176: this .m_progressBar.setValue(10);
177: } else {
178: this .m_progressBar
179: .setValue(this .m_progressBar
180: .getValue() + 1);
181: }
182: this .m_progressBar.print(this .m_progressBar
183: .getGraphics());
184: }
185: } else {
186: }
187: Thread.sleep(100);
188: }
189: } catch (InterruptedException e) {
190: e.printStackTrace(System.err);
191: }
192: }
193: }
|