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