KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > input > InputManager


1 /*
2  * Copyright 2004 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.buchuki.ensmer.input;
17
18 import com.buchuki.ensmer.*;
19 import com.buchuki.ensmer.input.event.*;
20 import com.buchuki.ensmer.input.command.*;
21 import java.awt.event.KeyEvent JavaDoc;
22
23 /**
24  * Class to manage input from the user. Listens for input events (from mouse
25  * and keyboard) and encapsulates them in EnsmerInputEvent objects. This event
26  * framework is more conducive to the sharing of events in the modal style
27  * used by this application. The InputManager forwards all input events to
28  * other managers for more specific processing.
29  *
30  * @author Dusty Phillips [dusty@buchuki.com]
31  */

32 public class InputManager implements InputProcessor {
33     
34     /**
35      * Get a reference to the CommandMap for user Input
36      *
37      * @return the Input CommandMap
38      */

39     public static CommandMap getCommandMap() {
40         return commandMap;
41     }
42     
43     /**
44      * Set up the InputManager to listen to events from the InterfaceManager
45      * and begin processing them.
46      */

47     public InputManager() {
48         InputEventConverter ic = new InputEventConverter(this, EnsmerManager.instance().getInterfaceManager().getCanvas());
49         Command escapeObjectMode = new Command() {
50             public boolean execute(EnsmerInputEvent event) {
51                 if (mode == InputMode.OBJECT) {
52                     systemMode();
53                     return true;
54                 }
55                 return false;
56             }
57         };
58         commandMap.addCommand(this, escapeObjectMode, new KeyPressEvent(
59         KeyEvent.VK_ESCAPE));
60     }
61     
62     /**
63      * Process an input event. This method determines which mode (object or
64      * system) the system is in and forwards the event to the appropriate
65      * manager.
66      *
67      * @param event the EnsmerInputEvent to be processed
68      */

69     public void processInput(EnsmerInputEvent event) {
70         if (!commandMap.interpretEvent(this, event)) {
71             if (mode == InputMode.SYSTEM) {
72                 systemManager.processInput(event);
73             }
74             else {
75                 objectManager.processInput(event);
76             }
77         }
78     }
79     
80     /**
81      * Change to System Mode. In this mode, all events are forwarded to a
82      * SystemInputManager to be interpreted as system events.
83      */

84     public void systemMode() {
85         mode = InputMode.SYSTEM;
86         objectManager.setFocusedObject(null);
87         EnsmerManager.instance().getInterfaceManager().setCursorVisible(false);
88     }
89     
90     /**
91      * Change to Object Mode. In this mode, all events are forwarded to an
92      * ObjectInputManager to be redirected to the object backend or frontend.
93      *
94      * @param objectID the identifier of the newly selected object
95      */

96     public void objectMode(Long JavaDoc objectID) {
97         mode = InputMode.OBJECT;
98         objectManager.setFocusedObject(objectID);
99         EnsmerManager.instance().getInterfaceManager().setCursorVisible(true);
100     }
101     
102     /**
103      * Get a reference to the ObjectInputManager.
104      * @return a reference to the ObjectInputManager being managed.
105      */

106     public ObjectInputManager getObjectManager() {
107         return objectManager;
108     }
109     
110     /**
111      * The CommandMap for all UserInput. This is static because it needs to be
112      * accessed by the DefaultSystemInputManager which is constructed when the
113      * InputManager is constructed, meaning the InputManager isn't yet fully
114      * available. This may not be the best way to do this, and it may also be
115      * a hint at a bad design decision; classes shouldn't have to access
116      * classes that aren't constructed!
117      */

118     private static CommandMap commandMap = new CommandMap();
119     
120     /**
121      * The SystemInputManager to forward events to in System mode
122      */

123     private SystemInputManager systemManager = new SystemInputManager();
124     
125     /**
126      * The ObjectInputManager to forward events to in Object mode
127      */

128     private ObjectInputManager objectManager = new ObjectInputManager();
129     
130     /**
131      * The current mode
132      */

133     private InputMode mode = InputMode.SYSTEM;
134     
135     /**
136      * Enumeration of modes in the InputManager. There are currently only two
137      * modes; system and object mode.
138      */

139     private enum InputMode {SYSTEM, OBJECT}
140 }
141
Popular Tags