Project

General

Profile

Download (8.58 KB) Statistics
| Branch: | Tag: | Revision:
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.AbstractBulkEditorInput;
42
import eu.etaxonomy.taxeditor.preference.Resources;
43

    
44
/**
45
 * <p>BulkEditorSearchComposite class.</p>
46
 *
47
 * @author p.ciardelli
48
 * @author e.-m.lee
49
 * @created 17.08.2009
50
 * @version 1.0
51
 */
52
public class BulkEditorSearchComposite extends Composite {
53
	private static final Logger logger = Logger.getLogger(BulkEditorSearchComposite.class);
54
	
55
	private IEditorPart editor;
56
	private IBulkEditorSortMenuProvider menuProvider;
57
	private Menu sortMenu;
58
	private Text text;
59
	private static final String DEFAULT_TEXT = "Use \"*\" for wildcard searching";
60
	public Object ORDER_BY = new Object();
61
	private ToolItem toolItem;
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 BulkEditorSearchComposite(IEditorPart editor, Composite parent, int style) {
71
		super(parent, style);
72
		this.editor = editor;
73
		this.menuProvider = new BulkEditorSortMenuProvider();
74

    
75
		createControl();
76
	}
77

    
78
	/*
79
	 * Creates the search control.
80
	 */
81
	/**
82
	 * <p>createControl</p>
83
	 */
84
	protected void createControl() {
85
		createLayout();
86
		createSearchTextField();
87
		createToolBar();
88
		registerAtFocusService();
89
	    setSearchEnabled(false);
90
	}
91

    
92

    
93
	/**
94
	 * Handles focus changes for the textfield.
95
	 */
96
	private void registerAtFocusService() {
97
		IFocusService focusService = 
98
			(IFocusService) PlatformUI.getWorkbench().getService(IFocusService.class);
99
		if (focusService != null) {
100
			focusService.addFocusTracker(text, "bulkeditor.textControlId");
101
		}
102
	}
103

    
104

    
105
	/**
106
	 * Creates the search toolbar.
107
	 */
108
	private void createToolBar() {
109
		final ToolBar toolBar = new ToolBar(this, SWT.NULL);
110
		
111
		toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
112
		toolItem.setText("Search");
113
		
114
		DropdownMenu dropdownMenu = new DropdownMenu(toolItem);
115
	    toolItem.addSelectionListener(dropdownMenu);
116
	}
117

    
118

    
119
	/**
120
	 * Creates the search textfield.
121
	 */
122
	private void createSearchTextField() {
123
		text = new Text(this, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
124
		text.setText(DEFAULT_TEXT);
125
		text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
126
		
127
		text.addFocusListener(new FocusListener() {
128

    
129
			public void focusGained(FocusEvent e) {
130
				text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOCUS));
131
				if (DEFAULT_TEXT.equals(text.getText())) {
132
					text.setText("");
133
				}
134
			}
135

    
136
			public void focusLost(FocusEvent e) {
137
				if (text.getText() == "") {
138
					text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
139
					text.setText(DEFAULT_TEXT);			
140
				    setSearchEnabled(false);		
141
				} else {
142
				    setSearchEnabled(true);					
143
				}
144
			}
145
		});
146
		
147
		text.addKeyListener(new KeyAdapter() {
148
			/* (non-Javadoc)
149
			 * @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent)
150
			 */
151
			@Override
152
			public void keyReleased(KeyEvent e) {
153
				if (e.keyCode == SWT.CR) {
154
					updateEditorInput();
155
				}
156
			}
157
		});
158
		
159
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
160
		text.setLayoutData(gridData);
161
	}
162

    
163

    
164
	/**
165
	 * Creates the search layout.
166
	 */
167
	private void createLayout() {
168
		GridLayout gridLayout = new GridLayout();
169
		gridLayout.numColumns = 3;
170
		gridLayout.marginHeight = 0;
171
		gridLayout.marginWidth = 12;
172
		setLayout(gridLayout);
173
	}
174
	
175
	/*
176
	 * Toggles the availability of the search toolItem.
177
	 */
178
	private void setSearchEnabled(boolean enabled) {
179
//		toolItem.setEnabled(enabled);		
180
	}
181
	
182
	/*
183
	 * Shows the results of the search.
184
	 */
