]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.processeditor/src/org/simantics/processeditor/views/ProcessEditor.java
a53463e470dd3cbf20671410f6209f1a1adc47c3
[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         /*\r
114         if (part instanceof ProcessEditor) {\r
115                 ProcessEditor sender = (ProcessEditor)part;\r
116             if (!sender.getPlantResource().equals(plantResource)) {\r
117                 selectionAdapter.setCurrentSelection(new StructuredResourceSelection());\r
118                 selectionAdapter.setEditorSelection();\r
119               return;\r
120             }\r
121         } \r
122                 */\r
123         \r
124         selectionAdapter.setEditorSelection();\r
125         \r
126     }\r
127 \r
128     @Override\r
129     protected void reloadFrom(IEntity thing) {\r
130         if (plantResource != null) {\r
131             throw new UnsupportedOperationException("Reloading instantiated viewer not supported");\r
132         }\r
133         if (thing.isInstanceOf(ProcessResource.plant3Dresource.Plant)) {\r
134             plantResource = thing.getResource();\r
135             G3DNode plant = new G3DNode(thing);\r
136             adapter.setRootNode(plant);\r
137             //adapter.addOutbound(plant);\r
138             ControlPointTools.reloadCache(thing.getGraph(),plant.getResource());\r
139         } else {\r
140             throw new IllegalArgumentException("Resource is not a plant");\r
141         } \r
142     }\r
143 \r
144     public Resource getPlantResource() {\r
145         return plantResource;\r
146     }\r
147     \r
148     public Plant getPlant(Graph g) {\r
149         return new Plant(g, plantResource);\r
150     }\r
151     \r
152     @Override\r
153     protected SelectionAdapter createSelectionAdapter() {\r
154         return new ProcessEditorSelectionAdapter(adapter);\r
155     }\r
156 \r
157     @Override\r
158     protected void hookDragAndDrop() {\r
159         super.hookDragAndDrop();\r
160         dropTarget.addDropListener(new InsertEquipmentAction(this));\r
161         dropTarget.addDropListener(new InsertNozzleAction(this));\r
162         dropTarget.addDropListener(new InsertComponentAction(this));\r
163         dropTarget.addDropListener(new RoutePipeAction(this));\r
164     }\r
165     \r
166     @Override\r
167         public Object getAdapter(Class adapter) {\r
168                 if (adapter == IContentOutlinePage.class) {\r
169                         if (getPlantResource() == null)\r
170                                 return null;\r
171                         final PlantStructureOutlinePage page = new PlantStructureOutlinePage(sessionContext,getPlantResource());\r
172                         \r
173                         getSelectionAdapter().addSelectionChangedListener(new ISelectionChangedListener() {\r
174                                 @Override\r
175                                 public void selectionChanged(SelectionChangedEvent event) {\r
176                                         page.setSelection(event.getSelection());\r
177                                         \r
178                                 }\r
179                         });\r
180                         parent.getDisplay().asyncExec(new Runnable() {\r
181                                 @Override\r
182                                 public void run() {\r
183                                         page.addSelectionChangedListener(new ISelectionChangedListener() {\r
184                                                 @Override\r
185                                                 public void selectionChanged(SelectionChangedEvent event) {\r
186                                                         selectionAdapter.setSelection(SelectionAdapter.transformSelection(event.getSelection()));\r
187                                                 }\r
188                                         });\r
189                                 }\r
190                         });\r
191 \r
192                         return page;\r
193                 }\r
194                 return null;\r
195         }\r
196 }