1 16 package com.buchuki.ensmer; 17 18 import java.util.*; 19 import javax.media.j3d.*; 20 import javax.vecmath.Matrix4f; 21 import com.buchuki.ensmer.input.InputManager; 22 import com.buchuki.ensmer.input.ObjectInputManager; 23 import com.buchuki.ensmer.input.event.semantic.ObjectEvent; 24 import com.buchuki.ensmer.object.*; 25 import com.buchuki.ensmer.prevayler.queries.*; 26 import org.prevayler.*; 27 28 35 public class Area { 36 37 38 43 @SuppressWarnings ({"unchecked"}) Area(Long identifier) { 44 prevayler = EnsmerManager.instance().getPrevayler(); 45 areaID = identifier; 46 branchGraph.setCapability(Group.ALLOW_CHILDREN_READ); 47 branchGraph.setCapability(Group.ALLOW_CHILDREN_WRITE); 48 branchGraph.setCapability(Group.ALLOW_CHILDREN_EXTEND); 49 branchGraph.setCapability(BranchGroup.ALLOW_DETACH); 50 loading = true; 51 } 52 53 64 public Long newObject(Backend backend, Matrix4f location) { 65 if (isReadOnly()) { 66 throw new IllegalStateException ("Area is read only"); 67 } 68 try { 69 Long newObjectId = (Long ) prevayler.execute( 71 new GetNextObjectIDQuery()); 72 prevayler.execute(new AddObjectTransaction( 73 newObjectId, backend.getClass())); 74 prevayler.execute(new SetObjectLocationTransaction( 75 newObjectId, location)); 76 prevayler.execute(new SetObjectDataTransaction( 77 newObjectId, backend.getSerializable())); 78 backend.setId(newObjectId); 79 setupNewObject(backend); 80 return newObjectId; 81 } catch (Exception e) { 82 e.printStackTrace(); 83 return null; 84 } 85 } 86 87 96 public void addObject(Long id) { 97 try { 98 Backend backend = EnsmerManager.instance().getBackhoe().getBackend(id); 99 addObject(backend); 100 } catch (Exception e) { 101 e.printStackTrace(); 102 } 103 } 104 105 113 public void addObject(Backend backend) { 114 try { 115 setupNewObject(backend); 116 } catch (Exception e) { 117 e.printStackTrace(); 118 } 119 } 120 121 127 public void removeObject(Long id) { 128 if (!objectMap.containsKey(id)) { 129 return; 130 } 131 SelectionManager sel = EnsmerManager.instance().getSelectionManager(); 132 if (sel.isSelected(id)) { 133 sel.toggleSelection(id); 134 } 135 InputManager inMan = EnsmerManager.instance().getUserManager().getInputManager(); 136 if (id.equals(inMan.getObjectManager().getFocusedObject())) { 137 inMan.systemMode(); 138 } 139 140 prevayler.execute(new SetObjectAreaTransaction(id, null)); 141 Frontend front = objectMap.remove(id); 142 BranchGroup group = transformMap.remove(id); 143 branchGraph.removeChild(group); 144 front.destroy(); 145 EnsmerManager.instance().getAreaManager().fireObjectEvent( 146 new ObjectEvent(this, id, ObjectEvent.EventType.OBJECT_REMOVED)); 147 } 148 149 157 public void destroyObject(Long id) { 158 if (!objectMap.containsKey(id)) { 159 return; 160 } 161 Frontend frontend = objectMap.get(id); 162 Backend backend = frontend.getBackend(); 163 removeObject(id); 164 InputManager.getCommandMap().removeAllCommands(backend); 165 prevayler.execute(new RemoveObjectTransaction(id)); 166 EnsmerManager.instance().getAreaManager().fireObjectEvent( 167 new ObjectEvent(this, id, ObjectEvent.EventType.OBJECT_DESTROYED)); 168 EnsmerManager.instance().getConfigManager().objectDestroyed(id); 169 } 170 171 175 public void destroyAll() { 176 Set<Long > set = new HashSet<Long >(objectMap.keySet()); 177 for (Long id : set) { 178 destroyObject(id); 179 } 180 } 181 182 190 public Frontend getFrontend(Long id) { 191 ensureLoaded(id); 192 return objectMap.get(id); 193 } 194 195 204 public BranchGroup getFrontendCopy(Long id) { 205 Frontend frontend = getFrontend(id); 206 if (frontend == null) { 207 return null; 208 } 209 BranchGroup retVal = frontend.getBranchGroup(); 210 BranchGroup newGroup = frontend.getBranchGroup(); 211 SceneGraphUtils.enableEnsmerCapabilities(newGroup); 212 newGroup.setCapability(BranchGroup.ALLOW_DETACH); 213 TransformGroup transGroup = (TransformGroup) transformMap.get(id).getChild(0); 214 transGroup.removeAllChildren(); 215 transGroup.addChild(newGroup); 216 return retVal; 217 } 218 219 224 public List<Backend> getBackends() { 225 List<Backend> list = new ArrayList<Backend>(); 226 for (Frontend front : objectMap.values()) { 227 list.add(front.getBackend()); 228 } 229 return list; 230 } 231 232 237 public Long getAreaID() { 238 return areaID; 239 } 240 241 246 public boolean isReadOnly() { 247 try { 248 return (Boolean ) prevayler.execute(new GetAreaReadOnlyQuery(areaID)); 249 } catch (Exception e) { 250 e.printStackTrace(); 251 return true; 252 } 253 } 254 255 262 public Matrix4f getUserPosition() { 263 try { 264 return (Matrix4f) prevayler.execute(new 265 GetAreaUserPositionQuery(areaID)); 266 } catch (Exception e) { 267 e.printStackTrace(); 268 return new Matrix4f(); 269 } 270 } 271 272 279 public Matrix4f getObjectPosition(Long id) { 280 ensureLoaded(id); 281 if (!objectMap.containsKey(id)) { 282 return null; 283 } 284 try { 285 return (Matrix4f) prevayler.execute(new GetObjectLocationQuery(id)); 286 } catch (Exception e) { 287 e.printStackTrace(); 288 return null; 289 } 290 } 291 292 297 public void setReadOnly(boolean readOnly) { 298 try { 299 prevayler.execute(new SetAreaReadOnlyTransaction(areaID, readOnly)); 300 } catch (Exception e) { 301 e.printStackTrace(); 302 } 303 } 304 305 311 public void setUserPosition(Matrix4f newPosition) { 312 try { 313 prevayler.execute( 314 new SetAreaUserPositionTransaction(areaID, newPosition)); 315 } catch (Exception e) { 316 e.printStackTrace(); 317 } 318 } 319 320 326 public void setObjectPosition(Long id, Matrix4f newPosition) { 327 if (!objectMap.containsKey(id)) { 328 return; 329 } 330 try { 331 prevayler.execute(new SetObjectLocationTransaction(id, newPosition)); 332 TransformGroup transform = (TransformGroup) transformMap.get(id).getChild(0); 333 transform.setTransform(new Transform3D(newPosition)); 334 } catch (Exception e) { 335 e.printStackTrace(); 336 } 337 } 338 339 346 protected BranchGroup getBranchGraph() { 347 return branchGraph; 348 } 349 350 357 void loadObjects() throws Exception { 358 if (!loading) { 361 return; 362 } 363 List<Long > prevayledObjects = (List<Long >) prevayler.execute( 365 new GetAreaObjectsQuery(areaID)); 366 for (Long id : prevayledObjects) { 367 if (!objectMap.containsKey(id)) { 369 addObject(id); 370 } 371 } 372 loading = false; 373 } 374 375 382 private void ensureLoaded(Long id) { 383 if (loading) { 384 if (objectMap.containsKey(id)) { 385 return; } 387 try { 388 Long objArea = (Long ) prevayler.execute(new GetAreaForObjectQuery(id)); 389 390 if (objArea != null && objArea.equals(areaID)) { 391 addObject(id); 392 } 393 } catch (Exception e) { 394 e.printStackTrace(); 395 } 396 } 397 } 398 399 411 private void setupNewObject(Backend backend) throws Exception { 412 Long id = backend.getId(); 413 Frontend frontend = EnsmerManager.instance().getBackhoe().loadFrontend(backend); 414 415 Matrix4f location = (Matrix4f) prevayler.execute(new 416 GetObjectLocationQuery(id)); 417 418 BranchGroup topGroup = new BranchGroup(); 419 TransformGroup transGroup = new TransformGroup(new Transform3D(location)); 420 BranchGroup objGroup = frontend.getBranchGroup(); 421 SceneGraphUtils.enableEnsmerCapabilities(objGroup); 422 objGroup.setCapability(BranchGroup.ALLOW_DETACH); 423 transGroup.addChild(objGroup); 424 transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 425 transGroup.setCapability(TransformGroup.ALLOW_CHILDREN_READ); 426 transGroup.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); 427 transGroup.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND); 428 topGroup.addChild(transGroup); 429 topGroup.setUserData(id); 430 topGroup.setBoundsAutoCompute(true); 431 topGroup.setCapability(BranchGroup.ENABLE_PICK_REPORTING); 432 topGroup.setCapability(Node.ALLOW_BOUNDS_READ); 433 topGroup.setCapability(Group.ALLOW_CHILDREN_READ); 434 topGroup.setCapability(BranchGroup.ALLOW_DETACH); 435 topGroup.compile(); 436 branchGraph.addChild(topGroup); 437 prevayler.execute(new SetObjectAreaTransaction(id, areaID)); 438 objectMap.put(id, frontend); 439 transformMap.put(id, topGroup); 440 EnsmerManager.instance().getAreaManager().fireObjectEvent( 441 new ObjectEvent(this, id, ObjectEvent.EventType.OBJECT_ADDED)); 442 } 443 444 449 private BranchGroup branchGraph = new BranchGroup(); 450 451 454 private Map<Long , Frontend> objectMap = new HashMap<Long , Frontend>(); 455 456 462 private Map<Long , BranchGroup> transformMap = new HashMap<Long , BranchGroup>(); 463 464 469 private Long areaID; 470 471 475 private Prevayler prevayler; 476 477 483 private boolean loading; 484 } 485 486 | Popular Tags |