From 4edd236e0e4048716de121b26dd04da1194cd8cb Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Tue, 12 Sep 2017 00:00:10 +0300 Subject: [PATCH] Allow contributing os.selectionview.SelectionProcessors from ontologies refs #7478 Change-Id: I647f4dda8b7dd125cd83624741fe0d6d87c818df --- .../graph/Selectionview.pgraph | 1 + .../StandardSelectionProcessor.java | 86 +++++++++++-------- 2 files changed, 49 insertions(+), 38 deletions(-) diff --git a/bundles/org.simantics.selectionview.ontology/graph/Selectionview.pgraph b/bundles/org.simantics.selectionview.ontology/graph/Selectionview.pgraph index cc1bc6200..4bfeebffa 100644 --- a/bundles/org.simantics.selectionview.ontology/graph/Selectionview.pgraph +++ b/bundles/org.simantics.selectionview.ontology/graph/Selectionview.pgraph @@ -7,6 +7,7 @@ SEL = : L0.Ontology SEL.Functions : L0.Library +SEL.SelectionProcessorContribution 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> contributions = new ArrayList>(); - + + ArrayList> 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 transforms = new ArrayList(); + ArrayList 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 inputs = new HashSet(); - HashSet newInputs = new HashSet(); + HashSet inputs = new HashSet<>(); + HashSet newInputs = new HashSet<>(); inputs.add(selection); boolean changed = true; while(changed) { @@ -98,56 +98,66 @@ public class StandardSelectionProcessor implements SelectionProcessor result = new HashSet(); + Set 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 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(); } -- 2.43.2