KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package com.buchuki.ensmer.input;
18
19 import com.buchuki.ensmer.EnsmerManager;
20 import com.buchuki.ensmer.input.command.CommandMap;
21 import com.buchuki.ensmer.input.event.*;
22 import com.buchuki.ensmer.input.event.semantic.*;
23 import com.buchuki.ensmer.object.Backend;
24 import javax.swing.event.EventListenerList JavaDoc;
25
26 /**
27  * Class to respond to events pertaining to a specific object. When in object
28  * mode, navigation does not occur; the mouse pointer moves across a specific
29  * static view of the screen.
30  *
31  * @author Dusty Phillips [dusty@buchuki.com]
32  */

33 public class ObjectInputManager implements InputProcessor {
34     
35     /**
36      * Process input on the currently selected object. Processing of the input
37      * occurs in two different ways depending on the type of input. Keyboard
38      * inputs are compared in the command map to the specific backend in
39      * question, and the associated command is executed, if there is one.
40      * Mouse inputs are forwarded to the frontend for further processing.
41      */

42     public void processInput(EnsmerInputEvent event) {
43         if (focused == null) {
44             return;
45         }
46         CommandMap map = InputManager.getCommandMap();
47         Backend backend = EnsmerManager.instance().getBackhoe().findBackend(focused);
48         if (backend != null) {
49             map.interpretEvent(backend, event);
50         }
51     }
52     
53     /**
54      * Set the currently focused object. Only one object can be focused at one
55      * time.
56      *
57      * @param id the identifier for the currently object. Set to null
58      * to unselect. This identifier should be for an object in the currently
59      * active area
60      */

61     public void setFocusedObject(Long JavaDoc id) {
62         if (id != null && id.equals(focused)) {
63             return;
64         }
65         Long JavaDoc oldFocused = focused;
66         focused = id;
67         if (oldFocused != null) {
68             fireFocusEvent(new FocusEvent(this, oldFocused, false));
69         }
70         if (focused != null) {
71             fireFocusEvent(new FocusEvent(this, focused, true));
72         }
73     }
74     
75     /**
76      * Get the id of the currently focused object, or null if no object is
77      * currently focused.
78      *
79      * @return the id of the currently focused object, or null if no object is
80      * focused
81      */

82     public Long JavaDoc getFocusedObject() {
83         return focused;
84     }
85     
86     /**
87      * Add a FocusListener to the list of items listening for focus change events
88      * on the object manager
89      *
90      * @param listener the focusistener to be added
91      */

92     public void addFocusListener(FocusListener listener) {
93         listenerList.add(FocusListener.class, listener);
94     }
95     
96     /**
97      * Remove a FocusListener from the list of items listening for focus change
98      * events on the Object manager
99      *
100      * @param listener the focusListener to be removed
101      */

102     public void removeFocusListener(FocusListener listener) {
103         listenerList.remove(FocusListener.class, listener);
104     }
105     
106     /**
107      * Fire a FocusEvent to all listening objects
108      *
109      * @param event the event to fire
110      */

111     protected void fireFocusEvent(FocusEvent event) {
112         Object JavaDoc[] listeners = listenerList.getListenerList();
113         for (int i = listeners.length - 2; i >= 0; i -= 2) {
114             if (listeners[i] == FocusListener.class) {
115                 ((FocusListener) listeners[i + 1]).focusChanged(event);
116             }
117         }
118     }
119     
120     /**
121      * The identifier of the currently focused object, if one is
122      */

123     private Long JavaDoc focused;
124     
125     /**
126      * List of FocusListeners for the object Manager
127      */

128     private EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
129 }
130
Popular Tags