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.
10 package eu
.etaxonomy
.taxeditor
.navigation
.search
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
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
;
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
.IdentifiableEntity
;
42 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
43 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
44 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
45 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
46 import eu
.etaxonomy
.taxeditor
.model
.ContextListenerAdapter
;
47 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
48 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
49 import eu
.etaxonomy
.taxeditor
.navigation
.search
.SearchBar
.SearchOption
;
50 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
53 * <p>SearchResultView class.</p>
60 public class SearchResultView
extends ViewPart
implements IConversationEnabled
{
62 private static Object
[] EMPTY
= new Object
[0];
64 private class ContextListener
extends ContextListenerAdapter
{
66 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
69 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
70 monitor
.subTask("Getting rid of search results");
71 AbstractUtility
.hideView(SearchResultView
.this);
75 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.searc"{trunked}</code> */
76 public static final String ID
=
77 "eu.etaxonomy.taxeditor.navigation.search.searchResultView"; //$NON-NLS-1$
79 private TableViewer resultViewer
;
81 private ConversationHolder conversation
;
83 private Text searchString
;
85 private Text configurationLabel
;
89 private SearchJob searchJob
;
91 private IContextListener contextListener
;
94 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
98 public void createPartControl(Composite parent
) {
100 conversation
= CdmStore
.createConversation();
101 contextListener
= new ContextListener();
102 CdmStore
.getContextManager().addContextListener(contextListener
);
104 GridLayout layout
= new GridLayout();
105 layout
.marginWidth
= 0;
106 layout
.marginHeight
= 0;
108 parent
.setLayout(layout
);
110 Composite infoComposite
= createInfoComposite(parent
);
111 infoComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
113 resultViewer
= new TableViewer(parent
, SWT
.NONE
);
114 resultViewer
.setContentProvider(new ArrayContentProvider());
115 resultViewer
.setLabelProvider(new SearchResultLabelProvider());
116 resultViewer
.addDoubleClickListener(new IDoubleClickListener() {
118 public void doubleClick(DoubleClickEvent event
) {
119 NavigationUtil
.executeEditHandler();
123 resultViewer
.getControl().setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
125 getSite().setSelectionProvider(resultViewer
);
127 // register context menu
128 MenuManager menuMgr
= new MenuManager();
129 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
130 getSite().registerContextMenu(menuMgr
, resultViewer
);
132 Control control
= resultViewer
.getControl();
133 Menu menu
= menuMgr
.createContextMenu(control
);
134 control
.setMenu(menu
);
137 private Composite
createInfoComposite(Composite parent
){
138 Composite composite
= new Composite(parent
, SWT
.NULL
);
140 composite
.setLayout(new GridLayout(2, false));
142 Label searchStringLabel
= new Label(composite
, SWT
.NULL
);
143 searchStringLabel
.setText("Search String:");
145 searchString
= new Text(composite
, SWT
.NULL
);
146 searchString
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
147 searchString
.setEditable(false);
148 // searchString.setText(" ");
150 Label configurationDescriptionLabel
= new Label(composite
, SWT
.NULL
);
151 configurationDescriptionLabel
.setText("Search for:");
153 configurationLabel
= new Text(composite
, SWT
.WRAP
);
154 configurationLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
155 configurationLabel
.setEditable(false);
157 Label statusLabel
= new Label(composite
, SWT
.NULL
);
158 statusLabel
.setText("Status:");
160 status
= new Text(composite
, SWT
.NULL
);
161 status
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
162 status
.setEditable(false);
168 * <p>performSearch</p>
170 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
172 public void performSearch(IFindTaxaAndNamesConfigurator configurator
){
173 setPartName("Search: '" + configurator
.getTitleSearchString() + "'");
175 searchString
.setText(configurator
.getTitleSearchString());
177 List
<String
> includedEntities
= new ArrayList
<String
>();
178 if(configurator
.isDoTaxa()) {
179 includedEntities
.add(SearchOption
.TAXON
.getLabel());
181 if(configurator
.isDoSynonyms()) {
182 includedEntities
.add(SearchOption
.SYNONYM
.getLabel());
184 if(configurator
.isDoNamesWithoutTaxa()) {
185 includedEntities
.add(SearchOption
.NAME
.getLabel());
187 if(configurator
.isDoTaxaByCommonNames()){
188 includedEntities
.add(SearchOption
.COMMON_NAME
.getLabel());
190 configurator
.setMatchMode(MatchMode
.LIKE
);
192 String includedEntitiesString
= "";
193 for (int i
= 0; i
< includedEntities
.size(); i
++){
194 includedEntitiesString
+= includedEntities
.get(i
);
195 if(i
< includedEntities
.size() -1){
196 includedEntitiesString
+= ", ";
200 configurationLabel
.setText(includedEntitiesString
);
202 status
.setText("Searching...");
204 searchJob
= new SearchJob(Display
.getCurrent(), configurator
);
205 searchJob
.schedule();
210 * <p>displaySearchResult</p>
212 * @param result a {@link java.util.List} object.
214 protected void displaySearchResult(List
<UuidAndTitleCache
<IdentifiableEntity
>> result
) {
215 if(result
.size() > 0){
216 resultViewer
.setInput(result
);
217 status
.setText(result
.size() + " entities found");
219 resultViewer
.setInput(EMPTY
);
220 status
.setText("Search returned no results");
225 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
229 public void setFocus() {
230 //logger.warn("Setting focus to search result viewer");
232 // pass focus to resultViewer
233 resultViewer
.getControl().setFocus();
237 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
240 * <p>getConversationHolder</p>
242 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
245 public ConversationHolder
getConversationHolder() {
246 return this.conversation
;
250 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
254 public void update(CdmDataChangeMap changeEvents
) {
255 // TODO Auto-generated method stub
260 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
264 public void dispose() {
266 conversation
.close();
267 if(searchJob
!= null) {
270 CdmStore
.getContextManager().removeContextListener(contextListener
);
276 * @created Feb 2, 2010
279 class SearchJob
extends Job
{
281 private final IFindTaxaAndNamesConfigurator configurator
;
283 private final Display display
;
288 public SearchJob(Display display
, IFindTaxaAndNamesConfigurator configurator
) {
289 super("Performing Search");
290 this.display
= display
;
291 this.configurator
= configurator
;
295 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
298 protected IStatus
run(IProgressMonitor monitor
) {
299 monitor
.beginTask("", 100);
302 final List
<UuidAndTitleCache
<IdentifiableEntity
>> searchResult
= CdmStore
.getSearchManager().findTaxaAndNames(configurator
);
305 if(! monitor
.isCanceled()){
306 display
.asyncExec(new Runnable() {
309 displaySearchResult(searchResult
);
313 display
.asyncExec(new Runnable() {
316 status
.setText("Cancelled");
321 return Status
.OK_STATUS
;