KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > window > StatusBar


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

19 package org.openharmonise.him.window;
20
21 import java.awt.*;
22
23 import javax.swing.*;
24 import javax.swing.border.*;
25
26 import org.openharmonise.him.context.StateHandler;
27 import org.openharmonise.vfs.context.*;
28
29
30 /**
31  * Status bar which sits across the bottom of the application window. Contains
32  * its own paint thread for updating the progress bar even when the main Swing
33  * event thread is busy.
34  *
35  * @author Matthew Large
36  * @version $Revision: 1.1 $
37  *
38  */

39 public class StatusBar extends JPanel implements ContextListener, Runnable JavaDoc {
40
41     /**
42      * Instance following singleton pattern.
43      */

44     private static StatusBar m_instance = null;
45
46     /**
47      * Progress bar.
48      */

49     private JProgressBar m_progressBar = null;
50     
51     /**
52      * True if the progress bar is moving.
53      */

54     private boolean m_bProgressMoving = false;
55     
56     /**
57      * True if the entire status bar needs to be repainted.
58      */

59     private boolean m_bFullPrint = false;
60     
61     /**
62      * Text to display in the status bar.
63      */

64     private JLabel m_infoText = null;
65
66     /**
67      * Constructs a new status bar.
68      *
69      */

70     private StatusBar() {
71         super();
72         this.setup();
73     }
74
75     /**
76      * @param arg0
77      */

78     private StatusBar(boolean arg0) {
79         super(arg0);
80     }
81
82     /**
83      * @param arg0
84      */

85     private StatusBar(LayoutManager arg0) {
86         super(arg0);
87     }
88
89     /**
90      * @param arg0
91      * @param arg1
92      */

93     private StatusBar(LayoutManager arg0, boolean arg1) {
94         super(arg0, arg1);
95     }
96     
97     /**
98      * Gets the status bar component, follows the singleton pattern.
99      *
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 JavaDoc(this).start();
116         ContextHandler.getInstance().addListener(ContextType.CONTEXT_WAIT, this);
117         
118         this.setPreferredSize( new Dimension(200,20));
119         this.setBorder( BorderFactory.createBevelBorder(BevelBorder.LOWERED) );
120         this.setLayout(new BorderLayout());
121         
122         this.m_infoText = new JLabel("Logging in to services...");
123         String JavaDoc fontName = "Dialog";
124         int fontSize = 10;
125         Font font = new Font(fontName, Font.PLAIN, fontSize);
126         this.m_infoText.setFont(font);
127         this.m_infoText.setPreferredSize( new Dimension(300,20));
128         this.add( this.m_infoText, BorderLayout.WEST );
129         
130         this.m_progressBar = new JProgressBar(0,200);
131         this.m_progressBar.setIndeterminate(false);
132         this.add(this.m_progressBar, BorderLayout.EAST);
133     }
134
135     /* (non-Javadoc)
136      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
137      */

138     public void contextMessage(ContextEvent ce) {
139         if(ce.CONTEXT_TYPE==ContextType.CONTEXT_WAIT) {
140             if(ce.getMessage().equals("ON")) {
141                 if(StateHandler.getInstance().getBarText()!=null) {
142                     this.m_infoText.setText(StateHandler.getInstance().getBarText());
143                 }
144                 this.revalidate();
145                 this.validate();
146                 this.paint(this.getGraphics());
147                 this.m_bProgressMoving = true;
148                 this.m_bFullPrint = true;
149             } else {
150                 this.m_infoText.setText("");
151                 this.m_bProgressMoving = false;
152                 this.m_progressBar.setValue(0);
153                 this.revalidate();
154                 this.validate();
155                 this.paint(this.getGraphics());
156             }
157         }
158     }
159
160     /* (non-Javadoc)
161      * @see java.lang.Runnable#run()
162      */

163     public void run() {
164         try {
165             while(true) {
166                 if(this.m_bProgressMoving) {
167                     if(this.m_bFullPrint) {
168                         this.print(this.getGraphics());
169                         this.m_bFullPrint=false;
170                     } else {
171                         if(this.m_progressBar.getValue()>=this.m_progressBar.getMaximum()) {
172                             this.m_progressBar.setValue(10);
173                         } else {
174                             this.m_progressBar.setValue(this.m_progressBar.getValue()+1);
175                         }
176                         this.m_progressBar.print(this.m_progressBar.getGraphics());
177                     }
178                 } else {
179                 }
180                 Thread.sleep(100);
181             }
182         } catch (InterruptedException JavaDoc e) {
183             e.printStackTrace(System.err);
184         }
185     }
186 }
187
Popular Tags