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:
020: package org.openharmonise.him.context;
021:
022: import java.awt.*;
023: import java.util.*;
024:
025: import javax.swing.*;
026:
027: import org.openharmonise.vfs.context.ContextEvent;
028: import org.openharmonise.vfs.context.ContextHandler;
029: import org.openharmonise.vfs.context.ContextType;
030:
031: /**
032: * The state handler maintains the "busy" state of the application.
033: * Following the singleton pattern, any object can add a "wait" to the
034: * state handler, which will then inform the UI elements that indicate
035: * this to the user. The state handler will deal with overlapping "waits"
036: * only removing the UI indications when all "waits" have been cleared.
037: *
038: * @author Matthew Large
039: * @version $Revision: 1.1 $
040: *
041: */
042: public class StateHandler {
043:
044: /**
045: * Handler instance, following singleton pattern.
046: */
047: private static StateHandler m_instance = null;
048:
049: /**
050: * Main frame to set cursor on.
051: */
052: private JFrame m_frame = null;
053:
054: /**
055: * List of "wait" names.
056: */
057: private ArrayList m_aWaits = new ArrayList();
058:
059: /**
060: * Message to be displyed in status bar.
061: */
062: private String m_sBarText = null;
063:
064: /**
065: *
066: */
067: private StateHandler() {
068: super ();
069: }
070:
071: /**
072: * Returns the message to be displayed in the status bar.
073: *
074: * @return Status bar message
075: */
076: public String getBarText() {
077: return this .m_sBarText;
078: }
079:
080: /**
081: * Returns the handler instance, follows the singleton pattern.
082: *
083: * @return Handler instance
084: */
085: public static StateHandler getInstance() {
086: if (m_instance == null) {
087: m_instance = new StateHandler();
088: }
089: return m_instance;
090: }
091:
092: /**
093: * Sets the main application window on which the cursor is to be
094: * set.
095: *
096: * @param frame Main application frame
097: */
098: public void setApplicationWindow(JFrame frame) {
099: 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 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 sWaitName, String sBarText) {
118: if (this .m_aWaits.size() == 0) {
119: if (sBarText != null) {
120: this .m_sBarText = sBarText;
121: }
122: ContextEvent ce = new ContextEvent(
123: ContextType.CONTEXT_WAIT, "ON");
124: ContextHandler.getInstance().fireContextEvent(ce);
125: this .m_frame.setCursor(Cursor.WAIT_CURSOR);
126: }
127: this .m_aWaits.add(sWaitName);
128: }
129:
130: /**
131: * Adds a "wait" to the state handler.
132: *
133: * @param comp Component to set the cursor on, for dialogs
134: * @param sWaitName Wait name
135: */
136: public void addWait(Component comp, String sWaitName) {
137: this .addWait(comp, sWaitName, null);
138: }
139:
140: /**
141: * Adds a "wait" to the state handler.
142: *
143: * @param comp Component to set the cursor on, for dialogs
144: * @param sWaitName Wait name
145: * @param sBarText Status bar message
146: */
147: public void addWait(Component comp, String sWaitName,
148: String sBarText) {
149: comp.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
150: this .addWait(sWaitName, sBarText);
151: }
152:
153: /**
154: * Removes a "wait" from the state handler.
155: *
156: * @param comp Component to set the cursor on, for dialogs
157: * @param sWaitName Wait name
158: */
159: public void removeWait(Component comp, String sWaitName) {
160: comp.setCursor(Cursor
161: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
162: this .removeWait(sWaitName);
163: }
164:
165: /**
166: * Removes a "wait" from the state handler.
167: *
168: * @param sWaitName Wait name
169: */
170: public void removeWait(String sWaitName) {
171: this .m_aWaits.remove(sWaitName);
172: this .m_sBarText = null;
173: if (this .m_aWaits.size() == 0) {
174: ContextEvent ce = new ContextEvent(
175: ContextType.CONTEXT_WAIT, "OFF");
176: ContextHandler.getInstance().fireContextEvent(ce);
177: this.m_frame.setCursor(Cursor.DEFAULT_CURSOR);
178: }
179: }
180:
181: }
|