X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.model%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fmodel%2Fbrowsecontexts%2FBrowseContexts.java;fp=bundles%2Forg.simantics.browsing.ui.model%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fmodel%2Fbrowsecontexts%2FBrowseContexts.java;h=1daf5dd9fe03731709b62d61eadc8711332869d9;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/browsecontexts/BrowseContexts.java b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/browsecontexts/BrowseContexts.java new file mode 100644 index 000000000..1daf5dd9f --- /dev/null +++ b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/browsecontexts/BrowseContexts.java @@ -0,0 +1,141 @@ +package org.simantics.browsing.ui.model.browsecontexts; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import org.simantics.browsing.ui.BuiltinKeys; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.common.NodeContextBuilder; +import org.simantics.browsing.ui.model.actions.ActionBrowseContext; +import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; +import org.simantics.db.Resource; +import org.simantics.db.common.procedure.adapter.TransientCacheListener; +import org.simantics.db.common.request.UnaryRead; +import org.simantics.db.exception.DatabaseException; + +/** + * Local utilities for BrowseContext/ActionBrowseContext internal use. + * + * @author Tuukka Lehtonen + */ +public final class BrowseContexts { + + public static String[] toSortedURIs(ReadGraph graph, Collection rs) throws DatabaseException { + String[] result = new String[rs.size()]; + int i = 0; + for (Resource r : rs) { + String uri = graph.getPossibleURI(r); + if (uri != null) + result[i++] = uri; + } + if (i > 1) + Arrays.sort(result, 0, i); + return i < result.length ? Arrays.copyOf(result, i) : result; + } + + /** + * Wrapper for SCL usage because SCL does not know effect RequestProcessor + * + * @param graph + * @param uris + * @return + * @throws DatabaseException + */ + public static BrowseContext toBrowseContextG(ReadGraph graph, String[] uris) throws DatabaseException { + return toBrowseContext(graph, uris); + } + + public static BrowseContext toBrowseContext(RequestProcessor processor, String[] uris) throws DatabaseException { + return processor.syncRequest(new URIsToBrowseContext(Arrays.asList(uris)), TransientCacheListener.instance()); + } + + /** + * Wrapper for SCL usage because SCL does not know effect RequestProcessor + * + * @param graph + * @param uris + * @return + * @throws DatabaseException + */ + public static ActionBrowseContext toActionBrowseContextG(ReadGraph graph, String[] uris) throws DatabaseException { + return toActionBrowseContext(graph, uris); + } + + public static ActionBrowseContext toActionBrowseContext(RequestProcessor processor, String[] uris) throws DatabaseException { + return processor.syncRequest(new URIsToActionBrowseContext(Arrays.asList(uris)), TransientCacheListener.instance()); + } + + public static NodeContext getNodeContextForResource(Resource resource) { + return NodeContextBuilder.buildWithData(BuiltinKeys.INPUT, resource); + } + + public static class URIsToBrowseContext extends UnaryRead, BrowseContext> { + + public URIsToBrowseContext(List parameter) { + super(parameter); + } + + @Override + public BrowseContext perform(ReadGraph graph) throws DatabaseException { + Collection resources = graph.syncRequest( new URIsToResources(parameter) ); + return resources.isEmpty() ? null : graph.syncRequest( new BrowseContextRequest(resources) ); + } + + } + + public static class URIsToActionBrowseContext extends UnaryRead, ActionBrowseContext> { + + public URIsToActionBrowseContext(List parameter) { + super(parameter); + } + + @Override + public ActionBrowseContext perform(ReadGraph graph) throws DatabaseException { + Collection resources = graph.syncRequest( new URIsToResources(parameter) ); + return resources.isEmpty() ? null : graph.syncRequest( new ActionBrowseContextRequest(resources) ); + } + + } + + public static class URIsToResources extends UnaryRead, Collection> { + + public URIsToResources(List parameter) { + super(parameter); + } + + @Override + public Collection perform(ReadGraph graph) throws DatabaseException { + Collection result = new ArrayList(parameter.size()); + for (String uri : parameter) { + Resource r = graph.getPossibleResource(uri); + if (r != null) + result.add(r); + } + return result; + } + + } + + public static class ResourcesToURIs extends UnaryRead, List> { + + public ResourcesToURIs(Collection parameter) { + super(parameter); + } + + @Override + public List perform(ReadGraph graph) throws DatabaseException { + List result = new ArrayList(parameter.size()); + for (Resource r : parameter) { + String uri = graph.getPossibleURI(r); + if (uri != null) + result.add(uri); + } + return result; + } + + } + +}