--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.template2d.ui.adapters;\r
+\r
+import org.simantics.browsing.ui.model.tests.Test;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.PossibleModel;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public enum IsInModelTest implements Test {\r
+\r
+ INSTANCE;\r
+\r
+ public static IsInModelTest get() {\r
+ return INSTANCE;\r
+ }\r
+\r
+ @Override\r
+ public boolean isCompatible(Class<?> contentType) {\r
+ return Variable.class.equals(contentType) ||\r
+ Resource.class.equals(contentType);\r
+ }\r
+\r
+ @Override\r
+ public boolean test(ReadGraph graph, Object content) throws DatabaseException {\r
+ if (content instanceof Variable) {\r
+ Variable v = (Variable) content;\r
+ //System.out.println("Test variable: " + v.getURI(graph));\r
+ Resource model = Variables.getPossibleModel(graph, v);\r
+ //System.out.println("variable model: " + NameUtils.getURIOrSafeNameInternal(graph, model));\r
+ return model != null;\r
+ } else if (content instanceof Resource) {\r
+ Resource r = (Resource) content;\r
+ //System.out.println("Test resource: " + NameUtils.getURIOrSafeNameInternal(graph, r));\r
+ Resource model = graph.syncRequest(new PossibleModel(r));\r
+ //System.out.println("resource model: " + NameUtils.getURIOrSafeNameInternal(graph, model));\r
+ return model != null;\r
+ }\r
+ return false;\r
+ }\r
+\r
+}\r