3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.bulkeditor
;
13 import java
.util
.Comparator
;
15 import org
.eclipse
.swt
.SWT
;
16 import org
.eclipse
.swt
.events
.FocusEvent
;
17 import org
.eclipse
.swt
.events
.FocusListener
;
18 import org
.eclipse
.swt
.events
.KeyAdapter
;
19 import org
.eclipse
.swt
.events
.KeyEvent
;
20 import org
.eclipse
.swt
.events
.SelectionAdapter
;
21 import org
.eclipse
.swt
.events
.SelectionEvent
;
22 import org
.eclipse
.swt
.layout
.GridData
;
23 import org
.eclipse
.swt
.layout
.GridLayout
;
24 import org
.eclipse
.swt
.widgets
.Button
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Label
;
27 import org
.eclipse
.swt
.widgets
.Text
;
28 import org
.eclipse
.ui
.PlatformUI
;
29 import org
.eclipse
.ui
.swt
.IFocusService
;
31 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
32 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
33 import eu
.etaxonomy
.taxeditor
.store
.SearchManager
;
42 public class BulkEditorSearch
{
44 private static final String SEARCH
= "Search";
46 private static final String DEFAULT_TEXT
= String
.format("Use \'%s\' for wildcard searching", SearchManager
.WILDCARD
);
48 private final BulkEditor editor
;
51 private BulkEditorSortCombo sortCombo
;
53 private Button button
;
56 public Object ORDER_BY
= new Object();
58 public BulkEditorSearch(BulkEditor editor
, Composite parent
, int style
) {
61 createControl(parent
, style
);
65 * Creates the search control.
67 protected void createControl(Composite parent
, int style
) {
69 final Composite container
= new Composite(parent
, style
);
70 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
71 container
.setLayoutData(gridData
);
72 container
.setLayout(new GridLayout(5, false));
74 createSearchTextField(container
, SWT
.BORDER
| SWT
.SINGLE
| SWT
.FULL_SELECTION
);
76 createSortCombo(container
, style
);
78 button
= new Button(container
, SWT
.PUSH
);
79 button
.setText(SEARCH
);
80 button
.addSelectionListener(new SelectionAdapter() {
82 public void widgetSelected(SelectionEvent e
) {
87 registerAtFocusService();
90 private void createSortCombo(Composite parent
, int style
) {
91 sortCombo
= new BulkEditorSortCombo(parent
, editor
.getEditorInput().getSortProviders());
96 * Handles focus changes for the textfield.
98 private void registerAtFocusService() {
99 IFocusService focusService
=
100 (IFocusService
) PlatformUI
.getWorkbench().getService(IFocusService
.class);
101 if (focusService
!= null) {
102 focusService
.addFocusTracker(text
, "bulkeditor.textControlId");
108 * Creates the search textfield.
110 private void createSearchTextField(Composite parent
, int style
) {
111 final Label label
= new Label(parent
, SWT
.NONE
);
112 label
.setText("Title Cache");
114 text
= new Text(parent
, style
);
115 text
.setText(DEFAULT_TEXT
);
116 text
.setForeground(BulkEditorUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
118 text
.addFocusListener(new FocusListener() {
121 public void focusGained(FocusEvent e
) {
122 text
.setForeground(BulkEditorUtil
.getColor(Resources
.SEARCH_VIEW_FOCUS
));
123 if (DEFAULT_TEXT
.equals(text
.getText())) {
129 public void focusLost(FocusEvent e
) {
130 if (CdmUtils
.isEmpty(text
.getText())) {
131 text
.setForeground(BulkEditorUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
132 text
.setText(DEFAULT_TEXT
);
137 text
.addKeyListener(new KeyAdapter() {
139 public void keyReleased(KeyEvent e
) {
140 if (e
.keyCode
== SWT
.CR
) {
146 GridData gridData
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
147 text
.setLayoutData(gridData
);
152 * Shows the results of the search.
154 private void updateEditorInput() {
156 String searchString
= getSearchString().trim();
158 if(DEFAULT_TEXT
.equals(searchString
) || CdmUtils
.isBlank(searchString
)){
162 BulkEditorQuery query
= new BulkEditorQuery(searchString
, getComparator());
163 editor
.performSearch(query
);
167 * Returns the current string in the search textfield.
168 * @return the content of the textfield
170 public String
getSearchString() {
171 return text
.getText().trim();
174 public Comparator
getComparator() {
175 return sortCombo
.getSelection();
178 public void setFocus() {
179 if(text
!= null && ! text
.isDisposed()){