Project

General

Profile

Download (6.54 KB) Statistics
| Branch: | Tag: | Revision:
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.io.File;
13
import java.io.FileInputStream;
14
import java.io.IOException;
15
import java.util.Properties;
16

    
17
import org.eclipse.nebula.widgets.nattable.persistence.command.DisplayPersistenceDialogCommand;
18
import org.eclipse.nebula.widgets.nattable.persistence.command.DisplayPersistenceDialogCommandHandler;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.events.FocusEvent;
21
import org.eclipse.swt.events.FocusListener;
22
import org.eclipse.swt.events.KeyAdapter;
23
import org.eclipse.swt.events.KeyEvent;
24
import org.eclipse.swt.events.SelectionAdapter;
25
import org.eclipse.swt.events.SelectionEvent;
26
import org.eclipse.swt.layout.GridData;
27
import org.eclipse.swt.layout.GridLayout;
28
import org.eclipse.swt.widgets.Button;
29
import org.eclipse.swt.widgets.Composite;
30
import org.eclipse.swt.widgets.Label;
31
import org.eclipse.swt.widgets.Text;
32
import org.eclipse.ui.PlatformUI;
33
import org.eclipse.ui.swt.IFocusService;
34

    
35
import eu.etaxonomy.cdm.common.CdmUtils;
36
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
37
import eu.etaxonomy.taxeditor.l10n.Messages;
38
import eu.etaxonomy.taxeditor.model.ColorResources;
39
import eu.etaxonomy.taxeditor.model.ImageResources;
40
import eu.etaxonomy.taxeditor.model.MessagingUtils;
41
import eu.etaxonomy.taxeditor.preference.Resources;
42
import eu.etaxonomy.taxeditor.store.SearchManager;
43
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
44

    
45
/**
46
 * @author p.ciardelli
47
 * @author e.-m.lee
48
 * @author n.hoffmann
49
 * @created 17.08.2009
50
 * @version 1.0
51
 */
