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() {
243 Map<NodeContext, AtomicInteger> modCount = new HashMap<NodeContext, AtomicInteger>();
246 public void modifyText(ModifyEvent e) {
248 final NodeContext context = getFilteredNode();
252 final String filter = filterText.getText();
253 //System.out.println("Scheduling setFilter(" + context + ", " + filter + ")");
255 AtomicInteger i = modCount.get(context);
257 modCount.put(context, new AtomicInteger());
258 final AtomicInteger counter = modCount.get(context);
259 final int count = counter.incrementAndGet();
261 ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {
264 int newCount = counter.get();
265 if (newCount != count)
267 //System.out.println("schedule setFilter(" + context + ", " + filter + ")");
268 modCount.remove(context);
271 ThreadUtils.asyncExec(SWTThread.getThreadAccess(getDisplay()), new Runnable() {
276 //System.out.println("queryProcessor.setFilter(" + context + ", " + filter + ")");
277 queryProcessor.setFilter(context, filter.isEmpty() ? null : filter);
281 }, FILTER_DELAY, TimeUnit.MILLISECONDS);
286 protected void addExplorerSelectionListener() {
287 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
288 selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {
290 public void selectionChanged(SelectionChangedEvent event) {
292 ISelection selection = event.getSelection();
293 NodeContext context = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, NodeContext.class);
294 if(context == null) {
295 context = ISelectionUtils.filterSingleSelection(selection, NodeContext.class);
298 if (context == null) {
299 context = explorer.getRoot();
302 assert (context != null);
304 String filter = queryProcessor.getFilter(context);
309 //filterText.setData(null);
310 //System.out.println("selection changed, new filter: " + filter + " for context " + context + ", currentContext=" + currentContext);
311 currentContext = context;
312 filterText.setText(filter);
313 //filterText.setData("context", context);
318 protected NodeContext getFilteredNode() {
319 if(currentContext != null) return currentContext;
320 else return explorer.getRoot();
321 // return currentContext;
322 // NodeContext context = (NodeContext)filterText.getData("context");
323 // if (context == null)
324 // context = explorer.getRoot();
330 public void focus() {
331 if (filterText.isDisposed())
333 Display d = filterText.getDisplay();
334 if (d.getThread() == Thread.currentThread()) {
337 d.asyncExec(new Runnable() {
340 if (filterText.isDisposed())
349 public void dispose() {
354 resourceManager.dispose();
356 queryProcessor = null;
358 currentContext = null;
359 clearButtonControl = null;
365 protected void doSetFocus() {
366 filterText.selectAll();
367 filterText.setFocus();
371 public IFilterArea getFilterArea() {