ref #6932 Migrate marker type handler
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / e4 / BulkEditorSearchE4.java
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.preference.Resources;
35 import eu.etaxonomy.taxeditor.store.SearchManager;
36
37 /**
38 * @author p.ciardelli
39 * @author e.-m.lee
40 * @author n.hoffmann
41 * @created 17.08.2009
42 * @version 1.0
43 */
44 public class BulkEditorSearchE4 {
45
46 private static final String SEARCH = "Search";
47
48 private static final String DEFAULT_TEXT = String.format("Use \'%s\' for wildcard searching", SearchManager.WILDCARD);
49
50 private final BulkEditorE4 editor;
51
52 private Text text;
53 private BulkEditorSortCombo sortCombo;
54
55 private Button button;
56
57
58 public Object ORDER_BY = new Object();
59
60 public BulkEditorSearchE4(BulkEditorE4 editor, Composite parent, int style) {
61 this.editor = editor;
62
63 createControl(parent, style);
64 }
65
66 /**
67 * Creates the search control.
68 */
69 protected void createControl(Composite parent, int style) {
70
71 final Composite container = new Composite(parent, style);
72 GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
73 container.setLayoutData(gridData);
74 container.setLayout(new GridLayout(5, false));
75
76 createSearchTextField(container, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
77
78 createSortCombo(container, style);
79
80 button = new Button(container, SWT.PUSH);
81 button.setText(SEARCH);
82 button.addSelectionListener(new SelectionAdapter() {
83 @Override
84 public void widgetSelected(SelectionEvent e) {
85 updateEditorInput();
86 }
87 });
88
89 registerAtFocusService();
90 }
91
92 private void createSortCombo(Composite parent, int style) {
93 sortCombo = new BulkEditorSortCombo(parent, editor.getEditorInput().getSortProviders());
94
95 }
96
97 /**
98 * Handles focus changes for the textfield.
99 */
100 private void registerAtFocusService() {
101 IFocusService focusService =
102 PlatformUI.getWorkbench().getService(IFocusService.class);
103 if (focusService != null) {
104 focusService.addFocusTracker(text, "bulkeditor.textControlId");
105 }
106 }
107
108
109 /**
110 * Creates the search textfield.
111 */
112 private void createSearchTextField(Composite parent, int style) {
113 final Label label = new Label(parent, SWT.NONE);
114 label.setText("Title Cache");
115
116 text = new Text(parent, style);
117 text.setText(DEFAULT_TEXT);
118 text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
119
120 text.addFocusListener(new FocusListener() {
121
122 @Override
123 public void focusGained(FocusEvent e) {
124 text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOCUS));
125 if (DEFAULT_TEXT.equals(text.getText())) {
126 text.setText("");
127 }
128 }
129
130 @Override
131 public void focusLost(FocusEvent e) {
132 if (CdmUtils.isEmpty(text.getText())) {
133 text.setForeground(BulkEditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
134 text.setText(DEFAULT_TEXT);
135 }
136 }
137 });
138
139 text.addKeyListener(new KeyAdapter() {
140 @Override
141 public void keyReleased(KeyEvent e) {
142 if (e.keyCode == SWT.CR) {
143 updateEditorInput();
144 }
145 }
146 });
147
148 GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
149 text.setLayoutData(gridData);
150 }
151
152
153 /**
154 * Shows the results of the search.
155 */
156 public void updateEditorInput() {
157
158 String searchString = getSearchString().trim();
159
160 if(DEFAULT_TEXT.equals(searchString) || CdmUtils.isBlank(searchString)){
161 return;
162 }
163
164 BulkEditorQuery query = new BulkEditorQuery(searchString, getComparator());
165 editor.performSearch(query);
166 }
167
168 /**
169 * Returns the current string in the search textfield.
170 * @return the content of the textfield
171 */
172 public String getSearchString() {
173 return text.getText().trim();
174 }
175
176 public Comparator getComparator() {
177 return sortCombo.getSelection();
178 }
179
180 public void setFocus() {
181 if(text != null && ! text.isDisposed()){
182 text.setFocus();
183 }
184 }
185 }