X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.selection.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fselection%2Fui%2FElementSelectionTools.java;fp=org.simantics.district.selection.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fselection%2Fui%2FElementSelectionTools.java;h=130e3f4f3550d8e98dc5b9795743dba56a6d867a;hb=9026bdb43820fcd07826ed649ede5b3a82e92f5a;hp=0000000000000000000000000000000000000000;hpb=c4446c36493a20b5aaf24031f70a76a3873ff9e5;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectionTools.java b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectionTools.java new file mode 100644 index 00000000..130e3f4f --- /dev/null +++ b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectionTools.java @@ -0,0 +1,84 @@ +package org.simantics.district.selection.ui; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.simantics.Simantics; +import org.simantics.browsing.ui.common.AdaptableHintContext; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ResourceRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.QueryIndexUtils; +import org.simantics.db.layer0.SelectionHints; +import org.simantics.db.layer0.request.ActiveModels; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.request.Read; +import org.simantics.district.selection.ElementSelectionResource; +import org.simantics.district.selection.ElementSelector; +import org.simantics.layer0.Layer0; +import org.simantics.ui.selection.AnyResource; +import org.simantics.ui.selection.AnyVariable; +import org.simantics.ui.selection.WorkbenchSelectionContentType; + +public class ElementSelectionTools { + + public static final class SelectionElement extends AdaptableHintContext { + public SelectionElement(Key[] keys) { + super(keys); + } + + @SuppressWarnings("unchecked") + public T getContent(WorkbenchSelectionContentType contentType) { + Resource element = getHint(SelectionHints.KEY_MAIN); + if (contentType instanceof AnyResource) { + return (T)element; + } + else if (contentType instanceof AnyVariable) { + try { + return (T) Simantics.getSession().syncRequest(new ResourceRead(element) { + public Variable perform(ReadGraph graph) throws DatabaseException { + return ElementSelector.getVariableForElement(graph, resource); + } + }); + } catch (DatabaseException e) { + return null; + } + } + + return null; + } + } + + public static final class SelectionsRequest implements Read> { + @Override + public Collection perform(ReadGraph graph) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + ElementSelectionResource ES = ElementSelectionResource.getInstance(graph); + + Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource()); + if (model == null) { + return Collections.emptyList(); + } + + List libs = QueryIndexUtils.searchByType(graph, model, ES.SelectionLibrary); + if (libs.isEmpty()) + return Collections.emptyList(); + + Resource lib = libs.get(0); + + List result = new ArrayList<>(); + for (Resource selection : graph.getObjects(lib, L0.ConsistsOf)) { + if (!graph.isInstanceOf(selection, ES.Selection)) + continue; + + result.add(ElementSelector.getSelector(graph, selection)); + } + + return result; + } + } + +}