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