1 package org.simantics.plant3d.editor;
3 import java.awt.Component;
4 import java.util.HashSet;
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;
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;
83 public class Plant3DEditor extends ResourceEditorPart {
85 private Composite parent;
86 private Resource input;
87 private InteractiveVtkPanel panel;
88 private SWTAWTComponent component;
90 private P3DRootNode rootNode;
91 private IMapping<Resource,Object> mapping;
93 private NodeSelectionProvider2<Resource,Object> selectionProvider;
95 private vtkCameraAndSelectorAction cameraAction;
96 private TranslateAction translateAction;
97 private RotateAction rotateAction;
98 private RemoveAction removeAction;
99 private RoutePipeAction routePipeAction;
100 private AddComponentAction addComponentAction;
102 private P3DNodeMap nodeMap;
105 public void createPartControl(Composite parent) {
106 this.parent = parent;
107 parent.setLayout (new FillLayout ());
108 component = new SWTAWTComponent(parent,SWT.NONE) {
111 protected Component createSwingComponent() {
113 panel = new InteractiveVtkPanel();
114 vtkPanelUtil.registerPanel(panel);
121 IResourceEditorInput rei = (IResourceEditorInput)getEditorInput();
122 input = rei.getResource();
125 //IActionBars actionBars = getEditorSite().getActionBars();
129 component.syncPopulate();
131 panel.addMouseListener(new java.awt.event.MouseAdapter() {
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() {
137 contextMenu.setLocation(e.getXOnScreen(), e.getYOnScreen());
138 contextMenu.setVisible(true);
146 cameraAction = new vtkCameraAndSelectorAction(panel);
147 panel.setDefaultAction(cameraAction);
148 panel.useDefaultAction();
149 panel.setPickType(4);
152 ControlPointFactory.preloadCache();
153 ComponentUtils.preloadCache();
154 } catch (Exception e) {
155 ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e);
160 getSession().syncRequest(new ReadRequest() {
162 @SuppressWarnings({ "rawtypes", "unchecked" })
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.
172 for (INode node : rootNode.getChild()) {
173 if (node instanceof PipeRun) {
174 for (PipelineComponent pc : ((PipeRun) node).getChild())
176 } else if (node instanceof Equipment) {
177 for (PipelineComponent pc : ((Equipment) node).getChild())
182 for (INode node : rootNode.getChild()) {
183 if (node instanceof PipeRun) {
184 for (PipelineComponent pc : ((PipeRun) node).getChild())
186 } else if (node instanceof Equipment) {
187 for (PipelineComponent pc : ((Equipment) node).getChild())
191 for (INode node : rootNode.getChild()) {
192 if (node instanceof PipeRun) {
193 PipingRules.validate((PipeRun)node);
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);
205 } catch (Exception e) {
206 throw new DatabaseException(e);
208 nodeMap = createNodeMap(getSession(), mapping, panel,rootNode);
212 if (rootNode == null)
213 throw new RuntimeException("Scenegraph loading failed.");
216 selectionProvider = new NodeSelectionProvider2<Resource,Object>(this,mapping,nodeMap);
218 cameraAction.addSelectionChangedListener(selectionProvider);
220 cameraAction.addHoverChangedListener(new HoverHighlighter(panel,nodeMap));
221 selectionProvider.addSelectionChangedListener(new SelectionHighlighter(panel,nodeMap));
223 getSite().setSelectionProvider(selectionProvider);
224 getSite().getPage().addPostSelectionListener(selectionProvider);
226 //outlinePage = new ScenegraphOutlinePage(rootNode);
229 parent.addDisposeListener(new DisposeListener() {
232 public void widgetDisposed(DisposeEvent e) {
233 getSite().getPage().removePostSelectionListener(selectionProvider);
235 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
239 PipingRules.setEnabled(false);
241 PipingRules.setEnabled(true);
242 vtkPanelUtil.unregisterPanel(panel);
252 } catch (DatabaseException e1) {
253 ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e1);
257 translateAction = new TranslateAction(panel,nodeMap);
258 rotateAction = new RotateAction(panel,nodeMap);
259 removeAction = new RemoveAction(nodeMap) {
260 public void setNode(IG3DNode node) {
266 routePipeAction = new RoutePipeAction(panel,rootNode);
267 addComponentAction = new AddComponentAction(panel, rootNode);
271 public void populate() {
272 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
282 protected IMappingSchema<Resource, Object> getSchema(ReadGraph graph) throws DatabaseException {
283 IMappingSchema<Resource,Object> schema = SchemaBuilder.getSchema(graph);
287 protected P3DNodeMap createNodeMap(Session session, IMapping<Resource, Object> mapping, InteractiveVtkPanel panel, P3DRootNode rootNode) {
288 return new P3DNodeMap(session, mapping, panel,rootNode);
292 public void setFocus() {
293 component.setFocus();
296 private void createScene() {
297 vtkRenderer ren1 = panel.GetRenderer();
299 boolean multiPass = false;
302 vtkLightsPass lightsPass = new vtkLightsPass();
303 vtkDefaultPass defaultPass = new vtkDefaultPass();
306 vtkRenderPassCollection passes = new vtkRenderPassCollection();
307 passes.AddItem(lightsPass);
308 passes.AddItem(defaultPass);
310 vtkSequencePass seq = new vtkSequencePass();
311 seq.SetPasses(passes);
315 vtkCameraPass cameraPass = new vtkCameraPass();
316 cameraPass.SetDelegatePass(seq);
318 ren1.SetPass(cameraPass);
321 // ren1.GetRenderWindow().LineSmoothingOn();
322 // ren1.GetRenderWindow().PointSmoothingOn();
323 // ren1.GetRenderWindow().PolygonSmoothingOn();
324 // ren1.GetRenderWindow().SetMultiSamples(2);
328 ren1.SetBackground2(1,1,1); // background color white
329 ren1.SetBackground(0.9,0.9,0.9);
330 ren1.SetGradientBackground(true);
332 // vtkActor grid = vtkShape.createGridActor(8,1.0,1|2|4);
333 vtkActor grid = vtkShape.createGridActor(8,1.0, 2 );
336 panel.addDeletable(grid);
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();
369 protected Menu contextMenu;
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);
380 contextMenu = menuMgr.createContextMenu(parent);
383 protected void createContextMenu(IMenuManager m) {
384 List<IG3DNode> selected = selectionProvider.getSelectedNodes();
386 if (selected.size() == 0) {
387 for (Item eq : P3DUtil.getEquipments()) {
388 m.add(new AddEquipmentAction(rootNode, eq));
390 } else if (selected.size() == 1) {
391 IP3DNode node = (IP3DNode)selected.get(0);
392 if (node instanceof Equipment) {
393 m.add(translateAction);
395 for (Item eq : P3DUtil.getNozzles()) {
396 AddNozzleAction add = new AddNozzleAction(rootNode, eq);
397 add.setEquipment((Equipment)node);
400 } else if (node instanceof Nozzle) {
401 m.add(translateAction);
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);
431 translateAction.setNode(node);
432 rotateAction.setNode(node);
433 removeAction.setNode(node);
436 } catch (DatabaseException e) {
437 ExceptionUtils.logAndShowError(e);
441 private IContentOutlinePage createOutline() {
442 if (rootNode == null || selectionProvider == 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);
450 outlinePage.addSelectionChangedListener(new ISelectionChangedListener() {
453 public void selectionChanged(SelectionChangedEvent event) {
454 selectionProvider.selectionChanged(event);
460 @SuppressWarnings("rawtypes")
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();
468 if (NodeMap.class.equals(adapter)) {
471 if (INode.class.equals(adapter)) {
474 if (IMapping.class.equals(adapter)) {
477 if (InteractiveVtkPanel.class.equals(adapter)) {
480 if (ISelectionProvider.class.equals(adapter))
481 return selectionProvider;
482 return super.getAdapter(adapter);
485 public Set<String> getPropertyContexts() {
486 Set<String> result = new HashSet<String>();
487 result.add("http://www.simantics.org/Project-1.0/ProjectBrowseContext");
491 public P3DRootNode getRootNode() {
495 public IMapping<Resource, Object> getMapping() {