1 16 package com.buchuki.ensmer; 17 18 import java.awt.ItemSelectable ; 19 import java.awt.event.*; 20 import java.util.*; 21 import javax.swing.event.*; 22 import javax.vecmath.Matrix4f; 23 24 34 public class SelectionManager implements ItemSelectable { 35 36 41 public void toggleSelection(Long 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 62 public void clearSelection() { 63 ArrayList<Long > temp = new ArrayList<Long >(selectedItems); 64 selectedItems.clear(); 65 for (Long id : temp) { 66 fireItemEvent(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, 67 id, ItemEvent.DESELECTED)); 68 } 69 } 70 71 77 public void addItemListener(ItemListener listener) { 78 listenerList.add(ItemListener.class, listener); 79 } 80 81 87 public void removeItemListener(ItemListener listener) { 88 listenerList.remove(ItemListener.class, listener); 89 } 90 91 94 public void removeSelectedObjects() { 95 Object [] selectedObjs = getSelectedObjects(); 96 if (selectedObjs == null) { 97 return; 98 } 99 Area currentArea = EnsmerManager.instance().getAreaManager().getCurrentArea(); 100 for (Object obj : selectedObjs) { 101 currentArea.destroyObject((Long ) obj); 102 } 103 clearSelection(); 104 } 105 106 113 public Object [] getSelectedObjects() { 114 if (selectedItems.isEmpty()) { 115 return null; 116 } 117 else { 118 return selectedItems.toArray(); 119 } 120 } 121 122 128 public boolean isSelected(Long id) { 129 return selectedItems.contains(id); 130 } 131 132 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 obj : getSelectedObjects()) { 150 Long id = (Long ) 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 169 protected void fireItemEvent(ItemEvent event) { 170 Object [] 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 181 private EventListenerList listenerList = new EventListenerList(); 182 183 186 private List<Long > selectedItems = new ArrayList<Long >(); 187 } 188 189 | Popular Tags |