Merge branch 'release/5.19.0'
[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 package eu.etaxonomy.taxeditor.bulkeditor.e4;
10
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.util.Properties;
15
16 import org.eclipse.nebula.widgets.nattable.persistence.command.DisplayPersistenceDialogCommand;
17 import org.eclipse.nebula.widgets.nattable.persistence.command.DisplayPersistenceDialogCommandHandler;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.FocusEvent;
20 import org.eclipse.swt.events.FocusListener;
21 import org.eclipse.swt.events.KeyAdapter;
22 import org.eclipse.swt.events.KeyEvent;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Text;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.swt.IFocusService;
33
34 import eu.etaxonomy.cdm.common.CdmUtils;
35 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
36 import eu.etaxonomy.taxeditor.l10n.Messages;
37 import eu.etaxonomy.taxeditor.model.ColorResources;
38 import eu.etaxonomy.taxeditor.model.ImageResources;
39 import eu.etaxonomy.taxeditor.model.MessagingUtils;
40 import eu.etaxonomy.taxeditor.preference.Resources;
41 import eu.etaxonomy.taxeditor.store.SearchManager;
42 import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
43
44 /**
45 * @author p.ciardelli
46 * @author e.-m.lee
47 * @author n.hoffmann
48 * @created 17.08.2009
49 */
50 public class BulkEditorSearchE4 {
51
52 private static final String SEARCH = Messages.BulkEditorSearchE4_SEARCH;
53
54 private static final String DEFAULT_TEXT = String.format(Messages.BulkEditorSearchE4_WILDCARD, SearchManager.WILDCARD);
55
56 private final BulkEditorE4Composite editor;
57
58 private DisplayPersistenceDialogCommandHandler displayPersistenceDialogCommandHandler;
59
60 private Properties natTableState;
61
62 private Text text;
63
64 private Button button;
65
66 private Button btnManageState;
67
68 public Object ORDER_BY = new Object();
69
70 public BulkEditorSearchE4(BulkEditorE4Composite editor, Composite parent, int style) {
71 this.editor = editor;
72
73 createControl(parent, style);
74 }
75
76 /**
77 * Creates the search control.
78 */
79 protected void createControl(Composite parent, int style) {
80
81 final Composite container = new Composite(parent, style);
82 GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
83 container.setLayoutData(gridData);
84 container.setLayout(new GridLayout(5, false));
85
86 createSearchTextField(container, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
87
88 button = new Button(container, SWT.PUSH);
89 button.setText(SEARCH);
90 button.addSelectionListener(new SelectionAdapter() {
91 @Override
92 public void widgetSelected(SelectionEvent e) {
93 updateEditorInput();
94 }
95 });
96
97 /**
98 * Table state persistence
99 */
100 natTableState = new Properties();
101 //load persisted state
102 File statePropertiesFile = getStatePropertiesFile();
103 FileInputStream inputStream;
104 try {
105 inputStream = new FileInputStream(statePropertiesFile);
106 natTableState.load(inputStream);
107 } catch (IOException e) {
108 MessagingUtils.info("No initial state properties file found for bulk editor"); //$NON-NLS-1$
109 }
110
111 // add settings button
112 displayPersistenceDialogCommandHandler = new DisplayPersistenceDialogCommandHandler(natTableState, editor.getNatTable());
113 btnManageState = new Button(container, SWT.PUSH);
114 btnManageState.setImage(ImageResources.getImage(ImageResources.SETTINGS));
115 btnManageState.setToolTipText(Messages.BulkEditorSearchE4_TABLE_SETTINGS);
116 btnManageState.addSelectionListener(new SelectionAdapter() {
117 @Override
118 public void widgetSelected(SelectionEvent e) {
119 editor.getNatTable().doCommand(new DisplayPersistenceDialogCommand(editor.getNatTable()));
120 }
121 });
122
123 registerAtFocusService();
124 }
125
126 DisplayPersistenceDialogCommandHandler getDisplayPersistenceDialogCommandHandler() {
127 return displayPersistenceDialogCommandHandler;
128 }
129
130 File getStatePropertiesFile() {
131 return new File(WorkbenchUtility.getBaseLocation(), "bulkeditor_tablestate.properties"); //$NON-NLS-1$
132 }
133
134 Properties getNatTableState() {
135 return natTableState;
136 }
137
138 /**
139 * Handles focus changes for the textfield.
140 */
141 private void registerAtFocusService() {
142 IFocusService focusService =
143 PlatformUI.getWorkbench().getService(IFocusService.class);
144 if (focusService != null) {
145 focusService.addFocusTracker(text, "bulkeditor.textControlId"); //$NON-NLS-1$
146 }
147 }
148
149 /**
150 * Creates the search textfield.
151 */
152 private void createSearchTextField(Composite parent, int style) {
153 final Label label = new Label(parent, SWT.NONE);
154 label.setText(Messages.BulkEditorSearchE4_TITLE_CACHE);
155
156 text = new Text(parent, style);
157 text.setText(DEFAULT_TEXT);
158 text.setForeground(ColorResources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
159
160 text.addFocusListener(new FocusListener() {
161
162 @Override
163 public void focusGained(FocusEvent e) {
164 text.setForeground(ColorResources.getColor(Resources.SEARCH_VIEW_FOCUS));
165 if (DEFAULT_TEXT.equals(text.getText())) {
166 text.setText(""); //$NON-NLS-1$
167 }
168 }
169
170 @Override
171 public void focusLost(FocusEvent e) {
172 if (CdmUtils.isBlank(text.getText())) {
173 text.setForeground(ColorResources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
174 text.setText(DEFAULT_TEXT);
175 }
176 }
177 });
178
179 text.addKeyListener(new KeyAdapter() {
180 @Override
181 public void keyReleased(KeyEvent e) {
182 if (e.keyCode == SWT.CR) {
183 updateEditorInput();
184 }
185 }
186 });
187
188 GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
189 text.setLayoutData(gridData);
190 text.setFocus();
191 }
192
193
194 /**
195 * Shows the results of the search.
196 */
197 public void updateEditorInput() {
198
199 String searchString = getSearchString().trim();
200
201 if(DEFAULT_TEXT.equals(searchString) || CdmUtils.isBlank(searchString)){
202 return;
203 }
204
205 BulkEditorQuery query = new BulkEditorQuery(searchString);
206 editor.performSearch(query);
207 }
208
209 /**
210 * Returns the current string in the search textfield.
211 * @return the content of the textfield
212 */
213 public String getSearchString() {
214 return text.getText().trim();
215 }
216
217 public void setFocus() {
218 if(text != null && ! text.isDisposed()){
219 text.setFocus();
220 }
221 }
222 }