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.browsing.ui.swt;
14 import java.util.HashMap;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.atomic.AtomicInteger;
19 import org.eclipse.jface.layout.GridDataFactory;
20 import org.eclipse.jface.layout.GridLayoutFactory;
21 import org.eclipse.jface.resource.FontDescriptor;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.resource.JFaceResources;
24 import org.eclipse.jface.resource.LocalResourceManager;
25 import org.eclipse.jface.viewers.IPostSelectionProvider;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.ISelectionChangedListener;
28 import org.eclipse.jface.viewers.SelectionChangedEvent;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.accessibility.ACC;
31 import org.eclipse.swt.accessibility.AccessibleAdapter;
32 import org.eclipse.swt.accessibility.AccessibleControlAdapter;
33 import org.eclipse.swt.accessibility.AccessibleControlEvent;
34 import org.eclipse.swt.accessibility.AccessibleEvent;
35 import org.eclipse.swt.events.ModifyEvent;
36 import org.eclipse.swt.events.ModifyListener;
37 import org.eclipse.swt.events.MouseAdapter;
38 import org.eclipse.swt.events.MouseEvent;
39 import org.eclipse.swt.events.MouseMoveListener;
40 import org.eclipse.swt.events.MouseTrackListener;
41 import org.eclipse.swt.events.SelectionAdapter;
42 import org.eclipse.swt.events.SelectionEvent;
43 import org.eclipse.swt.graphics.Image;
44 import org.eclipse.swt.graphics.Point;
45 import org.eclipse.swt.layout.GridData;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.swt.widgets.Display;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.Text;
51 import org.eclipse.ui.ISharedImages;
52 import org.eclipse.ui.internal.WorkbenchImages;
53 import org.eclipse.ui.internal.WorkbenchMessages;
54 import org.simantics.browsing.ui.GraphExplorer;
55 import org.simantics.browsing.ui.NodeContext;
56 import org.simantics.browsing.ui.common.processors.FilterSelectionRequestQueryProcessor;
57 import org.simantics.browsing.ui.common.views.IFilterArea;
58 import org.simantics.browsing.ui.common.views.IFilterAreaProvider;
59 import org.simantics.browsing.ui.common.views.IFocusable;
60 import org.simantics.db.layer0.SelectionHints;
61 import org.simantics.utils.threads.SWTThread;
62 import org.simantics.utils.threads.ThreadUtils;
63 import org.simantics.utils.ui.ISelectionUtils;
65 @SuppressWarnings("restriction")
66 public class FilterArea extends Composite implements IFocusable, IFilterArea, IFilterAreaProvider {
68 protected static final Integer FILTER_DELAY = 500;
70 private final LocalResourceManager resourceManager;
72 protected GraphExplorer explorer;
74 protected FilterSelectionRequestQueryProcessor queryProcessor;
76 private Text filterText;
77 private NodeContext currentContext;
80 * The control representing the clear button for the filter text entry. This
81 * value may be <code>null</code> if no such button exists, or if the
82 * controls have not yet been created.
84 protected Control clearButtonControl;
87 * Construct the filter area UI component.
90 * @param queryProcessor
94 public FilterArea(final GraphExplorer explorer, final FilterSelectionRequestQueryProcessor queryProcessor, Composite parent, int style) {
95 super(parent, style | SWT.BORDER);
97 this.explorer = explorer;
98 this.queryProcessor = queryProcessor;
100 resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()), this);
102 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(this);
103 GridLayoutFactory.fillDefaults().margins(0, 0).spacing(0, 0).numColumns(2).equalWidth(false).applyTo(this);
105 createFilterText(this);
106 createFilterCancelIcon(this);
108 this.setBackground(filterText.getBackground());
110 addTextModifyListener();
111 addExplorerSelectionListener();
114 private void createFilterText(FilterArea filterArea) {
115 //filterText = new Text(this, SWT.SINGLE | SWT.FLAT | SWT.SEARCH | SWT.ICON_CANCEL);
116 filterText = new Text(this, SWT.SINGLE | SWT.ICON_CANCEL);
117 GridDataFactory.fillDefaults().grab(true, false).applyTo(filterText);
119 filterText.setFont(resourceManager.createFont(FontDescriptor.createFrom(filterText.getFont()).increaseHeight(-1)));
121 // if we're using a field with built in cancel we need to listen for
122 // default selection changes (which tell us the cancel button has been
124 if ((filterText.getStyle() & SWT.ICON_CANCEL) != 0) {
125 filterText.addSelectionListener(new SelectionAdapter() {
127 public void widgetDefaultSelected(SelectionEvent e) {
128 if (e.detail == SWT.ICON_CANCEL)
134 GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, true);
136 // if the text widget supported cancel then it will have it's own
137 // integrated button. We can take all of the space.
138 if ((filterText.getStyle() & SWT.ICON_CANCEL) != 0)
139 gridData.horizontalSpan = 2;
140 filterText.setLayoutData(gridData);
143 private void createFilterCancelIcon(Composite parent) {
144 // only create the button if the text widget doesn't support one
146 if ((filterText.getStyle() & SWT.ICON_CANCEL) == 0) {
147 final Image inactiveImage = WorkbenchImages.getImage(ISharedImages.IMG_ETOOL_CLEAR_DISABLED);
148 final Image activeImage = WorkbenchImages.getImage(ISharedImages.IMG_ETOOL_CLEAR);
149 final Image pressedImage = (Image) resourceManager.get( ImageDescriptor.createWithFlags( ImageDescriptor.createFromImage( activeImage ), SWT.IMAGE_GRAY ) );
151 final Label clearButton= new Label(parent, SWT.NONE);
152 clearButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
153 clearButton.setImage(inactiveImage);
154 clearButton.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
155 clearButton.setToolTipText(WorkbenchMessages.FilteredTree_ClearToolTip);
156 clearButton.addMouseListener(new MouseAdapter() {
157 private MouseMoveListener fMoveListener;
160 public void mouseDown(MouseEvent e) {
161 clearButton.setImage(pressedImage);
162 fMoveListener= new MouseMoveListener() {
163 private boolean fMouseInButton= true;
166 public void mouseMove(MouseEvent e) {
167 boolean mouseInButton= isMouseInButton(e);
168 if (mouseInButton != fMouseInButton) {
169 fMouseInButton= mouseInButton;
170 clearButton.setImage(mouseInButton ? pressedImage : inactiveImage);
174 clearButton.addMouseMoveListener(fMoveListener);
178 public void mouseUp(MouseEvent e) {
179 if (fMoveListener != null) {
180 clearButton.removeMouseMoveListener(fMoveListener);
182 boolean mouseInButton= isMouseInButton(e);
183 clearButton.setImage(mouseInButton ? activeImage : inactiveImage);
186 filterText.setFocus();
191 private boolean isMouseInButton(MouseEvent e) {
192 Point buttonSize = clearButton.getSize();
193 return 0 <= e.x && e.x < buttonSize.x && 0 <= e.y && e.y < buttonSize.y;
196 clearButton.addMouseTrackListener(new MouseTrackListener() {
198 public void mouseEnter(MouseEvent e) {
199 clearButton.setImage(activeImage);
203 public void mouseExit(MouseEvent e) {
204 clearButton.setImage(inactiveImage);
208 public void mouseHover(MouseEvent e) {
211 clearButton.getAccessible().addAccessibleListener(
212 new AccessibleAdapter() {
214 public void getName(AccessibleEvent e) {
215 e.result= WorkbenchMessages.FilteredTree_AccessibleListenerClearButton;
218 clearButton.getAccessible().addAccessibleControlListener(
219 new AccessibleControlAdapter() {
221 public void getRole(AccessibleControlEvent e) {
222 e.detail= ACC.ROLE_PUSHBUTTON;
225 this.clearButtonControl = clearButton;
230 * Clears the text in the filter text widget.
232 protected void clearText() {
233 //System.out.println("clearText");
234 filterText.setText(""); //$NON-NLS-1$
235 // TODO: HACK: this can be used to access filter for root context
236 explorer.select(null);
240 protected void addTextModifyListener() {
241 filterText.addModifyListener(new ModifyListener() {
245 public void modifyText(ModifyEvent e) {
246 final String filter = filterText.getText();
247 //System.out.println("Scheduling setFilter(" + context + ", " + filter + ")");
248 setFilter(filter, false);
253 private Map<NodeContext, AtomicInteger> modCount = new HashMap<NodeContext, AtomicInteger>();
256 public void setFilter(String filter) {
257 setFilter(filter, true);
260 protected void setFilter(String filter, boolean updateUI) {
261 final NodeContext context = getFilteredNode();
265 AtomicInteger i = modCount.get(context);
267 modCount.put(context, new AtomicInteger());
268 final AtomicInteger counter = modCount.get(context);
269 final int count = counter.incrementAndGet();
271 ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {
274 int newCount = counter.get();
275 if (newCount != count)
277 //System.out.println("schedule setFilter(" + context + ", " + filter + ")");
278 modCount.remove(context);
281 ThreadUtils.asyncExec(SWTThread.getThreadAccess(getDisplay()), new Runnable() {
286 applyFilter(context, filter, updateUI);
290 }, FILTER_DELAY, TimeUnit.MILLISECONDS);
293 protected void applyFilter(String filter) {
294 final NodeContext context = getFilteredNode();
296 applyFilter(context, filter, true);
299 protected void applyFilter(NodeContext context, String filter, boolean updateUI) {
301 String current = filterText.getText();
302 if (!current.equals(filter))
303 filterText.setText(filter);
305 //System.out.println("queryProcessor.setFilter(" + context + ", " + filter + ")");
306 queryProcessor.setFilter(context, filter.isEmpty() ? null : filter);
309 protected void addExplorerSelectionListener() {
310 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
311 selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {
313 public void selectionChanged(SelectionChangedEvent event) {
315 ISelection selection = event.getSelection();
316 NodeContext context = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, NodeContext.class);
317 if(context == null) {
318 context = ISelectionUtils.filterSingleSelection(selection, NodeContext.class);
321 if (context == null) {
322 context = explorer.getRoot();
325 assert (context != null);
327 String filter = queryProcessor.getFilter(context);
332 //filterText.setData(null);
333 //System.out.println("selection changed, new filter: " + filter + " for context " + context + ", currentContext=" + currentContext);
334 currentContext = context;
335 filterText.setText(filter);
336 //filterText.setData("context", context);
341 protected NodeContext getFilteredNode() {
342 if(currentContext != null) return currentContext;
343 else return explorer.getRoot();
344 // return currentContext;
345 // NodeContext context = (NodeContext)filterText.getData("context");
346 // if (context == null)
347 // context = explorer.getRoot();
353 public void focus() {
354 if (filterText.isDisposed())
356 Display d = filterText.getDisplay();
357 if (d.getThread() == Thread.currentThread()) {
360 d.asyncExec(new Runnable() {
363 if (filterText.isDisposed())
372 public void dispose() {
377 resourceManager.dispose();
379 queryProcessor = null;
381 currentContext = null;
382 clearButtonControl = null;
388 protected void doSetFocus() {
389 filterText.selectAll();
390 filterText.setFocus();
394 public IFilterArea getFilterArea() {