1 package org.simantics.browsing.ui.model.browsecontexts;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
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;
20 * Local utilities for BrowseContext/ActionBrowseContext internal use.
22 * @author Tuukka Lehtonen
24 public final class BrowseContexts {
26 public static String[] toSortedURIs(ReadGraph graph, Collection<Resource> rs) throws DatabaseException {
27 String[] result = new String[rs.size()];
29 for (Resource r : rs) {
30 String uri = graph.getPossibleURI(r);
35 Arrays.sort(result, 0, i);
36 return i < result.length ? Arrays.copyOf(result, i) : result;
40 * Wrapper for SCL usage because SCL does not know effect <code>RequestProcessor</code>
45 * @throws DatabaseException
47 public static BrowseContext toBrowseContextG(ReadGraph graph, String[] uris) throws DatabaseException {
48 return toBrowseContext(graph, uris);
51 public static BrowseContext toBrowseContext(RequestProcessor processor, String[] uris) throws DatabaseException {
52 return processor.syncRequest(new URIsToBrowseContext(Arrays.asList(uris)), TransientCacheListener.<BrowseContext>instance());
56 * Wrapper for SCL usage because SCL does not know effect <code>RequestProcessor</code>
61 * @throws DatabaseException
63 public static ActionBrowseContext toActionBrowseContextG(ReadGraph graph, String[] uris) throws DatabaseException {
64 return toActionBrowseContext(graph, uris);
67 public static ActionBrowseContext toActionBrowseContext(RequestProcessor processor, String[] uris) throws DatabaseException {
68 return processor.syncRequest(new URIsToActionBrowseContext(Arrays.asList(uris)), TransientCacheListener.<ActionBrowseContext>instance());
71 public static NodeContext getNodeContextForResource(Resource resource) {
72 return NodeContextBuilder.buildWithData(BuiltinKeys.INPUT, resource);
75 public static class URIsToBrowseContext extends UnaryRead<List<String>, BrowseContext> {
77 public URIsToBrowseContext(List<String> parameter) {
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) );
89 public static class URIsToActionBrowseContext extends UnaryRead<List<String>, ActionBrowseContext> {
91 public URIsToActionBrowseContext(List<String> parameter) {
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) );
103 public static class URIsToResources extends UnaryRead<List<String>, Collection<Resource>> {
105 public URIsToResources(List<String> parameter) {
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);
122 public static class ResourcesToURIs extends UnaryRead<Collection<Resource>, List<String>> {
124 public ResourcesToURIs(Collection<Resource> parameter) {
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);