]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/TypicalPropertyTester.java
Rid TypicalPropertyTester of database read transactions
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / property / TypicalPropertyTester.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2019 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *     Semantum Oy - gitlab #399
12  *******************************************************************************/
13 package org.simantics.modeling.ui.property;
14
15 import java.util.Arrays;
16 import java.util.Collections;
17 import java.util.Set;
18
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;
36
37 /**
38  * @author Tuukka Lehtonen
39  */
40 public class TypicalPropertyTester extends PropertyTester {
41
42
43     /**
44      * Tests if the received object is considered deletable.
45      */
46     private static final String IS_TYPICAL_MASTER_EDITOR = "isMasterEditor";
47
48     /**
49      * Tests if the received object is considered modifiable.
50      */
51     private static final String IS_TYPICAL_INSTANCE_EDITOR = "isInstanceEditor";
52
53     @Override
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);
56
57         if (!(receiver instanceof IEditorPart))
58             return false;
59
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);
64         }
65
66         return false;
67     }
68
69     private static IDiagram getDiagram(IAdaptable editor) {
70         DiagramViewer viewer = editor.getAdapter(DiagramViewer.class);
71         return viewer != null ? viewer.getAdapter(IDiagram.class) : null;
72     }
73
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();
77     }
78
79     private static Set<String> getDiagramMappedCompositeTypes(IAdaptable editor) {
80         return getDiagramMappedCompositeTypes( getDiagram(editor) );
81     }
82
83     public static boolean isTypicalMasterEditor(IAdaptable editor) {
84         Set<String> types = getDiagramMappedCompositeTypes(editor);
85         return types.contains(ModelingResources.URIs.MasterTypicalCompositeType);
86     }
87
88     private static boolean hasDiagramSource(IDiagram diagram) {
89         return diagram.getHint(DiagramModelHints.KEY_HAS_DIAGRAM_SOURCE) != null;
90     }
91
92     public static boolean isTypicalInstanceEditor(IAdaptable editor) {
93         IDiagram diagram = getDiagram(editor);
94         if (diagram == null)
95             return false;
96         Set<String> types = getDiagramMappedCompositeTypes(diagram);
97         return !types.contains(ModelingResources.URIs.MasterTypicalCompositeType)
98                 && types.contains(ModelingResources.URIs.TypicalComposite)
99                 && hasDiagramSource(diagram);
100     }
101
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,
107                 false,
108                 read);
109     }
110
111     public static boolean isTypicalMasterEditor(RequestProcessor processor, Resource editorInputResource) throws DatabaseException, InterruptedException {
112         return timeoutingRead(processor, new UniqueRead<Boolean>() {
113             @Override
114             public Boolean perform(ReadGraph graph) throws DatabaseException {
115                 return isTypicalMasterEditor(graph, editorInputResource);
116             }
117         });
118     }
119
120     public static boolean isTypicalInstanceEditor(RequestProcessor processor, Resource editorInputResource) throws DatabaseException, InterruptedException {
121         return timeoutingRead(processor, new UniqueRead<Boolean>() {
122             @Override
123             public Boolean perform(ReadGraph graph) throws DatabaseException {
124                 return isTypicalInstanceEditor(graph, editorInputResource);
125             }
126         });
127     }
128
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);
134     }
135
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);
141     }
142
143 }