52
public class BulkEditorSearchE4 {
53

    
54
	private static final String SEARCH = Messages.BulkEditorSearchE4_SEARCH;
55

    
56
	private static final String DEFAULT_TEXT = String.format(Messages.BulkEditorSearchE4_WILDCARD, SearchManager.WILDCARD);
57

    
58
	private final BulkEditorE4Composite editor;
59

    
60
    private DisplayPersistenceDialogCommandHandler displayPersistenceDialogCommandHandler;
61

    
62
    private Properties natTableState;
63

    
64
	private Text text;
65

    
66
	private Button button;
67

    
68
	private Button btnManageState;
69

    
70

    
71
	public Object ORDER_BY = new Object();
72

    
73
	public BulkEditorSearchE4(BulkEditorE4Composite editor, Composite parent, int style) {
74
		this.editor = editor;
75

    
76
		createControl(parent, style);
77
	}
78

    
79
	/**
80
	 * Creates the search control.
81
	 */
82
	protected void createControl(Composite parent, int style) {
83

    
84
		final Composite container = new Composite(parent, style);
85
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
86
		container.setLayoutData(gridData);
87
		container.setLayout(new GridLayout(5, false));
88

    
89
		createSearchTextField(container, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
90

    
91
		button = new Button(container, SWT.PUSH);
92
		button.setText(SEARCH);
93
		button.addSelectionListener(new SelectionAdapter() {
94
			@Override
95
			public void widgetSelected(SelectionEvent e) {
96
				updateEditorInput();
97
			}
98
		});
99

    
100
		/**
101
		 * Table state persistence
102
		 */
103
		natTableState = new Properties();
104
		//load persisted state
105
		File statePropertiesFile = getStatePropertiesFile();
106
		FileInputStream inputStream;
107
		try {
108
		    inputStream = new FileInputStream(statePropertiesFile);
109
		    natTableState.load(inputStream);
110
		} catch (IOException e) {
111
		    MessagingUtils.info("No initial state properties file found for bulk editor"); //$NON-NLS-1$
112
		}
113

    
114
		// add settings button
115
		displayPersistenceDialogCommandHandler = new DisplayPersistenceDialogCommandHandler(natTableState, editor.getNatTable());
116
        btnManageState = new Button(container, SWT.PUSH);
117
        btnManageState.setImage(ImageResources.getImage(ImageResources.SETTINGS));
118
        btnManageState.setToolTipText(Messages.BulkEditorSearchE4_TABLE_SETTINGS);
119
        btnManageState.addSelectionListener(new SelectionAdapter() {
120
            @Override
121
            public void widgetSelected(SelectionEvent e) {
122
                editor.getNatTable().doCommand(new DisplayPersistenceDialogCommand(editor.getNatTable()));
123
            }
124
        });
125

    
126
		registerAtFocusService();
127
	}
128

    
129
    DisplayPersistenceDialogCommandHandler getDisplayPersistenceDialogCommandHandler() {
130
        return displayPersistenceDialogCommandHandler;
131
    }
132

    
133
    File getStatePropertiesFile() {
134
        return new File(WorkbenchUtility.getBaseLocation(), "bulkeditor_tablestate.properties"); //$NON-NLS-1$
135
    }
136

    
137
    Properties getNatTableState() {
138
        return natTableState;
139
    }
140

    
141
	/**
142
	 * Handles focus changes for the textfield.
143
	 */
144
	private void registerAtFocusService() {
145
		IFocusService focusService =
146
			PlatformUI.getWorkbench().getService(IFocusService.class);
147
		if (focusService != null) {
148
			focusService.addFocusTracker(text, "bulkeditor.textControlId"); //$NON-NLS-1$
149
		}
150
	}
151

    
152

    
153
	/**
154
	 * Creates the search textfield.
155
	 */
156
	private void createSearchTextField(Composite parent, int style) {
157
		final Label label = new Label(parent, SWT.NONE);
158
		label.setText(Messages.BulkEditorSearchE4_TITLE_CACHE);
159

    
160
		text = new Text(parent, style);
161
		text.setText(DEFAULT_TEXT);
162
		text.setForeground(ColorResources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
163

    
164
		text.addFocusListener(new FocusListener() {
165

    
166
			@Override
167
            public void focusGained(FocusEvent e) {
168
				text.setForeground(ColorResources.getColor(Resources.SEARCH_VIEW_FOCUS));
169
				if (DEFAULT_TEXT.equals(text.getText())) {
170
					text.setText(""); //$NON-NLS-1$
171
				}
172
			}
173

    
174
			@Override
175
            public void focusLost(FocusEvent e) {
176
				if (CdmUtils.isBlank(text.getText())) {
177
					text.setForeground(ColorResources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
178
					text.setText(DEFAULT_TEXT);
179
				}
180
			}
181
		});
182

    
183
		text.addKeyListener(new KeyAdapter() {
184
			@Override
185
			public void keyReleased(KeyEvent e) {
186
				if (e.keyCode == SWT.CR) {
187
					updateEditorInput();
188
				}
189
			}
190
		});
191

    
192
		GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
193
		text.setLayoutData(gridData);
194
		text.setFocus();
195
	}
196

    
197

    
198
	/**
199
	 * Shows the results of the search.
200
	 */
201
	public void updateEditorInput() {
202

    
203
		String searchString = getSearchString().trim();
204

    
205
		if(DEFAULT_TEXT.equals(searchString) || CdmUtils.isBlank(searchString)){
206
			return;
207
		}
208

    
209
		BulkEditorQuery query = new BulkEditorQuery(searchString);
210
		editor.performSearch(query);
211
	}
212

    
213
	/**
214
	 * Returns the current string in the search textfield.
215
	 * @return the content of the textfield
216
	 */
217
	public String getSearchString() {
218
		return text.getText().trim();
219
	}
220

    
221
	public void setFocus() {
222
		if(text != null && ! text.isDisposed()){
223
			text.setFocus();
224
		}
225
	}
226
}
(7-7/9)