1 package org.simantics.selectionview;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
9 import org.simantics.Simantics;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.common.request.PossibleIndexRoot;
13 import org.simantics.db.common.utils.Functions;
14 import org.simantics.db.common.utils.NameUtils;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.SelectionHints;
17 import org.simantics.db.layer0.adapter.Instances;
18 import org.simantics.db.layer0.request.VariableIndexRoot;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.layer0.variable.Variables;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.ui.selection.WorkbenchSelectionUtils;
23 import org.simantics.utils.ui.ISelectionUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class StandardSelectionProcessor implements SelectionProcessor<Object, ReadGraph> {
28 private static final Logger LOGGER = LoggerFactory.getLogger(StandardSelectionProcessor.class);
30 private static Resource getIndexRoot(ReadGraph graph, Object selection) throws DatabaseException {
32 Variable variable = WorkbenchSelectionUtils.getPossibleVariable(graph, selection);
33 if (variable != null) {
34 return graph.sync(new VariableIndexRoot(variable));
37 variable = ISelectionUtils.filterSingleSelection(selection, Variable.class);
38 if (variable != null) {
39 return graph.sync(new VariableIndexRoot(variable));
42 Resource indexRoot = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MODEL, Resource.class, false);
43 if (indexRoot == null) {
44 Resource r = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, Resource.class);
46 indexRoot = graph.sync(new PossibleIndexRoot(r));
49 return indexRoot != null && graph.isInstanceOf(indexRoot, Layer0.getInstance(graph).IndexRoot) ? indexRoot : null;
53 public Collection<?> process(Object selection, ReadGraph graph) {
54 return processStatic(selection, graph);
57 public static Collection<?> processStatic(Object selection, ReadGraph graph) {
61 SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
62 Instances contributionFinder = graph.adapt(SEL.TabContribution, Instances.class);
63 Instances transformationFinder = graph.adapt(SEL.SelectionTransformation, Instances.class);
65 ArrayList<TabContribution<?>> contributions = new ArrayList<TabContribution<?>>();
67 Resource index = getIndexRoot(graph, selection);
70 for(Resource r : contributionFinder.find(graph, index)) {
71 TabContribution<?> contribution = graph.adapt(r, TabContribution.class);
72 contributions.add(contribution);
73 if(DebugPolicy.DEBUG) System.err.println("-contribution " + graph.getURI(r));
76 ArrayList<Resource> transforms = new ArrayList<Resource>();
77 transforms.addAll(transformationFinder.find(graph, index));
78 if(DebugPolicy.DEBUG) {
79 for(Resource r : transforms)
80 System.err.println("-transform " + NameUtils.getSafeLabel(graph, r));
83 HashSet<Object> inputs = new HashSet<Object>();
84 HashSet<Object> newInputs = new HashSet<Object>();
85 inputs.add(selection);
86 boolean changed = true;
88 for(Resource tr : transforms) {
89 for(Object input : inputs) {
91 Object newInput = Functions.exec(graph, tr, graph, input);
93 newInputs.add(newInput);
94 } catch (DatabaseException e) {
98 changed = inputs.addAll(newInputs);
103 if(DebugPolicy.DEBUG) {
104 for(Object o : inputs)
105 System.err.println("-input " + inputName(graph, o));
108 Set<ComparableTabContributor> result = new HashSet<ComparableTabContributor>();
109 for(TabContribution c : contributions) {
110 for(Object input : inputs) {
112 if(DebugPolicy.DEBUG) {
113 System.err.println("contribution: " + c);
114 System.err.println("->input: " + input);
115 System.err.println("->input name: " + inputName(graph, input));
117 if (c.accept(graph, input))
118 c.contribute(graph, input, result);
119 } catch (Exception e) {
120 LOGGER.error("Selection view tab contribution failed.", e);
125 if(DebugPolicy.DEBUG) {
126 System.err.println("result: " + result);
134 } catch (DatabaseException e) {
140 return Collections.emptyList();
144 private static String inputName(ReadGraph graph, Object o) throws DatabaseException {
145 if(o instanceof Resource) {
146 return "Resource: " + NameUtils.getURIOrSafeNameInternal(graph, (Resource)o);
147 } else if(o instanceof Variable) {
148 return "Variable: " + ((Variable)o).getURI(graph) + " " + NameUtils.getSafeLabel(graph, (Resource)((Variable)o).getPossiblePropertyValue(graph, Variables.TYPE));
149 } else if(o instanceof SelectionInput) {
150 return "SelectionInput: " + o.toString();
152 return "Object(" + o.getClass().getSimpleName() + "): " + o.toString();