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