]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.workbench.internal.contributions.search;\r
13 \r
14 import org.eclipse.jface.layout.GridDataFactory;\r
15 import org.eclipse.jface.layout.GridLayoutFactory;\r
16 import org.eclipse.jface.resource.JFaceResources;\r
17 import org.eclipse.jface.resource.LocalResourceManager;\r
18 import org.eclipse.swt.SWT;\r
19 import org.eclipse.swt.events.DisposeEvent;\r
20 import org.eclipse.swt.events.DisposeListener;\r
21 import org.eclipse.swt.events.KeyAdapter;\r
22 import org.eclipse.swt.events.KeyEvent;\r
23 import org.eclipse.swt.graphics.Image;\r
24 import org.eclipse.swt.layout.GridData;\r
25 import org.eclipse.swt.widgets.Composite;\r
26 import org.eclipse.swt.widgets.Label;\r
27 import org.eclipse.swt.widgets.Shell;\r
28 import org.eclipse.swt.widgets.Text;\r
29 import org.eclipse.ui.PlatformUI;\r
30 import org.eclipse.ui.swt.IFocusService;\r
31 import org.simantics.utils.ui.BundleUtils;\r
32 import org.simantics.workbench.search.ISearchService;\r
33 import org.simantics.workbench.search.SearchQuery;\r
34 \r
35 /**\r
36  * @author Tuukka Lehtonen\r
37  */\r
38 public class SearchTrim extends Composite {\r
39 \r
40     private static final String SEARCH_TEXT_FOCUS_CONTROL_ID = "org.simantics.workbench.search.text";\r
41 \r
42         \r
43 \r
44     public static final String         KEY_TRIM    = "SearchTrim";\r
45 \r
46    \r
47 \r
48     private final LocalResourceManager resourceManager;\r
49 \r
50     private final boolean              disabled    = false;\r
51 \r
52     protected final Text                 searchText;\r
53 \r
54     /**\r
55      * Creates a new heap status control with the given parent, and using the\r
56      * given preference store to obtain settings such as the refresh interval.\r
57      * \r
58      * @param parent the parent composite\r
59      * @param prefStore the preference store\r
60      */\r
61     public SearchTrim(Composite parent) {\r
62         super(parent, SWT.NONE);\r
63 \r
64         // System.err.println("parent: " + parent.getLayout());\r
65         this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), this);\r
66 \r
67         searchText = new Text(this, SWT.BORDER | SWT.FLAT | SWT.SEARCH | SWT.ICON_SEARCH);\r
68         searchText.setToolTipText("Enter Search Criteria for Active Model");\r
69         GridDataFactory.fillDefaults().hint(100, SWT.DEFAULT).grab(true, true).applyTo(searchText);\r
70         searchText.addKeyListener(new KeyAdapter() {\r
71             @Override\r
72             public void keyPressed(KeyEvent e) {\r
73                 if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {\r
74                     nameAndTypeQuery(((e.stateMask & SWT.CTRL) == 0) ? ISearchService.ResultBrowser.VIEW : ISearchService.ResultBrowser.EDITOR);\r
75                 }\r
76             }\r
77         });\r
78 \r
79         if (PlatformUI.isWorkbenchRunning()) {\r
80             IFocusService service = (IFocusService) PlatformUI.getWorkbench().getService(IFocusService.class);\r
81             service.addFocusTracker(searchText, SEARCH_TEXT_FOCUS_CONTROL_ID);\r
82         }\r
83 \r
84         if ((searchText.getStyle() & SWT.ICON_SEARCH) == 0) {\r
85             final Label searchButton= new Label(this, SWT.NONE);\r
86             searchButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, true));\r
87             searchButton.setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromPlugin("com.famfamfam.silk", "/icons/magnifier.png")));\r
88             searchButton.moveAbove(searchText);\r
89             searchButton.setToolTipText("Search Active Model Contents");\r
90 \r
91             GridLayoutFactory.swtDefaults().margins(0, 0).spacing(0, 0).numColumns(3).applyTo(this);\r
92         } else {\r
93             GridLayoutFactory.swtDefaults().margins(0, 0).spacing(0, 0).numColumns(2).applyTo(this);\r
94         }\r
95 \r
96         Label filler = new Label(this, SWT.NONE);\r
97         GridDataFactory.swtDefaults().grab(true, false).minSize(3, SWT.DEFAULT).applyTo(filler);\r
98 \r
99         /*\r
100 \r
101         final Button b = new Button(this, SWT.PUSH);\r
102         // b.setText("Search by Name");\r
103         b.setText("&Name");\r
104         b.setToolTipText("Search by Name");\r
105         b.setImage(resourceManager.createImage(Activator.getImageDescriptor("img/magnifier.png")));\r
106         GridDataFactory.fillDefaults().applyTo(b);\r
107         b.addSelectionListener(new SelectionAdapter() {\r
108             @Override\r
109             public void widgetSelected(SelectionEvent e) {\r
110                 nameQuery(((e.stateMask & SWT.CTRL) == 0) ? ResultBrowser.VIEW : ResultBrowser.EDITOR);\r
111             }\r
112         });\r
113 \r
114         final Button b2 = new Button(this, SWT.PUSH);\r
115         b2.setText("&Type");\r
116         // b2.setText("Search by Type");\r
117         b2.setToolTipText("Search by Type");\r
118         GridDataFactory.fillDefaults().applyTo(b2);\r
119 \r
120         b2.setImage(resourceManager.createImage(Activator.getImageDescriptor("img/magnifier.png")));\r
121         b2.addSelectionListener(new SelectionAdapter() {\r
122             @Override\r
123             public void widgetSelected(SelectionEvent e) {\r
124                 typeQuery(((e.stateMask & SWT.CTRL) == 0) ? ResultBrowser.VIEW : ResultBrowser.EDITOR);\r
125             }\r
126         });\r
127         */\r
128 \r
129         // Register the KEY_TRIM datum into this controls parent Shell.\r
130         // This enables FocusSearchTrim handler to work (to focus this control).\r
131         final Shell shell = parent.getShell();\r
132         shell.setData(KEY_TRIM, searchText);\r
133         addDisposeListener(new DisposeListener() {\r
134             @Override\r
135             public void widgetDisposed(DisposeEvent e) {\r
136                 shell.setData(KEY_TRIM, null);\r
137             }\r
138         });\r
139     }\r
140 \r
141     protected void nameQuery(ISearchService.ResultBrowser browserType) {\r
142         if (disabled)\r
143             return;\r
144         String originalInput = searchText.getText();\r
145         String query = originalInput;\r
146 \r
147         // Don't allow empty queries\r
148         if (query.trim().isEmpty())\r
149             return;\r
150 \r
151         query = filter(query);\r
152 \r
153         SearchQuery searchQuery = new SearchQuery(originalInput);\r
154         searchQuery.setSearchFlag("Name", "on");\r
155         performQuery(searchQuery, browserType);\r
156     }\r
157 \r
158     protected void typeQuery(ISearchService.ResultBrowser browserType) {\r
159         if (disabled)\r
160             return;\r
161         String originalInput = searchText.getText();\r
162         String query = originalInput;\r
163 \r
164         // Don't allow empty queries\r
165         if (query.trim().isEmpty())\r
166             return;\r
167         \r
168         SearchQuery searchQuery = new SearchQuery(originalInput);\r
169         searchQuery.setSearchFlag("Types", "on");\r
170         performQuery(searchQuery, browserType);\r
171     }\r
172 \r
173     protected void nameAndTypeQuery(ISearchService.ResultBrowser browserType) {\r
174         if (disabled)\r
175             return;\r
176         String originalInput = searchText.getText();\r
177         String query = originalInput;\r
178 \r
179         // Don't allow empty queries\r
180         if (query.trim().isEmpty())\r
181             return;\r
182 \r
183         SearchQuery searchQuery = new SearchQuery(originalInput);\r
184         searchQuery.setSearchFlag("Name", "on");\r
185         searchQuery.setSearchFlag("Types", "on");\r
186         performQuery(searchQuery, browserType);\r
187     }\r
188 \r
189     protected String filter(String query) {\r
190         // TODO: implement\r
191         if (!query.endsWith("*"))\r
192             query += "*";\r
193         // ''' characters cannot be used in the query string, replace them with '"'\r
194         query = query.replaceAll("'", "\"");\r
195         return query;\r
196     }\r
197 \r
198     protected void performQuery(SearchQuery query, ISearchService.ResultBrowser browserType) {\r
199         ISearchService searchService = (ISearchService)PlatformUI.getWorkbench().getService(ISearchService.class);\r
200         searchService.performQuery(query, browserType, true);\r
201     }\r
202 \r
203    \r
204     \r
205 \r
206 \r
207 }