001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import jacareto.cleverphl.CleverPHL;
027: import jacareto.cleverphl.menu.CleverPHLAction;
028: import jacareto.cleverphl.session.Session;
029: import jacareto.cleverphl.session.SessionEvent;
030: import jacareto.cleverphl.session.SessionList;
031: import jacareto.cleverphl.session.SessionListEvent;
032: import jacareto.cleverphl.session.SessionListListener;
033: import jacareto.cleverphl.session.SessionListener;
034: import jacareto.system.EnvironmentMember;
035:
036: import java.util.Collection;
037: import java.util.Iterator;
038:
039: import javax.swing.Action;
040:
041: /**
042: * Stores the actual state of the menu. Some menu items should be enabled in some states an
043: * disabled in other states. Whenever the menu state changes this instance enables or disables the
044: * menu items dependent on the value of the <code>STATES_ENABLED</code> property of their actions.
045: *
046: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
047: * @version 1.0
048: */
049: public class MenuState extends EnvironmentMember implements
050: SessionListener, SessionListListener {
051: /** The state when no session is open. */
052: public static final long NO_SESSION = 0x1L;
053:
054: /** The standard state (no capturing and replaying). */
055: public static final long STANDARD = 0x2L;
056:
057: /** The state when the program captures events. */
058: public static final long CAPTURING = 0x4L;
059:
060: /** The state when the program replays the record. */
061: public static final long REPLAYING = 0x8L;
062:
063: /** The pause during a replay. */
064: public static final long REPLAY_PAUSE = 0x10L;
065:
066: /** Disables all menu items. */
067: public static final long ALL_DISABLED = 0x0L;
068:
069: /** All states. */
070: public static final long ALL_STATES = -1L;
071:
072: /** The actual state. */
073: private long state;
074:
075: /** The CleverPHL instance. */
076: private CleverPHL cleverPHL;
077:
078: /** The menu bar of the CleverPHL main frame. */
079: private CleverPHLMenuBar menuBar;
080:
081: /**
082: * Creates a new menu state instance with the given initial state and menu bar.
083: *
084: * @param cleverPHL the cleverPHL instance.
085: * @param menuBar the menu bar of the CleverPHL main frame
086: * @param initialState the initial state
087: */
088: public MenuState(CleverPHL cleverPHL, CleverPHLMenuBar menuBar,
089: long initialState) {
090: super (cleverPHL.getEnvironment());
091: this .cleverPHL = cleverPHL;
092: this .menuBar = menuBar;
093: setState(initialState);
094: cleverPHL.getSessionList().addSessionListListener(this );
095: }
096:
097: /**
098: * Sets a menu state to the given value. All menu items in the menu tree of the actual menu bar
099: * will be disabled if the new state is not contained in the <code>STATES_ENABLED</code>
100: * property of their actions. (or enabled if it is contained).
101: *
102: * @param state the new state
103: */
104: public void setState(long state) {
105: if (this .state != state) {
106: this .state = state;
107: getLogger().debug(
108: getLanguage().getString(
109: "CleverPHL.Menu.MenuState.Msg.StateSetTo")
110: + " " + state);
111: enableDisable();
112: }
113: }
114:
115: /**
116: * Returns the actual menu state.
117: *
118: * @return DOCUMENT ME!
119: */
120: public long getState() {
121: return state;
122: }
123:
124: /**
125: * Invoked when the session list has changed.
126: *
127: * @param event the session list event
128: */
129: public void sessionListChanged(SessionListEvent event) {
130: SessionList sessionList = cleverPHL.getSessionList();
131: Session session = event.getSession();
132: int ID = event.getID();
133:
134: if (ID == SessionListEvent.SESSION_ADDED) {
135: session.addSessionListener(this );
136:
137: if (sessionList.size() == 1) {
138: setState(STANDARD);
139: }
140: } else if (ID == SessionListEvent.SESSION_REMOVED) {
141: session.removeSessionListener(this );
142:
143: if (sessionList.isEmpty()) {
144: setState(NO_SESSION);
145: }
146:
147: if (sessionList.size() == 0) {
148: menuBar.adaptToNoSession();
149: }
150: } else if (ID == SessionListEvent.NEW_ACTUAL_SESSION) {
151: menuBar.rebuild(session.getType());
152: session.addAccelerators();
153:
154: if (session.isCapturing()) {
155: setState(CAPTURING);
156: } else if (session.isReplaying()) {
157: if (session.isReplayPause()) {
158: setState(REPLAY_PAUSE);
159: } else {
160: setState(REPLAYING);
161: }
162: } else {
163: setState(STANDARD);
164: }
165: }
166: }
167:
168: /**
169: * Invoked when the session state changed.
170: *
171: * @param event the session event
172: */
173: public void sessionStateChanged(SessionEvent event) {
174: SessionList sessionList = cleverPHL.getSessionList();
175: Session session = event.getSession();
176: int ID = event.getID();
177:
178: if (session == sessionList.getActual()) {
179: if (ID == SessionEvent.CAPTURE_START) {
180: setState(CAPTURING);
181: } else if (ID == SessionEvent.CAPTURE_STOP) {
182: setState(STANDARD);
183: } else if ((ID == SessionEvent.REPLAY_START)
184: || (ID == SessionEvent.REPLAY_CONTINUE)) {
185: setState(REPLAYING);
186: } else if (ID == SessionEvent.REPLAY_PAUSE) {
187: setState(REPLAY_PAUSE);
188: } else if (ID == SessionEvent.REPLAY_STOP) {
189: setState(STANDARD);
190: } else if (ID == SessionEvent.REPLAY_RESET) {
191: setState(STANDARD);
192: }
193: }
194: }
195:
196: /**
197: * Enables or disables the specified menu element and all its sub elements belonging to the
198: * <code>STATES_ENABLED</code> property of their actions.
199: */
200: private void enableDisable() {
201: Collection actions = menuBar.getActions();
202: Iterator it = actions.iterator();
203:
204: while (it.hasNext()) {
205: Action action = (Action) it.next();
206:
207: if ((action != null) && action instanceof CleverPHLAction) {
208: String sStatesEnabled = (String) action
209: .getValue(CleverPHLAction.STATES_ENABLED);
210: long statesEnabled = (new Long(sStatesEnabled))
211: .longValue();
212: boolean enabledState = (statesEnabled & getState()) != 0;
213: action.setEnabled(enabledState);
214: }
215: }
216: }
217:
218: /**
219: * Updates the enabled / disabled states of all menu items of the current menu bar.
220: */
221: public void update() {
222: setState(getState());
223: }
224: }
|