Ticket #1352 fixed.
[taxeditor.git] / taxeditor-bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / BulkEditorSearchComposite.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.apache.log4j.Logger;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.FocusEvent;
18 import org.eclipse.swt.events.FocusListener;
19 import org.eclipse.swt.events.KeyAdapter;
20 import org.eclipse.swt.events.KeyEvent;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.events.SelectionListener;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.graphics.Rectangle;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Menu;
30 import org.eclipse.swt.widgets.MenuItem;
31 import org.eclipse.swt.widgets.Text;
32 import org.eclipse.swt.widgets.ToolBar;
33 import org.eclipse.swt.widgets.ToolItem;
34 import org.eclipse.ui.IEditorInput;
35 import org.eclipse.ui.IEditorPart;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.swt.IFocusService;
38
39 import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
40 import eu.etaxonomy.cdm.api.service.config.impl.IdentifiableServiceConfiguratorImpl;
41 import eu.etaxonomy.taxeditor.bulkeditor.input.BulkEditorInput;
42 import eu.etaxonomy.taxeditor.model.Resources;
43
44 /**
45 * @author p.ciardelli
46 * @author e.-m.lee
47 * @created 17.08.2009
48 * @version 1.0
49 */
50 public class BulkEditorSearchComposite extends Composite {
51 private static final Logger logger = Logger.getLogger(BulkEditorSearchComposite.class);
52
53 private IEditorPart editor;
54 private IBulkEditorSortMenuProvider menuProvider;
55 private Menu sortMenu;
56 private Text text;
57 private static final String DEFAULT_TEXT = "Use \"*\" for wildcard searching";
58 public Object ORDER_BY = new Object();
59 private ToolItem toolItem;
60
61 /**
62 * @param parent
63 * @param style
64 */
65 public BulkEditorSearchComposite(IEditorPart editor, Composite parent, int style) {
66 super(parent, style);
67 this.editor = editor;
68 this.menuProvider = new BulkEditorSortMenuProvider();
69
70 createControl();
71 }
72
73 /*
74 * Creates the search control.
75 */
76 protected void createControl() {
77 createLayout();
78 createSearchTextField();
79 createToolBar();
80 registerAtFocusService();
81 setSearchEnabled(false);
82 }
83
84
85 /**
86 * Handles focus changes for the textfield.
87 */
88 private void registerAtFocusService() {
89 IFocusService focusService =
90 (IFocusService) PlatformUI.getWorkbench().getService(IFocusService.class);
91 if (focusService != null) {
92 focusService.addFocusTracker(text, "bulkeditor.textControlId");
93 }
94 }
95
96
97 /**
98 * Creates the search toolbar.
99 */
100 private void createToolBar() {
101 final ToolBar toolBar = new ToolBar(this, SWT.NULL);
102
103 toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
104 toolItem.setText("Search");
105
106 DropdownMenu dropdownMenu = new DropdownMenu(toolItem);
107 toolItem.addSelectionListener(dropdownMenu);
108 }
109
110
111 /**
112 * Creates the search textfield.
113 */
114 private void createSearchTextField() {
115 text = new Text(this, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
116 text.setText(DEFAULT_TEXT);
117 text.setForeground(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
118
119 text.addFocusListener(new FocusListener() {
120
121 public void focusGained(FocusEvent e) {
122 text.setForeground(Resources.getColor(Resources.SEARCH_VIEW_FOCUS));
123 if (DEFAULT_TEXT.equals(text.getText())) {
124 text.setText("");
125 }
126 }
127
128 public void focusLost(FocusEvent e) {
129 if (text.getText() == "") {
130 text.setForeground(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
131 text.setText(DEFAULT_TEXT);
132 setSearchEnabled(false);
133 } else {
134 setSearchEnabled(true);
135 }
136 }
137 });
138
139 text.addKeyListener(new KeyAdapter() {
140 /* (non-Javadoc)
141 * @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent)
142 */
143 @Override
144 public void keyReleased(KeyEvent e) {
145 if (e.keyCode == SWT.CR) {
146 updateEditorInput();
147 }
148 }
149 });
150
151 GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
152 text.setLayoutData(gridData);
153 }
154
155
156 /**
157 * Creates the search layout.
158 */
159 private void createLayout() {
160 GridLayout gridLayout = new GridLayout();
161 gridLayout.numColumns = 3;
162 gridLayout.marginHeight = 0;
163 gridLayout.marginWidth = 12;
164 setLayout(gridLayout);
165 }
166
167 /*
168 * Toggles the availability of the search toolItem.
169 */
170 private void setSearchEnabled(boolean enabled) {
171 // toolItem.setEnabled(enabled);
172 }
173
174 /*
175 * Shows the results of the search.
176 */
177 private void updateEditorInput() {
178
179 String searchString = getSearchString();
180
181 if(!DEFAULT_TEXT.equals(searchString.trim()) && searchString.length() > 0){
182 // update query in IEditorInput
183 IEditorInput input = editor.getEditorInput();
184 if (input instanceof BulkEditorInput) {
185 ((BulkEditorInput) input).setQuery(new BulkEditorQuery(getSearchString(), getComparator()));
186 }
187 }
188 }
189
190 /*
191 * Handles drop down menu selection.
192 */
193 class DropdownMenu extends SelectionAdapter {
194
195 /**
196 * @param dropdown
197 */
198 public DropdownMenu(ToolItem dropdown) {
199 sortMenu = new Menu(dropdown.getParent().getShell());
200
201 MenuItem menuItem = new MenuItem(sortMenu, SWT.PUSH);
202 menuItem.setText("Order by:");
203 menuItem.setData(ORDER_BY);
204 new MenuItem(sortMenu, SWT.SEPARATOR);
205
206 menuProvider.fillMenu(editor.getEditorInput(), sortMenu);
207
208 SelectionListener listener = new SortListSelectionListener();
209
210 for (MenuItem item : sortMenu.getItems()) {
211 if (item.getData() instanceof Comparator) {
212 item.addSelectionListener(listener);
213 }
214 }
215 }
216
217 /* (non-Javadoc)
218 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
219 */
220 public void widgetSelected(SelectionEvent event) {
221 if (event.detail == SWT.ARROW) {
222 ToolItem item = (ToolItem) event.widget;
223 Rectangle rect = item.getBounds();
224 Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));
225 sortMenu.setLocation(pt.x, pt.y + rect.height);
226 sortMenu.setVisible(true);
227 } else {
228 updateEditorInput();
229 }
230 }
231 }
232
233 /*
234 * Handles search configuration selection.
235 */
236 class SortListSelectionListener extends SelectionAdapter {
237
238 public void widgetSelected(SelectionEvent e) {
239 for (MenuItem item : sortMenu.getItems()) {
240 if (item.getData() instanceof Comparator) {
241 if (item.equals(e.getSource())) {
242 item.setSelection(true);
243 } else {
244 item.setSelection(false);
245 }
246 }
247 }
248 }
249 }
250
251 /*
252 * Returns the current string in the search textfield.
253 * @return the content of the textfield
254 */
255 public String getSearchString() {
256 return text.getText().trim();
257 }
258
259 /*
260 *
261 */
262 public Comparator getComparator() {
263 Comparator comparator;
264 for (MenuItem item : sortMenu.getItems()) {
265 if (item.getSelection() == true && item.getData() instanceof Comparator) {
266 return (Comparator) item.getData();
267 }
268 }
269 return null;
270 };
271
272 /*
273 *
274 */
275 class BulkEditorQuery implements IBulkEditorQuery {
276
277 private String searchString;
278 private Comparator comparator;
279 private IIdentifiableEntityServiceConfigurator searchConfigurator;
280
281 BulkEditorQuery (String searchString, Comparator comparator) {
282 this.searchString = searchString;
283 this.comparator = comparator;
284
285 searchConfigurator = IdentifiableServiceConfiguratorImpl.NewInstance();
286 searchConfigurator.setTitleSearchString(searchString);
287 }
288
289 /* (non-Javadoc)
290 * @see eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery#getComparator()
291 */
292 public Comparator getComparator() {
293 return comparator;
294 }
295
296 /* (non-Javadoc)
297 * @see eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery#getSearchString()
298 */
299 public String getSearchString() {
300 return searchString;
301 }
302
303 /* (non-Javadoc)
304 * @see eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery#getSearchConfigurator()
305 */
306 public IIdentifiableEntityServiceConfigurator getSearchConfigurator() {
307 return searchConfigurator;
308 }
309 }
310 }