Copy URI context menu action to Model Browser for development mode
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / tests / HasURITest.java
1 /*******************************************************************************
2  * Copyright (c) 2017 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.model.tests;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.exception.InvalidVariableException;
18 import org.simantics.db.layer0.variable.Variable;
19
20 /**
21  * @author Tuukka Lehtonen
22  * @since 1.30.0
23  */
24 public enum HasURITest implements Test {
25     INSTANCE;
26
27     public static HasURITest get() {
28         return INSTANCE;
29     }
30
31     @Override
32     public boolean isCompatible(Class<?> contentType) {
33         return contentType.equals(Resource.class) || contentType.equals(Variable.class);
34     }
35
36     @Override
37     public boolean test(ReadGraph graph, Object content) throws DatabaseException {
38         if (content instanceof Resource) {
39             return graph.getPossibleURI((Resource) content) != null;
40         } else if (content instanceof Variable) {
41             try {
42                 Variable v = (Variable) content;
43                 return v.getURI(graph) != null;
44             } catch (InvalidVariableException e) {
45                 return false;
46             }
47         }
48         return false;
49     }
50
51 }