]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
index 679647a724de548f444ef596be59f4ddef17ed3f..b222d23e4038816e4b70d0dfb98a241c242cb72f 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2012 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2019 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -8,26 +8,31 @@
  *
  * Contributors:
  *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - gitlab #399
  *******************************************************************************/
 package org.simantics.modeling.ui.property;
 
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Set;
+
 import org.eclipse.core.expressions.PropertyTester;
-import org.eclipse.ui.IEditorInput;
+import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.ui.IEditorPart;
-import org.simantics.DatabaseJob;
 import org.simantics.Simantics;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RequestProcessor;
 import org.simantics.db.Resource;
-import org.simantics.db.Session;
 import org.simantics.db.common.request.UniqueRead;
 import org.simantics.db.common.utils.RequestUtil;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Read;
 import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.ui.DiagramModelHints;
+import org.simantics.g2d.diagram.IDiagram;
 import org.simantics.modeling.ModelingResources;
+import org.simantics.modeling.ui.diagramEditor.DiagramViewer;
 import org.simantics.ui.SimanticsUI;
-import org.simantics.ui.workbench.IResourceEditorInput;
-import org.simantics.utils.ui.ErrorLogger;
 
 /**
  * @author Tuukka Lehtonen
@@ -49,74 +54,90 @@ public class TypicalPropertyTester extends PropertyTester {
     public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
         //System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
 
-        try {
-            Session session = Simantics.peekSession();
-            if (session == null)
-                return false;
-
-            if (!(receiver instanceof IEditorPart))
-                return false;
-            IEditorPart editor = (IEditorPart) receiver;
-            IEditorInput in = editor.getEditorInput();
-            if (!(in instanceof IResourceEditorInput))
-                return false;
-            IResourceEditorInput input = (IResourceEditorInput) in;
-            final Resource inputResource = input.getResource();
-
-            if (DatabaseJob.inProgress()) {
-                // See Apros issue #9115
-                // Return true because it is often possible that the database
-                // will be busy when these properties are tested. In such cases
-                // the handlers/menu contributions using these tests would
-                // become disabled unless we return true here. It is up to the
-                // handlers to also make sure that their input is valid.
-                return true;
-            }
+        if (!(receiver instanceof IEditorPart))
+            return false;
 
-            if (IS_TYPICAL_MASTER_EDITOR.equals(property)) {
-                return isTypicalMasterEditor(session, inputResource);
-            } else if (IS_TYPICAL_INSTANCE_EDITOR.equals(property)) {
-                return isTypicalInstanceEditor(session, inputResource);
-            }
-        } catch (DatabaseException | InterruptedException e) {
-            ErrorLogger.defaultLogError(e);
+        if (IS_TYPICAL_MASTER_EDITOR.equals(property)) {
+            return isTypicalMasterEditor((IEditorPart) receiver);
+        } else if (IS_TYPICAL_INSTANCE_EDITOR.equals(property)) {
+            return isTypicalInstanceEditor((IEditorPart) receiver);
         }
 
         return false;
     }
 
-    public static boolean isTypicalMasterEditor(RequestProcessor processor, final Resource editorInputResource) throws DatabaseException, InterruptedException {
+    private static IDiagram getDiagram(IAdaptable editor) {
+        DiagramViewer viewer = editor.getAdapter(DiagramViewer.class);
+        return viewer != null ? viewer.getAdapter(IDiagram.class) : null;
+    }
+
+    private static Set<String> getDiagramMappedCompositeTypes(IDiagram diagram) {
+        Set<String> result = diagram != null ? diagram.getHint(DiagramModelHints.KEY_MAPPED_COMPOSITE_RESOURCE_TYPE_URIS) : null;
+        return result != null ? result :  Collections.emptySet();
+    }
+
+    private static Set<String> getDiagramMappedCompositeTypes(IAdaptable editor) {
+        return getDiagramMappedCompositeTypes( getDiagram(editor) );
+    }
+
+    public static boolean isTypicalMasterEditor(IAdaptable editor) {
+        Set<String> types = getDiagramMappedCompositeTypes(editor);
+        return types.contains(ModelingResources.URIs.MasterTypicalCompositeType);
+    }
+
+    private static boolean hasDiagramSource(IDiagram diagram) {
+        return diagram.getHint(DiagramModelHints.KEY_HAS_DIAGRAM_SOURCE) != null;
+    }
+
+    public static boolean isTypicalInstanceEditor(IAdaptable editor) {
+        IDiagram diagram = getDiagram(editor);
+        if (diagram == null)
+            return false;
+        Set<String> types = getDiagramMappedCompositeTypes(diagram);
+        return !types.contains(ModelingResources.URIs.MasterTypicalCompositeType)
+                && types.contains(ModelingResources.URIs.TypicalComposite)
+                && hasDiagramSource(diagram);
+    }
+
+    private static boolean timeoutingRead(RequestProcessor processor, Read<Boolean> read) throws DatabaseException, InterruptedException {
         return RequestUtil.trySyncRequest(
                 Simantics.getSession(),
                 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
                 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
                 false,
-                new UniqueRead<Boolean>() {
+                read);
+    }
+
+    public static boolean isTypicalMasterEditor(RequestProcessor processor, Resource editorInputResource) throws DatabaseException, InterruptedException {
+        return timeoutingRead(processor, new UniqueRead<Boolean>() {
             @Override
             public Boolean perform(ReadGraph graph) throws DatabaseException {
-                ModelingResources MOD = ModelingResources.getInstance(graph);
-                Resource composite = graph.getPossibleObject(editorInputResource, MOD.DiagramToComposite);
-                return composite != null
-                        && graph.isInstanceOf(composite, MOD.MasterTypicalCompositeType);
+                return isTypicalMasterEditor(graph, editorInputResource);
             }
         });
     }
 
-    public static boolean isTypicalInstanceEditor(RequestProcessor processor, final Resource editorInputResource) throws DatabaseException, InterruptedException {
-        return RequestUtil.trySyncRequest(
-                Simantics.getSession(),
-                SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
-                SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
-                false,
-                new UniqueRead<Boolean>() {
+    public static boolean isTypicalInstanceEditor(RequestProcessor processor, Resource editorInputResource) throws DatabaseException, InterruptedException {
+        return timeoutingRead(processor, new UniqueRead<Boolean>() {
             @Override
             public Boolean perform(ReadGraph graph) throws DatabaseException {
-                DiagramResource DIA = DiagramResource.getInstance(graph);
-                ModelingResources MOD = ModelingResources.getInstance(graph);
-                return graph.isInstanceOf(editorInputResource, DIA.Diagram)
-                        && graph.hasStatement(editorInputResource, MOD.HasDiagramSource);
+                return isTypicalInstanceEditor(graph, editorInputResource);
             }
         });
     }
 
+    public static boolean isTypicalMasterEditor(ReadGraph graph, Resource editorInputResource) throws DatabaseException {
+        ModelingResources MOD = ModelingResources.getInstance(graph);
+        Resource composite = graph.getPossibleObject(editorInputResource, MOD.DiagramToComposite);
+        return composite != null
+                && graph.isInstanceOf(composite, MOD.MasterTypicalCompositeType);
+    }
+
+    public static boolean isTypicalInstanceEditor(ReadGraph graph, Resource editorInputResource) throws DatabaseException {
+        DiagramResource DIA = DiagramResource.getInstance(graph);
+        ModelingResources MOD = ModelingResources.getInstance(graph);
+        return graph.isInstanceOf(editorInputResource, DIA.Diagram)
+                && graph.hasStatement(editorInputResource, MOD.HasDiagramSource);
+    }
+
 }