From 304a9422a5d698963db84080645766b1a14f346c Mon Sep 17 00:00:00 2001 From: Jussi Koskela Date: Wed, 11 Apr 2018 13:16:01 +0300 Subject: [PATCH] Filter also history items in resource search dialog refs #7864 Change-Id: I5d9dac3fc1dd43c21ff4a63d9e7ecab0081c3dcb --- .../debug/ui/SearchResourceDialog.java | 190 ++++++++++-------- 1 file changed, 101 insertions(+), 89 deletions(-) diff --git a/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SearchResourceDialog.java b/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SearchResourceDialog.java index c96b8469e..29cfd63bd 100644 --- a/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SearchResourceDialog.java +++ b/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SearchResourceDialog.java @@ -255,110 +255,122 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog { @Override protected ItemsFilter createFilter() { // NOTE: filter must be created here. - return new ItemsFilter() { - @Override - public boolean matchItem(Object item) { - //return matches(item.toString()); - return true; - } + return new ItemsFilterWithSearchResults(); + } - // If this method returns true, it means fillContentProvider will - // not be called again, but the existing results are just re-filtered. - @Override - public boolean isSubFilter(ItemsFilter filter) { - //System.out.println(getPattern() + " vs. " + filter.getPattern()); - return false; - } + private class ItemsFilterWithSearchResults extends ItemsFilter { + private Set searchResults = new HashSet(); - @Override - public boolean isConsistentItem(Object item) { - return true; - } + public ItemsFilterWithSearchResults() { + + final String pattern = getPattern(); + final boolean findUris = pattern.trim().startsWith("http:/"); + final long referencedResourceId = referencedResourceId(pattern); + final boolean findIds = referencedResourceId != 0; - @Override - public boolean equalsFilter(ItemsFilter filter) { - return false; - } - }; - } + searchResults.clear(); + if (pattern.isEmpty()) return; + //progressMonitor.beginTask("Searching", IProgressMonitor.UNKNOWN); - @Override - protected void fillContentProvider(final AbstractContentProvider contentProvider, - final ItemsFilter itemsFilter, final IProgressMonitor progressMonitor) - throws CoreException { - final String pattern = itemsFilter.getPattern(); - final boolean findUris = pattern.trim().startsWith("http:/"); - final long referencedResourceId = referencedResourceId(pattern); - final boolean findIds = referencedResourceId != 0; - - //progressMonitor.beginTask("Searching", IProgressMonitor.UNKNOWN); - - try { - session.syncRequest(new ReadRequest() { - @Override - public void run(ReadGraph graph) throws DatabaseException { - // Find by ID first. - if (findIds) { - try { - Resource r = graph.getService(SerialisationSupport.class).getResource(referencedResourceId); - contentProvider.add(new LabeledResource(DebugUtils.getSafeLabel(graph, r), r), itemsFilter); - //contentProvider.add(new LabeledResource(pattern, r), itemsFilter); - } catch (DatabaseException e) { - // No resource for specified id. - } - } - if (findUris) { - String uri = pattern; - if (uri.endsWith(Role.CHILD.getIdentifier())) { - uri = uri.substring(0, uri.length() - 1); + try { + session.syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + // Find by ID first. + if (findIds) { + try { + Resource r = graph.getService(SerialisationSupport.class).getResource(referencedResourceId); + searchResults.add(new LabeledResource(DebugUtils.getSafeLabel(graph, r), r)); + } catch (DatabaseException e) { + // No resource for specified id. + } } - Resource r = graph.getPossibleResource(uri); - if (r != null) { - contentProvider.add(new LabeledResource(DebugUtils.getSafeURI(graph, r), r), itemsFilter ); + if (findUris) { + String uri = pattern; + if (uri.endsWith(Role.CHILD.getIdentifier())) { + uri = uri.substring(0, uri.length() - 1); + } + Resource r = graph.getPossibleResource(uri); + if (r != null) { + searchResults.add(new LabeledResource(DebugUtils.getSafeURI(graph, r), r)); - Map children = graph.syncRequest(new UnescapedChildMapOfResource(r)); - for (Resource child : children.values()) { - contentProvider.add(new LabeledResource(DebugUtils.getSafeURI(graph, child), child), itemsFilter ); + Map children = graph.syncRequest(new UnescapedChildMapOfResource(r)); + for (Resource child : children.values()) { + searchResults.add(new LabeledResource(DebugUtils.getSafeURI(graph, child), child)); + } } - } - } else { - Resource project = Simantics.peekProjectResource(); - if (project != null) { - IResourceFilter rf = resourceFilter; - String filter = getFilterForResourceFilter(rf); - if (!filter.isEmpty()) - filter += " AND "; - filter += "Name:" + pattern + "*"; - - Layer0 L0 = Layer0.getInstance(graph); - - Set indexRoots = new HashSet<>(); - indexRoots.addAll(graph.syncRequest(new ObjectsWithType(project, L0.ConsistsOf, L0.IndexRoot))); - indexRoots.addAll(graph.syncRequest(new OntologiesFromLibrary(graph.getRootLibrary()))); - for (Resource indexRoot : indexRoots) { - Collection hits = find(graph, indexRoot, filter); - for (Resource r : hits) { - if (rf != null && !rf.acceptResource(graph, r)) - continue; - contentProvider.add(new LabeledResource(DebugUtils.getSafeLabel(graph, r), r), itemsFilter); + } else { + Resource project = Simantics.peekProjectResource(); + if (project != null) { + IResourceFilter rf = resourceFilter; + String filter = getFilterForResourceFilter(rf); + if (!filter.isEmpty()) + filter += " AND "; + filter += "Name:" + pattern + "*"; + + Layer0 L0 = Layer0.getInstance(graph); + + Set indexRoots = new HashSet<>(); + indexRoots.addAll(graph.syncRequest(new ObjectsWithType(project, L0.ConsistsOf, L0.IndexRoot))); + indexRoots.addAll(graph.syncRequest(new OntologiesFromLibrary(graph.getRootLibrary()))); + for (Resource indexRoot : indexRoots) { + Collection hits = find(graph, indexRoot, filter); + for (Resource r : hits) { + if (rf != null && !rf.acceptResource(graph, r)) + continue; + searchResults.add(new LabeledResource(DebugUtils.getSafeLabel(graph, r), r)); + } } } } } - } - public Collection find(ReadGraph graph, Resource index, String filter) throws DatabaseException { - //TimeLogger.resetTimeAndLog("find(" + graph.getURI(index) + ", " + filter + ")"); - Collection indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.>instance()); - //TimeLogger.log("found " + indexResult.size()); - return indexResult; - } + public Collection find(ReadGraph graph, Resource index, String filter) throws DatabaseException { + //TimeLogger.resetTimeAndLog("find(" + graph.getURI(index) + ", " + filter + ")"); + Collection indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.>instance()); + //TimeLogger.log("found " + indexResult.size()); + return indexResult; + } + + }); + } catch (DatabaseException ex) { + Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)); + } - }); - } catch (DatabaseException ex) { - Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)); } + @Override + public boolean matchItem(Object item) { + return searchResults.contains(item); + } + + @Override + public boolean isSubFilter(ItemsFilter filter) { + return false; + } + + @Override + public boolean isConsistentItem(Object item) { + return true; + } + + @Override + public boolean equalsFilter(ItemsFilter filter) { + return false; + } + + public void fillContentProvider(final AbstractContentProvider contentProvider) { + for (Object item : searchResults) { + contentProvider.add(item, this); + } + } + } + + @Override + protected void fillContentProvider(final AbstractContentProvider contentProvider, + final ItemsFilter itemsFilter, final IProgressMonitor progressMonitor) + throws CoreException { + ((ItemsFilterWithSearchResults) itemsFilter).fillContentProvider(contentProvider); progressMonitor.done(); } -- 2.47.1