Project

General

Profile

Download (4.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.bulkeditor.e4;
11

    
12
import java.util.Comparator;
13

    
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.events.FocusEvent;
16
import org.eclipse.swt.events.FocusListener;
17
import org.eclipse.swt.events.KeyAdapter;
18
import org.eclipse.swt.events.KeyEvent;
19
import org.eclipse.swt.events.SelectionAdapter;
20
import org.eclipse.swt.events.SelectionEvent;
21
import org.eclipse.swt.layout.GridData;
22
import org.eclipse.swt.layout.GridLayout;
23
import org.eclipse.swt.widgets.Button;
24
import org.eclipse.swt.widgets.Composite;
25
import org.eclipse.swt.widgets.Label;
26
import org.eclipse.swt.widgets.Text;
27
import org.eclipse.ui.PlatformUI;
28
import org.eclipse.ui.swt.IFocusService;
29

    
30
import eu.etaxonomy.cdm.common.CdmUtils;
31
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
32
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorSortCombo;
33
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
34
import eu.etaxonomy.taxeditor.l10n.Messages;
35
import eu.etaxonomy.taxeditor.preference.Resources;
36
import eu.etaxonomy.taxeditor.store.SearchManager;
37

    
38
/**
39
 * @author p.ciardelli
40
 * @author e.-m.lee
41
 * @author n.hoffmann
42
 * @created 17.08.2009
43
 * @version 1.0
44
 */
45
public class BulkEditorSearchE4 {
46

    
47
	private static final String SEARCH = Messages.BulkEditorSearchE4_SEARCH;
48

    
49
	private static final String DEFAULT_TEXT = String.format(Messages.BulkEditorSearchE4_WILDCARD, SearchManager.WILDCARD);
50

    
51
	private final BulkEditorE4 editor;
52

    
53
	private Text text;
54
	private BulkEditorSortCombo sortCombo;
55

    
56
	private Button button;
57

    
58

    
59
	public Object ORDER_BY = new Object();
60

    
61
	public BulkEditorSearchE4(BulkEditorE4 editor, Composite parent, int style) {
62
		this.editor = editor;
63

    
64
		createControl(parent, style);
65
	}
66

    
67
	/**
68
	 * Creates the search control.
69
	 */
70
	protected void createControl(Composite parent, int style) {
71

    
72
		final Composite container = new Composite(parent, style);
73
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
74
		container.setLayoutData(gridData);
75
		container.setLayout(new GridLayout(5, false));
76

    
77
		createSearchTextField(container, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
78

    
79
		createSortCombo(container, style);
80

    
81
		button = new Button(container, SWT.PUSH);
82
		button.setText(SEARCH);
83
		button.addSelectionListener(new SelectionAdapter() {
84
			@Override
85
			public void widgetSelected(SelectionEvent e) {
86
				updateEditorInput();
87
			}
88
		});
89

    
90
		//FIXME E4 migrate/delete
91
//		registerAtFocusService();
92
	}
93

    
94
	private void createSortCombo(Composite parent, int style) {
95
		sortCombo = new BulkEditorSortCombo(parent, editor.getEditorInput().getSortProviders());
96

    
97
	}
98

    
99
	/**
100
	 * Handles focus changes for the textfield.
101
	 */
102
	private void registerAtFocusService() {
103
		IFocusService focusService =
104
			PlatformUI.getWorkbench().getService(IFocusService.class);
105
		if (focusService != null) {
106
			focusService.addFocusTracker(text, "bulkeditor.textControlId"); //$NON-NLS-1$
107
		}
108
	}
109

    
110

    
111
	/**
112
	 * Creates the search textfield.
113
	 */
114
	private void createSearchTextField(Composite parent, int style) {
115
		final Label label = new Label(parent, SWT.NONE);
116
		label.setText(Messages.BulkEditorSearchE4_TITLE_CACHE);
117

    
118
		text = new Text(parent, style);
119
		text.setText(DEFAULT_TEXT);
120
		text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
121

    
122
		text.addFocusListener(new FocusListener() {
123

    
124
			@Override
125
            public void focusGained(FocusEvent e) {
126
				text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOCUS));
127
				if (DEFAULT_TEXT.equals(text.getText())) {
128
					text.setText(""); //$NON-NLS-1$
129
				}
130
			}
131

    
132
			@Override
133
            public void focusLost(FocusEvent e) {
134
				if (CdmUtils.isEmpty(text.getText())) {
135
					text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
136
					text.setText(DEFAULT_TEXT);
137
				}
138
			}
139
		});
140

    
141
		text.addKeyListener(new KeyAdapter() {
142
			@Override
143
			public void keyReleased(KeyEvent e) {
144
				if (e.keyCode == SWT.CR) {
145
					updateEditorInput();
146
				}
147
			}
148
		});
149

    
150
		GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
151
		text.setLayoutData(gridData);
152
	}
153

    
154

    
155
	/**
156
	 * Shows the results of the search.
157
	 */
158
	public void updateEditorInput() {
159

    
160
		String searchString = getSearchString().trim();
161

    
162
		if(DEFAULT_TEXT.equals(searchString) || CdmUtils.isBlank(searchString)){
163
			return;
164
		}
165

    
166
		BulkEditorQuery query = new BulkEditorQuery(searchString, getComparator());
167
		editor.performSearch(query);
168
	}
169

    
170
	/**
171
	 * Returns the current string in the search textfield.
172
	 * @return the content of the textfield
173
	 */
174
	public String getSearchString() {
175
		return text.getText().trim();
176
	}
177

    
178
	public Comparator getComparator() {
179
		return sortCombo.getSelection();
180
	}
181

    
182
	public void setFocus() {
183
		if(text != null && ! text.isDisposed()){
184
			text.setFocus();
185
		}
186
	}
187
}
(3-3/3)