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
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
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
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
34 * Only useful with E4ResourceEditorBase extending part implementations.
\r
36 * @author Tuukka Lehtonen
\r
38 public class E4ResourceEditorSupport {
\r
40 private E4ResourceEditorBase editor;
\r
42 private ParametrizedRead<IResourceEditorInput, Boolean> inputValidator;
\r
44 private InputListener inputListener;
\r
46 public E4ResourceEditorSupport(E4ResourceEditorBase editor) {
\r
50 public E4ResourceEditorSupport(E4ResourceEditorBase editor, ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) {
\r
51 this.editor = editor;
\r
52 this.inputValidator = inputValidator;
\r
55 public void dispose() {
\r
56 deactivateValidation();
\r
57 inputValidator = null;
\r
61 protected boolean isDisposed() {
\r
62 return editor == null;
\r
65 public synchronized void activateValidation() {
\r
67 throw new IllegalStateException(this + " is disposed");
\r
68 if (inputListener != null)
\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
76 public synchronized void deactivateValidation() {
\r
78 throw new IllegalStateException(this + " is disposed");
\r
79 if (inputListener == null)
\r
81 inputListener.dispose();
\r
84 static enum InputState {
\r
89 public static InputState parse(boolean exists, boolean valid) {
\r
91 return NON_EXISTENT;
\r
92 return valid ? VALID : INVALID;
\r
96 static class Evaluation extends Tuple {
\r
97 public Evaluation(String editorElementId, InputState state) {
\r
98 super(editorElementId, state);
\r
101 public String getEditorElementId() {
\r
102 return (String) getField(0);
\r
105 public InputState getInputState() {
\r
106 return (InputState) getField(1);
\r
110 public static class InputEvaluator extends TernaryRead<String, IResourceEditorInput, ParametrizedRead<IResourceEditorInput, Boolean>, Evaluation> {
\r
112 public InputEvaluator(String editorElementId, IResourceEditorInput input, ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) {
\r
113 super(editorElementId, input, inputValidator);
\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
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
131 private static class InputListener extends ListenerAdapter<Evaluation> {
\r
133 private boolean disposed = false;
\r
135 public void dispose() {
\r
140 public void execute(Evaluation evaluation) {
\r
141 //System.out.println("InputListener: " + evaluation);
\r
142 switch (evaluation.getInputState()) {
\r
148 scheduleEditorClose(evaluation.getEditorElementId());
\r
154 public void exception(Throwable t) {
\r
155 ExceptionUtils.logError("E4ResourceEditorSupport.InputListener received an unexpected exception.", t);
\r
159 public boolean isDisposed() {
\r
164 private static void scheduleEditorClose(String editorElementId) {
\r
165 SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> {
\r
166 E4WorkbenchUtils.closeEditor(editorElementId);
\r