1 package org.simantics.ui.workbench.e4;
5 import javax.annotation.PostConstruct;
6 import javax.annotation.PreDestroy;
7 import javax.inject.Inject;
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;
30 public abstract class E4ResourceEditorBase {
33 private ESelectionService selectionService;
36 private IEclipseContext eclipseContext;
39 private boolean disposed;
41 private Resource resource;
42 private Variable variable;
44 private Resource model;
46 private IResourceEditorInput input;
47 private E4ResourceEditorSupport support;
50 public final void init(MPart mPart) throws NumberFormatException, DatabaseException {
53 if (resource == null && model == null && rvi == null && variable == null)
54 readPersistentState();
59 if (!part.getTags().contains(EPartService.REMOVE_ON_HIDE_TAG)) {
60 part.getTags().add(EPartService.REMOVE_ON_HIDE_TAG);
64 public void initImpl(MPart part) { }
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;
71 if (deriveModel || deriveModelAndRVI || deriveVariable) {
72 Simantics.getSession().syncRequest(new ReadRequest() {
75 public void run(ReadGraph graph) throws DatabaseException {
78 Variable configuration = Variables.getConfigurationContext(graph, model);
79 RVI rrvi = RVI.fromResourceFormat(graph, rvi);
80 variable = rrvi.resolve(graph, configuration);
84 variable = Variables.getVariable(graph, model);
88 if (deriveModelAndRVI) {
89 model = Variables.getPossibleModel(graph, variable);
90 rvi = variable.getRVI(graph).toString();
94 model = graph.syncRequest(new PossibleModel(resource));
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);
106 if (resource != null) {
108 input = new E4ResourceEditorInput2(resource, model, rvi);
110 input = new E4ResourceEditorInput(resource);
115 protected void activateValidation() {
116 this.support = new E4ResourceEditorSupport(this, getInputValidator());
117 this.support.activateValidation();
120 protected void deactivateValidation() {
121 if (support != null) {
128 * Override to define your own input resource editor input validator that
129 * the view uses by default in {@link #initializeSupport()}.
131 protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
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;
143 Object inputRVI = transientData.get(E4ResourceEditorConstants.KEY_RVI);
144 if (inputRVI != null && inputRVI instanceof String)
145 rvi = (String) inputRVI;
147 Object inputModel = transientData.get(E4ResourceEditorConstants.KEY_MODEL);
148 if (inputModel != null && inputModel instanceof Resource)
149 model = (Resource) inputModel;
151 // Read input variable
152 Object inputVariable = transientData.get(E4ResourceEditorConstants.KEY_VARIABLE);
153 if (inputVariable != null && inputVariable instanceof Variable)
154 variable = (Variable) inputVariable;
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));
165 String inputRVI = persistState.get(E4ResourceEditorConstants.KEY_RVI);
166 if (inputRVI != null)
169 String inputModel = persistState.get(E4ResourceEditorConstants.KEY_MODEL);
170 if (inputModel != null)
171 model = support.getResource(Long.parseLong(inputModel));
175 public void createEditor(Composite parent) {
176 createPartControl(parent);
179 public abstract void createPartControl(Composite parent);
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()));
188 persistedState.put(E4ResourceEditorConstants.KEY_MODEL, Long.toString(model.getResourceId()));
190 persistedState.put(E4ResourceEditorConstants.KEY_RVI, rvi);
194 public void persist() {
195 System.out.println("persist");
199 public void preDestroy() {
200 System.out.println("dispose");
203 deactivateValidation();
206 public abstract void dispose();
208 public boolean isDisposed() {
212 public abstract void setFocus();
215 public void focus() {
219 public MPart getPart() {
223 public Resource getInputResource() {
227 public Variable getInputVariable() {
231 public String getInputRVI() {
235 public Resource getInputModelResource() {
239 public Resource assertInputModelResource() {
241 throw new NullPointerException("Input model resource not defined, input resource is " + resource);
245 public IResourceEditorInput getResourceInput() {
249 public ESelectionService getSelectionService() {
250 return selectionService;
253 public IEclipseContext getContext() {
254 return eclipseContext;