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