2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.bulkeditor
;
12 import java
.util
.Comparator
;
14 import org
.eclipse
.swt
.SWT
;
15 import org
.eclipse
.swt
.events
.FocusEvent
;
16 import org
.eclipse
.swt
.events
.FocusListener
;
17 import org
.eclipse
.swt
.events
.KeyAdapter
;
18 import org
.eclipse
.swt
.events
.KeyEvent
;
19 import org
.eclipse
.swt
.events
.SelectionAdapter
;
20 import org
.eclipse
.swt
.events
.SelectionEvent
;
21 import org
.eclipse
.swt
.layout
.GridData
;
22 import org
.eclipse
.swt
.layout
.GridLayout
;
23 import org
.eclipse
.swt
.widgets
.Button
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Label
;
26 import org
.eclipse
.swt
.widgets
.Text
;
27 import org
.eclipse
.ui
.PlatformUI
;
28 import org
.eclipse
.ui
.swt
.IFocusService
;
30 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
31 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
32 import eu
.etaxonomy
.taxeditor
.store
.SearchManager
;
41 public class BulkEditorSearch
{
43 private static final String SEARCH
= "Search";
45 private static final String DEFAULT_TEXT
= String
.format("Use \'%s\' for wildcard searching", SearchManager
.WILDCARD
);
47 private final BulkEditor editor
;
50 private BulkEditorSortCombo sortCombo
;
52 private Button button
;
55 public Object ORDER_BY
= new Object();
57 public BulkEditorSearch(BulkEditor editor
, Composite parent
, int style
) {
60 createControl(parent
, style
);
64 * Creates the search control.
66 protected void createControl(Composite parent
, int style
) {
68 final Composite container
= new Composite(parent
, style
);
69 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
70 container
.setLayoutData(gridData
);
71 container
.setLayout(new GridLayout(5, false));
73 createSearchTextField(container
, SWT
.BORDER
| SWT
.SINGLE
| SWT
.FULL_SELECTION
);
75 createSortCombo(container
, style
);
77 button
= new Button(container
, SWT
.PUSH
);
78 button
.setText(SEARCH
);
79 button
.addSelectionListener(new SelectionAdapter() {
81 public void widgetSelected(SelectionEvent e
) {
86 registerAtFocusService();
89 private void createSortCombo(Composite parent
, int style
) {
90 sortCombo
= new BulkEditorSortCombo(parent
, editor
.getEditorInput().getSortProviders());
95 * Handles focus changes for the textfield.
97 private void registerAtFocusService() {
98 IFocusService focusService
=
99 (IFocusService
) PlatformUI
.getWorkbench().getService(IFocusService
.class);
100 if (focusService
!= null) {
101 focusService
.addFocusTracker(text
, "bulkeditor.textControlId");
107 * Creates the search textfield.
109 private void createSearchTextField(Composite parent
, int style
) {
110 final Label label
= new Label(parent
, SWT
.NONE
);
111 label
.setText("Title Cache");
113 text
= new Text(parent
, style
);
114 text
.setText(DEFAULT_TEXT
);
115 text
.setForeground(BulkEditorUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
117 text
.addFocusListener(new FocusListener() {
120 public void focusGained(FocusEvent e
) {
121 text
.setForeground(BulkEditorUtil
.getColor(Resources
.SEARCH_VIEW_FOCUS
));
122 if (DEFAULT_TEXT
.equals(text
.getText())) {
128 public void focusLost(FocusEvent e
) {
129 if (CdmUtils
.isEmpty(text
.getText())) {
130 text
.setForeground(BulkEditorUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
131 text
.setText(DEFAULT_TEXT
);
136 text
.addKeyListener(new KeyAdapter() {
138 public void keyReleased(KeyEvent e
) {
139 if (e
.keyCode
== SWT
.CR
) {
145 GridData gridData
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
146 text
.setLayoutData(gridData
);
151 * Shows the results of the search.
153 private void updateEditorInput() {
155 String searchString
= getSearchString().trim();
157 if(DEFAULT_TEXT
.equals(searchString
) || CdmUtils
.isBlank(searchString
)){
161 BulkEditorQuery query
= new BulkEditorQuery(searchString
, getComparator());
162 editor
.performSearch(query
);
166 * Returns the current string in the search textfield.
167 * @return the content of the textfield
169 public String
getSearchString() {
170 return text
.getText().trim();
173 public Comparator
getComparator() {
174 return sortCombo
.getSelection();
177 public void setFocus() {
178 if(text
!= null && ! text
.isDisposed()){