2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.bulkeditor
.e4
;
12 import java
.io
.FileInputStream
;
13 import java
.io
.IOException
;
14 import java
.util
.Properties
;
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
;
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
;
50 public class BulkEditorSearchE4
{
52 private static final String SEARCH
= Messages
.BulkEditorSearchE4_SEARCH
;
54 private static final String DEFAULT_TEXT
= String
.format(Messages
.BulkEditorSearchE4_WILDCARD
, SearchManager
.WILDCARD
);
56 private final BulkEditorE4Composite editor
;
58 private DisplayPersistenceDialogCommandHandler displayPersistenceDialogCommandHandler
;
60 private Properties natTableState
;
64 private Button button
;
66 private Button btnManageState
;
68 public Object ORDER_BY
= new Object();
70 public BulkEditorSearchE4(BulkEditorE4Composite editor
, Composite parent
, int style
) {
73 createControl(parent
, style
);
77 * Creates the search control.
79 protected void createControl(Composite parent
, int style
) {
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));
86 createSearchTextField(container
, SWT
.BORDER
| SWT
.SINGLE
| SWT
.FULL_SELECTION
);
88 button
= new Button(container
, SWT
.PUSH
);
89 button
.setText(SEARCH
);
90 button
.addSelectionListener(new SelectionAdapter() {
92 public void widgetSelected(SelectionEvent e
) {
98 * Table state persistence
100 natTableState
= new Properties();
101 //load persisted state
102 File statePropertiesFile
= getStatePropertiesFile();
103 FileInputStream inputStream
;
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$
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() {
118 public void widgetSelected(SelectionEvent e
) {
119 editor
.getNatTable().doCommand(new DisplayPersistenceDialogCommand(editor
.getNatTable()));
123 registerAtFocusService();
126 DisplayPersistenceDialogCommandHandler
getDisplayPersistenceDialogCommandHandler() {
127 return displayPersistenceDialogCommandHandler
;
130 File
getStatePropertiesFile() {
131 return new File(WorkbenchUtility
.getBaseLocation(), "bulkeditor_tablestate.properties"); //$NON-NLS-1$
134 Properties
getNatTableState() {
135 return natTableState
;
139 * Handles focus changes for the textfield.
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$
150 * Creates the search textfield.
152 private void createSearchTextField(Composite parent
, int style
) {
153 final Label label
= new Label(parent
, SWT
.NONE
);
154 label
.setText(Messages
.BulkEditorSearchE4_TITLE_CACHE
);
156 text
= new Text(parent
, style
);
157 text
.setText(DEFAULT_TEXT
);
158 text
.setForeground(ColorResources
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
160 text
.addFocusListener(new FocusListener() {
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$
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
);
179 text
.addKeyListener(new KeyAdapter() {
181 public void keyReleased(KeyEvent e
) {
182 if (e
.keyCode
== SWT
.CR
) {
188 GridData gridData
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
189 text
.setLayoutData(gridData
);
195 * Shows the results of the search.
197 public void updateEditorInput() {
199 String searchString
= getSearchString().trim();
201 if(DEFAULT_TEXT
.equals(searchString
) || CdmUtils
.isBlank(searchString
)){
205 BulkEditorQuery query
= new BulkEditorQuery(searchString
);
206 editor
.performSearch(query
);
210 * Returns the current string in the search textfield.
211 * @return the content of the textfield
213 public String
getSearchString() {
214 return text
.getText().trim();
217 public void setFocus() {
218 if(text
!= null && ! text
.isDisposed()){