]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/e4/E4ResourceEditorSupport.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / e4 / E4ResourceEditorSupport.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *     Semantum Oy - issue #4384
12  *******************************************************************************/
13 package org.simantics.ui.workbench.e4;
14
15 import org.eclipse.ui.PlatformUI;
16 import org.simantics.Simantics;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.procedure.adapter.ListenerAdapter;
20 import org.simantics.db.common.request.ParametrizedRead;
21 import org.simantics.db.common.request.TernaryRead;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.ui.workbench.IResourceEditorInput;
24 import org.simantics.utils.datastructures.map.Tuple;
25 import org.simantics.utils.ui.ExceptionUtils;
26 import org.simantics.utils.ui.SWTUtils;
27
28 /**
29  * A helper class for adding input validation and reaction to input invalidation
30  * to an E4 (editor) part that uses Simantics database {@link Resource}s as its
31  * input.
32  * 
33  * <p>
34  * Only useful with E4ResourceEditorBase extending part implementations.
35  * 
36  * @author Tuukka Lehtonen
37  */
38 public class E4ResourceEditorSupport {
39
40     private E4ResourceEditorBase editor;
41
42     private ParametrizedRead<IResourceEditorInput, Boolean> inputValidator;
43
44     private InputListener inputListener;
45
46     public E4ResourceEditorSupport(E4ResourceEditorBase editor) {
47         this(editor, null);
48     }
49
50     public E4ResourceEditorSupport(E4ResourceEditorBase editor, ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) {
51         this.editor = editor;
52         this.inputValidator = inputValidator;
53     }
54
55     public void dispose() {
56         deactivateValidation();
57         inputValidator = null;
58         editor = null;
59     }
60
61     protected boolean isDisposed() {
62         return editor == null;
63     }
64
65     public synchronized void activateValidation() {
66         if (isDisposed())
67             throw new IllegalStateException(this + " is disposed");
68         if (inputListener != null)
69             return;
70
71         E4ResourceEditorBase e = editor;
72         inputListener = new InputListener();
73         Simantics.getSession().asyncRequest(new InputEvaluator(e.getPart().getElementId(), e.getResourceInput(), inputValidator), inputListener);
74     }
75
76     public synchronized void deactivateValidation() {
77         if (isDisposed())
78             throw new IllegalStateException(this + " is disposed");
79         if (inputListener == null)
80             return;
81         inputListener.dispose();
82     }
83
84     static enum InputState {
85         VALID,
86         INVALID,
87         NON_EXISTENT;
88
89         public static InputState parse(boolean exists, boolean valid) {
90             if (!exists)
91                 return NON_EXISTENT;
92             return valid ? VALID : INVALID;
93         }
94     }
95
96     static class Evaluation extends Tuple {
97         public Evaluation(String editorElementId, InputState state) {
98             super(editorElementId, state);
99         }
100
101         public String getEditorElementId() {
102             return (String) getField(0);
103         }
104
105         public InputState getInputState() {
106             return (InputState) getField(1);
107         }
108     }
109
110     public static class InputEvaluator extends TernaryRead<String, IResourceEditorInput, ParametrizedRead<IResourceEditorInput, Boolean>, Evaluation> {
111
112         public InputEvaluator(String editorElementId, IResourceEditorInput input, ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) {
113             super(editorElementId, input, inputValidator);
114         }
115
116         @Override
117         public Evaluation perform(ReadGraph graph) throws DatabaseException {
118             //System.out.println(this + ": checking input " + parameter + ", " + parameter2);
119             IResourceEditorInput i = parameter2;
120             boolean exists = i.exists(graph);
121             boolean valid = exists && parameter3 != null
122                     ? graph.syncRequest(parameter3.get(i))
123                     : exists;
124             InputState state = InputState.parse(exists, valid);
125             Evaluation eval = new Evaluation(parameter, state);
126             //System.out.println(this + ": validation evaluation: " + eval);
127             return eval;
128         }
129     }
130
131     private static class InputListener extends ListenerAdapter<Evaluation> {
132
133         private boolean disposed = false;
134
135         public void dispose() {
136             disposed = true;
137         }
138
139         @Override
140         public void execute(Evaluation evaluation) {
141             //System.out.println("InputListener: " + evaluation);
142             switch (evaluation.getInputState()) {
143                 case VALID:
144                     break;
145
146                 case INVALID:
147                 case NON_EXISTENT:
148                     scheduleEditorClose(evaluation.getEditorElementId());
149                     break;
150             }
151         }
152
153         @Override
154         public void exception(Throwable t) {
155             ExceptionUtils.logError("E4ResourceEditorSupport.InputListener received an unexpected exception.", t);
156         }
157
158         @Override
159         public boolean isDisposed() {
160             return disposed;
161         }
162     }
163
164     private static void scheduleEditorClose(String editorElementId) {
165         SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> {
166             E4WorkbenchUtils.closeEditor(editorElementId);
167         });
168     }
169
170 }