]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/adapters/IsInModelTest.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / adapters / IsInModelTest.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.template2d.ui.adapters;
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.request.PossibleModel;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.layer0.variable.Variables;
21
22 /**
23  * @author Tuukka Lehtonen
24  */
25 public enum IsInModelTest implements Test {
26
27         INSTANCE;
28
29         public static IsInModelTest get() {
30                 return INSTANCE;
31         }
32
33         @Override
34         public boolean isCompatible(Class<?> contentType) {
35                 return Variable.class.equals(contentType) ||
36                                 Resource.class.equals(contentType);
37         }
38
39         @Override
40         public boolean test(ReadGraph graph, Object content) throws DatabaseException {
41                 if (content instanceof Variable) {
42                         Variable v = (Variable) content;
43                         //System.out.println("Test variable: " + v.getURI(graph));
44                         Resource model = Variables.getPossibleModel(graph, v);
45                         //System.out.println("variable model: " + NameUtils.getURIOrSafeNameInternal(graph, model));
46                         return model != null;
47                 } else if (content instanceof Resource) {
48                         Resource r = (Resource) content;
49                         //System.out.println("Test resource: " + NameUtils.getURIOrSafeNameInternal(graph, r));
50                         Resource model = graph.syncRequest(new PossibleModel(r));
51                         //System.out.println("resource model: " + NameUtils.getURIOrSafeNameInternal(graph, model));
52                         return model != null;
53                 }
54                 return false;
55         }
56
57 }