KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > context > StateHandler


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
20 package org.openharmonise.him.context;
21
22 import java.awt.*;
23 import java.util.*;
24
25 import javax.swing.*;
26
27 import org.openharmonise.vfs.context.ContextEvent;
28 import org.openharmonise.vfs.context.ContextHandler;
29 import org.openharmonise.vfs.context.ContextType;
30
31 /**
32  * The state handler maintains the "busy" state of the application.
33  * Following the singleton pattern, any object can add a "wait" to the
34  * state handler, which will then inform the UI elements that indicate
35  * this to the user. The state handler will deal with overlapping "waits"
36  * only removing the UI indications when all "waits" have been cleared.
37  *
38  * @author Matthew Large
39  * @version $Revision: 1.1 $
40  *
41  */

42 public class StateHandler {
43
44     /**
45      * Handler instance, following singleton pattern.
46      */

47     private static StateHandler m_instance = null;
48
49     /**
50      * Main frame to set cursor on.
51      */

52     private JFrame m_frame = null;
53
54     /**
55      * List of "wait" names.
56      */

57     private ArrayList m_aWaits = new ArrayList();
58     
59     /**
60      * Message to be displyed in status bar.
61      */

62     private String JavaDoc m_sBarText = null;
63
64     /**
65      *
66      */

67     private StateHandler() {
68         super();
69     }
70     
71     /**
72      * Returns the message to be displayed in the status bar.
73      *
74      * @return Status bar message
75      */

76     public String JavaDoc getBarText() {
77         return this.m_sBarText;
78     }
79     
80     /**
81      * Returns the handler instance, follows the singleton pattern.
82      *
83      * @return Handler instance
84      */

85     public static StateHandler getInstance() {
86         if(m_instance==null) {
87             m_instance = new StateHandler();
88         }
89         return m_instance;
90     }
91     
92     /**
93      * Sets the main application window on which the cursor is to be
94      * set.
95      *
96      * @param frame Main application frame
97      */

98     public void setApplicationWindow(JFrame frame) {
99         this.m_frame = frame;
100     }
101     
102     /**
103      * Adds a "wait" to the state handler.
104      *
105      * @param sWaitName Wait name
106      */

107     public void addWait(String JavaDoc sWaitName) {
108         this.addWait(sWaitName, null);
109     }
110     
111     /**
112      * Adds a "wait" to the state handler.
113      *
114      * @param sWaitName Wait name
115      * @param sBarText Status bar message
116      */

117     public void addWait(String JavaDoc sWaitName, String JavaDoc sBarText) {
118         if(this.m_aWaits.size()==0) {
119             if(sBarText!=null) {
120                 this.m_sBarText = sBarText;
121             }
122             ContextEvent ce = new ContextEvent(ContextType.CONTEXT_WAIT, "ON");
123             ContextHandler.getInstance().fireContextEvent(ce);
124             this.m_frame.setCursor(Cursor.WAIT_CURSOR);
125         }
126         this.m_aWaits.add(sWaitName);
127     }
128     
129     /**
130      * Adds a "wait" to the state handler.
131      *
132      * @param comp Component to set the cursor on, for dialogs
133      * @param sWaitName Wait name
134      */

135     public void addWait(Component comp, String JavaDoc sWaitName) {
136         this.addWait(comp, sWaitName, null);
137     }
138     
139     /**
140      * Adds a "wait" to the state handler.
141      *
142      * @param comp Component to set the cursor on, for dialogs
143      * @param sWaitName Wait name
144      * @param sBarText Status bar message
145      */

146     public void addWait(Component comp, String JavaDoc sWaitName, String JavaDoc sBarText) {
147         comp.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
148         this.addWait(sWaitName, sBarText);
149     }
150     
151     /**
152      * Removes a "wait" from the state handler.
153      *
154      * @param comp Component to set the cursor on, for dialogs
155      * @param sWaitName Wait name
156      */

157     public void removeWait(Component comp, String JavaDoc sWaitName) {
158         comp.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
159         this.removeWait(sWaitName);
160     }
161     
162     /**
163      * Removes a "wait" from the state handler.
164      *
165      * @param sWaitName Wait name
166      */

167     public void removeWait(String JavaDoc sWaitName) {
168         this.m_aWaits.remove(sWaitName);
169         this.m_sBarText = null;
170         if(this.m_aWaits.size()==0) {
171             ContextEvent ce = new ContextEvent(ContextType.CONTEXT_WAIT, "OFF");
172             ContextHandler.getInstance().fireContextEvent(ce);
173             this.m_frame.setCursor(Cursor.DEFAULT_CURSOR);
174         }
175     }
176
177 }
178
Popular Tags