185
	private void updateEditorInput() {
186
		
187
		String searchString = getSearchString();
188
		
189
		if(!DEFAULT_TEXT.equals(searchString.trim()) && searchString.length() > 0){
190
			// update query in IEditorInput
191
			IEditorInput input = editor.getEditorInput();
192
			if (input instanceof AbstractBulkEditorInput) {
193
				((AbstractBulkEditorInput) input).setQuery(new BulkEditorQuery(getSearchString(), getComparator()));
194
			}
195
		}
196
	}
197
	
198
	/*
199
	 * Handles drop down menu selection.
200
	 */
201
	class DropdownMenu extends SelectionAdapter {
202

    
203
		/**
204
		 * @param dropdown
205
		 */
206
		public DropdownMenu(ToolItem dropdown) {
207
			sortMenu = new Menu(dropdown.getParent().getShell());
208
			
209
			MenuItem menuItem = new MenuItem(sortMenu, SWT.PUSH);
210
		    menuItem.setText("Order by:");
211
		    menuItem.setData(ORDER_BY);
212
			new MenuItem(sortMenu, SWT.SEPARATOR);
213

    
214
			menuProvider.fillMenu(editor.getEditorInput(), sortMenu);
215
			
216
			SelectionListener listener = new SortListSelectionListener();
217
			
218
			for (MenuItem item : sortMenu.getItems()) {
219
				if (item.getData() instanceof Comparator) {
220
					item.addSelectionListener(listener);
221
				}
222
			}
223
		}
224

    
225
		/* (non-Javadoc)
226
		 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
227
		 */
228
		public void widgetSelected(SelectionEvent event) {
229
			if (event.detail == SWT.ARROW) {
230
				ToolItem item = (ToolItem) event.widget;
231
				Rectangle rect = item.getBounds();
232
				Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));
233
				sortMenu.setLocation(pt.x, pt.y + rect.height);
234
				sortMenu.setVisible(true);
235
		    } else {
236
		    	updateEditorInput();
237
		    }
238
		}
239
	}
240

    
241
	/*
242
	 * Handles search configuration selection.
243
	 */
244
	class SortListSelectionListener extends SelectionAdapter {
245
		
246
		public void widgetSelected(SelectionEvent e) {
247
			for (MenuItem item : sortMenu.getItems()) {
248
				if (item.getData() instanceof Comparator) {
249
					if (item.equals(e.getSource())) {
250
						item.setSelection(true);
251
					} else {
252
						item.setSelection(false);
253
					}
254
				}
255
			}				
256
		}
257
	}
258
	
259
	/*
260
	 * Returns the current string in the search textfield.
261
	 * @return the content of the textfield
262
	 */
263
	/**
264
	 * <p>getSearchString</p>
265
	 *
266
	 * @return a {@link java.lang.String} object.
267
	 */
268
	public String getSearchString() {
269
		return text.getText().trim();
270
	}
271
	
272
	/*
273
	 * 
274
	 */
275
	/**
276
	 * <p>getComparator</p>
277
	 *
278
	 * @return a {@link java.util.Comparator} object.
279
	 */
280
	public Comparator getComparator() {
281
		Comparator comparator;
282
		for (MenuItem item : sortMenu.getItems()) {
283
			if (item.getSelection() == true && item.getData() instanceof Comparator) {
284
				return (Comparator) item.getData();
285
			}
286
		}
287
		return null;
288
	};
289
	
290
	/*
291
	 * 
292
	 */
293
	class BulkEditorQuery implements IBulkEditorQuery {
294
		
295
		private String searchString;
296
		private Comparator comparator;
297
		private IIdentifiableEntityServiceConfigurator searchConfigurator;
298

    
299
		BulkEditorQuery (String searchString, Comparator comparator) {
300
			this.searchString = searchString;
301
			this.comparator = comparator;
302
			
303
			searchConfigurator = IdentifiableServiceConfiguratorImpl.NewInstance();
304
			searchConfigurator.setTitleSearchString(searchString);
305
		}
306

    
307
		/* (non-Javadoc)
308
		 * @see eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery#getComparator()
309
		 */
310
		public Comparator getComparator() {
311
			return comparator;
312
		}
313

    
314
		/* (non-Javadoc)
315
		 * @see eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery#getSearchString()
316
		 */
317
		public String getSearchString() {
318
			return searchString;
319
		}
320

    
321
		/* (non-Javadoc)
322
		 * @see eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery#getSearchConfigurator()
323
		 */
324
		public IIdentifiableEntityServiceConfigurator getSearchConfigurator() {
325
			return searchConfigurator;
326
		}
327
	}
328
}
(5-5/15)