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