]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/e4/E4ResourceEditorBase.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / e4 / E4ResourceEditorBase.java
1 package org.simantics.ui.workbench.e4;
2
3 import java.util.Map;
4
5 import javax.annotation.PostConstruct;
6 import javax.annotation.PreDestroy;
7 import javax.inject.Inject;
8
9 import org.eclipse.e4.core.contexts.IEclipseContext;
10 import org.eclipse.e4.ui.di.Focus;
11 import org.eclipse.e4.ui.di.Persist;
12 import org.eclipse.e4.ui.di.PersistState;
13 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
14 import org.eclipse.e4.ui.workbench.modeling.EPartService;
15 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
16 import org.eclipse.swt.widgets.Composite;
17 import org.simantics.Simantics;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.request.ParametrizedRead;
21 import org.simantics.db.common.request.ReadRequest;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.request.PossibleModel;
24 import org.simantics.db.layer0.variable.RVI;
25 import org.simantics.db.layer0.variable.Variable;
26 import org.simantics.db.layer0.variable.Variables;
27 import org.simantics.db.service.SerialisationSupport;
28 import org.simantics.ui.workbench.IResourceEditorInput;
29
30 public abstract class E4ResourceEditorBase {
31
32     @Inject
33     private ESelectionService selectionService;
34
35     @Inject
36     private IEclipseContext eclipseContext;
37
38     private MPart part;
39     private boolean disposed;
40
41     private Resource resource;
42     private Variable variable;
43     private String rvi;
44     private Resource model;
45     
46     private IResourceEditorInput input;
47     private E4ResourceEditorSupport support;
48
49     @Inject
50     public final void init(MPart mPart) throws NumberFormatException, DatabaseException {
51         this.part = mPart;
52         readTransientState();
53         if (resource == null && model == null && rvi == null && variable == null)
54             readPersistentState();
55
56         deriveState();
57         initImpl(part);
58
59         if (!part.getTags().contains(EPartService.REMOVE_ON_HIDE_TAG)) {
60             part.getTags().add(EPartService.REMOVE_ON_HIDE_TAG);
61         }
62     }
63
64     public void initImpl(MPart part) { }
65
66     private void deriveState() throws DatabaseException {
67         boolean deriveVariable = variable == null && model != null;
68         boolean deriveModelAndRVI = variable != null;
69         boolean deriveModel = resource != null && model == null;
70
71         if (deriveModel || deriveModelAndRVI || deriveVariable) {
72             Simantics.getSession().syncRequest(new ReadRequest() {
73                 
74                 @Override
75                 public void run(ReadGraph graph) throws DatabaseException {
76                     if (deriveVariable) {
77                         if (rvi != null) {
78                             Variable configuration = Variables.getConfigurationContext(graph, model);
79                             RVI rrvi = RVI.fromResourceFormat(graph, rvi);
80                             variable = rrvi.resolve(graph, configuration);
81                         }
82                         // Absolute URI
83                         else {
84                             variable = Variables.getVariable(graph, model);
85                         }
86                     }
87
88                     if (deriveModelAndRVI) {
89                         model = Variables.getPossibleModel(graph, variable);
90                         rvi = variable.getRVI(graph).toString();
91                     }
92
93                     if (deriveModel) {
94                         model = graph.syncRequest(new PossibleModel(resource));
95                     }
96                 }
97             });
98         }
99
100         Map<String, Object> transientData = part.getTransientData(); 
101         transientData.put(E4ResourceEditorConstants.KEY_RESOURCE, resource);
102         transientData.put(E4ResourceEditorConstants.KEY_MODEL, model);
103         transientData.put(E4ResourceEditorConstants.KEY_VARIABLE, variable);
104         transientData.put(E4ResourceEditorConstants.KEY_RVI, rvi);
105
106         if (resource != null) {
107             if (model != null) {
108                 input = new E4ResourceEditorInput2(resource, model, rvi);
109             } else {
110                 input = new E4ResourceEditorInput(resource);
111             }
112         }
113     }
114
115     protected void activateValidation() {
116         this.support = new E4ResourceEditorSupport(this, getInputValidator());
117         this.support.activateValidation();
118     }
119
120     protected void deactivateValidation() {
121         if (support != null) {
122             support.dispose();
123             support = null;
124         }
125     }
126
127     /**
128      * Override to define your own input resource editor input validator that
129      * the view uses by default in {@link #initializeSupport()}.
130      */
131     protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
132         return null;
133     }
134
135     private void readTransientState() throws DatabaseException {
136         Map<String, Object> transientData = part.getTransientData();
137         // Read input resource
138         Object inputResource = transientData.get(E4ResourceEditorConstants.KEY_RESOURCE);
139         if (inputResource != null && inputResource instanceof Resource)
140             resource = (Resource) inputResource;
141
142         // Read input RVI
143         Object inputRVI = transientData.get(E4ResourceEditorConstants.KEY_RVI);
144         if (inputRVI != null && inputRVI instanceof String)
145             rvi = (String) inputRVI;
146
147         Object inputModel = transientData.get(E4ResourceEditorConstants.KEY_MODEL);
148         if (inputModel != null && inputModel instanceof Resource)
149             model = (Resource) inputModel;
150
151         // Read input variable
152         Object inputVariable = transientData.get(E4ResourceEditorConstants.KEY_VARIABLE);
153         if (inputVariable != null && inputVariable instanceof Variable)
154             variable = (Variable) inputVariable;
155     }
156
157     private void readPersistentState() throws NumberFormatException, DatabaseException {
158         Map<String, String> persistState = part.getPersistedState();
159         SerialisationSupport support = Simantics.getSession().getService(SerialisationSupport.class);
160         String inputResource = persistState.get(E4ResourceEditorConstants.KEY_RESOURCE);
161         if (inputResource != null)
162             resource = support.getResource(Long.parseLong(inputResource));
163
164         // Read input RVI
165         String inputRVI = persistState.get(E4ResourceEditorConstants.KEY_RVI);
166         if (inputRVI != null)
167             rvi = inputRVI;
168
169         String inputModel = persistState.get(E4ResourceEditorConstants.KEY_MODEL);
170         if (inputModel != null)
171             model = support.getResource(Long.parseLong(inputModel));
172     }
173
174     @PostConstruct
175     public void createEditor(Composite parent) {
176         createPartControl(parent);
177     }
178
179     public abstract void createPartControl(Composite parent);
180
181     @PersistState
182     public void persistState() {
183         System.out.println("persistState");
184         Map<String, String> persistedState = part.getPersistedState();
185         if (resource != null)
186             persistedState.put(E4ResourceEditorConstants.KEY_RESOURCE, Long.toString(resource.getResourceId()));
187         if (model != null)
188             persistedState.put(E4ResourceEditorConstants.KEY_MODEL, Long.toString(model.getResourceId()));
189         if (rvi != null)
190             persistedState.put(E4ResourceEditorConstants.KEY_RVI, rvi);
191     }
192
193     @Persist
194     public void persist() {
195         System.out.println("persist");
196     }
197
198     @PreDestroy
199     public void preDestroy() {
200         System.out.println("dispose");
201         disposed = true;
202         dispose();
203         deactivateValidation();
204     }
205
206     public abstract void dispose();
207
208     public boolean isDisposed() {
209         return disposed;
210     }
211
212     public abstract void setFocus();
213
214     @Focus
215     public void focus() {
216         setFocus();
217     }
218
219     public MPart getPart() {
220         return part;
221     }
222
223     public Resource getInputResource() {
224         return resource;
225     }
226
227     public Variable getInputVariable() {
228         return variable;
229     }
230
231     public String getInputRVI() {
232         return rvi;
233     }
234
235     public Resource getInputModelResource() {
236         return model;
237     }
238
239     public Resource assertInputModelResource() {
240         if (model == null)
241             throw new NullPointerException("Input model resource not defined, input resource is " + resource);
242         return model;
243     }
244
245     public IResourceEditorInput getResourceInput() {
246         return input;
247     }
248
249     public ESelectionService getSelectionService() {
250         return selectionService;
251     }
252
253     public IEclipseContext getContext() {
254         return eclipseContext;
255     }
256
257 }