]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/TypicalPropertyTester.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / property / TypicalPropertyTester.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.property;\r
13 \r
14 import org.eclipse.core.expressions.PropertyTester;\r
15 import org.eclipse.ui.IEditorInput;\r
16 import org.eclipse.ui.IEditorPart;\r
17 import org.simantics.DatabaseJob;\r
18 import org.simantics.Simantics;\r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.RequestProcessor;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.Session;\r
23 import org.simantics.db.common.request.UniqueRead;\r
24 import org.simantics.db.common.utils.RequestUtil;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.diagram.stubs.DiagramResource;\r
27 import org.simantics.modeling.ModelingResources;\r
28 import org.simantics.ui.SimanticsUI;\r
29 import org.simantics.ui.workbench.IResourceEditorInput;\r
30 import org.simantics.utils.ui.ErrorLogger;\r
31 \r
32 /**\r
33  * @author Tuukka Lehtonen\r
34  */\r
35 public class TypicalPropertyTester extends PropertyTester {\r
36 \r
37 \r
38     /**\r
39      * Tests if the received object is considered deletable.\r
40      */\r
41     private static final String IS_TYPICAL_MASTER_EDITOR = "isMasterEditor";\r
42 \r
43     /**\r
44      * Tests if the received object is considered modifiable.\r
45      */\r
46     private static final String IS_TYPICAL_INSTANCE_EDITOR = "isInstanceEditor";\r
47 \r
48     @Override\r
49     public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {\r
50         //System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);\r
51 \r
52         try {\r
53             Session session = Simantics.peekSession();\r
54             if (session == null)\r
55                 return false;\r
56 \r
57             if (!(receiver instanceof IEditorPart))\r
58                 return false;\r
59             IEditorPart editor = (IEditorPart) receiver;\r
60             IEditorInput in = editor.getEditorInput();\r
61             if (!(in instanceof IResourceEditorInput))\r
62                 return false;\r
63             IResourceEditorInput input = (IResourceEditorInput) in;\r
64             final Resource inputResource = input.getResource();\r
65 \r
66             if (DatabaseJob.inProgress()) {\r
67                 // See Apros issue #9115\r
68                 // Return true because it is often possible that the database\r
69                 // will be busy when these properties are tested. In such cases\r
70                 // the handlers/menu contributions using these tests would\r
71                 // become disabled unless we return true here. It is up to the\r
72                 // handlers to also make sure that their input is valid.\r
73                 return true;\r
74             }\r
75 \r
76             if (IS_TYPICAL_MASTER_EDITOR.equals(property)) {\r
77                 return isTypicalMasterEditor(session, inputResource);\r
78             } else if (IS_TYPICAL_INSTANCE_EDITOR.equals(property)) {\r
79                 return isTypicalInstanceEditor(session, inputResource);\r
80             }\r
81         } catch (DatabaseException | InterruptedException e) {\r
82             ErrorLogger.defaultLogError(e);\r
83         }\r
84 \r
85         return false;\r
86     }\r
87 \r
88     public static boolean isTypicalMasterEditor(RequestProcessor processor, final Resource editorInputResource) throws DatabaseException, InterruptedException {\r
89         return RequestUtil.trySyncRequest(\r
90                 Simantics.getSession(),\r
91                 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
92                 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
93                 false,\r
94                 new UniqueRead<Boolean>() {\r
95             @Override\r
96             public Boolean perform(ReadGraph graph) throws DatabaseException {\r
97                 ModelingResources MOD = ModelingResources.getInstance(graph);\r
98                 Resource composite = graph.getPossibleObject(editorInputResource, MOD.DiagramToComposite);\r
99                 return composite != null\r
100                         && graph.isInstanceOf(composite, MOD.MasterTypicalCompositeType);\r
101             }\r
102         });\r
103     }\r
104 \r
105     public static boolean isTypicalInstanceEditor(RequestProcessor processor, final Resource editorInputResource) throws DatabaseException, InterruptedException {\r
106         return RequestUtil.trySyncRequest(\r
107                 Simantics.getSession(),\r
108                 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
109                 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
110                 false,\r
111                 new UniqueRead<Boolean>() {\r
112             @Override\r
113             public Boolean perform(ReadGraph graph) throws DatabaseException {\r
114                 DiagramResource DIA = DiagramResource.getInstance(graph);\r
115                 ModelingResources MOD = ModelingResources.getInstance(graph);\r
116                 return graph.isInstanceOf(editorInputResource, DIA.Diagram)\r
117                         && graph.hasStatement(editorInputResource, MOD.HasDiagramSource);\r
118             }\r
119         });\r
120     }\r
121 \r
122 }\r