Project

General

Profile

Download (9.35 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.navigation.search;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14

    
15
import org.eclipse.core.runtime.IProgressMonitor;
16
import org.eclipse.core.runtime.IStatus;
17
import org.eclipse.core.runtime.Status;
18
import org.eclipse.core.runtime.jobs.Job;
19
import org.eclipse.jface.action.GroupMarker;
20
import org.eclipse.jface.action.MenuManager;
21
import org.eclipse.jface.viewers.ArrayContentProvider;
22
import org.eclipse.jface.viewers.DoubleClickEvent;
23
import org.eclipse.jface.viewers.IDoubleClickListener;
24
import org.eclipse.jface.viewers.TableViewer;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.layout.GridData;
27
import org.eclipse.swt.layout.GridLayout;
28
import org.eclipse.swt.widgets.Composite;
29
import org.eclipse.swt.widgets.Control;
30
import org.eclipse.swt.widgets.Display;
31
import org.eclipse.swt.widgets.Label;
32
import org.eclipse.swt.widgets.Menu;
33
import org.eclipse.swt.widgets.Text;
34
import org.eclipse.ui.IMemento;
35
import org.eclipse.ui.IWorkbenchActionConstants;
36
import org.eclipse.ui.part.ViewPart;
37

    
38
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
39
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
40
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
41
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
42
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
44
import eu.etaxonomy.taxeditor.model.ContextListenerAdapter;
45
import eu.etaxonomy.taxeditor.model.IContextListener;
46
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
47
import eu.etaxonomy.taxeditor.navigation.search.SearchBar.SearchOption;
48
import eu.etaxonomy.taxeditor.store.CdmStore;
49

    
50
/**
51
 * <p>SearchResultView class.</p>
52
 *
53
 * @author p.ciardelli
54
 * @author n.hoffmann
55
 * @created 19.01.2009
56
 * @version 1.0
57
 */
58
public class SearchResultView extends ViewPart implements IConversationEnabled{
59
	
60
	private static Object[] EMPTY = new Object[0];
61
	
62
	private class ContextListener extends ContextListenerAdapter{
63
		/* (non-Javadoc)
64
		 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
65
		 */
66
		@Override
67
		public void contextStop(IMemento memento, IProgressMonitor monitor) {
68
			monitor.subTask("Getting rid of search results");
69
			NavigationUtil.hideView(SearchResultView.this);
70
		}
71
	}
72
	
73
	/** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.searc"{trunked}</code> */
74
	public static final String ID = 
75
			"eu.etaxonomy.taxeditor.navigation.search.searchResultView"; //$NON-NLS-1$
76

    
77
	private TableViewer resultViewer;
78

    
79
	private ConversationHolder conversation;
80

    
81
	private Text searchString;
82

    
83
	private Text configurationLabel;
84

    
85
	private Text status;
86

    
87
	private SearchJob searchJob;
88
	
89
	private IContextListener contextListener;
90
	
91
	/* (non-Javadoc)
92
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
93
	 */
94
	/** {@inheritDoc} */
95
	@Override
96
	public void createPartControl(Composite parent) {
97
		
98
		conversation = CdmStore.createConversation();
99
		contextListener = new ContextListener();
100
		CdmStore.getContextManager().addContextListener(contextListener);
101
		
102
		GridLayout layout = new GridLayout();
103
		layout.marginWidth = 0;
104
		layout.marginHeight = 0;
105
		
106
		parent.setLayout(layout);
107
		
108
		Composite infoComposite = createInfoComposite(parent);
109
		infoComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
110
		
111
		resultViewer = new TableViewer(parent, SWT.NONE);
112
		resultViewer.setContentProvider(new ArrayContentProvider());
113
		resultViewer.setLabelProvider(new SearchResultLabelProvider());
114
		resultViewer.addDoubleClickListener(new IDoubleClickListener() {
115
			public void doubleClick(DoubleClickEvent event) {
116
				NavigationUtil.executeEditHandler();
117
			}
118
		});
119
		
120
		resultViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
121
		
122
		getSite().setSelectionProvider(resultViewer);
123
		
124
		// register context menu
125
		MenuManager menuMgr = new MenuManager();
126
		menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
127
		getSite().registerContextMenu(menuMgr, resultViewer);
128

    
129
		Control control = resultViewer.getControl();
130
		Menu menu = menuMgr.createContextMenu(control);
131
		control.setMenu(menu);	
132
	}
133
	
134
	private Composite createInfoComposite(Composite parent){
135
		Composite composite = new Composite(parent, SWT.NULL);
136
		
137
		composite.setLayout(new GridLayout(2, false));
138
		
139
		Label searchStringLabel = new Label(composite, SWT.NULL);
140
		searchStringLabel.setText("Search String:");
141
		
142
		searchString = new Text(composite, SWT.NULL);
143
		searchString.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
144
		searchString.setEditable(false);
145
//		searchString.setText("                                               ");
146
		
147
		Label configurationDescriptionLabel = new Label(composite, SWT.NULL);
148
		configurationDescriptionLabel.setText("Search for:");
149
		
150
		configurationLabel = new Text(composite, SWT.WRAP);
151
		configurationLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
152
		configurationLabel.setEditable(false);
153
		
154
		Label statusLabel = new Label(composite, SWT.NULL);
155
		statusLabel.setText("Status:");
156
		
157
		status = new Text(composite, SWT.NULL);
158
		status.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
159
		status.setEditable(false);
160
		
161
		return composite;
162
	}
163
	
164
	/**
165
	 * <p>performSearch</p>
166
	 *
167
	 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
168
	 */
169
	public void performSearch(IFindTaxaAndNamesConfigurator configurator){
170
		setPartName("Search: '" + configurator.getTitleSearchString() + "'");
171
		
172
		searchString.setText(configurator.getTitleSearchString());
173
		
174
		List<String> includedEntities = new ArrayList<String>();
175
		if(configurator.isDoTaxa())
176
			includedEntities.add(SearchOption.TAXON.getLabel());
177
		if(configurator.isDoSynonyms())
178
			includedEntities.add(SearchOption.SYNONYM.getLabel());
179
		if(configurator.isDoNamesWithoutTaxa())
180
			includedEntities.add(SearchOption.NAME.getLabel());
181
		if(configurator.isDoTaxaByCommonNames()){
182
			includedEntities.add(SearchOption.COMMON_NAME.getLabel());
183
		}
184
		
185
		String includedEntitiesString = "";
186
		for (int i = 0; i < includedEntities.size(); i++){
187
			includedEntitiesString += includedEntities.get(i);
188
			if(i < includedEntities.size() -1){
189
				includedEntitiesString += ", ";
190
			}
191
		}
192
		
193
		configurationLabel.setText(includedEntitiesString);
194
		
195
		status.setText("Searching...");
196
		
197
		searchJob = new SearchJob(Display.getCurrent(), configurator);
198
		searchJob.schedule();
199
		
200
	}
201
	
202
	/**
203
	 * <p>displaySearchResult</p>
204
	 *
205
	 * @param result a {@link java.util.List} object.
206
	 */
207
	protected void displaySearchResult(List<UuidAndTitleCache<TaxonBase>> result) {
208
		if(result.size() > 0){
209
			resultViewer.setInput(result); 
210
			status.setText(result.size() + " entities found");
211
		}else{
212
			resultViewer.setInput(EMPTY); 
213
			status.setText("Search returned no results");
214
		}
215
	}
216

    
217
	/* (non-Javadoc)
218
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
219
	 */
220
	/** {@inheritDoc} */
221
	@Override
222
	public void setFocus() {
223
		//logger.warn("Setting focus to search result viewer");
224
		conversation.bind();
225
		// pass focus to resultViewer
226
		resultViewer.getControl().setFocus();
227
	}
228

    
229
	/* (non-Javadoc)
230
	 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
231
	 */
232
	/**
233
	 * <p>getConversationHolder</p>
234
	 *
235
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
236
	 */
237
	public ConversationHolder getConversationHolder() {
238
		return this.conversation;
239
	}
240

    
241
	/* (non-Javadoc)
242
	 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
243
	 */
244
	/** {@inheritDoc} */
245
	public void update(CdmDataChangeMap changeEvents) {
246
		// TODO Auto-generated method stub
247
		
248
	}
249
	
250
	/* (non-Javadoc)
251
	 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
252
	 */
253
	/** {@inheritDoc} */
254
	@Override
255
	public void dispose() {
256
		super.dispose();
257
		conversation.close();
258
		if(searchJob != null)
259
			searchJob.cancel();
260
		CdmStore.getContextManager().removeContextListener(contextListener);
261
	}
262
	
263
	/**
264
	 * 
265
	 * @author n.hoffmann
266
	 * @created Feb 2, 2010
267
	 * @version 1.0
268
	 */
269
	class SearchJob extends Job{
270

    
271
		private IFindTaxaAndNamesConfigurator configurator;
272
		
273
		private Display display;
274
		
275
		/**
276
		 * @param name
277
		 */
278
		public SearchJob(Display display, IFindTaxaAndNamesConfigurator configurator) {
279
			super("Performing Search");
280
			this.display = display;
281
			this.configurator = configurator;
282
		}
283

    
284
		/* (non-Javadoc)
285
		 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
286
		 */
287
		@Override
288
		protected IStatus run(IProgressMonitor monitor) {
289
			monitor.beginTask("", 100);
290
			monitor.worked(20);
291
			
292
			final List<UuidAndTitleCache<TaxonBase>> searchResult = CdmStore.getSearchManager().findTaxaAndNames(configurator);
293
			monitor.worked(40);
294
			
295
			if(! monitor.isCanceled()){
296
				display.asyncExec(new Runnable() {
297
					public void run() {
298
						displaySearchResult(searchResult);
299
					}
300
				});
301
			}else{
302
				display.asyncExec(new Runnable() {
303
					public void run() {
304
						status.setText("Cancelled");
305
					}
306
				});
307
			}
308
			monitor.done();
309
			return Status.OK_STATUS;
310
		}
311
		
312
	}	
313
}
(3-3/3)