1 16 package com.buchuki.ensmer.prevayler; 17 18 import java.io.Serializable ; 19 import java.util.*; 20 21 import javax.media.j3d.Transform3D; 22 import javax.vecmath.Matrix4f; 23 24 import com.buchuki.annotations.*; 25 26 32 @Optimize("previousList grows without bound") 33 public class AreaPrevayler implements Serializable { 34 35 38 static final long serialVersionUID = 20050328L; 39 40 48 public long nextAreaIdentifier() { 49 return ++lastAreaIdentifier; 50 } 51 52 57 public void addArea(Long areaID) { 58 areaMap.put(areaID, new PrevayledArea()); 59 } 60 61 66 public void removeArea(Long areaID) { 67 areaMap.remove(areaID); 68 while (previousAreas.contains(areaID)) { 70 previousAreas.remove(areaID); 71 } 72 while (nextAreas.contains(areaID)) { 73 nextAreas.remove(areaID); 74 } 75 76 } 77 78 84 public long[] getAreas() { 85 Set areaList = areaMap.keySet(); 86 long areas[] = new long[areaList.size()]; 87 Iterator it = areaList.iterator(); 88 int count = 0; 89 while (it.hasNext()) { 90 areas[count] = (Long ) it.next(); 91 count++; 92 } 93 return areas; 94 } 95 96 100 public Long previousArea() { 101 if (previousAreas.isEmpty()) { 102 return null; 103 } 104 nextAreas.addFirst(currentArea); 105 currentArea = previousAreas.removeFirst(); 106 return currentArea; 107 } 108 109 113 public Long nextArea() { 114 if (nextAreas.isEmpty()) { 115 return null; 116 } 117 previousAreas.addFirst(currentArea); 118 currentArea = nextAreas.removeFirst(); 119 return currentArea; 120 } 121 122 140 public Long getHistoricalAreaID(int index) { 141 if (index == 0) { 142 return currentArea; 143 } 144 else if (index > 0) { 145 if (previousAreas.size() < index) { 146 return null; 147 } 148 return previousAreas.get(index - 1); 149 } 150 else { 151 int trueIndex = index * -1; 152 if (nextAreas.size() < trueIndex) { 153 return null; 154 } 155 return nextAreas.get(trueIndex - 1); 156 } 157 158 } 159 160 170 public Matrix4f getUserPosition(long id) { 171 PrevayledArea area = areaMap.get(id); 172 if (area == null) { 173 return null; 174 } 175 else if (area.getUserPosition() == null) { 176 Transform3D transform = new Transform3D(); 177 Matrix4f matrix = new Matrix4f(); 178 transform.get(matrix); 179 return matrix; 180 } 181 else { 182 return area.getUserPosition(); 183 } 184 } 185 186 191 public Long getCurrentArea() { 192 return currentArea; 193 } 194 195 203 public void setUserPosition(Long id, Matrix4f position) { 204 PrevayledArea area = areaMap.get(id); 205 if (area != null) { 206 area.setUserPosition(position); 207 } 208 } 209 210 216 public void setCurrentArea(Long areaID) { 217 if (currentArea != null) { 218 previousAreas.addFirst(currentArea); 219 } 220 if (!nextAreas.isEmpty()) { 221 nextAreas.clear(); 222 } 223 currentArea = areaID; 224 } 225 226 233 public void setReadOnly(Long areaID, boolean readOnly) { 234 PrevayledArea area = areaMap.get(areaID); 235 if (area != null) { 236 area.setReadOnly(readOnly); 237 } 238 } 239 240 247 public boolean isReadOnly(Long areaID) { 248 PrevayledArea area = areaMap.get(areaID); 249 if (area != null) { 250 return area.isReadOnly(); 251 } 252 return true; 253 } 254 255 258 private long lastAreaIdentifier; 259 260 267 private Map<Long , PrevayledArea> areaMap = new HashMap<Long , PrevayledArea>(); 268 269 272 private Long currentArea; 273 274 275 278 private LinkedList<Long > previousAreas = new LinkedList<Long >(); 279 280 285 private LinkedList<Long > nextAreas = new LinkedList<Long >(); 286 287 } 288 289 | Popular Tags |