1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - issue #4384
12 *******************************************************************************/
13 package org.simantics.ui.workbench.e4;
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;
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
34 * Only useful with E4ResourceEditorBase extending part implementations.
36 * @author Tuukka Lehtonen
38 public class E4ResourceEditorSupport {
40 private E4ResourceEditorBase editor;
42 private ParametrizedRead<IResourceEditorInput, Boolean> inputValidator;
44 private InputListener inputListener;
46 public E4ResourceEditorSupport(E4ResourceEditorBase editor) {
50 public E4ResourceEditorSupport(E4ResourceEditorBase editor, ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) {
52 this.inputValidator = inputValidator;
55 public void dispose() {
56 deactivateValidation();
57 inputValidator = null;
61 protected boolean isDisposed() {
62 return editor == null;
65 public synchronized void activateValidation() {
67 throw new IllegalStateException(this + " is disposed");
68 if (inputListener != null)
71 E4ResourceEditorBase e = editor;
72 inputListener = new InputListener();
73 Simantics.getSession().asyncRequest(new InputEvaluator(e.getPart().getElementId(), e.getResourceInput(), inputValidator), inputListener);
76 public synchronized void deactivateValidation() {
78 throw new IllegalStateException(this + " is disposed");
79 if (inputListener == null)
81 inputListener.dispose();
84 static enum InputState {
89 public static InputState parse(boolean exists, boolean valid) {
92 return valid ? VALID : INVALID;
96 static class Evaluation extends Tuple {
97 public Evaluation(String editorElementId, InputState state) {
98 super(editorElementId, state);
101 public String getEditorElementId() {
102 return (String) getField(0);
105 public InputState getInputState() {
106 return (InputState) getField(1);
110 public static class InputEvaluator extends TernaryRead<String, IResourceEditorInput, ParametrizedRead<IResourceEditorInput, Boolean>, Evaluation> {
112 public InputEvaluator(String editorElementId, IResourceEditorInput input, ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) {
113 super(editorElementId, input, inputValidator);
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))
124 InputState state = InputState.parse(exists, valid);
125 Evaluation eval = new Evaluation(parameter, state);
126 //System.out.println(this + ": validation evaluation: " + eval);
131 private static class InputListener extends ListenerAdapter<Evaluation> {
133 private boolean disposed = false;
135 public void dispose() {
140 public void execute(Evaluation evaluation) {
141 //System.out.println("InputListener: " + evaluation);
142 switch (evaluation.getInputState()) {
148 scheduleEditorClose(evaluation.getEditorElementId());
154 public void exception(Throwable t) {
155 ExceptionUtils.logError("E4ResourceEditorSupport.InputListener received an unexpected exception.", t);
159 public boolean isDisposed() {
164 private static void scheduleEditorClose(String editorElementId) {
165 SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> {
166 E4WorkbenchUtils.closeEditor(editorElementId);