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