]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
70dbb6f7dba7bfc9648287e43f27233841b6cb0b
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.properties.widgets.functions;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 \r
17 import org.simantics.browsing.ui.common.node.AbstractNode;\r
18 import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor;\r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.common.request.ObjectsWithType;\r
22 import org.simantics.db.common.utils.NameUtils;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.layer0.Layer0;\r
25 import org.simantics.sysdyn.SysdynResource;\r
26 import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionLibraryNode;\r
27 \r
28 public class SharedFunctionLibraries extends ViewpointContributor<Resource> {\r
29 \r
30         @Override\r
31         public Collection<?> getContribution(ReadGraph graph, Resource input)\r
32         throws DatabaseException {\r
33                 Layer0 l0 = Layer0.getInstance(graph);\r
34                 SysdynResource sr = SysdynResource.getInstance(graph);\r
35                 \r
36                 ArrayList<AbstractNode<Resource>> result = new ArrayList<AbstractNode<Resource>>();\r
37 \r
38                 // Find shared ontologies. graph.getPossibleResource("http://SharedOntologies") does not update if it was null for the first time.\r
39                 Resource sharedOntologies = null;\r
40                 Resource root = input;\r
41                 while(graph.getPossibleObject(root, l0.PartOf) != null)\r
42                         root = graph.getPossibleObject(root, l0.PartOf);\r
43                 \r
44                 for(Resource r : graph.getObjects(root, l0.ConsistsOf)) {\r
45                         if("SharedOntologies".equals(NameUtils.getSafeName(graph, r))) {\r
46                                 sharedOntologies = r;\r
47                                 break;\r
48                         }\r
49                 }\r
50                 \r
51                 if(sharedOntologies == null) {\r
52                         return result;\r
53                 }\r
54                 \r
55                 // Find all shared function ontologies. \r
56                 // (Don't know why ObjectsWithType works only the first time, then gives nothing) \r
57                 ArrayList<Resource> sharedFunctionLibraries = new ArrayList<Resource>();\r
58                 for(Resource r : graph.getObjects(sharedOntologies, l0.ConsistsOf)) {\r
59                         if(graph.isInstanceOf(r, sr.SharedFunctionOntology))\r
60                                 sharedFunctionLibraries.add(r);\r
61                 }\r
62 \r
63                 // Find all shared function ontologies that have already been selected\r
64                 Collection<Resource> selectedSharedFunctionLibraries = graph.syncRequest(new ObjectsWithType(\r
65                                 input, l0.IsLinkedTo, sr.SharedFunctionOntology));\r
66 \r
67                 // Remove all selected ontologies from the shared function ontologies\r
68                 sharedFunctionLibraries.removeAll(selectedSharedFunctionLibraries);\r
69 \r
70                 for(Resource ontology : sharedFunctionLibraries) {\r
71                         result.add(new SharedFunctionLibraryNode(ontology));\r
72                 }\r
73 \r
74                 return result;\r
75         }\r
76 \r
77 }\r