KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > SelectionManager


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;
17
18 import java.awt.ItemSelectable JavaDoc;
19 import java.awt.event.*;
20 import java.util.*;
21 import javax.swing.event.*;
22 import javax.vecmath.Matrix4f;
23
24 /**
25  * Class to manage selection of objects. The Selector class calls the methods
26  * in this class, but it is also possible other user objects (especially
27  * frontends and possibly backends) would want to manipulate the selection.
28  * Multiple objects can be selected at once. this class maintains a list of
29  * selected objects and issues ItemEvents each time a selection is made. Only
30  * objects in the currently active Area can be selected.
31  *
32  * @author Dusty Phillips [dusty@buchuki.com]
33  */

34 public class SelectionManager implements ItemSelectable JavaDoc {
35
36     /**
37      * Toggle the selection on a particular object
38      *
39      * @param id the identifier of the object to toggle the selection for
40      */

41     public void toggleSelection(Long JavaDoc id) {
42         int stateid;
43
44         if (selectedItems.contains(id)) {
45             selectedItems.remove(id);
46             stateid = ItemEvent.DESELECTED;
47         }
48         else {
49             selectedItems.add(id);
50             stateid = ItemEvent.SELECTED;
51         }
52
53         fireItemEvent(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
54             id, stateid));
55     }
56
57     /**
58      * Clear the selection so that no objects are selected. To ensure business
59      * logic is properly maintained, this method should be called each time the
60      * current area is changed.
61      */

62     public void clearSelection() {
63         ArrayList<Long JavaDoc> temp = new ArrayList<Long JavaDoc>(selectedItems);
64         selectedItems.clear();
65         for (Long JavaDoc id : temp) {
66             fireItemEvent(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
67                 id, ItemEvent.DESELECTED));
68         }
69     }
70
71     /**
72      * Add an ItemListener to the list of items listening for item change events
73      * on the selection manager
74      *
75      * @param listener the itemListener to be added
76      */

77     public void addItemListener(ItemListener listener) {
78         listenerList.add(ItemListener.class, listener);
79     }
80
81     /**
82      * Remove an ItemListener from the list of items listening for item change
83      * events on the Selection manager
84      *
85      * @param listener the itemListener to be removed
86      */

87     public void removeItemListener(ItemListener listener) {
88         listenerList.remove(ItemListener.class, listener);
89     }
90
91     /**
92      * Remove the currently selected objects from the current Area.
93      */

94     public void removeSelectedObjects() {
95         Object JavaDoc[] selectedObjs = getSelectedObjects();
96         if (selectedObjs == null) {
97             return;
98         }
99         Area currentArea = EnsmerManager.instance().getAreaManager().getCurrentArea();
100         for (Object JavaDoc obj : selectedObjs) {
101             currentArea.destroyObject((Long JavaDoc) obj);
102         }
103         clearSelection();
104     }
105
106     /**
107      * Retrieve an array of Long objects representing the identifiers of
108      * selected objects. The actual object can be retrieved from the current
109      * area.
110      *
111      * @return array of selected Long ids, or null if no objects are selected
112      */

113     public Object JavaDoc[] getSelectedObjects() {
114         if (selectedItems.isEmpty()) {
115             return null;
116         }
117         else {
118             return selectedItems.toArray();
119         }
120     }
121
122     /**
123      * Determine if a particular object is currently selected
124      *
125      * @param id the identifier of the object to determine selection on
126      * @return true if the object is selected, false otherwise
127      */

128     public boolean isSelected(Long JavaDoc id) {
129         return selectedItems.contains(id);
130     }
131
132     /**
133      * Move all selected objects by the relative position in the given matrix.
134      *
135      * @param matrix A relative movement matrix to move the object by
136      * @param right Ugly implementation detail; determines of the
137      * multiplcation should be on the right side when combining matrices.
138      * If true, multiply on the right. In general, it should be multiplied
139      * on the left for rotation matrices and on the right for translation.
140      * The problem seems to originate in the
141      * com.buchuki.ensmer.input.command.navigation package, but I cannot
142      * devise any other workaround.
143      */

144     public void setSelectedObjectPositions(Matrix4f matrix, boolean right) {
145         if (selectedItems.isEmpty()) {
146             return;
147         }
148         Area currentArea = EnsmerManager.instance().getAreaManager().getCurrentArea();
149         for (Object JavaDoc obj : getSelectedObjects()) {
150             Long JavaDoc id = (Long JavaDoc) obj;
151             Matrix4f currentPos = currentArea.getObjectPosition(id);
152             Matrix4f result = new Matrix4f();
153             if (right) {
154                 result.mul(matrix, currentPos);
155             }
156             else {
157                 result.mul(currentPos, matrix);
158             }
159
160             currentArea.setObjectPosition(id, result);
161         }
162     }
163
164     /**
165      * Fire an ItemEvent to all listening objects
166      *
167      * @param event Description of the Parameter
168      */

169     protected void fireItemEvent(ItemEvent event) {
170         Object JavaDoc[] listeners = listenerList.getListenerList();
171         for (int i = listeners.length - 2; i >= 0; i -= 2) {
172             if (listeners[i] == ItemListener.class) {
173                 ((ItemListener) listeners[i + 1]).itemStateChanged(event);
174             }
175         }
176     }
177
178     /**
179      * List of ItemListeners for the Selection Manager
180      */

181     private EventListenerList listenerList = new EventListenerList();
182
183     /**
184      * List of items that are selected in the current area.
185      */

186     private List<Long JavaDoc> selectedItems = new ArrayList<Long JavaDoc>();
187 }
188
189
Popular Tags