]> gerrit.simantics Code Review - simantics/platform.git/blob
760f866e3d463d167b2e62a24d30a2dd2705641c
[simantics/platform.git] /
1 package org.simantics.browsing.ui.model.browsecontexts;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.List;
7
8 import org.simantics.browsing.ui.BuiltinKeys;
9 import org.simantics.browsing.ui.NodeContext;
10 import org.simantics.browsing.ui.common.NodeContextBuilder;
11 import org.simantics.browsing.ui.model.actions.ActionBrowseContext;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.RequestProcessor;
14 import org.simantics.db.Resource;
15 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
16 import org.simantics.db.common.request.UnaryRead;
17 import org.simantics.db.exception.DatabaseException;
18
19 /**
20  * Local utilities for BrowseContext/ActionBrowseContext internal use.
21  * 
22  * @author Tuukka Lehtonen
23  */
24 public final class BrowseContexts {
25
26         public static String[] toSortedURIs(ReadGraph graph, Collection<Resource> rs) throws DatabaseException {
27                 String[] result = new String[rs.size()];
28                 int i = 0;
29                 for (Resource r : rs) {
30                         String uri = graph.getPossibleURI(r);
31                         if (uri != null)
32                                 result[i++] = uri;
33                 }
34                 if (i > 1)
35                         Arrays.sort(result, 0, i);
36                 return i < result.length ? Arrays.copyOf(result, i) : result;
37         }
38
39         /**
40          * Wrapper for SCL usage because SCL does not know effect <code>RequestProcessor</code>
41          * 
42          * @param graph
43          * @param uris
44          * @return
45          * @throws DatabaseException
46          */
47         public static BrowseContext toBrowseContextG(ReadGraph graph, String[] uris) throws DatabaseException {
48             return toBrowseContext(graph, uris);
49         }
50         
51         public static BrowseContext toBrowseContext(RequestProcessor processor, String[] uris) throws DatabaseException {
52                 return processor.syncRequest(new URIsToBrowseContext(Arrays.asList(uris)), TransientCacheListener.<BrowseContext>instance());
53         }
54
55            /**
56      * Wrapper for SCL usage because SCL does not know effect <code>RequestProcessor</code>
57      * 
58      * @param graph
59      * @param uris
60      * @return
61      * @throws DatabaseException
62      */
63         public static ActionBrowseContext toActionBrowseContextG(ReadGraph graph, String[] uris) throws DatabaseException {
64             return toActionBrowseContext(graph, uris);
65         }
66         
67         public static ActionBrowseContext toActionBrowseContext(RequestProcessor processor, String[] uris) throws DatabaseException {
68                 return processor.syncRequest(new URIsToActionBrowseContext(Arrays.asList(uris)), TransientCacheListener.<ActionBrowseContext>instance());
69         }
70         
71         public static NodeContext getNodeContextForResource(Resource resource) {
72             return NodeContextBuilder.buildWithData(BuiltinKeys.INPUT, resource);
73         }
74
75         public static class URIsToBrowseContext extends UnaryRead<List<String>, BrowseContext> {
76
77                 public URIsToBrowseContext(List<String> parameter) {
78                         super(parameter);
79                 }
80
81                 @Override
82                 public BrowseContext perform(ReadGraph graph) throws DatabaseException {
83                         Collection<Resource> resources = graph.syncRequest( new URIsToResources(parameter) );
84                         return resources.isEmpty() ? null : graph.syncRequest( new BrowseContextRequest(resources) );
85                 }
86
87         }
88
89         public static class URIsToActionBrowseContext extends UnaryRead<List<String>, ActionBrowseContext> {
90
91                 public URIsToActionBrowseContext(List<String> parameter) {
92                         super(parameter);
93                 }
94
95                 @Override
96                 public ActionBrowseContext perform(ReadGraph graph) throws DatabaseException {
97                         Collection<Resource> resources = graph.syncRequest( new URIsToResources(parameter) );
98                         return resources.isEmpty() ? null : graph.syncRequest( new ActionBrowseContextRequest(resources) );
99                 }
100
101         }
102
103         public static class URIsToResources extends UnaryRead<List<String>, Collection<Resource>> {
104
105                 public URIsToResources(List<String> parameter) {
106                         super(parameter);
107                 }
108
109                 @Override
110                 public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
111                         Collection<Resource> result = new ArrayList<Resource>(parameter.size());
112                         for (String uri : parameter) {
113                                 Resource r = graph.getPossibleResource(uri);
114                                 if (r != null)
115                                         result.add(r);
116                         }
117                         return result;
118                 }
119
120         }
121
122         public static class ResourcesToURIs extends UnaryRead<Collection<Resource>, List<String>> {
123
124                 public ResourcesToURIs(Collection<Resource> parameter) {
125                         super(parameter);
126                 }
127
128                 @Override
129                 public List<String> perform(ReadGraph graph) throws DatabaseException {
130                         List<String> result = new ArrayList<String>(parameter.size());
131                         for (Resource r : parameter) {
132                                 String uri = graph.getPossibleURI(r);
133                                 if (uri != null)
134                                         result.add(uri);
135                         }
136                         return result;
137                 }
138
139         }
140
141 }