]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/StandardSelectionProcessor.java
Revert "Default property editing restores assertions"
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / StandardSelectionProcessor.java
1 package org.simantics.selectionview;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.Set;
8
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;
26
27 public class StandardSelectionProcessor implements SelectionProcessor<Object, ReadGraph> {
28     private static final Logger LOGGER = LoggerFactory.getLogger(StandardSelectionProcessor.class);
29
30         private static Resource getIndexRoot(ReadGraph graph, Object selection) throws DatabaseException {
31                 
32                 Variable variable = WorkbenchSelectionUtils.getPossibleVariable(graph, selection);
33                 if (variable != null) {
34                         return graph.sync(new VariableIndexRoot(variable));
35                 }
36                 
37                 variable = ISelectionUtils.filterSingleSelection(selection, Variable.class);
38                 if (variable != null) {
39                         return graph.sync(new VariableIndexRoot(variable));
40                 }
41                 
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);
45                         if (r != null)
46                             indexRoot = graph.sync(new PossibleIndexRoot(r));
47                 }
48
49                 return indexRoot != null && graph.isInstanceOf(indexRoot, Layer0.getInstance(graph).IndexRoot) ? indexRoot : null;
50         }
51
52         @Override
53         public Collection<?> process(Object selection, ReadGraph graph) {
54                 return processStatic(selection, graph);
55         }
56         
57         public static Collection<?> processStatic(Object selection, ReadGraph graph) {
58                 
59                 try {
60                         
61                         SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
62                         Instances contributionFinder = graph.adapt(SEL.TabContribution, Instances.class);
63                         Instances transformationFinder = graph.adapt(SEL.SelectionTransformation, Instances.class);
64                         
65                         ArrayList<TabContribution<?>> contributions = new ArrayList<TabContribution<?>>();
66                         
67                         Resource index = getIndexRoot(graph, selection);
68                         if(index != null) {
69                         
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));
74                                 }
75
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));
81                                 }
82                                 
83                                 HashSet<Object> inputs = new HashSet<Object>();
84                                 HashSet<Object> newInputs = new HashSet<Object>();
85                                 inputs.add(selection);
86                                 boolean changed = true;
87                                 while(changed) {
88                                         for(Resource tr : transforms) {
89                                                 for(Object input : inputs) {
90                                                         try {
91                                                                 Object newInput = Functions.exec(graph, tr, graph, input);
92                                                                 if(newInput != null)
93                                                                         newInputs.add(newInput);
94                                                         } catch (DatabaseException e) {
95                                                         }
96                                                 }
97                                         }
98                                         changed = inputs.addAll(newInputs);
99                                         newInputs.clear();
100                                 }
101                                 
102
103                                 if(DebugPolicy.DEBUG) {
104                                         for(Object o : inputs)
105                                                 System.err.println("-input " + inputName(graph, o));
106                                 }
107
108                                 Set<ComparableTabContributor> result = new HashSet<ComparableTabContributor>();
109                                 for(TabContribution c : contributions) {
110                                         for(Object input : inputs) {
111                                                 try {
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));
116                                                         }
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);
121                                                 }
122                                         }
123                                 }
124                                 
125                                 if(DebugPolicy.DEBUG) {
126                                         System.err.println("result: " + result);
127                                 }
128                                 
129                                 return result;
130                         
131                         }
132                         
133                 
134                 } catch (DatabaseException e) {
135                         
136                         e.printStackTrace();
137                         
138                 }
139         
140                 return Collections.emptyList();
141                 
142         }
143         
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();
151                 } else {
152                         return "Object(" + o.getClass().getSimpleName() + "): " + o.toString();
153                 }
154         }
155
156 }