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