]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Allow contributing os.selectionview.SelectionProcessors from ontologies 48/948/2
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 11 Sep 2017 21:00:10 +0000 (00:00 +0300)
committerJani Simomaa <jani.simomaa@semantum.fi>
Tue, 12 Sep 2017 05:16:52 +0000 (08:16 +0300)
refs #7478

Change-Id: I647f4dda8b7dd125cd83624741fe0d6d87c818df

bundles/org.simantics.selectionview.ontology/graph/Selectionview.pgraph
bundles/org.simantics.selectionview/src/org/simantics/selectionview/StandardSelectionProcessor.java

index cc1bc6200966b072421cffb47631929cfb636c01..4bfeebffadc11bad23a571dfa5f576fcbb3b6ec9 100644 (file)
@@ -7,6 +7,7 @@ SEL = <http://www.simantics.org/SelectionView-1.2> : L0.Ontology
 
 SEL.Functions : L0.Library
 
+SEL.SelectionProcessorContribution <T L0.Entity
 SEL.TabContribution <T L0.Entity
 SEL.SelectionTransformation <T L0.Function
 
index 5d770b4fe5172baeb5a7628ca0002a6b21cf853d..797302c441be509723d412fd5b9391b1c6dd2542 100644 (file)
@@ -6,7 +6,6 @@ import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 
-import org.simantics.Simantics;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.request.PossibleIndexRoot;
@@ -28,22 +27,22 @@ public class StandardSelectionProcessor implements SelectionProcessor<Object, Re
     private static final Logger LOGGER = LoggerFactory.getLogger(StandardSelectionProcessor.class);
 
        private static Resource getIndexRoot(ReadGraph graph, Object selection) throws DatabaseException {
-               
+
                Variable variable = WorkbenchSelectionUtils.getPossibleVariable(graph, selection);
                if (variable != null) {
                        return graph.sync(new VariableIndexRoot(variable));
                }
-               
+
                variable = ISelectionUtils.filterSingleSelection(selection, Variable.class);
                if (variable != null) {
                        return graph.sync(new VariableIndexRoot(variable));
                }
-               
+
                Resource indexRoot = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MODEL, Resource.class, false);
                if (indexRoot == null) {
                        Resource r = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, Resource.class);
                        if (r != null)
-                           indexRoot = graph.sync(new PossibleIndexRoot(r));
+                               indexRoot = graph.sync(new PossibleIndexRoot(r));
                }
 
                return indexRoot != null && graph.isInstanceOf(indexRoot, Layer0.getInstance(graph).IndexRoot) ? indexRoot : null;
@@ -53,35 +52,36 @@ public class StandardSelectionProcessor implements SelectionProcessor<Object, Re
        public Collection<?> process(Object selection, ReadGraph graph) {
                return processStatic(selection, graph);
        }
