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