Using Eclipse SWT.SEARCH

After reading Prakash G.R. great blog posts (here and here) about the Eclipse search field I have submitted some updates to the algorithm search in JCrypTool. And noticed some additional things which are in my eyes quite important when using SWT.SEARCH.

What happens in JCrypTool when using the algorithm search is that the Algorithm view gets updated in real time, so the search works as a filter. When clicking the x icon the filter should be set to empty, and the view must display all elements again. But the default clear action is to empty only the search field. Fortunately this is as easy to extend as mentioned in Eclipse Javadoc:


Text search = new Text(parent, SWT.SEARCH | SWT.CANCEL);
search.addSelectionListener(new SelectionAdapter() {
 public void widgetDefaultSelected(SelectionEvent e) {
  if (e.detail == SWT.CANCEL) {
    // action when clicking on the x icon
  }
 }
});

You have to use the widgetDefaultSelected method in case you need to do more than just clearing the user input. Remember that the SWT.CANCEL check is only possible when using SWT.SEARCH | SWT.CANCEL as options for the text field.

The really sad thing is (at least for Windows users), such a stylish text field is only available on Mac, but not on Windows (at least Windows XP does not support it). Since I am a happy Mac user I have no idea about Linux. At least the search text field is shown as a normal text field when it comes to Windows (better than being invisible or unusable at all). So it is necessary to provide an additional clear icon when the operating system does not support the clear action inside the text field. Simply implement the following check after creating the search text field:

if ((search.getStyle() && SWT.CANCEL) == 0) { }

Put all actions and GUI elements for antiquated operating systems in this block and you are done. Since the return code on Mac is different this part won't be executed on Mac systems and therefore only the stylish text field is visible.

Update, 2009-07-19:
With Eclipse 3.5 you should initialize the search text field in this way:

Text search = new Text(parent, SWT.SEARCH | SWT.ICON_CANCEL | SWT.ICON_SEARCH);

On Mac this offers an additional search icon in the text field. The Eclipse 3.5 M6 News contain the information, that this style is now available on all platforms. I wasn't able to confirm this with JCrypTool, the only supported platform I'm aware of is the Mac.


Posted

in

,

by

Tags:

Comments

One response to “Using Eclipse SWT.SEARCH”

  1. Thanks. The evaluation of the selection listener was something I need in this moment. Simple enough, good soluton.