]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SearchResourceDialog.java
Index tokenized lowercase versions of name and types for UI searches
[simantics/platform.git] / bundles / org.simantics.debug.ui / src / org / simantics / debug / ui / SearchResourceDialog.java
index 29cfd63bd45d9786bed42ad25a04b9e0cdc670a7..06710f6e29bd03a3dec4ecbdc50d5f4afdbd4b64 100644 (file)
@@ -13,6 +13,7 @@
 package org.simantics.debug.ui;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
@@ -23,6 +24,7 @@ import java.util.Set;
 import java.util.TreeSet;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
@@ -56,6 +58,8 @@ import org.simantics.db.common.request.UniqueRead;
 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.genericrelation.Dependencies;
+import org.simantics.db.layer0.genericrelation.IndexQueries;
 import org.simantics.db.layer0.migration.OntologiesFromLibrary;
 import org.simantics.db.layer0.variable.Variables.Role;
 import org.simantics.db.request.Read;
@@ -85,13 +89,13 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
      */
     private static final int DEFAULT_MAX_INDEX_HITS = 1000;
 
-    private static final Pattern ID_PATTERN = Pattern.compile("\\$([0-9]+)");
+    private static final Pattern ID_PATTERN = Pattern.compile("\\$([0-9]+)"); //$NON-NLS-1$
 
     private static final String SEARCH_RESOURCE_DIALOG = "SearchResourceDialog"; //$NON-NLS-1$
 
     private static final int SHOW_IN_BROWSER_ID = IDialogConstants.CLIENT_ID + 1;
 
-    private static final String SHOW_IN_BROWSER_LABEL = "Show In Browser";
+    private static final String SHOW_IN_BROWSER_LABEL = Messages.SearchResourceDialog_ShowInBrowser;
 
     private Session session;
     @SuppressWarnings("unused")
@@ -103,7 +107,7 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
         @Override
         public String getText(Object element) {
             if (element == null)
-                return "null";
+                return "null"; //$NON-NLS-1$
             // This may happen if multiple choice is enabled
             if (element instanceof String)
                 return (String) element;
@@ -117,15 +121,15 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
                         String name = NameUtils.getSafeName(g, r);
                         String uri = DebugUtils.getPossibleRootRelativePath(g, r);
                         return
-                                "[" + r.getResourceId() + "] - "
+                                "[" + r.getResourceId() + "] - " //$NON-NLS-1$ //$NON-NLS-2$
                                 + name
-                                + (uri != null ? " - " : "")
-                                + (uri != null ? uri : "");
+                                + (uri != null ? " - " : "") //$NON-NLS-1$ //$NON-NLS-2$
+                                + (uri != null ? uri : ""); //$NON-NLS-1$
                     }
                 });
             } catch (DatabaseException e) {
-                Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Resource label provider failed unexpectedly.", e));
-                return "";
+                Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.SearchResourceDialog_ActivatorResourceLabelProviderFailed, e));
+                return ""; //$NON-NLS-1$
             }
         }
     };
@@ -137,7 +141,7 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
         @Override
         public String getText(Object element) {
             if (element==null)
-                return "null";
+                return "null"; //$NON-NLS-1$
             return element.toString();
         }
         @Override
@@ -162,19 +166,19 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
         this.session = s;
         this.selection = selection;
         this.labelProvider = new ElementLabelProvider(shell.getDisplay());
-        setMessage("Enter name, resource URI or ID");
+        setMessage(Messages.SearchResourceDialog_EnterNameResURIOrId);
         setListLabelProvider(labelProvider);
         setDetailsLabelProvider(detailsLabelProvider);
         setTitle(title);
         //setInitialPattern("*", FilteredItemsSelectionDialog.FULL_SELECTION);
         setSelectionHistory(new ResourceSelectionHistory());
-        setSeparatorLabel("Previously selected above, others below");
+        setSeparatorLabel(Messages.SearchResourceDialog_SeperatorLblPreviouslySelected);
     }
 
     @Override
     protected void configureShell(Shell shell) {
         this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), shell);
