Filter also history items in resource search dialog 08/1708/4
authorJussi Koskela <jussi.koskela@semantum.fi>
Wed, 11 Apr 2018 10:16:01 +0000 (13:16 +0300)
committerJani Simomaa <jani.simomaa@semantum.fi>
Tue, 15 May 2018 09:57:15 +0000 (09:57 +0000)
refs #7864

Change-Id: I5d9dac3fc1dd43c21ff4a63d9e7ecab0081c3dcb

bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SearchResourceDialog.java

index c96b8469ee2bb573fb7524877709383560028eff..29cfd63bd45d9786bed42ad25a04b9e0cdc670a7 100644 (file)
@@ -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<Object> searchResults = new HashSet<Object>();
 
-            @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<String, Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(r));
-                            for (Resource child : children.values()) {
-                                contentProvider.add(new LabeledResource(DebugUtils.getSafeURI(graph, child), child), itemsFilter );
+                                Map<String, Resource> 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<Resource> 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<Resource> 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<Resource> 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<Resource> 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<Resource> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {
-                    //TimeLogger.resetTimeAndLog("find(" + graph.getURI(index) + ", " + filter + ")");
-                    Collection<Resource> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.<Collection<Resource>>instance());
-                    //TimeLogger.log("found " + indexResult.size());
-                    return indexResult;
-                }
+                    public Collection<Resource> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {
+                        //TimeLogger.resetTimeAndLog("find(" + graph.getURI(index) + ", " + filter + ")");
+                        Collection<Resource> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.<Collection<Resource>>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();
     }