ref #7518 Adjust columns size
[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 org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.FocusEvent;
14 import org.eclipse.swt.events.FocusListener;
15 import org.eclipse.swt.events.KeyAdapter;
16 import org.eclipse.swt.events.KeyEvent;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Text;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.swt.IFocusService;
27
28 import eu.etaxonomy.cdm.common.CdmUtils;
29 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
30 import eu.etaxonomy.taxeditor.l10n.Messages;
31 import eu.etaxonomy.taxeditor.model.ColorResources;
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 BulkEditorSearchE4 {
43
44 private static final String SEARCH = Messages.BulkEditorSearchE4_SEARCH;
45
46 private static final String DEFAULT_TEXT = String.format(Messages.BulkEditorSearchE4_WILDCARD, SearchManager.WILDCARD);
47
48 private final BulkEditorE4 editor;
49
50 private Text text;
51
52 private Button button;
53
54
55 public Object ORDER_BY = new Object();
56
57 public BulkEditorSearchE4(BulkEditorE4 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 button = new Button(container, SWT.PUSH);
76 button.setText(SEARCH);
77 button.addSelectionListener(new SelectionAdapter() {
78 @Override
79 public void widgetSelected(SelectionEvent e) {
80 updateEditorInput();
81 }
82 });
83
84 registerAtFocusService();
85 }
86
87 /**
88 * Handles focus changes for the textfield.
89 */
90 private void registerAtFocusService() {
91 IFocusService focusService =
92 PlatformUI.getWorkbench().getService(IFocusService.class);
93 if (focusService != null) {
94 focusService.addFocusTracker(text, "bulkeditor.textControlId"); //$NON-NLS-1$
95 }
96 }
97
98
99 /**
100 * Creates the search textfield.
101 */
102 private void createSearchTextField(Composite parent, int style) {
103 final Label label = new Label(parent, SWT.NONE);
104 label.setText(Messages.BulkEditorSearchE4_TITLE_CACHE);
105
106 text = new Text(parent, style);
107 text.setText(DEFAULT_TEXT);
108 text.setForeground(ColorResources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
109
110 text.addFocusListener(new FocusListener() {
111
112 @Override
113 public void focusGained(FocusEvent e) {
114 text.setForeground(ColorResources.getColor(Resources.SEARCH_VIEW_FOCUS));
115 if (DEFAULT_TEXT.equals(text.getText())) {
116 text.setText(""); //$NON-NLS-1$
117 }
118 }
119
120 @Override
121 public void focusLost(FocusEvent e) {
122 if (CdmUtils.isBlank(text.getText())) {
123 text.setForeground(ColorResources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
124 text.setText(DEFAULT_TEXT);
125 }
126 }
127 });
128
129 text.addKeyListener(new KeyAdapter() {
130 @Override
131 public void keyReleased(KeyEvent e) {
132 if (e.keyCode == SWT.CR) {
133 updateEditorInput();
134 }
135 }
136 });
137
138 GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
139 text.setLayoutData(gridData);
140 text.setFocus();
141 }
142
143
144 /**
145 * Shows the results of the search.
146 */
147 public void updateEditorInput() {
148
149 String searchString = getSearchString().trim();
150
151 if(DEFAULT_TEXT.equals(searchString) || CdmUtils.isBlank(searchString)){
152 return;
153 }
154
155 BulkEditorQuery query = new BulkEditorQuery(searchString);
156 editor.performSearch(query);
157 }
158
159 /**
160 * Returns the current string in the search textfield.
161 * @return the content of the textfield
162 */
163 public String getSearchString() {
164 return text.getText().trim();
165 }
166
167 public void setFocus() {
168 if(text != null && ! text.isDisposed()){
169 text.setFocus();
170 }
171 }
172 }