View Javadoc
1 /***************************************************************************** 2 * Virtual Mockup for Machine Vision 3 * Copyright (C) 2001-2003 Fabio R. de Miranda, João E. Kogler Jr., 4 * Carlos S. Santos. 5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP 6 * 7 * Permission is granted to redistribute and/or modify this 8 * software under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This software is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html) 16 * for more details. 17 * 18 *****************************************************************************/ 19 20 package camera3d; 21 22 23 import java.util.*; 24 import camera3d.gui.*; 25 import camera3d.event.*; 26 27 /*** 28 * This class keeps track of which objects are selected in the scene. It acts as a 29 * manager for selection activities. It must register itself with a J3DBase object 30 * so it gets informed of the list of scene graph objects. SelectionList is also the 31 * object which should receive input from mouse picking objects, by means of the 32 * PickListener interface. It interacts with graphical components dedicated to 33 * selection/edition of objects, receiving requests to selected/unselect certain 34 * objects and notifying interested listeners of changes made to the group of selected 35 * scene graph objects. 36 * 37 * @author Fábio Roberto de Miranda 38 * @version 1.0 39 */ 40 public class SelectionList implements SceneObjectsChangeListener, PickListener { 41 42 /*** List of objects which get notified when selection changes. */ 43 private Vector listeners = new Vector(10, 5); 44 /*** List of objects present in the scene graph.*/ 45 private Vector sceneObjects; 46 /*** List of objects currently selected. */ 47 private Vector selectedObjects; 48 /*** Event used to notify listeners of changes in selectedObjects. */ 49 private SelectionChangedEvent currentEvent = new SelectionChangedEvent(this); 50 /*** Indicates whether simultaneous selection of multiple objects is allowed. */ 51 private boolean singleObjectSelectionMode = true; 52 53 /*** Creates a new SelectionList, both sceneObjects and selectedObjects remain 54 * empty. 55 */ 56 public SelectionList() { 57 sceneObjects = new Vector(20,5); 58 selectedObjects = new Vector(20,5 ); 59 } 60 61 62 /*** 63 * Informs SelectionList of changes in the list of objects in the scene graph. 64 * In case an object has been removed from the scene graph notifyChangeListeners 65 * will be called by the end of execution. 66 * Method from sceneObjectsChangeListener interface. 67 */ 68 public void sceneObjectsChanged(SceneObjectsChangedEvent event){ 69 /* event generated when SelectionList is set as listener to a 70 J3DBase object; necessary to set initial list of objects 71 present in the scene */ 72 if(event instanceof InitialListEvent){ 73 setSceneObjects( ((InitialListEvent)event).getInitialList() ); 74 } 75 if(event instanceof ObjectAddedEvent){ 76 ObjectAddedEvent addEvent = (ObjectAddedEvent) event; 77 VcObject vcObj = (VcObject) addEvent.getObject(); 78 addToSelectionList(vcObj); 79 } 80 if(event instanceof ObjectRemovedEvent){ 81 ObjectRemovedEvent removeEvent = (ObjectRemovedEvent) event; 82 removeFromSelectionList((VcObject) removeEvent.getObject()); 83 } 84 } 85 86 /*** 87 * Called when a picking action is performed. Informs selection list of picked 88 * objects and modifiers applied during picking. 89 * Method from PickListener interface. 90 * @param mod modifiers, as defined in java.awt.event.MouseEvent 91 */ 92 public void pickingOcurred(int mod, List pickedObjects){ 93 // clears event from prior results 94 currentEvent.getAddedObjects().clear(); 95 currentEvent.getRemovedObjects().clear(); 96 if(pickedObjects==null){ 97 System.out.println("__SelectionList.pickingOcurred: null list"); 98 return; 99 } 100 101 // in case picked ocurred in the void, we will clear current selection 102 if(pickedObjects.isEmpty()){ 103 flushSelection(); 104 return; 105 } 106 107 if(singleObjectSelectionMode){ 108 try{ 109 VcObject tempObj = (VcObject)pickedObjects.get(0); 110 if(tempObj.isSelected()) removeFromSelectedObjects(tempObj); 111 else addToSelectedObjects(tempObj); 112 } catch(java.lang.ClassCastException cce){ 113 cce.printStackTrace(); 114 } 115 } else{ 116 // code for multiple object selection should be put here. 117 } 118 119 } 120 121 /*** Adds input object to list of scene objects, but NOT to list of selected objects. 122 */ 123 private void addToSelectionList(VcObject vc){ 124 // prevents double addition 125 if(sceneObjects.contains(vc)) return; 126 sceneObjects.addElement(vc); 127 } 128 129 /*** 130 * Removes all objects in vc from list of scene graph objects. As a consequence, 131 * those which are selected are also removed from list of selected objects. 132 */ 133 private void removeFromSelectionList(Vector vc){ 134 sceneObjects.removeAll(vc); 135 Iterator iter = vc.iterator(); 136 while(iter.hasNext()){ 137 removeFromSelectedObjects((VcObject)iter.next()); 138 } 139 } 140 141 /*** 142 * Removes vc from list of objects in scene graph. In case it is selected, vc is 143 * also removed from list of selected objects. 144 */ 145 private void removeFromSelectionList(VcObject vc){ 146 sceneObjects.removeElement(vc); 147 removeFromSelectedObjects(vc); 148 } 149 150 /*** 151 * Prints debug messages. Its execution is controlled by GUIControl.debugflag. 152 * @param s message to be printed. 153 */ 154 private void debugln(String s){ 155 if (GUIControl.debugflag){ 156 System.out.println(s); 157 } 158 } 159 160 /*** 161 * Adds vcObj to selected objects list. 162 */ 163 public void addToSelectedObjects(VcObject vcObj){ 164 if (singleObjectSelectionMode){ 165 // Doesn't let more than one object be selected 166 Iterator iter = selectedObjects.iterator(); 167 while (iter.hasNext()){ 168 ((VcObject)iter.next()).unselect(); 169 } 170 selectedObjects.removeAllElements(); 171 } 172 173 selectedObjects.add(vcObj); 174 currentEvent.getAddedObjects().add(vcObj); 175 vcObj.select(); 176 177 /* 178 Iterator iter = selectedObjects.iterator(); 179 debugln("___SelectionList->Selected objects:"); 180 while (iter.hasNext()){ 181 debugln("_____"+((VcObject)iter.next()).getLabel()); 182 }*/ 183 notifyListeners(); 184 } 185 186 /*** 187 * Removes the input object from list of selected objects. The object is unselected 188 * and listeners are notified. 189 */ 190 public void removeFromSelectedObjects(VcObject vcObj){ 191 if (selectedObjects.contains(vcObj)){ 192 this.selectedObjects.remove(vcObj); 193 vcObj.unselect(); 194 currentEvent.getRemovedObjects().add(vcObj); 195 notifyListeners(); 196 } 197 } 198 199 /*** 200 * Clears current selection. All objects currently selected are unselected, 201 * listeners are notified. 202 */ 203 public void flushSelection(){ 204 Iterator iter = selectedObjects.iterator(); 205 while (iter.hasNext()){ 206 ((VcObject)iter.next()).unselect(); 207 } 208 currentEvent.getAddedObjects().clear(); 209 currentEvent.getRemovedObjects().addAll(this.selectedObjects); 210 this.selectedObjects.removeAllElements(); 211 notifyListeners(); 212 } 213 214 /*** 215 * Returns list of objects currently selected. 216 */ 217 public List getSelectedObjects(){ 218 return this.selectedObjects; 219 } 220 221 /*** 222 * Sets list of objects currently in the scene graph. 223 * @param sceneObjects List of VcObjects. 224 */ 225 public void setSceneObjects(List sceneObjects){ 226 //this.sceneObjects = sceneObjects; 227 //if(sceneObjects!=null) notifyListeners(); 228 sceneObjects.clear(); 229 sceneObjects.addAll(sceneObjects); 230 } 231 232 /*** 233 * Sets flag which controls whether multi-object selection is permitted. 234 */ 235 public void setSingleObjectSelectionMode(boolean b){ 236 this.singleObjectSelectionMode = b; 237 } 238 239 /*** 240 * Returns flag indicating whether multi-object selection is permitted. 241 */ 242 public boolean getSingleObjectSelectionMode(){ 243 return this.singleObjectSelectionMode; 244 } 245 246 /*** 247 * Adds listener to the list of objects notified when scene graph objects 248 * get (un)selected. 249 */ 250 public void addSelectionChangeListener(SelectionChangeListener listener){ 251 if(listeners.contains(listener)) return; 252 listeners.add(listener); 253 } 254 255 /*** 256 * Notifies listeners of changes in the list of selected objects. 257 */ 258 void notifyListeners(){ 259 Iterator iter = listeners.iterator(); 260 while (iter.hasNext()){ 261 ((SelectionChangeListener)iter.next()).selectionChanged(currentEvent); 262 } 263 /* 264 Clean results from current selection, avoiding that the next selection 265 gets messed by them. That might happen because we are reusing the event 266 object, tentatively reducing memory usage and garbage collecting. 267 */ 268 currentEvent.getAddedObjects().clear(); 269 currentEvent.getRemovedObjects().clear(); 270 } 271 272 }

This page was automatically generated by Maven