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.diagram.symbollibrary.ui;
14 import org.eclipse.jface.layout.GridDataFactory;
15 import org.eclipse.jface.layout.GridLayoutFactory;
16 import org.eclipse.jface.resource.FontDescriptor;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.resource.JFaceResources;
19 import org.eclipse.jface.resource.LocalResourceManager;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.accessibility.ACC;
22 import org.eclipse.swt.accessibility.AccessibleAdapter;
23 import org.eclipse.swt.accessibility.AccessibleControlAdapter;
24 import org.eclipse.swt.accessibility.AccessibleControlEvent;
25 import org.eclipse.swt.accessibility.AccessibleEvent;
26 import org.eclipse.swt.events.MouseAdapter;
27 import org.eclipse.swt.events.MouseEvent;
28 import org.eclipse.swt.events.MouseMoveListener;
29 import org.eclipse.swt.events.MouseTrackListener;
30 import org.eclipse.swt.events.SelectionAdapter;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.graphics.Image;
33 import org.eclipse.swt.graphics.Point;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Text;
40 import org.eclipse.ui.ISharedImages;
41 import org.eclipse.ui.internal.WorkbenchImages;
42 import org.eclipse.ui.internal.WorkbenchMessages;
44 @SuppressWarnings("restriction")
45 public class FilterArea extends Composite {
47 private final LocalResourceManager resourceManager;
49 private Text filterText;
52 * The control representing the clear button for the filter text entry. This
53 * value may be <code>null</code> if no such button exists, or if the
54 * controls have not yet been created.
56 protected Control clearButtonControl;
59 * Construct the filter area UI component.
62 * @param queryProcessor
66 public FilterArea(final Composite parent, int style) {
67 super(parent, style | SWT.BORDER);
69 resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()), this);
71 // GridDataFactory.fillDefaults().grab(true, false).applyTo(this);
72 // GridLayoutFactory.fillDefaults().applyTo(this);
74 // filterText = new Text(this, SWT.BORDER | SWT.SEARCH | SWT.ICON_CANCEL);
75 // filterText.setFont(resourceManager.createFont(FontDescriptor.createFrom(filterText.getFont()).increaseHeight(-1)));
76 // filterText.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.BEGINNING).create());
78 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(this);
79 GridLayoutFactory.fillDefaults().margins(0, 0).spacing(0, 0).numColumns(2).equalWidth(false).applyTo(this);
81 createFilterText(this);
82 createFilterCancelIcon(this);
84 this.setBackground(filterText.getBackground());
87 private void createFilterText(FilterArea filterArea) {
88 //filterText = new Text(this, SWT.SINGLE | SWT.FLAT | SWT.SEARCH | SWT.ICON_CANCEL);
89 filterText = new Text(this, SWT.SINGLE | SWT.ICON_CANCEL);
90 GridDataFactory.fillDefaults().grab(true, false).applyTo(filterText);
92 filterText.setFont(resourceManager.createFont(FontDescriptor.createFrom(filterText.getFont()).increaseHeight(-1)));
94 // if we're using a field with built in cancel we need to listen for
95 // default selection changes (which tell us the cancel button has been
97 if ((filterText.getStyle() & SWT.ICON_CANCEL) != 0) {
98 filterText.addSelectionListener(new SelectionAdapter() {
100 public void widgetDefaultSelected(SelectionEvent e) {
101 if (e.detail == SWT.ICON_CANCEL)
107 GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, true);
109 // if the text widget supported cancel then it will have it's own
110 // integrated button. We can take all of the space.
111 if ((filterText.getStyle() & SWT.ICON_CANCEL) != 0)
112 gridData.horizontalSpan = 2;
113 filterText.setLayoutData(gridData);
116 private void createFilterCancelIcon(Composite parent) {
117 // only create the button if the text widget doesn't support one
119 if ((filterText.getStyle() & SWT.ICON_CANCEL) == 0) {
120 final Image inactiveImage = WorkbenchImages.getImage(ISharedImages.IMG_ETOOL_CLEAR_DISABLED);
121 final Image activeImage = WorkbenchImages.getImage(ISharedImages.IMG_ETOOL_CLEAR);
122 final Image pressedImage = (Image) resourceManager.get( ImageDescriptor.createWithFlags( ImageDescriptor.createFromImage( activeImage ), SWT.IMAGE_GRAY ) );
124 final Label clearButton= new Label(parent, SWT.NONE);
125 clearButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
126 clearButton.setImage(inactiveImage);
127 clearButton.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
128 clearButton.setToolTipText(WorkbenchMessages.FilteredTree_ClearToolTip);
129 clearButton.addMouseListener(new MouseAdapter() {
130 private MouseMoveListener fMoveListener;
133 public void mouseDown(MouseEvent e) {
134 clearButton.setImage(pressedImage);
135 fMoveListener= new MouseMoveListener() {
136 private boolean fMouseInButton= true;
138 public void mouseMove(MouseEvent e) {
139 boolean mouseInButton= isMouseInButton(e);
140 if (mouseInButton != fMouseInButton) {
141 fMouseInButton= mouseInButton;
142 clearButton.setImage(mouseInButton ? pressedImage : inactiveImage);
146 clearButton.addMouseMoveListener(fMoveListener);
150 public void mouseUp(MouseEvent e) {
151 if (fMoveListener != null) {
152 clearButton.removeMouseMoveListener(fMoveListener);
154 boolean mouseInButton= isMouseInButton(e);
155 clearButton.setImage(mouseInButton ? activeImage : inactiveImage);
158 filterText.setFocus();
163 private boolean isMouseInButton(MouseEvent e) {
164 Point buttonSize = clearButton.getSize();
165 return 0 <= e.x && e.x < buttonSize.x && 0 <= e.y && e.y < buttonSize.y;
168 clearButton.addMouseTrackListener(new MouseTrackListener() {
169 public void mouseEnter(MouseEvent e) {
170 clearButton.setImage(activeImage);
173 public void mouseExit(MouseEvent e) {
174 clearButton.setImage(inactiveImage);
177 public void mouseHover(MouseEvent e) {
180 clearButton.getAccessible().addAccessibleListener(
181 new AccessibleAdapter() {
183 public void getName(AccessibleEvent e) {
184 e.result= WorkbenchMessages.FilteredTree_AccessibleListenerClearButton;
187 clearButton.getAccessible().addAccessibleControlListener(
188 new AccessibleControlAdapter() {
190 public void getRole(AccessibleControlEvent e) {
191 e.detail= ACC.ROLE_PUSHBUTTON;
194 this.clearButtonControl = clearButton;
199 * Clears the text in the filter text widget.
201 protected void clearText() {
202 filterText.setText(""); //$NON-NLS-1$
206 public Text getText() {
210 public void focus() {
211 if (filterText.isDisposed())
213 Display d = filterText.getDisplay();
214 if (d.getThread() == Thread.currentThread()) {
217 d.asyncExec(new Runnable() {
220 if (filterText.isDisposed())
228 protected void doSetFocus() {
229 filterText.selectAll();
230 filterText.setFocus();