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
.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
;
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
;
50 public class BulkEditorSearchComposite
extends Composite
{
51 private static final Logger logger
= Logger
.getLogger(BulkEditorSearchComposite
.class);
53 private IEditorPart editor
;
54 private IBulkEditorSortMenuProvider menuProvider
;
55 private Menu sortMenu
;
57 private static final String DEFAULT_TEXT
= "Use \"*\" for wildcard searching";
58 public Object ORDER_BY
= new Object();
59 private ToolItem toolItem
;
65 public BulkEditorSearchComposite(IEditorPart editor
, Composite parent
, int style
) {
68 this.menuProvider
= new BulkEditorSortMenuProvider();
74 * Creates the search control.
76 protected void createControl() {
78 createSearchTextField();
80 registerAtFocusService();
81 setSearchEnabled(false);
86 * Handles focus changes for the textfield.
88 private void registerAtFocusService() {
89 IFocusService focusService
=
90 (IFocusService
) PlatformUI
.getWorkbench().getService(IFocusService
.class);
91 if (focusService
!= null) {
92 focusService
.addFocusTracker(text
, "bulkeditor.textControlId");
98 * Creates the search toolbar.
100 private void createToolBar() {
101 final ToolBar toolBar
= new ToolBar(this, SWT
.NULL
);
103 toolItem
= new ToolItem(toolBar
, SWT
.DROP_DOWN
| SWT
.BORDER
);
104 toolItem
.setText("Search");
106 DropdownMenu dropdownMenu
= new DropdownMenu(toolItem
);
107 toolItem
.addSelectionListener(dropdownMenu
);
112 * Creates the search textfield.
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
));
119 text
.addFocusListener(new FocusListener() {
121 public void focusGained(FocusEvent e
) {
122 text
.setForeground(Resources
.getColor(Resources
.SEARCH_VIEW_FOCUS
));
123 if (DEFAULT_TEXT
.equals(text
.getText())) {
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);
134 setSearchEnabled(true);
139 text
.addKeyListener(new KeyAdapter() {
141 * @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent)
144 public void keyReleased(KeyEvent e
) {
145 if (e
.keyCode
== SWT
.CR
) {
151 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
152 text
.setLayoutData(gridData
);
157 * Creates the search layout.
159 private void createLayout() {
160 GridLayout gridLayout
= new GridLayout();
161 gridLayout
.numColumns
= 3;
162 gridLayout
.marginHeight
= 0;
163 gridLayout
.marginWidth
= 12;
164 setLayout(gridLayout
);
168 * Toggles the availability of the search toolItem.
170 private void setSearchEnabled(boolean enabled
) {
171 // toolItem.setEnabled(enabled);
175 * Shows the results of the search.
177 private void updateEditorInput() {
179 String searchString
= getSearchString();
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()));
191 * Handles drop down menu selection.
193 class DropdownMenu
extends SelectionAdapter
{
198 public DropdownMenu(ToolItem dropdown
) {
199 sortMenu
= new Menu(dropdown
.getParent().getShell());
201 MenuItem menuItem
= new MenuItem(sortMenu
, SWT
.PUSH
);
202 menuItem
.setText("Order by:");
203 menuItem
.setData(ORDER_BY
);
204 new MenuItem(sortMenu
, SWT
.SEPARATOR
);
206 menuProvider
.fillMenu(editor
.getEditorInput(), sortMenu
);
208 SelectionListener listener
= new SortListSelectionListener();
210 for (MenuItem item
: sortMenu
.getItems()) {
211 if (item
.getData() instanceof Comparator
) {
212 item
.addSelectionListener(listener
);
218 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
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);
234 * Handles search configuration selection.
236 class SortListSelectionListener
extends SelectionAdapter
{
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);
244 item
.setSelection(false);
252 * Returns the current string in the search textfield.
253 * @return the content of the textfield
255 public String
getSearchString() {
256 return text
.getText().trim();
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();
275 class BulkEditorQuery
implements IBulkEditorQuery
{
277 private String searchString
;
278 private Comparator comparator
;
279 private IIdentifiableEntityServiceConfigurator searchConfigurator
;
281 BulkEditorQuery (String searchString
, Comparator comparator
) {
282 this.searchString
= searchString
;
283 this.comparator
= comparator
;
285 searchConfigurator
= IdentifiableServiceConfiguratorImpl
.NewInstance();
286 searchConfigurator
.setTitleSearchString(searchString
);
290 * @see eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery#getComparator()
292 public Comparator
getComparator() {
297 * @see eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery#getSearchString()
299 public String
getSearchString() {
304 * @see eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery#getSearchConfigurator()
306 public IIdentifiableEntityServiceConfigurator
getSearchConfigurator() {
307 return searchConfigurator
;