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