1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 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 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.property;
\r
14 import org.eclipse.core.expressions.PropertyTester;
\r
15 import org.eclipse.ui.IEditorInput;
\r
16 import org.eclipse.ui.IEditorPart;
\r
17 import org.simantics.DatabaseJob;
\r
18 import org.simantics.Simantics;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.RequestProcessor;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.Session;
\r
23 import org.simantics.db.common.request.UniqueRead;
\r
24 import org.simantics.db.common.utils.RequestUtil;
\r
25 import org.simantics.db.exception.DatabaseException;
\r
26 import org.simantics.diagram.stubs.DiagramResource;
\r
27 import org.simantics.modeling.ModelingResources;
\r
28 import org.simantics.ui.SimanticsUI;
\r
29 import org.simantics.ui.workbench.IResourceEditorInput;
\r
30 import org.simantics.utils.ui.ErrorLogger;
\r
33 * @author Tuukka Lehtonen
\r
35 public class TypicalPropertyTester extends PropertyTester {
\r
39 * Tests if the received object is considered deletable.
\r
41 private static final String IS_TYPICAL_MASTER_EDITOR = "isMasterEditor";
\r
44 * Tests if the received object is considered modifiable.
\r
46 private static final String IS_TYPICAL_INSTANCE_EDITOR = "isInstanceEditor";
\r
49 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
\r
50 //System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
\r
53 Session session = Simantics.peekSession();
\r
54 if (session == null)
\r
57 if (!(receiver instanceof IEditorPart))
\r
59 IEditorPart editor = (IEditorPart) receiver;
\r
60 IEditorInput in = editor.getEditorInput();
\r
61 if (!(in instanceof IResourceEditorInput))
\r
63 IResourceEditorInput input = (IResourceEditorInput) in;
\r
64 final Resource inputResource = input.getResource();
\r
66 if (DatabaseJob.inProgress()) {
\r
67 // See Apros issue #9115
\r
68 // Return true because it is often possible that the database
\r
69 // will be busy when these properties are tested. In such cases
\r
70 // the handlers/menu contributions using these tests would
\r
71 // become disabled unless we return true here. It is up to the
\r
72 // handlers to also make sure that their input is valid.
\r
76 if (IS_TYPICAL_MASTER_EDITOR.equals(property)) {
\r
77 return isTypicalMasterEditor(session, inputResource);
\r
78 } else if (IS_TYPICAL_INSTANCE_EDITOR.equals(property)) {
\r
79 return isTypicalInstanceEditor(session, inputResource);
\r
81 } catch (DatabaseException | InterruptedException e) {
\r
82 ErrorLogger.defaultLogError(e);
\r
88 public static boolean isTypicalMasterEditor(RequestProcessor processor, final Resource editorInputResource) throws DatabaseException, InterruptedException {
\r
89 return RequestUtil.trySyncRequest(
\r
90 Simantics.getSession(),
\r
91 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
\r
92 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
\r
94 new UniqueRead<Boolean>() {
\r
96 public Boolean perform(ReadGraph graph) throws DatabaseException {
\r
97 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
98 Resource composite = graph.getPossibleObject(editorInputResource, MOD.DiagramToComposite);
\r
99 return composite != null
\r
100 && graph.isInstanceOf(composite, MOD.MasterTypicalCompositeType);
\r
105 public static boolean isTypicalInstanceEditor(RequestProcessor processor, final Resource editorInputResource) throws DatabaseException, InterruptedException {
\r
106 return RequestUtil.trySyncRequest(
\r
107 Simantics.getSession(),
\r
108 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
\r
109 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
\r
111 new UniqueRead<Boolean>() {
\r
113 public Boolean perform(ReadGraph graph) throws DatabaseException {
\r
114 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
115 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
116 return graph.isInstanceOf(editorInputResource, DIA.Diagram)
\r
117 && graph.hasStatement(editorInputResource, MOD.HasDiagramSource);
\r