]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser2/contributions/IsUserDefinedComponentTest.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser2 / contributions / IsUserDefinedComponentTest.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management in
3  * 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.modelBrowser2.contributions;
13
14 import org.simantics.browsing.ui.model.tests.Test;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.structural.stubs.StructuralResource2;
20
21 /**
22  * @author Tuukka Lehtonen
23  */
24 public enum IsUserDefinedComponentTest implements Test {
25
26         INSTANCE;
27
28         public static IsUserDefinedComponentTest get() {
29                 return INSTANCE;
30         }
31
32         @Override
33         public boolean isCompatible(Class<?> contentType) {
34                 return Variable.class.equals(contentType) ||
35                                 Resource.class.equals(contentType);
36         }
37
38         @Override
39         public boolean test(ReadGraph graph, Object content) throws DatabaseException {
40                 if (content instanceof Variable) {
41                         Variable v = (Variable) content;
42                         //System.out.println("Test variable: " + v.getURI(graph));
43                         StructuralResource2 STR = StructuralResource2.getInstance(graph);
44                         Resource type = v.getPossibleType(graph, STR.Component);
45                         //System.out.println("variable component type: " + NameUtils.getURIOrSafeNameInternal(graph, type));
46                         return isUserComponentType(graph, type, STR);
47                 } else if (content instanceof Resource) {
48                         Resource r = (Resource) content;
49                         //System.out.println("Test resource: " + NameUtils.getURIOrSafeNameInternal(graph, r));
50                         StructuralResource2 STR = StructuralResource2.getInstance(graph);
51                         Resource type = graph.getPossibleType(r, STR.Component);
52                         //System.out.println("resource component type: " + NameUtils.getURIOrSafeNameInternal(graph, type));
53                         return isUserComponentType(graph, type, STR);
54                 }
55                 return false;
56         }
57
58         private static boolean isUserComponentType(ReadGraph graph, Resource type, StructuralResource2 STR) throws DatabaseException {
59                 if (type == null)
60                         return false;
61                 Resource definedBy = graph.getPossibleObject(type, STR.IsDefinedBy);
62                 if (definedBy != null)
63                         return true;
64                 boolean procedural = graph.isInstanceOf(type, STR.ProceduralComponentType);
65                 return procedural;
66         }
67
68 }