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