Merge branch 'hotfix/3.7.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / BulkEditorSearch.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.bulkeditor;
12
13 import java.util.Comparator;
14
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;
30
31 import eu.etaxonomy.cdm.common.CdmUtils;
32 import eu.etaxonomy.taxeditor.preference.Resources;
33 import eu.etaxonomy.taxeditor.store.SearchManager;
34
35 /**
36 * <p>BulkEditorSearchComposite class.</p>
37 *
38 * @author p.ciardelli
39 * @author e.-m.lee
40 * @author n.hoffmann
41 * @created 17.08.2009
42 * @version 1.0
43 */
44 public class BulkEditorSearch {
45
46 /**
47 *
48 */
49 private static final String SEARCH = "Search";
50
51 private static final String DEFAULT_TEXT = String.format("Use \'%s\' for wildcard searching", SearchManager.WILDCARD);
52
53 private final BulkEditor editor;
54
55 private Text text;
56 private BulkEditorSortCombo sortCombo;
57
58 private Button button;
59
60
61 public Object ORDER_BY = new Object();
62
63 /**
64 * <p>Constructor for BulkEditorSearchComposite.</p>
65 *
66 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
67 * @param style a int.
68 * @param editor a {@link org.eclipse.ui.IEditorPart} object.
69 */
70 public BulkEditorSearch(BulkEditor editor, Composite parent, int style) {
71 this.editor = editor;
72
73 createControl(parent, style);
74 }
75
76 /*
77 * Creates the search control.
78 */
79 /**
80 * <p>createControl</p>
81 */
82 protected void createControl(Composite parent, int style) {
83
84 final Composite container = new Composite(parent, style);
85 GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
86 container.setLayoutData(gridData);
87 container.setLayout(new GridLayout(5, false));
88
89 createSearchTextField(container, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
90
91 createSortCombo(container, style);
92
93 button = new Button(container, SWT.PUSH);
94 button.setText(SEARCH);
95 button.addSelectionListener(new SelectionAdapter() {
96 /* (non-Javadoc)
97 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
98 */
99 @Override
100 public void widgetSelected(SelectionEvent e) {
101 updateEditorInput();
102 }
103 });
104
105 registerAtFocusService();
106 }
107
108 /**
109 * @param container
110 * @param style
111 */
112 private void createSortCombo(Composite parent, int style) {
113 sortCombo = new BulkEditorSortCombo(parent, editor.getEditorInput().getSortProviders());
114
115 }
116
117 /**
118 * Handles focus changes for the textfield.
119 */
120 private void registerAtFocusService() {
121 IFocusService focusService =
122 (IFocusService) PlatformUI.getWorkbench().getService(IFocusService.class);
123 if (focusService != null) {
124 focusService.addFocusTracker(text, "bulkeditor.textControlId");
125 }
126 }
127
128
129 /**
130 * Creates the search textfield.
131 */
132 private void createSearchTextField(Composite parent, int style) {
133 final Label label = new Label(parent, SWT.NONE);
134 label.setText("Title Cache");
135
136 text = new Text(parent, style);
137 text.setText(DEFAULT_TEXT);
138 text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
139
140 text.addFocusListener(new FocusListener() {
141
142 public void focusGained(FocusEvent e) {
143 text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOCUS));
144 if (DEFAULT_TEXT.equals(text.getText())) {
145 text.setText("");
146 }
147 }
148
149 public void focusLost(FocusEvent e) {
150 if (CdmUtils.isEmpty(text.getText())) {
151 text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
152 text.setText(DEFAULT_TEXT);
153 }
154 }
155 });
156
157 text.addKeyListener(new KeyAdapter() {
158 /* (non-Javadoc)
159 * @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent)
160 */
161 @Override
162 public void keyReleased(KeyEvent e) {
163 if (e.keyCode == SWT.CR) {
164 updateEditorInput();
165 }
166 }
167 });
168
169 GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
170 text.setLayoutData(gridData);
171 }
172
173
174 /*
175 * Shows the results of the search.
176 */
177 private void updateEditorInput() {
178
179 String searchString = getSearchString().trim();
180
181 if(DEFAULT_TEXT.equals(searchString) || CdmUtils.isEmpty(searchString)){
182 return;
183 }
184
185 BulkEditorQuery query = new BulkEditorQuery(getSearchString(), getComparator());
186 editor.performSearch(query);
187 }
188
189 /*
190 * Returns the current string in the search textfield.
191 * @return the content of the textfield
192 */
193 /**
194 * <p>getSearchString</p>
195 *
196 * @return a {@link java.lang.String} object.
197 */
198 public String getSearchString() {
199 return text.getText().trim();
200 }
201
202 /*
203 *
204 */
205 /**
206 * <p>getComparator</p>
207 *
208 * @return a {@link java.util.Comparator} object.
209 */
210 public Comparator getComparator() {
211 return sortCombo.getSelection();
212 };
213
214 /*
215 *
216 */
217
218 /**
219 *
220 */
221 public void setFocus() {
222 if(text != null && ! text.isDisposed()){
223 text.setFocus();
224 }
225 }
226 }