]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.processeditor/src/org/simantics/processeditor/views/ProcessEditor.java
699bd1e345fbed861786e9af95bfb8a069bbb3ec
[simantics/3d.git] / org.simantics.proconf.processeditor / src / org / simantics / processeditor / views / ProcessEditor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.processeditor.views;\r
12 \r
13 import org.eclipse.jface.viewers.ISelection;\r
14 import org.eclipse.jface.viewers.ISelectionChangedListener;\r
15 import org.eclipse.jface.viewers.SelectionChangedEvent;\r
16 import org.eclipse.swt.widgets.Composite;\r
17 import org.eclipse.ui.IWorkbenchPart;\r
18 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;\r
19 import org.simantics.db.Graph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.management.ISessionContext;\r
22 import org.simantics.layer0.utils.IEntity;\r
23 import org.simantics.processeditor.ProcessResource;\r
24 import org.simantics.processeditor.actions.ConfigureFloorAction;\r
25 import org.simantics.processeditor.actions.InsertComponentAction;\r
26 import org.simantics.processeditor.actions.InsertEquipmentAction;\r
27 import org.simantics.processeditor.actions.InsertNozzleAction;\r
28 import org.simantics.processeditor.actions.RoutePipeAction;\r
29 import org.simantics.processeditor.adapters.ProcessEditorAdapter;\r
30 import org.simantics.processeditor.adapters.ProcessEditorSelectionAdapter;\r
31 import org.simantics.processeditor.common.ControlPointTools;\r
32 import org.simantics.processeditor.stubs.Plant;\r
33 import org.simantics.processeditor.tools.PlantEditContribution;\r
34 import org.simantics.processeditor.tools.PlantVisualizationContribution;\r
35 import org.simantics.proconf.g3d.base.JmeRenderingComponent;\r
36 import org.simantics.proconf.g3d.base.ScenegraphAdapter;\r
37 import org.simantics.proconf.g3d.base.SelectionAdapter;\r
38 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
39 import org.simantics.proconf.g3d.common.StructuredResourceSelection;\r
40 import org.simantics.proconf.g3d.shapes.FloorShape;\r
41 import org.simantics.proconf.g3d.stubs.G3DNode;\r
42 import org.simantics.utils.ui.jface.MenuTools;\r
43 \r
44 import com.jme.math.Vector3f;\r
45 import com.jme.scene.Geometry;\r
46 \r
47 \r
48 public class ProcessEditor extends ThreeDimensionalEditorBase {\r
49     \r
50     private Resource plantResource = null;\r
51     \r
52     private ConfigureFloorAction configureFloorAction = null;\r
53 \r
54     private Geometry floorShape = null;\r
55 \r
56     public ProcessEditor(ISessionContext session) {\r
57         super(session);\r
58         addEditorContribution(new PlantEditContribution(this));\r
59         addEditorContribution(new PlantVisualizationContribution(this));\r
60     }\r
61     \r
62     public ProcessEditor(ISessionContext session,JmeRenderingComponent component) {\r
63         super(session,component);\r
64         addEditorContribution(new PlantEditContribution(this));\r
65         addEditorContribution(new PlantVisualizationContribution(this));\r
66     }\r
67     \r
68     @Override\r
69     protected ScenegraphAdapter createScenegraphAdapter() {\r
70         return new ProcessEditorAdapter(this,session,getRenderingComponent());\r
71     }\r
72     \r
73     @Override\r
74     public void createControl(Graph graph,Composite parent) {\r
75         super.createControl(graph,parent);\r
76 \r
77         floorShape = FloorShape.getShape(getRenderingComponent().getDisplaySystem().getRenderer(), 100.f,0.2f);\r
78         getRenderingComponent().getNoCastRoot().attachChild(floorShape);\r
79         floorShape.setLocalTranslation(new Vector3f(0.f,-0.01f,0.f));\r
80         configureFloorAction.setFloorShape(floorShape);\r
81     }\r
82 \r
83     @Override\r
84     protected void makeActions(Graph graph) {\r
85         super.makeActions(graph);\r
86 \r
87         //actions.add(new ShowTrendsAction(this));\r
88 \r
89         configureFloorAction = new ConfigureFloorAction(this);\r
90 \r
91 //        ContextActionFactory extended[] = ContextActionRegistry.getActions("fi.vtt.proconf.shapeeditor.processeditorview");\r
92 //        for (ContextActionFactory c : extended) {\r
93 //              actions.add(c.createAction(this));\r
94 //        }\r
95     }\r
96     \r
97     protected void fillLocalPullDown() {\r
98         super.fillLocalPullDown();\r
99         MenuTools.getOrCreate(getMenuID(),"Advanced", menuManager).add(configureFloorAction);\r
100     }\r
101     \r
102 \r
103     @Override\r
104     protected void pageSelectionChanged(IWorkbenchPart part, ISelection selection) {\r
105         if (!(selection instanceof StructuredResourceSelection)) {\r
106             return;\r
107         }\r
108         \r
109         StructuredResourceSelection s = (StructuredResourceSelection) selection;\r
110         selectionAdapter.setCurrentSelection(s);\r
111         viewChanged = true;\r
112         \r
113         //if (s.getRootSelection() == null) {\r
114         if (!(part instanceof ProcessEditor)) {\r
115             //System.out.println("ShapeEditorView.pageSelectionChanged() no root selection");\r
116                 ((ProcessEditorSelectionAdapter)selectionAdapter).setEditorSelection(true);\r
117             return;\r
118         } \r
119         //if (!s.getRootSelection().getResource().getId().equals(plant.getResource().getId())) {\r
120         ProcessEditor sender = (ProcessEditor)part;\r
121         if (!sender.getPlantResource().equals(plantResource)) {\r
122 //          System.out.println("ShapeEditorView.pageSelectionChanged() not right group "\r
123 //                  + s.getRootSelection().getResource().getId() + " != " + model.getResource().getId());\r
124                 selectionAdapter.setCurrentSelection(new StructuredResourceSelection());\r
125                 ((ProcessEditorSelectionAdapter)selectionAdapter).setEditorSelection(false);\r
126           return;\r
127       }\r
128         selectionAdapter.setEditorSelection();\r
129         \r
130     }\r
131 \r
132     @Override\r
133     protected void reloadFrom(IEntity thing) {\r
134         if (plantResource != null) {\r
135             throw new UnsupportedOperationException("Reloading instantiated viewer not supported");\r
136         }\r
137         if (thing.isInstanceOf(ProcessResource.plant3Dresource.Plant)) {\r
138             plantResource = thing.getResource();\r
139             G3DNode plant = new G3DNode(thing);\r
140             adapter.setRootNode(plant);\r
141             //adapter.addOutbound(plant);\r
142             ControlPointTools.reloadCache(thing.getGraph(),plant.getResource());\r
143         } else {\r
144             throw new IllegalArgumentException("Resource is not a plant");\r
145         } \r
146     }\r
147 \r
148     public Resource getPlantResource() {\r
149         return plantResource;\r
150     }\r
151     \r
152     public Plant getPlant(Graph g) {\r
153         return new Plant(g, plantResource);\r
154     }\r
155     \r
156     @Override\r
157     protected SelectionAdapter createSelectionAdapter() {\r
158         return new ProcessEditorSelectionAdapter(adapter);\r
159     }\r
160 \r
161     @Override\r
162     protected void hookDragAndDrop() {\r
163         super.hookDragAndDrop();\r
164         dropTarget.addDropListener(new InsertEquipmentAction(this));\r
165         dropTarget.addDropListener(new InsertNozzleAction(this));\r
166         dropTarget.addDropListener(new InsertComponentAction(this));\r
167         dropTarget.addDropListener(new RoutePipeAction(this));\r
168     }\r
169     \r
170     @Override\r
171         public Object getAdapter(Class adapter) {\r
172                 if (adapter == IContentOutlinePage.class) {\r
173                         if (getPlantResource() == null)\r
174                                 return null;\r
175                         final PlantStructureOutlinePage page = new PlantStructureOutlinePage(sessionContext,getPlantResource());\r
176                         \r
177                         getSelectionAdapter().addSelectionChangedListener(new ISelectionChangedListener() {\r
178                                 @Override\r
179                                 public void selectionChanged(SelectionChangedEvent event) {\r
180                                         page.setSelection(event.getSelection());\r
181                                         \r
182                                 }\r
183                         });\r
184                         parent.getDisplay().asyncExec(new Runnable() {\r
185                                 @Override\r
186                                 public void run() {\r
187                                         page.addSelectionChangedListener(new ISelectionChangedListener() {\r
188                                                 @Override\r
189                                                 public void selectionChanged(SelectionChangedEvent event) {\r
190                                                         selectionAdapter.setSelection(SelectionAdapter.transformSelection(event.getSelection()));\r
191                                                 }\r
192                                         });\r
193                                 }\r
194                         });\r
195 \r
196                         return page;\r
197                 }\r
198                 return null;\r
199         }\r
200 }