]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/ui/FilterArea.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbollibrary / ui / FilterArea.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.diagram.symbollibrary.ui;
13
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;
43
44 @SuppressWarnings("restriction")
45 public class FilterArea extends Composite {
46
47     private final LocalResourceManager resourceManager;
48
49     private Text                       filterText;
50
51     /**
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.
55      */
56     protected Control                  clearButtonControl;
57
58     /**
59      * Construct the filter area UI component.
60      * 
61      * @param explorer
62      * @param queryProcessor
63      * @param parent
64      * @param style
65      */
66     public FilterArea(final Composite parent, int style) {
67         super(parent, style | SWT.BORDER);
68
69         resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()), this);
70
71 //        GridDataFactory.fillDefaults().grab(true, false).applyTo(this);
72 //        GridLayoutFactory.fillDefaults().applyTo(this);
73 //
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());
77
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);
80
81         createFilterText(this);
82         createFilterCancelIcon(this);
83
84         this.setBackground(filterText.getBackground());
85     }
86
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);
91
92         filterText.setFont(resourceManager.createFont(FontDescriptor.createFrom(filterText.getFont()).increaseHeight(-1)));
93
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
96         // pressed)
97         if ((filterText.getStyle() & SWT.ICON_CANCEL) != 0) {
98             filterText.addSelectionListener(new SelectionAdapter() {
99                 @Override
100                 public void widgetDefaultSelected(SelectionEvent e) {
101                     if (e.detail == SWT.ICON_CANCEL)
102                         clearText();
103                 }
104             });
105         }
106
107         GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, true);
108
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);
114     }
115
116     private void createFilterCancelIcon(Composite parent) {
117         // only create the button if the text widget doesn't support one
118         // natively
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 ) );
123
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;
131
132                 @Override
133                 public void mouseDown(MouseEvent e) {
134                     clearButton.setImage(pressedImage);
135                     fMoveListener= new MouseMoveListener() {
136                         private boolean fMouseInButton= true;
137
138                         public void mouseMove(MouseEvent e) {
139                             boolean mouseInButton= isMouseInButton(e);
140                             if (mouseInButton != fMouseInButton) {
141                                 fMouseInButton= mouseInButton;
142                                 clearButton.setImage(mouseInButton ? pressedImage : inactiveImage);
143                             }
144                         }
145                     };
146                     clearButton.addMouseMoveListener(fMoveListener);
147                 }
148
149                 @Override
150                 public void mouseUp(MouseEvent e) {
151                     if (fMoveListener != null) {
152                         clearButton.removeMouseMoveListener(fMoveListener);
153                         fMoveListener= null;
154                         boolean mouseInButton= isMouseInButton(e);
155                         clearButton.setImage(mouseInButton ? activeImage : inactiveImage);
156                         if (mouseInButton) {
157                             clearText();
158                             filterText.setFocus();
159                         }
160                     }
161                 }
162
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;
166                 }
167             });
168             clearButton.addMouseTrackListener(new MouseTrackListener() {
169                 public void mouseEnter(MouseEvent e) {
170                     clearButton.setImage(activeImage);
171                 }
172
173                 public void mouseExit(MouseEvent e) {
174                     clearButton.setImage(inactiveImage);
175                 }
176
177                 public void mouseHover(MouseEvent e) {
178                 }
179             });
180             clearButton.getAccessible().addAccessibleListener(
181                     new AccessibleAdapter() {
182                         @Override
183                         public void getName(AccessibleEvent e) {
184                             e.result= WorkbenchMessages.FilteredTree_AccessibleListenerClearButton;
185                         }
186                     });
187             clearButton.getAccessible().addAccessibleControlListener(
188                     new AccessibleControlAdapter() {
189                         @Override
190                         public void getRole(AccessibleControlEvent e) {
191                             e.detail= ACC.ROLE_PUSHBUTTON;
192                         }
193                     });
194             this.clearButtonControl = clearButton;
195         }
196     }
197
198     /**
199      * Clears the text in the filter text widget.
200      */
201     protected void clearText() {
202         filterText.setText(""); //$NON-NLS-1$
203         //textChanged();
204     }
205
206     public Text getText() {
207         return filterText;
208     }
209
210     public void focus() {
211         if (filterText.isDisposed())
212             return;
213         Display d = filterText.getDisplay();
214         if (d.getThread() == Thread.currentThread()) {
215             doSetFocus();
216         } else {
217             d.asyncExec(new Runnable() {
218                 @Override
219                 public void run() {
220                     if (filterText.isDisposed())
221                         return;
222                     doSetFocus();
223                 }
224             });
225         }
226     }
227
228     protected void doSetFocus() {
229         filterText.selectAll();
230         filterText.setFocus();
231     }
232
233 }