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