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