]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views/src/org/simantics/views/All.java
Sync git svn branch with SVN repository r33153.
[simantics/platform.git] / bundles / org.simantics.views / src / org / simantics / views / All.java
1 package org.simantics.views;\r
2 \r
3 import java.io.IOException;\r
4 import java.net.MalformedURLException;\r
5 import java.net.URL;\r
6 import java.util.ArrayList;\r
7 import java.util.Collection;\r
8 import java.util.List;\r
9 \r
10 import org.eclipse.core.runtime.FileLocator;\r
11 import org.eclipse.core.runtime.Path;\r
12 import org.eclipse.core.runtime.Platform;\r
13 import org.osgi.framework.Bundle;\r
14 import org.simantics.databoard.util.StreamUtil;\r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.common.utils.ListUtils;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.db.layer0.variable.Variable;\r
20 import org.simantics.db.layer0.variable.Variables;\r
21 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;\r
22 import org.simantics.scl.reflection.annotations.SCLValue;\r
23 import org.simantics.views.ViewUtils.ColumnBean;\r
24 import org.simantics.views.ontology.ViewsResources;\r
25 \r
26 public class All {\r
27 \r
28     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
29     public static Object resourceURI(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {\r
30         \r
31         ViewsResources VIEW = ViewsResources.getInstance(graph);\r
32         Resource resource = graph.getSingleObject(converter, VIEW.ResourceURI_HasResource);\r
33         return graph.getURI(resource);\r
34         \r
35     }\r
36 \r
37     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
38     public static Object parameterValue(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {\r
39         \r
40         Variable runtime = ScenegraphLoaderUtils.getRuntimeVariable(graph, context);\r
41         Variable base = ScenegraphLoaderUtils.getBaseVariable(graph, context);\r
42         //System.err.println("parameterValue " + context.getURI(graph) + " " + base.getURI(graph) + " " + runtime.getURI(graph));\r
43         String rvi = context.getURI(graph).substring(base.getURI(graph).length());\r
44         //System.err.println("rvi " + rvi);\r
45         Variable parameter = runtime.browse(graph, rvi);\r
46         return parameter.getValue(graph);\r
47         \r
48     }\r
49     \r
50     @SCLValue(type = "ReadGraph -> Resource -> Variable -> Resource")\r
51     public static Resource singleResourceSelection(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {\r
52         return ScenegraphLoaderUtils.getPossibleResourceSelection(graph, context);\r
53     }\r
54     \r
55     @SCLValue(type = "ReadGraph -> Resource -> Variable -> [String]")\r
56     public static List<String> tabChildNames(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {\r
57         \r
58         Collection<Variable> children = context.getParent(graph).getChildren(graph);\r
59         \r
60         List<String> result = new ArrayList<String>();\r
61 \r
62                 for(Variable child : children) {\r
63                         String label = child.getPropertyValue(graph, Variables.LABEL); \r
64                         result.add(label);\r
65                 }\r
66                 \r
67                 return result;          \r
68         \r
69     }\r
70     \r
71     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
72     public static Object bundleImage(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {\r
73         \r
74         ViewsResources VIEW = ViewsResources.getInstance(graph);\r
75         String reference = graph.getRelatedValue(converter, VIEW.BundleImage_HasReference);\r
76         \r
77         String[] parts = reference.split(":");\r
78         if(parts.length != 2) return null;\r
79         \r
80         Bundle bundle = Platform.getBundle(parts[0]);\r
81         \r
82         if (bundle == null) {\r
83             throw new IllegalArgumentException("null bundle");\r
84         }\r
85 \r
86         // look for the image (this will check both the plugin and fragment folders\r
87         URL fullPathString = FileLocator.find(bundle, new Path(parts[1]), null);\r
88         if (fullPathString == null) {\r
89             try {\r
90                 fullPathString = new URL(parts[1]);\r
91             } catch (MalformedURLException e) {\r
92                 return null;\r
93             }\r
94         }\r
95         \r
96         try {\r
97                         return StreamUtil.readFully(fullPathString.openStream());\r
98                 } catch (IOException e) {\r
99                         e.printStackTrace();\r
100                         return null;\r
101                 }\r
102         \r
103     }\r
104 \r
105     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
106     public static Object gridData(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {\r
107         return ViewUtils.getGridData(graph, converter);\r
108     }\r
109 \r
110     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
111     public static Object gridLayout(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {\r
112         return ViewUtils.getLayout(graph, converter);\r
113     }\r
114 \r
115     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
116     public static Object rowData(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {\r
117         return ViewUtils.getRowData(graph, converter);\r
118     }\r
119 \r
120     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
121     public static Object rowLayout(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {\r
122         return ViewUtils.getRowLayout(graph, converter);\r
123     }\r
124 \r
125     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
126     public static Object style(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {\r
127         return ViewUtils.getStyle(graph, converter);\r
128     }\r
129     \r
130     @SCLValue(type = "ReadGraph -> Resource -> a -> b")\r
131     public static Object columnList(ReadGraph graph, Resource resource, Object context) throws DatabaseException {\r
132         ArrayList<ColumnBean> result = new ArrayList<ColumnBean>();\r
133         for(Resource item : ListUtils.toList(graph, resource)) {\r
134                 result.add(ViewUtils.getColumn(graph, item));\r
135         }\r
136         return result;\r
137     }\r
138     \r
139     \r
140 }