1 /*******************************************************************************
2 * Copyright (c) 2007, 2019 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 - gitlab #399
12 *******************************************************************************/
13 package org.simantics.modeling.ui.property;
15 import java.util.Arrays;
16 import java.util.Collections;
19 import org.eclipse.core.expressions.PropertyTester;
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.ui.IEditorPart;
22 import org.simantics.Simantics;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.RequestProcessor;
25 import org.simantics.db.Resource;
26 import org.simantics.db.common.request.UniqueRead;
27 import org.simantics.db.common.utils.RequestUtil;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.request.Read;
30 import org.simantics.diagram.stubs.DiagramResource;
31 import org.simantics.diagram.ui.DiagramModelHints;
32 import org.simantics.g2d.diagram.IDiagram;
33 import org.simantics.modeling.ModelingResources;
34 import org.simantics.modeling.ui.diagramEditor.DiagramViewer;
35 import org.simantics.ui.SimanticsUI;
38 * @author Tuukka Lehtonen
40 public class TypicalPropertyTester extends PropertyTester {
44 * Tests if the received object is considered deletable.
46 private static final String IS_TYPICAL_MASTER_EDITOR = "isMasterEditor";
49 * Tests if the received object is considered modifiable.
51 private static final String IS_TYPICAL_INSTANCE_EDITOR = "isInstanceEditor";
54 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
55 //System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
57 if (!(receiver instanceof IEditorPart))
60 if (IS_TYPICAL_MASTER_EDITOR.equals(property)) {
61 return isTypicalMasterEditor((IEditorPart) receiver);
62 } else if (IS_TYPICAL_INSTANCE_EDITOR.equals(property)) {
63 return isTypicalInstanceEditor((IEditorPart) receiver);
69 private static IDiagram getDiagram(IAdaptable editor) {
70 DiagramViewer viewer = editor.getAdapter(DiagramViewer.class);
71 return viewer != null ? viewer.getAdapter(IDiagram.class) : null;
74 private static Set<String> getDiagramMappedCompositeTypes(IDiagram diagram) {
75 Set<String> result = diagram != null ? diagram.getHint(DiagramModelHints.KEY_MAPPED_COMPOSITE_RESOURCE_TYPE_URIS) : null;
76 return result != null ? result : Collections.emptySet();
79 private static Set<String> getDiagramMappedCompositeTypes(IAdaptable editor) {
80 return getDiagramMappedCompositeTypes( getDiagram(editor) );
83 public static boolean isTypicalMasterEditor(IAdaptable editor) {
84 Set<String> types = getDiagramMappedCompositeTypes(editor);
85 return types.contains(ModelingResources.URIs.MasterTypicalCompositeType);
88 private static boolean hasDiagramSource(IDiagram diagram) {
89 return diagram.getHint(DiagramModelHints.KEY_HAS_DIAGRAM_SOURCE) != null;
92 public static boolean isTypicalInstanceEditor(IAdaptable editor) {
93 IDiagram diagram = getDiagram(editor);
96 Set<String> types = getDiagramMappedCompositeTypes(diagram);
97 return !types.contains(ModelingResources.URIs.MasterTypicalCompositeType)
98 && types.contains(ModelingResources.URIs.TypicalComposite)
99 && hasDiagramSource(diagram);
102 private static boolean timeoutingRead(RequestProcessor processor, Read<Boolean> read) throws DatabaseException, InterruptedException {
103 return RequestUtil.trySyncRequest(
104 Simantics.getSession(),
105 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
106 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
111 public static boolean isTypicalMasterEditor(RequestProcessor processor, Resource editorInputResource) throws DatabaseException, InterruptedException {
112 return timeoutingRead(processor, new UniqueRead<Boolean>() {
114 public Boolean perform(ReadGraph graph) throws DatabaseException {
115 return isTypicalMasterEditor(graph, editorInputResource);
120 public static boolean isTypicalInstanceEditor(RequestProcessor processor, Resource editorInputResource) throws DatabaseException, InterruptedException {
121 return timeoutingRead(processor, new UniqueRead<Boolean>() {
123 public Boolean perform(ReadGraph graph) throws DatabaseException {
124 return isTypicalInstanceEditor(graph, editorInputResource);
129 public static boolean isTypicalMasterEditor(ReadGraph graph, Resource editorInputResource) throws DatabaseException {
130 ModelingResources MOD = ModelingResources.getInstance(graph);
131 Resource composite = graph.getPossibleObject(editorInputResource, MOD.DiagramToComposite);
132 return composite != null
133 && graph.isInstanceOf(composite, MOD.MasterTypicalCompositeType);
136 public static boolean isTypicalInstanceEditor(ReadGraph graph, Resource editorInputResource) throws DatabaseException {
137 DiagramResource DIA = DiagramResource.getInstance(graph);
138 ModelingResources MOD = ModelingResources.getInstance(graph);
139 return graph.isInstanceOf(editorInputResource, DIA.Diagram)
140 && graph.hasStatement(editorInputResource, MOD.HasDiagramSource);