]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java
Allow adding adjustable length components in the middle of a pipe
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / editor / Plant3DEditor.java
1 package org.simantics.plant3d.editor;
2
3 import java.util.ArrayList;
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.layout.GridDataFactory;
12 import org.eclipse.jface.layout.GridLayoutFactory;
13 import org.eclipse.jface.viewers.ISelectionChangedListener;
14 import org.eclipse.jface.viewers.ISelectionProvider;
15 import org.eclipse.jface.viewers.SelectionChangedEvent;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.DisposeEvent;
18 import org.eclipse.swt.events.DisposeListener;
19 import org.eclipse.swt.layout.FillLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Menu;
22 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.Session;
26 import org.simantics.db.common.request.ReadRequest;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.g3d.scenegraph.NodeMap;
29 import org.simantics.g3d.scenegraph.RenderListener;
30 import org.simantics.g3d.scenegraph.base.INode;
31 import org.simantics.g3d.toolbar.ToolComposite;
32 import org.simantics.g3d.vtk.action.RemoveAction;
33 import org.simantics.g3d.vtk.common.HoverHighlighter;
34 import org.simantics.g3d.vtk.common.NodeSelectionProvider2;
35 import org.simantics.g3d.vtk.common.SelectionHighlighter;
36 import org.simantics.g3d.vtk.common.VTKSelectionItem;
37 import org.simantics.g3d.vtk.common.VtkView;
38 import org.simantics.g3d.vtk.shape.vtkShape;
39 import org.simantics.g3d.vtk.swt.ContextMenuListener;
40 import org.simantics.g3d.vtk.swt.FocusAction;
41 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
42 import org.simantics.g3d.vtk.swt.RotateAction;
43 import org.simantics.g3d.vtk.swt.TranslateAction;
44 import org.simantics.g3d.vtk.swt.vtkCameraAndSelectorAction;
45 import org.simantics.g3d.vtk.utils.AxesDisplay;
46 import org.simantics.objmap.graph.IMapping;
47 import org.simantics.objmap.graph.Mappings;
48 import org.simantics.objmap.graph.schema.IMappingSchema;
49 import org.simantics.plant3d.actions.AddComponentAction;
50 import org.simantics.plant3d.actions.AddEquipmentAction;
51 import org.simantics.plant3d.actions.AddNozzleAction;
52 import org.simantics.plant3d.actions.RemoveAndSplitAction;
53 import org.simantics.plant3d.actions.ReversePipeRunAction;
54 import org.simantics.plant3d.actions.RoutePipeAction;
55 import org.simantics.plant3d.actions.TranslateFreeVariableLengthAction;
56 import org.simantics.plant3d.actions.TranslateInlineAction;
57 import org.simantics.plant3d.ontology.Plant3D;
58 import org.simantics.plant3d.scenegraph.EndComponent;
59 import org.simantics.plant3d.scenegraph.Equipment;
60 import org.simantics.plant3d.scenegraph.IP3DNode;
61 import org.simantics.plant3d.scenegraph.InlineComponent;
62 import org.simantics.plant3d.scenegraph.Nozzle;
63 import org.simantics.plant3d.scenegraph.P3DRootNode;
64 import org.simantics.plant3d.scenegraph.PipeRun;
65 import org.simantics.plant3d.scenegraph.PipelineComponent;
66 import org.simantics.plant3d.scenegraph.SchemaBuilder;
67 import org.simantics.plant3d.scenegraph.TurnComponent;
68 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
69 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
70 import org.simantics.plant3d.utils.ComponentUtils;
71 import org.simantics.plant3d.utils.Item;
72 import org.simantics.plant3d.utils.P3DUtil;
73 import org.simantics.selectionview.StandardPropertyPage;
74 import org.simantics.ui.workbench.IPropertyPage;
75 import org.simantics.ui.workbench.IResourceEditorInput;
76 import org.simantics.ui.workbench.ResourceEditorPart;
77 import org.simantics.utils.threads.ThreadUtils;
78 import org.simantics.utils.ui.ExceptionUtils;
79
80 import vtk.vtkActor;
81 import vtk.vtkCameraPass;
82 import vtk.vtkDefaultPass;
83 import vtk.vtkGaussianBlurPass;
84 import vtk.vtkLightsPass;
85 import vtk.vtkProp;
86 import vtk.vtkRenderPassCollection;
87 import vtk.vtkRenderer;
88 import vtk.vtkSSAAPass;
89 import vtk.vtkSequencePass;
90 import vtk.vtkSimpleMotionBlurPass;
91
92
93 public class Plant3DEditor extends ResourceEditorPart {
94
95         private Composite parent;
96         protected ToolComposite toolComposite;
97         private Resource input;
98 //      private InteractiveVtkPanel panel;
99 //      private SWTAWTComponent component;
100         private InteractiveVtkComposite panel;
101
102         
103         private P3DRootNode rootNode;
104         private IMapping<Resource,INode> mapping;
105         
106         protected NodeSelectionProvider2<Resource, INode> selectionProvider;
107         
108         
109         protected vtkCameraAndSelectorAction cameraAction;
110         protected FocusAction focusAction;
111         protected TranslateAction translateAction;
112         protected TranslateInlineAction translateInlineAction;
113         protected TranslateFreeVariableLengthAction translateFreeVariableLengthAction;
114         protected RotateAction rotateAction;
115         protected RemoveAction removeAction;
116         protected RemoveAndSplitAction removeSplitAction;
117         protected RoutePipeAction routePipeAction;
118         protected AddComponentAction addComponentAction;
119         protected ReversePipeRunAction reversePipeRunAction;
120         
121         private P3DNodeMap nodeMap;
122         
123         @Override
124         public void createPartControl(Composite parent) {
125                 this.parent = parent;
126                 //parent.setLayout (new FillLayout ());
127                 
128         
129 //              component = new SWTAWTComponent(parent,SWT.NONE) {
130 //                      
131 //                      @Override
132 //                      protected Component createSwingComponent() {
133 //                              if (panel == null) {
134 //                                      panel = new InteractiveVtkPanel();
135 //                                      vtkPanelUtil.registerPanel(panel);
136 //                                      createScene();
137 //                              }
138 //                              return panel;
139 //                      }
140 //              };
141
142                 IResourceEditorInput rei = (IResourceEditorInput)getEditorInput();
143                 input = rei.getResource();
144                 
145                 toolComposite = new ToolComposite(parent, SWT.BORDER);
146             toolComposite.setVisible(true);
147                 
148                 panel = new InteractiveVtkComposite(parent);
149                 
150                 GridLayoutFactory.fillDefaults().margins(0, 0).spacing(0, 0).applyTo(parent);
151         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(getPanel().getComponent());
152                 
153       
154         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(toolComposite);
155         
156                 //IActionBars actionBars = getEditorSite().getActionBars();
157
158                 hookContextMenu();
159                 
160                 createScene();
161                 
162                 new ContextMenuListener(panel, contextMenu);
163                 
164                 cameraAction = createCameraAction();
165                 panel.setDefaultAction(cameraAction);
166                 panel.useDefaultAction();
167                 panel.setPickType(4);
168                 
169                 try {
170                         ControlPointFactory.preloadCache(getLibraryUri());
171                         ComponentUtils.preloadCache();
172                 } catch (Exception e) {
173                         ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e);
174                         return;
175                 }
176                 
177                 try {
178                         getSession().syncRequest(new ReadRequest() {
179                                 
180                                 @Override
181                                 public void run(ReadGraph graph) throws DatabaseException {
182                                     System.out.println("START PLANT3D LOAD");
183                                         PipingRules.setEnabled(false);
184                                         IMappingSchema<Resource,INode> schema = getSchema(graph);
185                                         mapping = Mappings.createWithListening(schema);
186                                         rootNode = (P3DRootNode)mapping.map(graph, input);
187                                         // update control points.
188                                         // TODO : this should be optimized.
189
190                                         try {
191                                             P3DUtil.finalizeDBLoad(rootNode);
192                                             nodeMap = createNodeMap(getSession(), mapping, panel,rootNode);
193                                            
194                     } catch (Exception e) {
195                         throw new DatabaseException(e);
196                     }
197                                         
198                                         System.out.println("END PLANT3D LOAD");
199                                 }
200                         });
201                         
202                         if (rootNode == null)
203                                 throw new RuntimeException("Scenegraph loading failed.");
204                         populate();
205                         
206                         selectionProvider = createSelectionProvider();
207
208                         cameraAction.addSelectionChangedListener(selectionProvider);
209
210                         cameraAction.addHoverChangedListener(createHoverHighlhighter());
211                         selectionProvider.addSelectionChangedListener(createSelectionHighlighter());
212                         
213                         getSite().setSelectionProvider(selectionProvider);
214                         getSite().getPage().addPostSelectionListener(selectionProvider);
215                         
216                         //outlinePage = new ScenegraphOutlinePage(rootNode);
217                         
218                         
219                         parent.addDisposeListener(new DisposeListener() {
220                                 
221                                 @Override
222                                 public void widgetDisposed(DisposeEvent e) {
223                                         getSite().getPage().removePostSelectionListener(selectionProvider);
224                                         
225                                         PipingRules.setEnabled(false);
226                                         nodeMap.delete();
227                                         PipingRules.setEnabled(true);
228                                         mapping.dispose();
229 //                                      component.dispose();
230                                         //panel.getComponent().dispose();
231                                         
232                                 }
233                         });
234                 } catch (DatabaseException e1) {
235                         ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e1);
236                         return;
237                 }
238                 
239                 createActions();
240                 
241         }
242         
243         protected vtkCameraAndSelectorAction createCameraAction() {
244             return new vtkCameraAndSelectorAction(panel); 
245         }
246         
247         protected NodeSelectionProvider2<Resource,INode> createSelectionProvider() {
248             return new NodeSelectionProvider2<Resource,INode>(this,mapping,nodeMap);
249         }
250         
251         protected HoverHighlighter<Resource> createHoverHighlhighter() {
252             return new HoverHighlighter<>(panel,nodeMap);
253         }
254         
255         protected SelectionHighlighter<Resource> createSelectionHighlighter() {
256             return new SelectionHighlighter<Resource>(panel,nodeMap);
257         }
258         
259         protected String getLibraryUri() {
260             return Plant3D.URIs.Builtin;
261         }
262         
263         protected void createActions() {
264             focusAction = new FocusAction(panel, cameraAction);
265                 translateAction = new TranslateAction(panel,nodeMap,toolComposite);
266                 translateInlineAction = new TranslateInlineAction(panel, nodeMap,toolComposite);
267                 translateFreeVariableLengthAction = new TranslateFreeVariableLengthAction(panel, getRootNode(), toolComposite);
268                 rotateAction = new RotateAction(panel,nodeMap,toolComposite);
269                 removeAction = new RemoveAction(nodeMap);
270                 removeSplitAction = new RemoveAndSplitAction(nodeMap);
271                 routePipeAction = new RoutePipeAction(panel,rootNode, toolComposite);
272                 addComponentAction = new AddComponentAction(panel, rootNode, getLibraryUri());
273                 reversePipeRunAction = new ReversePipeRunAction(nodeMap);
274         }
275         
276         public void populate() {
277                 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
278                         
279                         @Override
280                         public void run() {
281                                 nodeMap.populate();
282                                 panel.addListener(new RenderListener() {
283                     
284                     @Override
285                     public void preRender() {
286                    
287                     }
288                     
289                     @Override
290                     public void postRender() {
291                         try {
292                             P3DUtil.finalizeDBLoad2(rootNode);
293                             if (nodeMap.isRangeModified());
294                                 nodeMap.commit("Load sync");
295                         } catch (Exception e) {
296                             //throw new DatabaseException(e);
297                         }
298                         panel.removeListener(this);  
299                         
300                     }
301                 });
302                                 
303                         }
304                 });
305                 
306         }
307         
308         protected IMappingSchema<Resource, INode> getSchema(ReadGraph graph) throws DatabaseException {
309                 IMappingSchema<Resource,INode> schema = SchemaBuilder.getSchema(graph);
310                 return schema;
311         }
312         
313         protected P3DNodeMap createNodeMap(Session session, IMapping<Resource, INode> mapping, VtkView panel, P3DRootNode rootNode) {
314                 return new P3DNodeMap(session, mapping, panel,rootNode);
315         }
316         
317         @Override
318         public void setFocus() {
319                 //component.setFocus();
320                 panel.getComponent().setFocus();
321         }
322         
323         private void createScene() {
324                 vtkRenderer ren1 = panel.getRenderer();
325                 
326                 boolean multiPass = false;
327                 boolean blur = false;
328                 boolean ssaa = false;
329                 //boolean sobel = true;
330                 boolean mblur = false;
331                 if (multiPass) {
332                         
333                         vtkLightsPass lightsPass = new vtkLightsPass();
334                         vtkDefaultPass defaultPass = new vtkDefaultPass();
335                         
336                         
337                         vtkRenderPassCollection passes = new vtkRenderPassCollection();
338                         passes.AddItem(lightsPass);
339                         passes.AddItem(defaultPass);
340                         
341                         vtkSequencePass seq = new vtkSequencePass();
342                         seq.SetPasses(passes);
343                         
344                         vtkCameraPass cameraPass = new vtkCameraPass();
345                         cameraPass.SetDelegatePass(seq);
346                         
347                         if (blur) {
348                             vtkGaussianBlurPass blurPass = new vtkGaussianBlurPass();
349                             blurPass.SetDelegatePass(cameraPass);
350                             ren1.SetPass(blurPass);
351                         } else if (ssaa) {
352                             vtkSSAAPass ssaaPass = new vtkSSAAPass();
353                             ssaaPass.SetDelegatePass(cameraPass);
354                             ren1.SetPass(ssaaPass);
355                         } else if (mblur) {
356                             vtkSimpleMotionBlurPass mBlurPass = new vtkSimpleMotionBlurPass();
357                             mBlurPass.SetDelegatePass(cameraPass);
358                 ren1.SetPass(mBlurPass);
359 //                      } else if (sobel) {
360 //                          vtkSobelGradientMagnitudePass sobelPass = new vtkSobelGradientMagnitudePass();
361 //                          sobelPass.SetDelegatePass(sobelPass);
362 //                          ren1.SetPass(sobelPass);
363                 } else {
364                     ren1.SetPass(cameraPass);
365                 }
366                          
367                 }
368 //              ren1.GetRenderWindow().LineSmoothingOn();
369 //              ren1.GetRenderWindow().PointSmoothingOn();
370 //              ren1.GetRenderWindow().PolygonSmoothingOn();
371 //              ren1.GetRenderWindow().SetMultiSamples(2);
372
373                 
374
375                 ren1.SetBackground2(1,1,1); // background color white
376                 ren1.SetBackground(0.9,0.9,0.9);
377                 ren1.SetGradientBackground(true);
378
379                 // vtkActor grid = vtkShape.createGridActor(8,1.0,1|2|4);
380                 vtkActor grid = vtkShape.createGridActor(8,1.0, 2 );
381                 grid.SetPickable(0);
382                 ren1.AddActor(grid);
383                 panel.addDeletable(grid);
384                 
385                 AxesDisplay axesDisplay = new AxesDisplay(panel);
386                 axesDisplay.show();
387                 
388         }
389         
390         protected Menu contextMenu;
391         
392         protected void hookContextMenu() {
393                 MenuManager menuMgr = new MenuManager("#PopupMenu");
394                 menuMgr.setRemoveAllWhenShown(true);
395                 menuMgr.addMenuListener(new IMenuListener() {
396                         public void menuAboutToShow(IMenuManager manager) {             
397                                 createContextMenu(manager);
398                         }
399                 });
400
401                 contextMenu = menuMgr.createContextMenu(parent);
402         }
403         
404         protected void createContextMenu(IMenuManager m) {
405                 List<INode> selected = selectionProvider.getSelectedNodes();
406                 List<VTKSelectionItem<Resource>> selectedItems = selectionProvider.getSelectedItems();
407                 if (selectedItems.size() > 0) {
408                     List<vtkProp> props = new ArrayList<>();
409                     for (VTKSelectionItem<Resource> i : selectedItems) {
410                         vtkProp p = (vtkProp)i.getAdapter(vtkProp.class);
411                         if (p != null)
412                             props.add(p);
413                     }
414                     if (props.size() > 0) {
415                         focusAction.setProps(props);
416                         m.add(focusAction);
417                     }
418                 }
419                 try {
420                         if (selected.size() == 0) {
421                             m.add(new AddEquipmentAction(rootNode, getLibraryUri()));
422 //                              for (Item eq : P3DUtil.getEquipments(getLibraryUri())) {
423 //                                      m.add(new AddEquipmentAction(rootNode, eq));
424 //                              }
425                         } else if (selected.size() == 1) {
426                                 IP3DNode node = (IP3DNode)selected.get(0);
427                                 if (node instanceof Equipment) {
428                                         m.add(translateAction);
429                                         m.add(rotateAction);
430                                         for (Item eq : P3DUtil.getNozzles(getLibraryUri())) {
431                                                 AddNozzleAction add = new AddNozzleAction(rootNode, eq);
432                                                 add.setEquipment((Equipment)node);
433                                                 m.add(add);
434                                         }
435                                         m.add(removeAction);
436                                 } else if (node instanceof Nozzle) {
437                                         Nozzle nozzle = (Nozzle)node;
438                                         if (!nozzle.isFixed()) {
439                                                 m.add(translateAction);
440                                                 m.add(rotateAction);
441                                         }
442                                         m.add(routePipeAction);
443                                         routePipeAction.setComponent(nozzle);
444                                         routePipeAction.setEnabled(nozzle.getNext() == null && nozzle.getPrevious() == null);
445                                         m.add(addComponentAction);
446                                         addComponentAction.setComponent(nozzle);
447                                         m.add(removeAction);
448                                 } else if (node instanceof TurnComponent) {
449                                         m.add(translateAction);
450                                         TurnComponent component = (TurnComponent)node;
451                                         m.add(routePipeAction);
452                                         routePipeAction.setComponent(component);
453                                         routePipeAction.setEnabled(component.getNext() == null || component.getPrevious() == null);
454                                         m.add(addComponentAction);
455                                         addComponentAction.setComponent(component);
456                                         m.add(removeAction);
457                                         m.add(removeSplitAction);
458                                         removeSplitAction.setNode(node);
459                                 } else if (node instanceof EndComponent) {
460                                         m.add(translateAction);
461                                         m.add(addComponentAction);
462                                         addComponentAction.setComponent((PipelineComponent)node);
463                                         m.add(removeAction);
464                                 } else if (node instanceof InlineComponent) {
465                                         //m.add(translateInlineAction);
466                                         InlineComponent component = (InlineComponent)node;
467                                         if (component.isVariableLength())
468                                             m.add(translateFreeVariableLengthAction);
469                                         else
470                                             m.add(translateInlineAction);
471                                         m.add(routePipeAction);
472                                         routePipeAction.setComponent(component);
473                                         m.add(addComponentAction);
474                                         addComponentAction.setComponent(component);
475                                         m.add(removeAction);
476                                         m.add(removeSplitAction);
477                                         removeSplitAction.setNode(node);
478                                 } else if (node instanceof PipeRun) {
479                                     m.add(reversePipeRunAction);
480                                     m.add(removeAction);
481                                     reversePipeRunAction.setNode(node);
482                                 } else {
483                                     m.add(removeAction);
484                                 }
485                                 
486                                 
487                                 translateAction.setNode(node);
488                                 translateInlineAction.setNode(node);
489                                 translateFreeVariableLengthAction.setNode(node);
490                                 rotateAction.setNode(node);
491                                 removeAction.setNode(node);
492                                 
493                         } 
494                 } catch (DatabaseException e) {
495                         ExceptionUtils.logAndShowError(e);
496                 }
497         }
498         
499         private IContentOutlinePage createOutline() {
500                 if (rootNode == null || selectionProvider == null)
501                         return null;
502                 //IContentOutlinePage outlinePage = new VTKContentOutlinePage<Resource,Object>(rootNode, selectionProvider);
503                 IContentOutlinePage outlinePage = new P3DContentOutlinePage(rootNode, selectionProvider) {
504                         protected void createContextMenu(IMenuManager manager) {
505                                 Plant3DEditor.this.createContextMenu(manager);
506                         };
507                 };
508                 outlinePage.addSelectionChangedListener(new ISelectionChangedListener() {
509                         
510                         @Override
511                         public void selectionChanged(SelectionChangedEvent event) {
512                                 selectionProvider.selectionChanged(event);
513                         }
514                 });
515                 return outlinePage;
516         }
517
518         @Override
519         public <T> T getAdapter(Class<T> adapter) {
520                 if (adapter.isAssignableFrom(IPropertyPage.class))
521                         return adapter.cast(new StandardPropertyPage(getSite(),getPropertyContexts()));
522                 if (adapter.isAssignableFrom(ISelectionProvider.class))
523                         return adapter.cast(selectionProvider);
524                 if (adapter.isAssignableFrom(IContentOutlinePage.class)) {
525                         return adapter.cast(createOutline());
526                 }
527                 if (adapter.isAssignableFrom(NodeMap.class)) {
528                         return adapter.cast(nodeMap);
529                 }
530                 if (adapter.isAssignableFrom(INode.class)) {
531                         return adapter.cast(rootNode);
532                 }
533                 if (adapter.isAssignableFrom(IMapping.class)) {
534                         return adapter.cast(mapping);
535                 }
536 //              if (adapter.isAssignableFrom(InteractiveVtkPanel.class)) {
537 //                      return adapter.cast(panel);
538 //              }
539                 if (adapter.isAssignableFrom(VtkView.class))
540                         return adapter.cast(panel);
541                 return super.getAdapter(adapter);
542         }
543         
544         public Set<String> getPropertyContexts() {
545                 Set<String> result = new HashSet<String>();
546                 result.add("http://www.simantics.org/Project-1.0/ProjectBrowseContext");
547                 return result;
548         }
549         
550         public InteractiveVtkComposite getPanel() {
551         return panel;
552     }
553         
554         public P3DRootNode getRootNode() {
555                 return rootNode;
556         }
557         
558         public IMapping<Resource, INode> getMapping() {
559                 return mapping;
560         }
561         
562         public P3DNodeMap getNodeMap() {
563         return nodeMap;
564     }
565 }