]> gerrit.simantics Code Review - simantics/3d.git/blob - dev/org.simantics.proconf.g3d.shapeeditor/src/org/simantics/proconf/g3d/shapeeditor/views/ShapeEditorBase.java
Release
[simantics/3d.git] / dev / org.simantics.proconf.g3d.shapeeditor / src / org / simantics / proconf / g3d / shapeeditor / views / ShapeEditorBase.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.proconf.g3d.shapeeditor.views;\r
12 \r
13 import java.util.Collection;\r
14 import java.util.HashSet;\r
15 import java.util.Iterator;\r
16 import java.util.List;\r
17 \r
18 import org.eclipse.jface.action.Action;\r
19 import org.eclipse.jface.action.IStatusLineManager;\r
20 import org.eclipse.jface.viewers.ISelection;\r
21 import org.eclipse.jface.viewers.ISelectionChangedListener;\r
22 import org.eclipse.jface.viewers.SelectionChangedEvent;\r
23 import org.eclipse.swt.widgets.Composite;\r
24 import org.eclipse.ui.IWorkbenchPart;\r
25 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;\r
26 import org.simantics.db.ContextGraph;\r
27 import org.simantics.db.Graph;\r
28 import org.simantics.db.GraphRequestAdapter;\r
29 import org.simantics.db.GraphRequestStatus;\r
30 import org.simantics.db.GraphRequestWithResult;\r
31 import org.simantics.db.Resource;\r
32 import org.simantics.db.Session;\r
33 import org.simantics.db.management.ISessionContext;\r
34 import org.simantics.equation.solver.Solver;\r
35 import org.simantics.layer0.stubs.Property;\r
36 import org.simantics.layer0.utils.EntityFactory;\r
37 import org.simantics.layer0.utils.IEntity;\r
38 import org.simantics.proconf.g3d.actions.InteractiveAction;\r
39 import org.simantics.proconf.g3d.actions.TranslateAction;\r
40 import org.simantics.proconf.g3d.base.G3DTools;\r
41 import org.simantics.proconf.g3d.base.JmeRenderingComponent;\r
42 import org.simantics.proconf.g3d.base.ScenegraphAdapter;\r
43 import org.simantics.proconf.g3d.base.ScenegraphAdapterImpl;\r
44 import org.simantics.proconf.g3d.base.SelectionAdapter;\r
45 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
46 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorProvider;\r
47 import org.simantics.proconf.g3d.common.StructuredResourceSelection;\r
48 import org.simantics.proconf.g3d.csg.stubs.CSGModel;\r
49 import org.simantics.proconf.g3d.csg.stubs.Primitive;\r
50 import org.simantics.proconf.g3d.dnd.DropListener;\r
51 import org.simantics.proconf.g3d.scenegraph.AbstractGraphicsNode;\r
52 import org.simantics.proconf.g3d.scenegraph.IGeometryNode;\r
53 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;\r
54 import org.simantics.proconf.g3d.scenegraph.ISelectableNode;\r
55 import org.simantics.proconf.g3d.shapeeditor.ShapeEditorResources;\r
56 import org.simantics.proconf.g3d.shapeeditor.actions.ExportAction;\r
57 import org.simantics.proconf.g3d.shapeeditor.actions.ImportAction;\r
58 import org.simantics.proconf.g3d.shapeeditor.scenegraph.CSGShapeNode;\r
59 import org.simantics.proconf.g3d.shapeeditor.tools.AnimationContribution;\r
60 import org.simantics.proconf.g3d.shapeeditor.tools.CSGModellingContribution;\r
61 import org.simantics.proconf.g3d.shapeeditor.tools.ParameterizationContribution;\r
62 import org.simantics.proconf.g3d.shapes.GridShape;\r
63 import org.simantics.proconf.g3d.stubs.G3DModel;\r
64 import org.simantics.proconf.g3d.stubs.G3DNode;\r
65 import org.simantics.proconf.g3d.stubs.Shape;\r
66 import org.simantics.utils.ErrorLogger;\r
67 import org.simantics.utils.ui.jface.MenuTools;\r
68 \r
69 public class ShapeEditorBase extends ThreeDimensionalEditorBase {\r
70 \r
71         // currently each 3D-model has a root object which is ShapeGroup\r
72         protected Resource model = null;\r
73         \r
74         protected boolean isParameterized;\r
75         \r
76         private Action exportAction;\r
77         private Action importAction;\r
78         \r
79         public ShapeEditorBase(ISessionContext session) {\r
80                 super(session);\r
81                 addEditorContribution(new CSGModellingContribution(this));\r
82                 addEditorContribution(new AnimationContribution(this));\r
83                 addEditorContribution(new ParameterizationContribution(this));\r
84         }\r
85 \r
86         public ShapeEditorBase(ISessionContext session, JmeRenderingComponent component) {\r
87                 super(session,component);\r
88                 addEditorContribution(new CSGModellingContribution(this));\r
89                 addEditorContribution(new AnimationContribution(this));\r
90                 addEditorContribution(new ParameterizationContribution(this));\r
91         }\r
92 \r
93         @Override\r
94         protected ScenegraphAdapter createScenegraphAdapter() {\r
95                 return new ShapeEditorAdapter(session, getRenderingComponent());\r
96         }\r
97 \r
98         @Override\r
99         public void createControl(Graph graph,Composite parent) {\r
100                 super.createControl(graph,parent);\r
101                 getRenderingComponent().getNoCastRoot().attachChild(GridShape.getShape(getRenderingComponent().getDisplaySystem().getRenderer(), 10, 1.f));\r
102         }\r
103 \r
104 //      private void loadGroup(Graph graph) {\r
105 //              assert (model != null);\r
106 //              adapter.addOutbound(EntityFactory.create(graph,model));\r
107 //              //assert (abstractGraphicsNodes.size() == 1);\r
108 //\r
109 //      }\r
110 \r
111         protected void makeActions(Graph graph) {\r
112                 super.makeActions(graph);\r
113                 exportAction = new ExportAction(this);\r
114                 importAction = new ImportAction(this);\r
115         }\r
116 \r
117         @Override\r
118         protected void fillLocalPullDown() {\r
119                 super.fillLocalPullDown();\r
120                 MenuTools.getOrCreate(getMenuID(),"Model", menuManager).add(exportAction);\r
121                 MenuTools.getOrCreate(getMenuID(),"Model", menuManager).add(importAction);\r
122         }\r
123         \r
124 \r
125         \r
126         /*\r
127          * These are used for updating CSG models geometry when internal shapes are moved. Interactive update is not possible\r
128          * because recalculation of geometry takes too much time. There are several problems in this method:\r
129          * 1. it relies on instanceof check \r
130          * 2. when shape is moved, transformations of its children are updated, which causes all\r
131          *    child geometries to be updated, which is not necessary. We want to update only moved\r
132          *    shape, since updateAllGemetry method takes care of parents. \r
133          * \r
134          *  TODO : this functionality should be moved to TranslateAction\r
135          *  TODO : prevent moved shape's children to be updated.\r
136          */\r
137         \r
138         @Override\r
139         public void setCurrentAction(InteractiveAction action) {\r
140                 if (getCurrentAction() == action)\r
141                 return;\r
142                 if (getCurrentAction() != null && getCurrentAction() instanceof TranslateAction) {\r
143                         runGeometryUpdates();\r
144                 }\r
145                 super.setCurrentAction(action);\r
146         }\r
147         \r
148         private void runGeometryUpdates() {\r
149                 // now we'll just filter out all parents so that they won't be updated multiple times.\r
150                 HashSet<CSGShapeNode> parents = new HashSet<CSGShapeNode>();\r
151                 for (CSGShapeNode n : geometryUpdates) {\r
152                         IGraphicsNode parent = n.getParent();\r
153                         if (parent instanceof CSGShapeNode)\r
154                                 parents.add((CSGShapeNode)parent);\r
155                 }\r
156                 for (CSGShapeNode n : geometryUpdates) {\r
157                         if (!parents.contains(n))\r
158                                 n.updateAllGeometry();\r
159                 }\r
160                 geometryUpdates.clear();\r
161         }\r
162         \r
163         private HashSet<CSGShapeNode> geometryUpdates = new HashSet<CSGShapeNode>();\r
164         \r
165         private void geometryUpdate(CSGShapeNode shape) {\r
166                 \r
167                 if (!(getCurrentAction() instanceof TranslateAction)) {\r
168                         shape.updateAllGeometry();\r
169                 } else {\r
170                         geometryUpdates.add(shape);\r
171                 }\r
172         }\r
173         \r
174         public Graph createParameterization(Graph g) {\r
175                 if (isParameterized) {\r
176                         ContextGraph graph;\r
177                         if (!(g instanceof ContextGraph)) {\r
178                                 graph = new ContextGraph(g);\r
179                                 graph.setContext(model);\r
180                         } else {\r
181                                 graph = (ContextGraph)g;\r
182                         }\r
183                         Solver solver = new Solver();\r
184                         Collection<org.simantics.layer0.utils.Property> parameters = getModel(graph).getRelatedProperties(ShapeEditorResources.g3dResource.HasSizingParameter);\r
185                         for (org.simantics.layer0.utils.Property p : parameters) {\r
186                                 IEntity t = EntityFactory.create(graph, p.getResource());\r
187                                 Collection<IEntity> exp = t.getRelatedObjects(ShapeEditorResources.equationResource.HasTarget);\r
188                                 if (exp.size() > 0) {\r
189                                         Iterator<IEntity> i = exp.iterator();\r
190                                         while (i.hasNext())\r
191                                                 solver.evaluate(i.next());\r
192                                 } else {\r
193                                         ErrorLogger.defaultLogWarning("Model property " + p + " is not bound to a expression", null);\r
194                                 }\r
195                         }\r
196                         solver.pushToGraph(graph);\r
197                         return graph;\r
198                 } else {\r
199                         return g;\r
200                 }\r
201         }\r
202         \r
203         \r
204 \r
205         protected class ShapeEditorAdapter extends ScenegraphAdapterImpl {\r
206 \r
207                 public ShapeEditorAdapter(Session session,JmeRenderingComponent component) {\r
208                         super(session,component);\r
209                 }\r
210                 \r
211                 @Override\r
212                 public synchronized void updateGeometry(Graph graph) {\r
213                         if (isParameterized) {\r
214                                 graph = createParameterization(graph);  \r
215                         }\r
216                         super.updateGeometry(graph);\r
217                 }\r
218                 \r
219                 \r
220                 \r
221                 protected AbstractGraphicsNode instantiateNode(IGraphicsNode comp,\r
222                                 G3DNode node) {\r
223                         CSGShapeNode mo = new CSGShapeNode(ShapeEditorBase.this, comp, node.getGraph(),node.getResource());\r
224                         updateGeometry(mo);\r
225                         return mo;\r
226                 }\r
227 \r
228                 \r
229                 private class ShapeEditorScenegraphQuery extends ScenegraphQuery {\r
230                         \r
231                         public ShapeEditorScenegraphQuery(Resource nodeResource) {\r
232                                 super(nodeResource);\r
233                         }\r
234                         \r
235                         @Override\r
236                         public void shapeAdded(Graph graph,IGraphicsNode n) {\r
237                                 updateGeometry((CSGShapeNode) n);\r
238 \r
239                                         if (n.getG3DNode(graph).getParent() == null) {\r
240                                                 if (DEBUG)System.out.println("ShapeSubnodeListener "\r
241                                                                 + n.getResource()\r
242                                                                 + " has no parent");\r
243                                                 return;\r
244                                         }\r
245                                         if (DEBUG) System.out.print("ShapeSubnodeListener " + n.getResource());\r
246                                         if (n.getG3DNode(graph).getRelatedObjects(ShapeEditorResources.g3dResource.GeometryDefinitionOf).size() == 0) {\r
247                                                 if (DEBUG) System.out.println(" visible");\r
248                                                 ((ISelectableNode)n).setVisible(true);\r
249                                         } else {\r
250                                                 if (DEBUG) System.out.println(" invisible");\r
251                                                 ((ISelectableNode)n).setVisible(false);\r
252                                         }\r
253                         }\r
254                         \r
255 //                      @Override\r
256 //                      public NodeQuery instantiateQuery(Resource node) {\r
257 //                              return new ShapeEditorScenegraphQuery(node);\r
258 //                      }\r
259                 }\r
260                 \r
261                 @Override\r
262                 protected ScenegraphQuery newSubnodeListener(G3DNode node) {\r
263                         return new ShapeEditorScenegraphQuery(node.getResource());\r
264                 }\r
265                 \r
266                 private class ShapeEditorNodePropertyQuery extends NodePropertyQuery {\r
267                         public ShapeEditorNodePropertyQuery(Resource nodeResource) {\r
268                                 super(nodeResource);\r
269                         }\r
270                         \r
271                         @Override\r
272                         public void shapeUpdated(Graph graph,final IGraphicsNode shape) {\r
273                                 if (DEBUG) System.out.println("Tri - Shape id " + shape + " modified");\r
274                                 ((CSGShapeNode) shape).updateAllGeometry();\r
275                         }\r
276                         \r
277 //                      @Override\r
278 //                      public NodeQuery instantiateQuery(Resource node) {\r
279 //                              return new ShapeEditorNodePropertyQuery(node);\r
280 //                      }\r
281                 }\r
282 \r
283                 @Override\r
284                 protected NodePropertyQuery newPropertyListener(G3DNode node) {\r
285                         return new ShapeEditorNodePropertyQuery(node.getResource());\r
286                 }\r
287                 \r
288                 private class ShapeEditorNodeTransformationQuery extends NodeTransformationQuery {\r
289                         public ShapeEditorNodeTransformationQuery(Resource nodeResource) {\r
290                                 super(nodeResource);\r
291                         }\r
292                         \r
293                         @Override\r
294                         public void shapeUpdated(Graph graph,final IGraphicsNode shape) {\r
295                                 if (DEBUG) System.out.println("Tra - Shape id " + shape + " modified");\r
296                                 ((CSGShapeNode) shape).updateTransform(graph);\r
297                                 geometryUpdate((CSGShapeNode)shape);\r
298                         }\r
299                         \r
300 //                      @Override\r
301 //                      public NodeQuery instantiateQuery(Resource node) {\r
302 //                              return new ShapeEditorNodePropertyQuery(node);\r
303 //                      }\r
304                 }\r
305 \r
306                 @Override\r
307                 protected NodeTransformationQuery newTransformationListener(G3DNode node) {\r
308                         return new ShapeEditorNodeTransformationQuery(node.getResource());\r
309                 }\r
310                 \r
311                 private class ShapeEditorRootPropertyQuery extends NodePropertyQuery {\r
312                         public ShapeEditorRootPropertyQuery(Resource nodeResource) {\r
313                                 super(nodeResource);\r
314                         }\r
315                         \r
316                         @Override\r
317                         public void shapeUpdated(Graph graph, final IGraphicsNode shape) {\r
318                                 if (DEBUG)System.out.println("Tri - Shape id " + shape + " modified");\r
319 \r
320                                 updateParameterizationStatus(graph);\r
321                                 if (isParameterized) {\r
322                                         for (IGraphicsNode n : getNodes())\r
323                                                 if (n instanceof IGeometryNode)\r
324                                                         updateGeometry((IGeometryNode) n);\r
325                                 }\r
326                         }\r
327                         \r
328 //                      @Override\r
329 //                      public NodeQuery instantiateQuery(Resource node) {\r
330 //                              return new ShapeEditorRootPropertyQuery(node);\r
331 //                      }\r
332                 }\r
333                 \r
334                 @Override\r
335                 protected NodePropertyQuery newRootPropertyListener(G3DNode root) {\r
336                         return new ShapeEditorRootPropertyQuery(root.getResource());\r
337                 }\r
338                 \r
339         }\r
340 \r
341         protected void contributeStatusBar(IStatusLineManager manager) {\r
342         }\r
343 \r
344         /**\r
345          * Loads the initial scene: all further updates to the view are done by\r
346          * listening changes in the shapes and int the shape group\r
347          * \r
348          * @param resource\r
349          */\r
350         protected void reloadFrom(IEntity thing) {\r
351                 if (model != null) {\r
352                         throw new UnsupportedOperationException(\r
353                                         "Reloading instantiated viewer not supported");\r
354 \r
355                 }\r
356                 if (thing.isInstanceOf(ShapeEditorResources.csgResource.CSGModel)) {\r
357                         //System.out.print("ShapeEditorView.reloadFrom() : model");\r
358                         Graph g = thing.getGraph();\r
359                         model = thing.getResource();\r
360                         //System.out.println(" " + model.getResource());\r
361                         adapter.setRootNode(new G3DNode(thing));\r
362                         updateParameterizationStatus(g);\r
363                         \r
364                         //loadGroup(thing.getGraph());\r
365                         \r
366                 } else {\r
367                         throw new UnsupportedOperationException("Cannot load ShapeViewer for Resource:" + thing);\r
368                 }\r
369 \r
370         }\r
371         \r
372         private void updateParameterizationStatus(Graph graph) {\r
373                 G3DModel model = getModel(graph);\r
374                 if(model.getRelatedObjects(ShapeEditorResources.g3dResource.HasSizingParameter).size() > 0) {\r
375                         isParameterized = true;\r
376                 } else {\r
377                         isParameterized = false;\r
378                 }\r
379                 parent.getDisplay().asyncExec(new Runnable() {\r
380                         @Override\r
381                         public void run() {\r
382 //                              for (Action a : addActions)\r
383 //                                      a.setEnabled(!isParameterized);\r
384 //                              unionAction.setEnabled(!isParameterized);\r
385 //                              differenceAction.setEnabled(!isParameterized);\r
386 //                              intersectionAction.setEnabled(!isParameterized);\r
387 //                              linkAction.setEnabled(!isParameterized);\r
388 //                              unlinkAction.setEnabled(!isParameterized);\r
389 //                              translateAction.setEnabled(!isParameterized);\r
390 //                              rotateAction.setEnabled(!isParameterized);\r
391 //                              removeAction.setEnabled(!isParameterized);\r
392                         }\r
393                         \r
394                 });\r
395         }\r
396 \r
397         public Resource getModelResource() {\r
398                 return model;\r
399         }\r
400         \r
401         public G3DModel getModel(Graph graph) {\r
402                 return new G3DModel(graph, model);\r
403         }\r
404 \r
405         @Override\r
406         protected SelectionAdapter createSelectionAdapter() {\r
407                 return new ShapeEditorSelectionAdapter(adapter);\r
408         }\r
409 \r
410         protected class ShapeEditorSelectionAdapter extends SelectionAdapter {\r
411 \r
412                 public ShapeEditorSelectionAdapter(ScenegraphAdapter adapter) {\r
413                         super(adapter);\r
414                 }\r
415 \r
416                 public void setEditorSelection() {\r
417                         List<IGraphicsNode> sel = getSelectedObjects();\r
418                         for (IGraphicsNode o : adapter.getNodes())\r
419                                 if (o instanceof ISelectableNode) {\r
420                                         ISelectableNode n = (ISelectableNode)o;\r
421                                         if (sel.contains(o))\r
422                                                 n.setSelected(true);\r
423                                         else\r
424                                                 n.setSelected(false);\r
425                                 }\r
426                         List<Resource> selected = getSelectedResources();\r
427                         for (Resource r : selected) {\r
428                                 if (!adapter.hasNode(r)) {\r
429                                         //adapter.addInbound(r).setSelected(true);\r
430                                 }\r
431                         }\r
432                 }\r
433 \r
434                 public void setEditorHighlightSelection() {\r
435                         List<IGraphicsNode> sel = getInteractiveSelectedObjects();\r
436                         for (IGraphicsNode o : adapter.getNodes())\r
437                                 if (o instanceof CSGShapeNode) {\r
438                                         if (sel.contains(o))\r
439                                                 ((CSGShapeNode) o).setHighlighted(true);\r
440                                         else\r
441                                                 ((CSGShapeNode) o).setHighlighted(false);\r
442                                 }\r
443                 }\r
444 \r
445                 public void setEditorSelection(boolean addShapes) {\r
446 \r
447                         List<IGraphicsNode> sel = getSelectedObjects();\r
448                         for (IGraphicsNode o : adapter.getNodes()) \r
449                                 if (o instanceof ISelectableNode) {\r
450                                         ISelectableNode n = (ISelectableNode)o;\r
451                                         if (sel.contains(o)) \r
452                                                 n.setSelected(true);\r
453                                         else\r
454                                                 n.setSelected(false);\r
455                                 }\r
456                         viewChanged = true;\r
457                         if (addShapes) {\r
458                                 session.syncRead(new GraphRequestAdapter() {\r
459                                         @Override\r
460                                         public GraphRequestStatus perform(Graph g) throws Exception {\r
461                                                 List<Resource> selected = getSelectedResources();\r
462                                                 for (Resource r : selected) {\r
463                                                         if (!adapter.hasNode(r)) {\r
464                                                                 IEntity t = EntityFactory.create(g, r);\r
465                                                                 if (t.isInstanceOf(ShapeEditorResources.g3dResource.Shape)) {\r
466                                                                         G3DNode group = G3DTools.getModelFromResource(g,r);\r
467                                                                         if (group != null\r
468                                                                                         && group.getResource().equals(model.getResource())) {\r
469                                                                                 //adapter.addInbound(g).setSelected(true);\r
470                                                                         }\r
471 \r
472                                                                 }\r
473                                                         }\r
474                                                 }\r
475                                                 return GraphRequestStatus.transactionComplete();\r
476                                         }\r
477                                 });\r
478                                 \r
479                         }\r
480                 }\r
481 \r
482                 public StructuredResourceSelection filterSelection(ISelection selection) {\r
483                         if (!(selection instanceof StructuredResourceSelection))\r
484                                 return new StructuredResourceSelection();\r
485                         return (StructuredResourceSelection) selection;\r
486                 }\r
487 \r
488         }\r
489 \r
490         /**\r
491          * Receives selection changes\r
492          * \r
493          * @param part\r
494          * @param selection\r
495          */\r
496         protected void pageSelectionChanged(IWorkbenchPart part, ISelection selection) {\r
497                 \r
498                 StructuredResourceSelection s = SelectionAdapter.transformSelection(selection);\r
499                 //System.out.println("ShapeEditorBase.pageSelectionChanged " + s);\r
500                 selectionAdapter.setCurrentSelection(s);\r
501 \r
502                 if (!(part instanceof ThreeDimensionalEditorProvider)) {\r
503                         ((ShapeEditorSelectionAdapter) selectionAdapter).setEditorSelection(true);\r
504                         return;\r
505                 }\r
506                 ThreeDimensionalEditorBase e = ((ThreeDimensionalEditorProvider)part).getEditor();\r
507                 if (!(e instanceof ShapeEditorBase)) {\r
508                         ((ShapeEditorSelectionAdapter) selectionAdapter).setEditorSelection(true);\r
509                         return;\r
510                 }\r
511                 \r
512                 ShapeEditorBase editor = (ShapeEditorBase)e;\r
513                 \r
514                 if (!editor.getModelResource().equals(model.getResource())) {\r
515                         selectionAdapter.setCurrentSelection(new StructuredResourceSelection());\r
516                         ((ShapeEditorSelectionAdapter) selectionAdapter).setEditorSelection(false);\r
517                         return;\r
518                 }\r
519                 selectionAdapter.setEditorSelection();\r
520         }\r
521 \r
522         @Override\r
523         protected void hookDragAndDrop() {\r
524                 super.hookDragAndDrop();\r
525                 dropTarget.addDropListener(new DropListener() {\r
526                         public boolean acceptDrop(StructuredResourceSelection s, Resource[] ids) {\r
527                                 if (!s.isEmpty())\r
528                                         return false;\r
529                                 if (ids == null)\r
530                                         return false;\r
531                                 if (ids.length != 1)\r
532                                         return false;\r
533                                 final Resource r = ids[0];\r
534                                 GraphRequestWithResult<Boolean> rq = new GraphRequestWithResult<Boolean>() {\r
535                                         @Override\r
536                                         public Boolean performWithResult(Graph g) throws Exception {\r
537                                                 IEntity t = EntityFactory.create(g, r);\r
538                                                 return t.isInstanceOf(ShapeEditorResources.csgResource.Primitive);\r
539                                         }\r
540                                 };\r
541                                 session.syncRead(rq);\r
542                                 return rq.getResult();\r
543                         }\r
544 \r
545                         public void doDrop(StructuredResourceSelection s, Resource[] ids) {\r
546                                 session.asyncWrite(new GraphRequestAdapter() {\r
547                                         Resource r;\r
548 \r
549                                         public GraphRequestStatus perform(Graph g) throws Exception {\r
550                                                 IEntity type = EntityFactory.create(g);\r
551                                                 IEntity instance = type.instantiate();\r
552                                                 Shape shape = new Shape(instance);\r
553                                                 resetShape(shape);\r
554                                                 CSGModel m = new CSGModel(g, model);\r
555                                                 m.getChild().add(shape.toG3DNode()); // FIXME : stubcast\r
556                                                 return GraphRequestStatus.transactionComplete();\r
557                                                 \r
558                                         };\r
559                                         \r
560                                         @Override\r
561                                         public void requestCompleted(GraphRequestStatus status) {\r
562                                                 selectionAdapter\r
563                                                 .updateSelection(new StructuredResourceSelection(r));\r
564                                                 super.requestCompleted(status);\r
565                                         }\r
566                                 }\r
567                                 );\r
568 \r
569                         }\r
570                 });\r
571         }\r
572         \r
573         private void resetShape(Shape shape) {\r
574                 G3DTools.resetTransformation(shape);\r
575                 Graph graph = shape.getGraph();\r
576                 if (shape.isInstanceOf(ShapeEditorResources.csgResource.Primitive)) {\r
577                         Primitive prim = new Primitive(shape);\r
578                         Collection<Property> c = prim.getSizingProperty();\r
579                         if (c.size() == 0)\r
580                                 ErrorLogger.getDefault().logWarning("Shape does not contain sizing properties.", null);\r
581                         \r
582                         for (Property p : c) {\r
583                                 if (p.isInstanceOf(graph.getBuiltins().Double)) {\r
584                                         graph.setScalarDouble(p.getResource(), 1.0);\r
585                                 } else if (p.isInstanceOf(graph.getBuiltins().Integer)) {\r
586                                         graph.setScalarInteger(p.getResource(), 1);\r
587                                 } else {\r
588                                         ErrorLogger.getDefault().logWarning("Cannot handle sizing property " + p.getName() , null);\r
589                                 }\r
590                         }\r
591                 }\r
592                 \r
593         }\r
594         \r
595         @Override\r
596         public Object getAdapter(Class adapter) {\r
597                 if (adapter == IContentOutlinePage.class) {\r
598                         if (getModelResource() == null)\r
599                                 return null;\r
600                         final StructureOutlinePage page = new StructureOutlinePage(sessionContext,getModelResource());\r
601                         \r
602                         getSelectionAdapter().addSelectionChangedListener(new ISelectionChangedListener() {\r
603                                 @Override\r
604                                 public void selectionChanged(SelectionChangedEvent event) {\r
605                                         page.setSelection(event.getSelection());\r
606                                         \r
607                                 }\r
608                         });\r
609                         parent.getDisplay().asyncExec(new Runnable() {\r
610                                 @Override\r
611                                 public void run() {\r
612                                         page.addSelectionChangedListener(new ISelectionChangedListener() {\r
613                                                 @Override\r
614                                                 public void selectionChanged(SelectionChangedEvent event) {\r
615                                                         selectionAdapter.setSelection(SelectionAdapter.transformSelection(event.getSelection()));\r
616                                                 }\r
617                                         });\r
618                                 }\r
619                         });\r
620                         \r
621                         \r
622                         \r
623                         return page;\r
624                 }\r
625                 return null;\r
626         }\r
627 \r
628 }\r