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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.workbench.internal.contributions.search;
\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
36 * @author Tuukka Lehtonen
\r
38 public class SearchTrim extends Composite {
\r
40 private static final String SEARCH_TEXT_FOCUS_CONTROL_ID = "org.simantics.workbench.search.text";
\r
44 public static final String KEY_TRIM = "SearchTrim";
\r
48 private final LocalResourceManager resourceManager;
\r
50 private final boolean disabled = false;
\r
52 protected final Text searchText;
\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
58 * @param parent the parent composite
\r
59 * @param prefStore the preference store
\r
61 public SearchTrim(Composite parent) {
\r
62 super(parent, SWT.NONE);
\r
64 // System.err.println("parent: " + parent.getLayout());
\r
65 this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), this);
\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
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
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
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
91 GridLayoutFactory.swtDefaults().margins(0, 0).spacing(0, 0).numColumns(3).applyTo(this);
\r
93 GridLayoutFactory.swtDefaults().margins(0, 0).spacing(0, 0).numColumns(2).applyTo(this);
\r
96 Label filler = new Label(this, SWT.NONE);
\r
97 GridDataFactory.swtDefaults().grab(true, false).minSize(3, SWT.DEFAULT).applyTo(filler);
\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
109 public void widgetSelected(SelectionEvent e) {
\r
110 nameQuery(((e.stateMask & SWT.CTRL) == 0) ? ResultBrowser.VIEW : ResultBrowser.EDITOR);
\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
120 b2.setImage(resourceManager.createImage(Activator.getImageDescriptor("img/magnifier.png")));
\r
121 b2.addSelectionListener(new SelectionAdapter() {
\r
123 public void widgetSelected(SelectionEvent e) {
\r
124 typeQuery(((e.stateMask & SWT.CTRL) == 0) ? ResultBrowser.VIEW : ResultBrowser.EDITOR);
\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
135 public void widgetDisposed(DisposeEvent e) {
\r
136 shell.setData(KEY_TRIM, null);
\r
141 protected void nameQuery(ISearchService.ResultBrowser browserType) {
\r
144 String originalInput = searchText.getText();
\r
145 String query = originalInput;
\r
147 // Don't allow empty queries
\r
148 if (query.trim().isEmpty())
\r
151 query = filter(query);
\r
153 SearchQuery searchQuery = new SearchQuery(originalInput);
\r
154 searchQuery.setSearchFlag("Name", "on");
\r
155 performQuery(searchQuery, browserType);
\r
158 protected void typeQuery(ISearchService.ResultBrowser browserType) {
\r
161 String originalInput = searchText.getText();
\r
162 String query = originalInput;
\r
164 // Don't allow empty queries
\r
165 if (query.trim().isEmpty())
\r
168 SearchQuery searchQuery = new SearchQuery(originalInput);
\r
169 searchQuery.setSearchFlag("Types", "on");
\r
170 performQuery(searchQuery, browserType);
\r
173 protected void nameAndTypeQuery(ISearchService.ResultBrowser browserType) {
\r
176 String originalInput = searchText.getText();
\r
177 String query = originalInput;
\r
179 // Don't allow empty queries
\r
180 if (query.trim().isEmpty())
\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
189 protected String filter(String query) {
\r
191 if (!query.endsWith("*"))
\r
193 // ''' characters cannot be used in the query string, replace them with '"'
\r
194 query = query.replaceAll("'", "\"");
\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