]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.workbench/src/org/simantics/workbench/internal/contributions/search/SearchTrim.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.workbench / src / org / simantics / workbench / internal / contributions / search / SearchTrim.java
diff --git a/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/contributions/search/SearchTrim.java b/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/contributions/search/SearchTrim.java
new file mode 100644 (file)
index 0000000..a0c2d55
--- /dev/null
@@ -0,0 +1,207 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.workbench.internal.contributions.search;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.resource.JFaceResources;\r
+import org.eclipse.jface.resource.LocalResourceManager;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.DisposeEvent;\r
+import org.eclipse.swt.events.DisposeListener;\r
+import org.eclipse.swt.events.KeyAdapter;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.graphics.Image;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.swt.IFocusService;\r
+import org.simantics.utils.ui.BundleUtils;\r
+import org.simantics.workbench.search.ISearchService;\r
+import org.simantics.workbench.search.SearchQuery;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SearchTrim extends Composite {\r
+\r
+    private static final String SEARCH_TEXT_FOCUS_CONTROL_ID = "org.simantics.workbench.search.text";\r
+\r
+       \r
+\r
+    public static final String         KEY_TRIM    = "SearchTrim";\r
+\r
+   \r
+\r
+    private final LocalResourceManager resourceManager;\r
+\r
+    private final boolean              disabled    = false;\r
+\r
+    protected final Text                 searchText;\r
+\r
+    /**\r
+     * Creates a new heap status control with the given parent, and using the\r
+     * given preference store to obtain settings such as the refresh interval.\r
+     * \r
+     * @param parent the parent composite\r
+     * @param prefStore the preference store\r
+     */\r
+    public SearchTrim(Composite parent) {\r
+        super(parent, SWT.NONE);\r
+\r
+        // System.err.println("parent: " + parent.getLayout());\r
+        this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), this);\r
+\r
+        searchText = new Text(this, SWT.BORDER | SWT.FLAT | SWT.SEARCH | SWT.ICON_SEARCH);\r
+        searchText.setToolTipText("Enter Search Criteria for Active Model");\r
+        GridDataFactory.fillDefaults().hint(100, SWT.DEFAULT).grab(true, true).applyTo(searchText);\r
+        searchText.addKeyListener(new KeyAdapter() {\r
+            @Override\r
+            public void keyPressed(KeyEvent e) {\r
+                if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {\r
+                    nameAndTypeQuery(((e.stateMask & SWT.CTRL) == 0) ? ISearchService.ResultBrowser.VIEW : ISearchService.ResultBrowser.EDITOR);\r
+                }\r
+            }\r
+        });\r
+\r
+        if (PlatformUI.isWorkbenchRunning()) {\r
+            IFocusService service = (IFocusService) PlatformUI.getWorkbench().getService(IFocusService.class);\r
+            service.addFocusTracker(searchText, SEARCH_TEXT_FOCUS_CONTROL_ID);\r
+        }\r
+\r
+        if ((searchText.getStyle() & SWT.ICON_SEARCH) == 0) {\r
+            final Label searchButton= new Label(this, SWT.NONE);\r
+            searchButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, true));\r
+            searchButton.setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromPlugin("com.famfamfam.silk", "/icons/magnifier.png")));\r
+            searchButton.moveAbove(searchText);\r
+            searchButton.setToolTipText("Search Active Model Contents");\r
+\r
+            GridLayoutFactory.swtDefaults().margins(0, 0).spacing(0, 0).numColumns(3).applyTo(this);\r
+        } else {\r
+            GridLayoutFactory.swtDefaults().margins(0, 0).spacing(0, 0).numColumns(2).applyTo(this);\r
+        }\r
+\r
+        Label filler = new Label(this, SWT.NONE);\r
+        GridDataFactory.swtDefaults().grab(true, false).minSize(3, SWT.DEFAULT).applyTo(filler);\r
+\r
+        /*\r
+\r
+        final Button b = new Button(this, SWT.PUSH);\r
+        // b.setText("Search by Name");\r
+        b.setText("&Name");\r
+        b.setToolTipText("Search by Name");\r
+        b.setImage(resourceManager.createImage(Activator.getImageDescriptor("img/magnifier.png")));\r
+        GridDataFactory.fillDefaults().applyTo(b);\r
+        b.addSelectionListener(new SelectionAdapter() {\r
+            @Override\r
+            public void widgetSelected(SelectionEvent e) {\r
+                nameQuery(((e.stateMask & SWT.CTRL) == 0) ? ResultBrowser.VIEW : ResultBrowser.EDITOR);\r
+            }\r
+        });\r
+\r
+        final Button b2 = new Button(this, SWT.PUSH);\r
+        b2.setText("&Type");\r
+        // b2.setText("Search by Type");\r
+        b2.setToolTipText("Search by Type");\r
+        GridDataFactory.fillDefaults().applyTo(b2);\r
+\r
+        b2.setImage(resourceManager.createImage(Activator.getImageDescriptor("img/magnifier.png")));\r
+        b2.addSelectionListener(new SelectionAdapter() {\r
+            @Override\r
+            public void widgetSelected(SelectionEvent e) {\r
+                typeQuery(((e.stateMask & SWT.CTRL) == 0) ? ResultBrowser.VIEW : ResultBrowser.EDITOR);\r
+            }\r
+        });\r
+        */\r
+\r
+        // Register the KEY_TRIM datum into this controls parent Shell.\r
+        // This enables FocusSearchTrim handler to work (to focus this control).\r
+        final Shell shell = parent.getShell();\r
+        shell.setData(KEY_TRIM, searchText);\r
+        addDisposeListener(new DisposeListener() {\r
+            @Override\r
+            public void widgetDisposed(DisposeEvent e) {\r
+                shell.setData(KEY_TRIM, null);\r
+            }\r
+        });\r
+    }\r
+\r
+    protected void nameQuery(ISearchService.ResultBrowser browserType) {\r
+        if (disabled)\r
+            return;\r
+        String originalInput = searchText.getText();\r
+        String query = originalInput;\r
+\r
+        // Don't allow empty queries\r
+        if (query.trim().isEmpty())\r
+            return;\r
+\r
+        query = filter(query);\r
+\r
+        SearchQuery searchQuery = new SearchQuery(originalInput);\r
+        searchQuery.setSearchFlag("Name", "on");\r
+        performQuery(searchQuery, browserType);\r
+    }\r
+\r
+    protected void typeQuery(ISearchService.ResultBrowser browserType) {\r
+        if (disabled)\r
+            return;\r
+        String originalInput = searchText.getText();\r
+        String query = originalInput;\r
+\r
+        // Don't allow empty queries\r
+        if (query.trim().isEmpty())\r
+            return;\r
+        \r
+        SearchQuery searchQuery = new SearchQuery(originalInput);\r
+        searchQuery.setSearchFlag("Types", "on");\r
+        performQuery(searchQuery, browserType);\r
+    }\r
+\r
+    protected void nameAndTypeQuery(ISearchService.ResultBrowser browserType) {\r
+        if (disabled)\r
+            return;\r
+        String originalInput = searchText.getText();\r
+        String query = originalInput;\r
+\r
+        // Don't allow empty queries\r
+        if (query.trim().isEmpty())\r
+            return;\r
+\r
+        SearchQuery searchQuery = new SearchQuery(originalInput);\r
+        searchQuery.setSearchFlag("Name", "on");\r
+        searchQuery.setSearchFlag("Types", "on");\r
+        performQuery(searchQuery, browserType);\r
+    }\r
+\r
+    protected String filter(String query) {\r
+        // TODO: implement\r
+        if (!query.endsWith("*"))\r
+            query += "*";\r
+        // ''' characters cannot be used in the query string, replace them with '"'\r
+        query = query.replaceAll("'", "\"");\r
+        return query;\r
+    }\r
+\r
+    protected void performQuery(SearchQuery query, ISearchService.ResultBrowser browserType) {\r
+       ISearchService searchService = (ISearchService)PlatformUI.getWorkbench().getService(ISearchService.class);\r
+       searchService.performQuery(query, browserType, true);\r
+    }\r
+\r
+   \r
+    \r
+\r
+\r
+}
\ No newline at end of file