-       
+
        public static Collection<?> processStatic(Object selection, ReadGraph graph) {
-               
+
                try {
-                       
+
                        SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
+                       Instances selectionProcessorFinder = graph.adapt(SEL.SelectionProcessorContribution, Instances.class);
                        Instances contributionFinder = graph.adapt(SEL.TabContribution, Instances.class);
                        Instances transformationFinder = graph.adapt(SEL.SelectionTransformation, Instances.class);
-                       
-                       ArrayList<TabContribution<?>> contributions = new ArrayList<TabContribution<?>>();
-                       
+
+                       ArrayList<TabContribution<?>> contributions = new ArrayList<>();
+
                        Resource index = getIndexRoot(graph, selection);
                        if(index != null) {
-                       
+
                                for(Resource r : contributionFinder.find(graph, index)) {
                                        TabContribution<?> contribution = graph.adapt(r, TabContribution.class);
                                        contributions.add(contribution);
-                                       if(DebugPolicy.DEBUG) System.err.println("-contribution " + graph.getURI(r));
+                                       if(DebugPolicy.DEBUG) LOGGER.debug("-contribution " + graph.getURI(r));
                                }
 
-                               ArrayList<Resource> transforms = new ArrayList<Resource>();
+                               ArrayList<Resource> transforms = new ArrayList<>();
                                transforms.addAll(transformationFinder.find(graph, index));
                                if(DebugPolicy.DEBUG) {
                                        for(Resource r : transforms)
-                                               System.err.println("-transform " + NameUtils.getSafeLabel(graph, r));
+                                               LOGGER.debug("-transform " + NameUtils.getSafeLabel(graph, r));
                                }
                                
-                               HashSet<Object> inputs = new HashSet<Object>();
-                               HashSet<Object> newInputs = new HashSet<Object>();
+                               HashSet<Object> inputs = new HashSet<>();
+                               HashSet<Object> newInputs = new HashSet<>();
                                inputs.add(selection);
                                boolean changed = true;
                                while(changed) {
@@ -98,56 +98,66 @@ public class StandardSelectionProcessor implements SelectionProcessor<Object, Re
                                        changed = inputs.addAll(newInputs);
                                        newInputs.clear();
                                }
-                               
 
                                if(DebugPolicy.DEBUG) {
                                        for(Object o : inputs)
-                                               System.err.println("-input " + inputName(graph, o));
+                                               LOGGER.debug("-input " + inputName(graph, o));
                                }
 
-                               Set<ComparableTabContributor> result = new HashSet<ComparableTabContributor>();
+                               Set<ComparableTabContributor> result = new HashSet<>();
                                for(TabContribution c : contributions) {
                                        for(Object input : inputs) {
                                                try {
                                                        if(DebugPolicy.DEBUG) {
-                                                               System.err.println("contribution: " + c);
-                                                               System.err.println("->input: " + input);
-                                                               System.err.println("->input name: " + inputName(graph, input));
+                                                               LOGGER.debug("contribution: " + c);
+                                                               LOGGER.debug("->input: " + input);
+                                                               LOGGER.debug("->input name: " + inputName(graph, input));
                                                        }
                                                        if (c.accept(graph, input))
                                                                c.contribute(graph, input, result);
                                                } catch (Exception e) {
-                                                       LOGGER.error("Selection view tab contribution failed.", e);
+                                                       LOGGER.error("Selection view tab contribution failed for contribution " + c, e);
                                                }
                                        }
                                }
-                               
+
+                               for(Resource r : selectionProcessorFinder.find(graph, index)) {
+                                       if (DebugPolicy.DEBUG)
+                                               LOGGER.debug("-SelectionProcessor contribution " + graph.getURI(r));
+                                       @SuppressWarnings("unchecked")
+                                       SelectionProcessor<Object, ReadGraph> processor = graph.adapt(r, SelectionProcessor.class);
+                                       Collection<?> contribs = processor.process(selection, graph);
+                                       if (contribs != null && !contribs.isEmpty()) {
+                                               for (Object contrib : contribs) {
+                                                       if (contrib instanceof ComparableTabContributor) {
+                                                               result.add((ComparableTabContributor) contrib);
+                                                       }
+                                               }
+                                       }
+                               }
+
                                if(DebugPolicy.DEBUG) {
-                                       System.err.println("result: " + result);
+                                       LOGGER.debug("result: " + result);
                                }
-                               
+
                                return result;
-                       
+
                        }
-                       
-               
+
                } catch (DatabaseException e) {
-                       
-                       e.printStackTrace();
-                       
+                       LOGGER.error("Selection view tab contribution failed unexpectedly.", e);
                }
-        
+
                return Collections.emptyList();
-               
        }
-       
+
        private static String inputName(ReadGraph graph, Object o) throws DatabaseException {
                if(o instanceof Resource) {
                        return "Resource: " + NameUtils.getURIOrSafeNameInternal(graph, (Resource)o);
                } else if(o instanceof Variable) {
                        return "Variable: " + ((Variable)o).getURI(graph) + " " + NameUtils.getSafeLabel(graph, (Resource)((Variable)o).getPossiblePropertyValue(graph, Variables.TYPE));
-        } else if(o instanceof SelectionInput) {
-            return "SelectionInput: " + o.toString();
+               } else if(o instanceof SelectionInput) {
+                       return "SelectionInput: " + o.toString();
                } else {
                        return "Object(" + o.getClass().getSimpleName() + "): " + o.toString();
                }