-        setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_blue.png")));
+        setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_blue.png"))); //$NON-NLS-1$
         super.configureShell(shell);
     }
 
@@ -193,7 +197,9 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
         if (buttonId == SHOW_IN_BROWSER_ID) {
             okPressed();
             LabeledResource lr = (LabeledResource) getFirstResult();
-            ShowInBrowser.defaultExecute(new StructuredSelection(new ResourceWorkbenchSelectionElement(lr.resource)));
+            if (lr != null) {
+               ShowInBrowser.defaultExecute(new StructuredSelection(new ResourceWorkbenchSelectionElement(lr.resource)));
+            }
             return;
         }
         super.buttonPressed(buttonId);
@@ -219,16 +225,16 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
                             try {
                                 return DebugUtils.getSafeLabel(g, r);
                             } catch (Exception ex) {
-                                System.out.println("Exception thrown from restoreItemFromMemento");
+                                System.out.println("Exception thrown from restoreItemFromMemento"); //$NON-NLS-1$
                             }
                         } catch (Throwable t) {}
-                        return "" + r.getResourceId();
+                        return "" + r.getResourceId(); //$NON-NLS-1$
                     }
                 });
                 if (name==null) return null;
                 return new LabeledResource(name, r);
             } catch (NumberFormatException | DatabaseException e) {
-                LOGGER.info("Search memento restoration failed.", e);
+                LOGGER.info("Search memento restoration failed.", e); //$NON-NLS-1$
                 return null;
             }
         }
@@ -264,7 +270,7 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
         public ItemsFilterWithSearchResults() {
             
             final String pattern = getPattern();
-            final boolean findUris = pattern.trim().startsWith("http:/");
+            final boolean findUris = pattern.trim().startsWith("http:/"); //$NON-NLS-1$
             final long referencedResourceId = referencedResourceId(pattern);
             final boolean findIds = referencedResourceId != 0;
 
@@ -300,13 +306,19 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
                                 }
                             }
                         } else {
+                            String[] terms = pattern.trim().split("\\s+"); //$NON-NLS-1$
+                            if (terms.length == 0) return;
+
                             Resource project = Simantics.peekProjectResource();
                             if (project != null) {
                                 IResourceFilter rf = resourceFilter;
                                 String filter = getFilterForResourceFilter(rf);
                                 if (!filter.isEmpty())
-                                    filter += " AND ";
-                                filter += "Name:" + pattern + "*";
+                                    filter += " AND "; //$NON-NLS-1$
+                                
+                                filter += Dependencies.FIELD_NAME_SEARCH + ":("; //$NON-NLS-1$
+                                filter += Arrays.stream(terms).map(term -> "+" + IndexQueries.escape(term.toLowerCase(), false) + "*").collect(Collectors.joining(" ")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+                                filter +=")"; //$NON-NLS-1$
 
                                 Layer0 L0 = Layer0.getInstance(graph);
 
@@ -436,7 +448,7 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
     @SuppressWarnings("unchecked")
     @Override
     public String getElementName(Object item) {
-        return ((Container<Resource>)item).get().getResourceId()+"";
+        return ((Container<Resource>)item).get().getResourceId()+""; //$NON-NLS-1$
         //return item.toString();
     }
 
@@ -462,12 +474,12 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
 
     private String getFilterForResourceFilter(IResourceFilter filter) {
         if (filter == null || filter == ResourceSearch.FILTER_ALL)
-            return "";
+            return ""; //$NON-NLS-1$
         if (filter == ResourceSearch.FILTER_RELATIONS)
-            return "Types:Relation"; 
+            return "Types:Relation";  //$NON-NLS-1$
         if (filter == ResourceSearch.FILTER_TYPES)
-            return "Types:Type"; 
-        return "";
+            return "Types:Type";  //$NON-NLS-1$
+        return ""; //$NON-NLS-1$
     }
 
 }