]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java
Translate action for inline components
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / editor / Plant3DEditor.java
1 package org.simantics.plant3d.editor;
2
3 import java.awt.Component;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.eclipse.jface.action.IMenuListener;
9 import org.eclipse.jface.action.IMenuManager;
10 import org.eclipse.jface.action.MenuManager;
11 import org.eclipse.jface.viewers.ISelectionChangedListener;
12 import org.eclipse.jface.viewers.ISelectionProvider;
13 import org.eclipse.jface.viewers.SelectionChangedEvent;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.DisposeEvent;
16 import org.eclipse.swt.events.DisposeListener;
17 import org.eclipse.swt.layout.FillLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Menu;
21 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.Session;
25 import org.simantics.db.common.request.ReadRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.g3d.scenegraph.IG3DNode;
28 import org.simantics.g3d.scenegraph.NodeMap;
29 import org.simantics.g3d.scenegraph.base.INode;
30 import org.simantics.g3d.vtk.action.RemoveAction;
31 import org.simantics.g3d.vtk.action.RotateAction;
32 import org.simantics.g3d.vtk.action.TranslateAction;
33 import org.simantics.g3d.vtk.action.vtkCameraAndSelectorAction;
34 import org.simantics.g3d.vtk.common.ContextMenuListener;
35 import org.simantics.g3d.vtk.common.HoverHighlighter;
36 import org.simantics.g3d.vtk.common.InteractiveVtkPanel;
37 import org.simantics.g3d.vtk.common.NodeSelectionProvider2;
38 import org.simantics.g3d.vtk.common.SelectionHighlighter;
39 import org.simantics.g3d.vtk.shape.vtkShape;
40 import org.simantics.g3d.vtk.utils.AxesDisplay;
41 import org.simantics.g3d.vtk.utils.vtkPanelUtil;
42 import org.simantics.objmap.graph.IMapping;
43 import org.simantics.objmap.graph.Mappings;
44 import org.simantics.objmap.graph.schema.IMappingSchema;
45 import org.simantics.plant3d.actions.AddComponentAction;
46 import org.simantics.plant3d.actions.AddEquipmentAction;
47 import org.simantics.plant3d.actions.AddNozzleAction;
48 import org.simantics.plant3d.actions.RoutePipeAction;
49 import org.simantics.plant3d.actions.TranslateInlineAction;
50 import org.simantics.plant3d.scenegraph.EndComponent;
51 import org.simantics.plant3d.scenegraph.Equipment;
52 import org.simantics.plant3d.scenegraph.IP3DNode;
53 import org.simantics.plant3d.scenegraph.InlineComponent;
54 import org.simantics.plant3d.scenegraph.Nozzle;
55 import org.simantics.plant3d.scenegraph.P3DRootNode;
56 import org.simantics.plant3d.scenegraph.PipeRun;
57 import org.simantics.plant3d.scenegraph.PipelineComponent;
58 import org.simantics.plant3d.scenegraph.SchemaBuilder;
59 import org.simantics.plant3d.scenegraph.TurnComponent;
60 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
61 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
62 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
63 import org.simantics.plant3d.utils.ComponentUtils;
64 import org.simantics.plant3d.utils.Item;
65 import org.simantics.plant3d.utils.P3DUtil;
66 import org.simantics.selectionview.StandardPropertyPage;
67 import org.simantics.ui.workbench.IPropertyPage;
68 import org.simantics.ui.workbench.IResourceEditorInput;
69 import org.simantics.ui.workbench.ResourceEditorPart;
70 import org.simantics.utils.threads.AWTThread;
71 import org.simantics.utils.threads.ThreadUtils;
72 import org.simantics.utils.ui.ExceptionUtils;
73 import org.simantics.utils.ui.SWTAWTComponent;
74
75 import vtk.vtkActor;
76 import vtk.vtkAxesActor;
77 import vtk.vtkCameraPass;
78 import vtk.vtkDefaultPass;
79 import vtk.vtkLightsPass;
80 import vtk.vtkOrientationMarkerWidget;
81 import vtk.vtkRenderPassCollection;
82 import vtk.vtkRenderer;
83 import vtk.vtkSequencePass;
84
85
86 public class Plant3DEditor extends ResourceEditorPart {
87
88         private Composite parent;
89         private Resource input;
90         private InteractiveVtkPanel panel;
91         private SWTAWTComponent component;
92         
93         private P3DRootNode rootNode;
94         private IMapping<Resource,Object> mapping;
95         
96         private NodeSelectionProvider2<Resource,Object> selectionProvider;
97         
98         private vtkCameraAndSelectorAction cameraAction;
99         private TranslateAction translateAction;
100         private TranslateInlineAction translateInlineAction;
101         private RotateAction rotateAction;
102         private RemoveAction removeAction;
103         private RoutePipeAction routePipeAction;
104         private AddComponentAction addComponentAction;
105         
106         private P3DNodeMap nodeMap;
107         
108         @Override
109         public void createPartControl(Composite parent) {
110                 this.parent = parent;
111                 parent.setLayout (new FillLayout ());
112                 component = new SWTAWTComponent(parent,SWT.NONE) {
113                         
114                         @Override
115                         protected Component createSwingComponent() {
116                                 if (panel == null) {
117                                         panel = new InteractiveVtkPanel();
118                                         vtkPanelUtil.registerPanel(panel);
119                                         createScene();
120                                 }
121                                 return panel;
122                         }
123                 };
124
125                 IResourceEditorInput rei = (IResourceEditorInput)getEditorInput();
126                 input = rei.getResource();
127                 
128                 
129                 //IActionBars actionBars = getEditorSite().getActionBars();
130
131                 hookContextMenu();
132                 
133                 component.syncPopulate();
134                 
135                 new ContextMenuListener(panel, contextMenu);
136                 
137                 cameraAction = new vtkCameraAndSelectorAction(panel);   
138                 panel.setDefaultAction(cameraAction);
139                 panel.useDefaultAction();
140                 panel.setPickType(4);
141                 
142                 try {
143                         ControlPointFactory.preloadCache();
144                         ComponentUtils.preloadCache();
145                 } catch (Exception e) {
146                         ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e);
147                         return;
148                 }
149                 
150                 try {
151                         getSession().syncRequest(new ReadRequest() {
152                                 
153                                 @SuppressWarnings({ "rawtypes", "unchecked" })
154                                 @Override
155                                 public void run(ReadGraph graph) throws DatabaseException {
156                                         PipingRules.setEnabled(false);
157                                         IMappingSchema<Resource, Object> schema = getSchema(graph);
158                                         mapping = Mappings.createWithListening(schema);
159                                         rootNode = (P3DRootNode)mapping.map(graph, input);
160                                         // update control points.
161                                         // TODO : this should be optimized.
162                                         try {
163                                                 for (INode node : rootNode.getChild()) {
164                                                         if (node instanceof PipeRun) {
165                                                                 for (PipelineComponent pc : ((PipeRun) node).getChild())
166                                                                         pc.sync();
167                                                         } else if (node instanceof Equipment) {
168                                                                 for (PipelineComponent pc : ((Equipment) node).getChild())
169                                                                         pc.sync();
170                                                         }
171                                                 }
172                                                 
173                                                 for (INode node : rootNode.getChild()) {
174                                                         if (node instanceof PipeRun) {
175                                                                 for (PipelineComponent pc : ((PipeRun) node).getChild())
176                                                                         pc.sync2();
177                                                         } else if (node instanceof Equipment) {
178                                                                 for (PipelineComponent pc : ((Equipment) node).getChild())
179                                                                         pc.sync2();
180                                                         }
181                                                 }
182                                                 for (INode node : rootNode.getChild()) {
183                                                         if (node instanceof PipeRun) {
184                                                                 PipingRules.validate((PipeRun)node);
185                                                         }
186                                                 }
187                                                 PipingRules.setEnabled(true);
188                                                 for (INode node : rootNode.getChild()) {
189                                                         if (node instanceof PipeRun) {
190                                                                 PipeRun run = (PipeRun)node;
191                                                                 for (PipeControlPoint pcp : run.getControlPoints())
192                                                                         PipingRules.positionUpdate(pcp);
193                                                                         
194                                                         }
195                                                 }
196                                         } catch (Exception e) {
197                                                 throw new DatabaseException(e);
198                                         }
199                                         nodeMap = createNodeMap(getSession(), mapping, panel,rootNode);
200                                 }
201                         });
202                         
203                         if (rootNode == null)
204                                 throw new RuntimeException("Scenegraph loading failed.");
205                         populate();
206                         
207                         selectionProvider = new NodeSelectionProvider2<Resource,Object>(this,mapping,nodeMap);
208
209                         cameraAction.addSelectionChangedListener(selectionProvider);
210
211                         cameraAction.addHoverChangedListener(new HoverHighlighter(panel,nodeMap));
212                         selectionProvider.addSelectionChangedListener(new SelectionHighlighter(panel,nodeMap));
213                         
214                         getSite().setSelectionProvider(selectionProvider);
215                         getSite().getPage().addPostSelectionListener(selectionProvider);
216                         
217                         //outlinePage = new ScenegraphOutlinePage(rootNode);
218                         
219                         
220                         parent.addDisposeListener(new DisposeListener() {
221                                 
222                                 @Override
223                                 public void widgetDisposed(DisposeEvent e) {
224                                         getSite().getPage().removePostSelectionListener(selectionProvider);
225                                         
226                                         ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
227                                                 
228                                                 @Override
229                                                 public void run() {
230                                                         PipingRules.setEnabled(false);
231                                                         nodeMap.delete();
232                                                         PipingRules.setEnabled(true);
233                                                         vtkPanelUtil.unregisterPanel(panel);
234
235                                                 }
236                                         });
237                                         mapping.dispose();
238                                         component.dispose();
239                                         
240                                         
241                                 }
242                         });
243                 } catch (DatabaseException e1) {
244                         ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e1);
245                         return;
246                 }
247                 
248                 translateAction = new TranslateAction(panel,nodeMap);
249                 translateInlineAction = new TranslateInlineAction(panel, nodeMap);
250                 rotateAction = new RotateAction(panel,nodeMap);
251                 removeAction = new RemoveAction(nodeMap) {
252                         public void setNode(IG3DNode node) {
253                                 super.setNode(node);
254                                 
255                                 
256                         }
257                 };
258                 routePipeAction = new RoutePipeAction(panel,rootNode);
259                 addComponentAction = new AddComponentAction(panel, rootNode);
260                 
261         }
262         
263         public void populate() {
264                 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
265                         
266                         @Override
267                         public void run() {
268                                 nodeMap.populate();
269                         }
270                 });
271                 
272         }
273         
274         protected IMappingSchema<Resource, Object> getSchema(ReadGraph graph) throws DatabaseException {
275                 IMappingSchema<Resource,Object> schema = SchemaBuilder.getSchema(graph);
276                 return schema;
277         }
278         
279         protected P3DNodeMap createNodeMap(Session session, IMapping<Resource, Object> mapping, InteractiveVtkPanel panel, P3DRootNode rootNode) {
280                  return new P3DNodeMap(session, mapping, panel,rootNode);
281         }
282         
283         @Override
284         public void setFocus() {
285                 component.setFocus();
286         }
287         
288         private void createScene() {
289                 vtkRenderer ren1 = panel.GetRenderer();
290                 
291                 boolean multiPass = false;
292                 if (multiPass) {
293                         
294                         vtkLightsPass lightsPass = new vtkLightsPass();
295                         vtkDefaultPass defaultPass = new vtkDefaultPass();
296                         
297                         
298                         vtkRenderPassCollection passes = new vtkRenderPassCollection();
299                         passes.AddItem(lightsPass);
300                         passes.AddItem(defaultPass);
301                         
302                         vtkSequencePass seq = new vtkSequencePass();
303                         seq.SetPasses(passes);
304                         
305                         vtkCameraPass cameraPass = new vtkCameraPass();
306                         cameraPass.SetDelegatePass(seq);
307                         
308                         ren1.SetPass(cameraPass);
309                         
310                 }
311 //              ren1.GetRenderWindow().LineSmoothingOn();
312 //              ren1.GetRenderWindow().PointSmoothingOn();
313 //              ren1.GetRenderWindow().PolygonSmoothingOn();
314 //              ren1.GetRenderWindow().SetMultiSamples(2);
315
316                 
317
318                 ren1.SetBackground2(1,1,1); // background color white
319                 ren1.SetBackground(0.9,0.9,0.9);
320                 ren1.SetGradientBackground(true);
321
322            // vtkActor grid = vtkShape.createGridActor(8,1.0,1|2|4);
323             vtkActor grid = vtkShape.createGridActor(8,1.0, 2 );
324             grid.SetPickable(0);
325             ren1.AddActor(grid);
326             panel.addDeletable(grid);
327             
328             AxesDisplay axesDisplay = new AxesDisplay(panel);
329             axesDisplay.show();
330                 
331         }
332         
333         protected Menu contextMenu;
334         
335         protected void hookContextMenu() {
336         MenuManager menuMgr = new MenuManager("#PopupMenu");
337         menuMgr.setRemoveAllWhenShown(true);
338         menuMgr.addMenuListener(new IMenuListener() {
339             public void menuAboutToShow(IMenuManager manager) {                 
340                 createContextMenu(manager);
341             }
342         });
343
344         contextMenu = menuMgr.createContextMenu(parent);
345     }
346         
347         protected void createContextMenu(IMenuManager m) {
348                 List<IG3DNode> selected = selectionProvider.getSelectedNodes();
349                 try {
350                         if (selected.size() == 0) {
351                                 for (Item eq : P3DUtil.getEquipments()) {
352                                         m.add(new AddEquipmentAction(rootNode, eq));
353                                 }
354                         } else if (selected.size() == 1) {
355                                 IP3DNode node = (IP3DNode)selected.get(0);
356                                 if (node instanceof Equipment) {
357                                         m.add(translateAction);
358                                         m.add(rotateAction);
359                                         for (Item eq : P3DUtil.getNozzles()) {
360                                                 AddNozzleAction add = new AddNozzleAction(rootNode, eq);
361                                                 add.setEquipment((Equipment)node);
362                                                 m.add(add);
363                                         }
364                                 } else if (node instanceof Nozzle) {
365                                         m.add(translateAction);
366                                         m.add(rotateAction);
367                                         Nozzle nozzle = (Nozzle)node;
368                                         m.add(routePipeAction);
369                                         routePipeAction.setComponent(nozzle);
370                                         routePipeAction.setEnabled(nozzle.getNext() == null && nozzle.getPrevious() == null);
371                                         m.add(addComponentAction);
372                                         addComponentAction.setComponent(nozzle);
373                                 } else if (node instanceof TurnComponent) {
374                                         m.add(translateAction);
375                                         TurnComponent component = (TurnComponent)node;
376                                         m.add(routePipeAction);
377                                         routePipeAction.setComponent(component);
378                                         routePipeAction.setEnabled(component.getNext() == null || component.getPrevious() == null);
379                                         m.add(addComponentAction);
380                                         addComponentAction.setComponent(component);
381                                 } else if (node instanceof EndComponent) {
382                                         m.add(translateAction);
383                                         m.add(addComponentAction);
384                                         addComponentAction.setComponent((PipelineComponent)node);
385                                 } else if (node instanceof InlineComponent) {
386                                         //m.add(translateInlineAction);
387                                         InlineComponent component = (InlineComponent)node;
388                                         m.add(translateInlineAction);
389                                         m.add(routePipeAction);
390                                         routePipeAction.setComponent(component);
391                                         m.add(addComponentAction);
392                                         addComponentAction.setComponent(component);
393                                 }
394                                 
395                                 m.add(removeAction);
396                                 translateAction.setNode(node);
397                                 translateInlineAction.setNode(node);
398                                 rotateAction.setNode(node);
399                                 removeAction.setNode(node);
400                                 
401                         } 
402                 } catch (DatabaseException e) {
403                 ExceptionUtils.logAndShowError(e);
404         }
405         }
406         
407         private IContentOutlinePage createOutline() {
408                 if (rootNode == null || selectionProvider == null)
409                         return null;
410                 //IContentOutlinePage outlinePage = new VTKContentOutlinePage<Resource,Object>(rootNode, selectionProvider);
411                 IContentOutlinePage outlinePage = new P3DContentOutlinePage(rootNode, selectionProvider) {
412                         protected void createContextMenu(IMenuManager manager) {
413                                 Plant3DEditor.this.createContextMenu(manager);
414                         };
415                 };
416                 outlinePage.addSelectionChangedListener(new ISelectionChangedListener() {
417                         
418                         @Override
419                         public void selectionChanged(SelectionChangedEvent event) {
420                                 selectionProvider.selectionChanged(event);
421                         }
422                 });
423                 return outlinePage;
424         }
425
426         @SuppressWarnings("rawtypes")
427         @Override
428         public Object getAdapter(Class adapter) {
429                 if (IPropertyPage.class.equals(adapter))
430                         return new StandardPropertyPage(getSite(),getPropertyContexts());
431                 if (IContentOutlinePage.class.equals(adapter)) {
432                         return createOutline();
433                 }
434                 if (NodeMap.class.equals(adapter)) {
435                         return nodeMap;
436                 }
437                 if (INode.class.equals(adapter)) {
438                         return rootNode;
439                 }
440                 if (IMapping.class.equals(adapter)) {
441                         return mapping;
442                 }
443                 if (InteractiveVtkPanel.class.equals(adapter)) {
444                         return panel;
445                 }
446                 if (ISelectionProvider.class.equals(adapter))
447                         return selectionProvider;
448                 return super.getAdapter(adapter);
449         }
450         
451         public Set<String> getPropertyContexts() {
452                 Set<String> result = new HashSet<String>();
453                 result.add("http://www.simantics.org/Project-1.0/ProjectBrowseContext");
454                 return result;
455         }
456         
457         public P3DRootNode getRootNode() {
458                 return rootNode;
459         }
460         
461         public IMapping<Resource, Object> getMapping() {
462                 return mapping;
463         }
464 }