1 package org.simantics.plant3d.editor;
3 import java.util.HashSet;
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;
71 import vtk.vtkCameraPass;
72 import vtk.vtkDefaultPass;
73 import vtk.vtkLightsPass;
74 import vtk.vtkRenderPassCollection;
75 import vtk.vtkRenderer;
76 import vtk.vtkSequencePass;
79 public class Plant3DEditor extends ResourceEditorPart {
81 private Composite parent;
82 private Resource input;
83 // private InteractiveVtkPanel panel;
84 // private SWTAWTComponent component;
85 private InteractiveVtkComposite panel;
87 private P3DRootNode rootNode;
88 private IMapping<Resource,Object> mapping;
90 private NodeSelectionProvider2<Resource,Object> selectionProvider;
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;
100 private P3DNodeMap nodeMap;
103 public void createPartControl(Composite parent) {
104 this.parent = parent;
105 parent.setLayout (new FillLayout ());
106 // component = new SWTAWTComponent(parent,SWT.NONE) {
109 // protected Component createSwingComponent() {
110 // if (panel == null) {
111 // panel = new InteractiveVtkPanel();
112 // vtkPanelUtil.registerPanel(panel);
119 IResourceEditorInput rei = (IResourceEditorInput)getEditorInput();
120 input = rei.getResource();
122 panel = new InteractiveVtkComposite(parent);
125 //IActionBars actionBars = getEditorSite().getActionBars();
131 //component.syncPopulate();
133 new ContextMenuListener(panel, contextMenu);
135 cameraAction = new vtkCameraAndSelectorAction(panel);
136 panel.setDefaultAction(cameraAction);
137 panel.useDefaultAction();
138 panel.setPickType(4);
141 ControlPointFactory.preloadCache();
142 ComponentUtils.preloadCache();
143 } catch (Exception e) {
144 ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e);
149 getSession().syncRequest(new ReadRequest() {
151 @SuppressWarnings({ "rawtypes", "unchecked" })
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.
161 P3DUtil.finalizeDBLoad(rootNode);
163 } catch (Exception e) {
164 throw new DatabaseException(e);
166 nodeMap = createNodeMap(getSession(), mapping, panel,rootNode);
170 if (rootNode == null)
171 throw new RuntimeException("Scenegraph loading failed.");
174 selectionProvider = new NodeSelectionProvider2<Resource,Object>(this,mapping,nodeMap);
176 cameraAction.addSelectionChangedListener(selectionProvider);
178 cameraAction.addHoverChangedListener(new HoverHighlighter(panel,nodeMap));
179 selectionProvider.addSelectionChangedListener(new SelectionHighlighter(panel,nodeMap));
181 getSite().setSelectionProvider(selectionProvider);
182 getSite().getPage().addPostSelectionListener(selectionProvider);
184 //outlinePage = new ScenegraphOutlinePage(rootNode);
187 parent.addDisposeListener(new DisposeListener() {
190 public void widgetDisposed(DisposeEvent e) {
191 getSite().getPage().removePostSelectionListener(selectionProvider);
193 PipingRules.setEnabled(false);
195 PipingRules.setEnabled(true);
197 // component.dispose();
198 //panel.getComponent().dispose();
202 } catch (DatabaseException e1) {
203 ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e1);
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) {
217 routePipeAction = new RoutePipeAction(panel,rootNode);
218 addComponentAction = new AddComponentAction(panel, rootNode);
222 public void populate() {
223 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
233 protected IMappingSchema<Resource, Object> getSchema(ReadGraph graph) throws DatabaseException {
234 IMappingSchema<Resource,Object> schema = SchemaBuilder.getSchema(graph);
238 protected P3DNodeMap createNodeMap(Session session, IMapping<Resource, Object> mapping, VtkView panel, P3DRootNode rootNode) {
239 return new P3DNodeMap(session, mapping, panel,rootNode);
243 public void setFocus() {
244 //component.setFocus();
245 panel.getComponent().setFocus();
248 private void createScene() {
249 vtkRenderer ren1 = panel.getRenderer();
251 boolean multiPass = false;
254 vtkLightsPass lightsPass = new vtkLightsPass();
255 vtkDefaultPass defaultPass = new vtkDefaultPass();
258 vtkRenderPassCollection passes = new vtkRenderPassCollection();
259 passes.AddItem(lightsPass);
260 passes.AddItem(defaultPass);
262 vtkSequencePass seq = new vtkSequencePass();
263 seq.SetPasses(passes);
265 vtkCameraPass cameraPass = new vtkCameraPass();
266 cameraPass.SetDelegatePass(seq);
268 ren1.SetPass(cameraPass);
271 // ren1.GetRenderWindow().LineSmoothingOn();
272 // ren1.GetRenderWindow().PointSmoothingOn();
273 // ren1.GetRenderWindow().PolygonSmoothingOn();
274 // ren1.GetRenderWindow().SetMultiSamples(2);
278 ren1.SetBackground2(1,1,1); // background color white
279 ren1.SetBackground(0.9,0.9,0.9);
280 ren1.SetGradientBackground(true);
282 // vtkActor grid = vtkShape.createGridActor(8,1.0,1|2|4);
283 vtkActor grid = vtkShape.createGridActor(8,1.0, 2 );
286 panel.addDeletable(grid);
288 AxesDisplay axesDisplay = new AxesDisplay(panel);
293 protected Menu contextMenu;
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);
304 contextMenu = menuMgr.createContextMenu(parent);
307 protected void createContextMenu(IMenuManager m) {
308 List<IG3DNode> selected = selectionProvider.getSelectedNodes();
310 if (selected.size() == 0) {
311 for (Item eq : P3DUtil.getEquipments()) {
312 m.add(new AddEquipmentAction(rootNode, eq));
314 } else if (selected.size() == 1) {
315 IP3DNode node = (IP3DNode)selected.get(0);
316 if (node instanceof Equipment) {
317 m.add(translateAction);
319 for (Item eq : P3DUtil.getNozzles()) {
320 AddNozzleAction add = new AddNozzleAction(rootNode, eq);
321 add.setEquipment((Equipment)node);
324 } else if (node instanceof Nozzle) {
325 Nozzle nozzle = (Nozzle)node;
326 if (!nozzle.isFixed()) {
327 m.add(translateAction);
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);
358 translateAction.setNode(node);
359 translateInlineAction.setNode(node);
360 rotateAction.setNode(node);
361 removeAction.setNode(node);
364 } catch (DatabaseException e) {
365 ExceptionUtils.logAndShowError(e);
369 private IContentOutlinePage createOutline() {
370 if (rootNode == null || selectionProvider == 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);
378 outlinePage.addSelectionChangedListener(new ISelectionChangedListener() {
381 public void selectionChanged(SelectionChangedEvent event) {
382 selectionProvider.selectionChanged(event);
388 @SuppressWarnings("rawtypes")
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();
396 if (NodeMap.class.equals(adapter)) {
399 if (INode.class.equals(adapter)) {
402 if (IMapping.class.equals(adapter)) {
405 // if (InteractiveVtkPanel.class.equals(adapter)) {
408 if (VtkView.class.equals(adapter))
410 if (ISelectionProvider.class.equals(adapter))
411 return selectionProvider;
412 return super.getAdapter(adapter);
415 public Set<String> getPropertyContexts() {
416 Set<String> result = new HashSet<String>();
417 result.add("http://www.simantics.org/Project-1.0/ProjectBrowseContext");
421 public P3DRootNode getRootNode() {
425 public IMapping<Resource, Object> getMapping() {