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.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;
37 * @author Tuukka Lehtonen
39 public class SearchTrim extends Composite {
41 private static final String SEARCH_TEXT_FOCUS_CONTROL_ID = "org.simantics.workbench.search.text";
45 public static final String KEY_TRIM = "SearchTrim";
49 private final LocalResourceManager resourceManager;
51 private final boolean disabled = false;
53 protected final Text searchText;
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.
59 * @param parent the parent composite
60 * @param prefStore the preference store
62 public SearchTrim(Composite parent) {
63 super(parent, SWT.NONE);
65 // System.err.println("parent: " + parent.getLayout());
66 this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), this);
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() {
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);
80 if (PlatformUI.isWorkbenchRunning()) {
81 IFocusService service = (IFocusService) PlatformUI.getWorkbench().getService(IFocusService.class);
82 service.addFocusTracker(searchText, SEARCH_TEXT_FOCUS_CONTROL_ID);
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");
92 GridLayoutFactory.swtDefaults().margins(0, 0).spacing(0, 0).numColumns(3).applyTo(this);
94 GridLayoutFactory.swtDefaults().margins(0, 0).spacing(0, 0).numColumns(2).applyTo(this);
97 Label filler = new Label(this, SWT.NONE);
98 GridDataFactory.swtDefaults().grab(true, false).minSize(3, SWT.DEFAULT).applyTo(filler);
102 final Button b = new Button(this, SWT.PUSH);
103 // b.setText("Search by 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() {
110 public void widgetSelected(SelectionEvent e) {
111 nameQuery(((e.stateMask & SWT.CTRL) == 0) ? ResultBrowser.VIEW : ResultBrowser.EDITOR);
115 final Button b2 = new Button(this, SWT.PUSH);
117 // b2.setText("Search by Type");
118 b2.setToolTipText("Search by Type");
119 GridDataFactory.fillDefaults().applyTo(b2);
121 b2.setImage(resourceManager.createImage(Activator.getImageDescriptor("img/magnifier.png")));
122 b2.addSelectionListener(new SelectionAdapter() {
124 public void widgetSelected(SelectionEvent e) {
125 typeQuery(((e.stateMask & SWT.CTRL) == 0) ? ResultBrowser.VIEW : ResultBrowser.EDITOR);
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() {
136 public void widgetDisposed(DisposeEvent e) {
137 shell.setData(KEY_TRIM, null);
142 protected void nameQuery(ISearchService.ResultBrowser browserType) {
145 String originalInput = searchText.getText();
146 String query = originalInput;
148 // Don't allow empty queries
149 if (query.trim().isEmpty())
152 query = filter(query);
154 SearchQuery searchQuery = new SearchQuery(originalInput);
155 searchQuery.setSearchFlag(Dependencies.FIELD_NAME_SEARCH, "on");
156 performQuery(searchQuery, browserType);
159 protected void typeQuery(ISearchService.ResultBrowser browserType) {
162 String originalInput = searchText.getText();
163 String query = originalInput;
165 // Don't allow empty queries
166 if (query.trim().isEmpty())
169 SearchQuery searchQuery = new SearchQuery(originalInput);
170 searchQuery.setSearchFlag(Dependencies.FIELD_TYPES_SEARCH, "on");
171 performQuery(searchQuery, browserType);
174 protected void nameAndTypeQuery(ISearchService.ResultBrowser browserType) {
177 String originalInput = searchText.getText();
178 String query = originalInput;
180 // Don't allow empty queries
181 if (query.trim().isEmpty())
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);
190 protected String filter(String query) {
192 if (!query.endsWith("*"))
194 // ''' characters cannot be used in the query string, replace them with '"'
195 query = query.replaceAll("'", "\"");
199 protected void performQuery(SearchQuery query, ISearchService.ResultBrowser browserType) {
200 ISearchService searchService = (ISearchService)PlatformUI.getWorkbench().getService(ISearchService.class);
201 searchService.performQuery(query, browserType, true);