-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.diagram.symbollibrary.ui;\r
-\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.layout.GridLayoutFactory;\r
-import org.eclipse.jface.resource.FontDescriptor;\r
-import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.jface.resource.JFaceResources;\r
-import org.eclipse.jface.resource.LocalResourceManager;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.accessibility.ACC;\r
-import org.eclipse.swt.accessibility.AccessibleAdapter;\r
-import org.eclipse.swt.accessibility.AccessibleControlAdapter;\r
-import org.eclipse.swt.accessibility.AccessibleControlEvent;\r
-import org.eclipse.swt.accessibility.AccessibleEvent;\r
-import org.eclipse.swt.events.MouseAdapter;\r
-import org.eclipse.swt.events.MouseEvent;\r
-import org.eclipse.swt.events.MouseMoveListener;\r
-import org.eclipse.swt.events.MouseTrackListener;\r
-import org.eclipse.swt.events.SelectionAdapter;\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.graphics.Image;\r
-import org.eclipse.swt.graphics.Point;\r
-import org.eclipse.swt.layout.GridData;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.swt.widgets.Label;\r
-import org.eclipse.swt.widgets.Text;\r
-import org.eclipse.ui.ISharedImages;\r
-import org.eclipse.ui.internal.WorkbenchImages;\r
-import org.eclipse.ui.internal.WorkbenchMessages;\r
-\r
-@SuppressWarnings("restriction")\r
-public class FilterArea extends Composite {\r
-\r
- private final LocalResourceManager resourceManager;\r
-\r
- private Text filterText;\r
-\r
- /**\r
- * The control representing the clear button for the filter text entry. This\r
- * value may be <code>null</code> if no such button exists, or if the\r
- * controls have not yet been created.\r
- */\r
- protected Control clearButtonControl;\r
-\r
- /**\r
- * Construct the filter area UI component.\r
- * \r
- * @param explorer\r
- * @param queryProcessor\r
- * @param parent\r
- * @param style\r
- */\r
- public FilterArea(final Composite parent, int style) {\r
- super(parent, style | SWT.BORDER);\r
-\r
- resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()), this);\r
-\r
-// GridDataFactory.fillDefaults().grab(true, false).applyTo(this);\r
-// GridLayoutFactory.fillDefaults().applyTo(this);\r
-//\r
-// filterText = new Text(this, SWT.BORDER | SWT.SEARCH | SWT.ICON_CANCEL);\r
-// filterText.setFont(resourceManager.createFont(FontDescriptor.createFrom(filterText.getFont()).increaseHeight(-1)));\r
-// filterText.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.BEGINNING).create());\r
-\r
- GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(this);\r
- GridLayoutFactory.fillDefaults().margins(0, 0).spacing(0, 0).numColumns(2).equalWidth(false).applyTo(this);\r
-\r
- createFilterText(this);\r
- createFilterCancelIcon(this);\r
-\r
- this.setBackground(filterText.getBackground());\r
- }\r
-\r
- private void createFilterText(FilterArea filterArea) {\r
- //filterText = new Text(this, SWT.SINGLE | SWT.FLAT | SWT.SEARCH | SWT.ICON_CANCEL);\r
- filterText = new Text(this, SWT.SINGLE | SWT.ICON_CANCEL);\r
- GridDataFactory.fillDefaults().grab(true, false).applyTo(filterText);\r
-\r
- filterText.setFont(resourceManager.createFont(FontDescriptor.createFrom(filterText.getFont()).increaseHeight(-1)));\r
-\r
- // if we're using a field with built in cancel we need to listen for\r
- // default selection changes (which tell us the cancel button has been\r
- // pressed)\r
- if ((filterText.getStyle() & SWT.ICON_CANCEL) != 0) {\r
- filterText.addSelectionListener(new SelectionAdapter() {\r
- @Override\r
- public void widgetDefaultSelected(SelectionEvent e) {\r
- if (e.detail == SWT.ICON_CANCEL)\r
- clearText();\r
- }\r
- });\r
- }\r
-\r
- GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, true);\r
-\r
- // if the text widget supported cancel then it will have it's own\r
- // integrated button. We can take all of the space.\r
- if ((filterText.getStyle() & SWT.ICON_CANCEL) != 0)\r
- gridData.horizontalSpan = 2;\r
- filterText.setLayoutData(gridData);\r
- }\r
-\r
- private void createFilterCancelIcon(Composite parent) {\r
- // only create the button if the text widget doesn't support one\r
- // natively\r
- if ((filterText.getStyle() & SWT.ICON_CANCEL) == 0) {\r
- final Image inactiveImage = WorkbenchImages.getImage(ISharedImages.IMG_ETOOL_CLEAR_DISABLED);\r
- final Image activeImage = WorkbenchImages.getImage(ISharedImages.IMG_ETOOL_CLEAR);\r
- final Image pressedImage = (Image) resourceManager.get( ImageDescriptor.createWithFlags( ImageDescriptor.createFromImage( activeImage ), SWT.IMAGE_GRAY ) );\r
-\r
- final Label clearButton= new Label(parent, SWT.NONE);\r
- clearButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));\r
- clearButton.setImage(inactiveImage);\r
- clearButton.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));\r
- clearButton.setToolTipText(WorkbenchMessages.FilteredTree_ClearToolTip);\r
- clearButton.addMouseListener(new MouseAdapter() {\r
- private MouseMoveListener fMoveListener;\r
-\r
- @Override\r
- public void mouseDown(MouseEvent e) {\r
- clearButton.setImage(pressedImage);\r
- fMoveListener= new MouseMoveListener() {\r
- private boolean fMouseInButton= true;\r
-\r
- public void mouseMove(MouseEvent e) {\r
- boolean mouseInButton= isMouseInButton(e);\r
- if (mouseInButton != fMouseInButton) {\r
- fMouseInButton= mouseInButton;\r
- clearButton.setImage(mouseInButton ? pressedImage : inactiveImage);\r
- }\r
- }\r
- };\r
- clearButton.addMouseMoveListener(fMoveListener);\r
- }\r
-\r
- @Override\r
- public void mouseUp(MouseEvent e) {\r
- if (fMoveListener != null) {\r
- clearButton.removeMouseMoveListener(fMoveListener);\r
- fMoveListener= null;\r
- boolean mouseInButton= isMouseInButton(e);\r
- clearButton.setImage(mouseInButton ? activeImage : inactiveImage);\r
- if (mouseInButton) {\r
- clearText();\r
- filterText.setFocus();\r
- }\r
- }\r
- }\r
-\r
- private boolean isMouseInButton(MouseEvent e) {\r
- Point buttonSize = clearButton.getSize();\r
- return 0 <= e.x && e.x < buttonSize.x && 0 <= e.y && e.y < buttonSize.y;\r
- }\r
- });\r
- clearButton.addMouseTrackListener(new MouseTrackListener() {\r
- public void mouseEnter(MouseEvent e) {\r
- clearButton.setImage(activeImage);\r
- }\r
-\r
- public void mouseExit(MouseEvent e) {\r
- clearButton.setImage(inactiveImage);\r
- }\r
-\r
- public void mouseHover(MouseEvent e) {\r
- }\r
- });\r
- clearButton.getAccessible().addAccessibleListener(\r
- new AccessibleAdapter() {\r
- @Override\r
- public void getName(AccessibleEvent e) {\r
- e.result= WorkbenchMessages.FilteredTree_AccessibleListenerClearButton;\r
- }\r
- });\r
- clearButton.getAccessible().addAccessibleControlListener(\r
- new AccessibleControlAdapter() {\r
- @Override\r
- public void getRole(AccessibleControlEvent e) {\r
- e.detail= ACC.ROLE_PUSHBUTTON;\r
- }\r
- });\r
- this.clearButtonControl = clearButton;\r
- }\r
- }\r
-\r
- /**\r
- * Clears the text in the filter text widget.\r
- */\r
- protected void clearText() {\r
- filterText.setText(""); //$NON-NLS-1$\r
- //textChanged();\r
- }\r
-\r
- public Text getText() {\r
- return filterText;\r
- }\r
-\r
- public void focus() {\r
- if (filterText.isDisposed())\r
- return;\r
- Display d = filterText.getDisplay();\r
- if (d.getThread() == Thread.currentThread()) {\r
- doSetFocus();\r
- } else {\r
- d.asyncExec(new Runnable() {\r
- @Override\r
- public void run() {\r
- if (filterText.isDisposed())\r
- return;\r
- doSetFocus();\r
- }\r
- });\r
- }\r
- }\r
-\r
- protected void doSetFocus() {\r
- filterText.selectAll();\r
- filterText.setFocus();\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.diagram.symbollibrary.ui;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.jface.resource.FontDescriptor;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.resource.LocalResourceManager;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.accessibility.ACC;
+import org.eclipse.swt.accessibility.AccessibleAdapter;
+import org.eclipse.swt.accessibility.AccessibleControlAdapter;
+import org.eclipse.swt.accessibility.AccessibleControlEvent;
+import org.eclipse.swt.accessibility.AccessibleEvent;
+import org.eclipse.swt.events.MouseAdapter;
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.events.MouseMoveListener;
+import org.eclipse.swt.events.MouseTrackListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.ISharedImages;
+import org.eclipse.ui.internal.WorkbenchImages;
+import org.eclipse.ui.internal.WorkbenchMessages;
+
+@SuppressWarnings("restriction")
+public class FilterArea extends Composite {
+
+ private final LocalResourceManager resourceManager;
+
+ private Text filterText;
+
+ /**
+ * The control representing the clear button for the filter text entry. This
+ * value may be <code>null</code> if no such button exists, or if the
+ * controls have not yet been created.
+ */
+ protected Control clearButtonControl;
+
+ /**
+ * Construct the filter area UI component.
+ *
+ * @param explorer
+ * @param queryProcessor
+ * @param parent
+ * @param style
+ */
+ public FilterArea(final Composite parent, int style) {
+ super(parent, style | SWT.BORDER);
+
+ resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()), this);
+
+// GridDataFactory.fillDefaults().grab(true, false).applyTo(this);
+// GridLayoutFactory.fillDefaults().applyTo(this);
+//
+// filterText = new Text(this, SWT.BORDER | SWT.SEARCH | SWT.ICON_CANCEL);
+// filterText.setFont(resourceManager.createFont(FontDescriptor.createFrom(filterText.getFont()).increaseHeight(-1)));
+// filterText.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.BEGINNING).create());
+
+ GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(this);
+ GridLayoutFactory.fillDefaults().margins(0, 0).spacing(0, 0).numColumns(2).equalWidth(false).applyTo(this);
+
+ createFilterText(this);
+ createFilterCancelIcon(this);
+
+ this.setBackground(filterText.getBackground());
+ }
+
+ private void createFilterText(FilterArea filterArea) {
+ //filterText = new Text(this, SWT.SINGLE | SWT.FLAT | SWT.SEARCH | SWT.ICON_CANCEL);
+ filterText = new Text(this, SWT.SINGLE | SWT.ICON_CANCEL);
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(filterText);
+
+ filterText.setFont(resourceManager.createFont(FontDescriptor.createFrom(filterText.getFont()).increaseHeight(-1)));
+
+ // if we're using a field with built in cancel we need to listen for
+ // default selection changes (which tell us the cancel button has been
+ // pressed)
+ if ((filterText.getStyle() & SWT.ICON_CANCEL) != 0) {
+ filterText.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetDefaultSelected(SelectionEvent e) {
+ if (e.detail == SWT.ICON_CANCEL)
+ clearText();
+ }
+ });
+ }
+
+ GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, true);
+
+ // if the text widget supported cancel then it will have it's own
+ // integrated button. We can take all of the space.
+ if ((filterText.getStyle() & SWT.ICON_CANCEL) != 0)
+ gridData.horizontalSpan = 2;
+ filterText.setLayoutData(gridData);
+ }
+
+ private void createFilterCancelIcon(Composite parent) {
+ // only create the button if the text widget doesn't support one
+ // natively
+ if ((filterText.getStyle() & SWT.ICON_CANCEL) == 0) {
+ final Image inactiveImage = WorkbenchImages.getImage(ISharedImages.IMG_ETOOL_CLEAR_DISABLED);
+ final Image activeImage = WorkbenchImages.getImage(ISharedImages.IMG_ETOOL_CLEAR);
+ final Image pressedImage = (Image) resourceManager.get( ImageDescriptor.createWithFlags( ImageDescriptor.createFromImage( activeImage ), SWT.IMAGE_GRAY ) );
+
+ final Label clearButton= new Label(parent, SWT.NONE);
+ clearButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
+ clearButton.setImage(inactiveImage);
+ clearButton.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
+ clearButton.setToolTipText(WorkbenchMessages.FilteredTree_ClearToolTip);
+ clearButton.addMouseListener(new MouseAdapter() {
+ private MouseMoveListener fMoveListener;
+
+ @Override
+ public void mouseDown(MouseEvent e) {
+ clearButton.setImage(pressedImage);
+ fMoveListener= new MouseMoveListener() {
+ private boolean fMouseInButton= true;
+
+ public void mouseMove(MouseEvent e) {
+ boolean mouseInButton= isMouseInButton(e);
+ if (mouseInButton != fMouseInButton) {
+ fMouseInButton= mouseInButton;
+ clearButton.setImage(mouseInButton ? pressedImage : inactiveImage);
+ }
+ }
+ };
+ clearButton.addMouseMoveListener(fMoveListener);
+ }
+
+ @Override
+ public void mouseUp(MouseEvent e) {
+ if (fMoveListener != null) {
+ clearButton.removeMouseMoveListener(fMoveListener);
+ fMoveListener= null;
+ boolean mouseInButton= isMouseInButton(e);
+ clearButton.setImage(mouseInButton ? activeImage : inactiveImage);
+ if (mouseInButton) {
+ clearText();
+ filterText.setFocus();
+ }
+ }
+ }
+
+ private boolean isMouseInButton(MouseEvent e) {
+ Point buttonSize = clearButton.getSize();
+ return 0 <= e.x && e.x < buttonSize.x && 0 <= e.y && e.y < buttonSize.y;
+ }
+ });
+ clearButton.addMouseTrackListener(new MouseTrackListener() {
+ public void mouseEnter(MouseEvent e) {
+ clearButton.setImage(activeImage);
+ }
+
+ public void mouseExit(MouseEvent e) {
+ clearButton.setImage(inactiveImage);
+ }
+
+ public void mouseHover(MouseEvent e) {
+ }
+ });
+ clearButton.getAccessible().addAccessibleListener(
+ new AccessibleAdapter() {
+ @Override
+ public void getName(AccessibleEvent e) {
+ e.result= WorkbenchMessages.FilteredTree_AccessibleListenerClearButton;
+ }
+ });
+ clearButton.getAccessible().addAccessibleControlListener(
+ new AccessibleControlAdapter() {
+ @Override
+ public void getRole(AccessibleControlEvent e) {
+ e.detail= ACC.ROLE_PUSHBUTTON;
+ }
+ });
+ this.clearButtonControl = clearButton;
+ }
+ }
+
+ /**
+ * Clears the text in the filter text widget.
+ */
+ protected void clearText() {
+ filterText.setText(""); //$NON-NLS-1$
+ //textChanged();
+ }
+
+ public Text getText() {
+ return filterText;
+ }
+
+ public void focus() {
+ if (filterText.isDisposed())
+ return;
+ Display d = filterText.getDisplay();
+ if (d.getThread() == Thread.currentThread()) {
+ doSetFocus();
+ } else {
+ d.asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ if (filterText.isDisposed())
+ return;
+ doSetFocus();
+ }
+ });
+ }
+ }
+
+ protected void doSetFocus() {
+ filterText.selectAll();
+ filterText.setFocus();
+ }
+
+}