]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/TypicalPropertyTester.java
Prevent unnecessary read transaction for synch master typical handle
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / property / TypicalPropertyTester.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 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  *******************************************************************************/
12 package org.simantics.modeling.ui.property;
13
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;
31
32 /**
33  * @author Tuukka Lehtonen
34  */
35 public class TypicalPropertyTester extends PropertyTester {
36
37
38     /**
39      * Tests if the received object is considered deletable.
40      */
41     private static final String IS_TYPICAL_MASTER_EDITOR = "isMasterEditor";
42
43     /**
44      * Tests if the received object is considered modifiable.
45      */
46     private static final String IS_TYPICAL_INSTANCE_EDITOR = "isInstanceEditor";
47
48     @Override
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);
51
52         try {
53             Session session = Simantics.peekSession();
54             if (session == null)
55                 return false;
56
57             if (!(receiver instanceof IEditorPart))
58                 return false;
59             IEditorPart editor = (IEditorPart) receiver;
60             IEditorInput in = editor.getEditorInput();
61             if (!(in instanceof IResourceEditorInput))
62                 return false;
63             IResourceEditorInput input = (IResourceEditorInput) in;
64             final Resource inputResource = input.getResource();
65
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.
73                 return true;
74             }
75
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);
80             }
81         } catch (DatabaseException | InterruptedException e) {
82             ErrorLogger.defaultLogError(e);
83         }
84
85         return false;
86     }
87
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,
93                 false,
94                 new UniqueRead<Boolean>() {
95             @Override
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);
101             }
102         });
103     }
104
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,
110                 false,
111                 new UniqueRead<Boolean>() {
112             @Override
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);
118             }
119         });
120     }
121
122 }