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
.ITaxonServiceConfigurator
;
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
;
51 * <p>SearchResultView class.</p>
58 public class SearchResultView
extends ViewPart
implements IConversationEnabled
{
60 private static Object
[] EMPTY
= new Object
[0];
62 private class ContextListener
extends ContextListenerAdapter
{
64 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
67 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
68 monitor
.subTask("Getting rid of search results");
69 NavigationUtil
.hideView(SearchResultView
.this);
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$
77 private TableViewer resultViewer
;
79 private ConversationHolder conversation
;
81 private Text searchString
;
83 private Text configurationLabel
;
87 private SearchJob searchJob
;
89 private IContextListener contextListener
;
92 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
96 public void createPartControl(Composite parent
) {
98 conversation
= CdmStore
.createConversation();
99 contextListener
= new ContextListener();
100 CdmStore
.getContextManager().addContextListener(contextListener
);
102 GridLayout layout
= new GridLayout();
103 layout
.marginWidth
= 0;
104 layout
.marginHeight
= 0;
106 parent
.setLayout(layout
);
108 Composite infoComposite
= createInfoComposite(parent
);
109 infoComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
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();
120 resultViewer
.getControl().setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
122 getSite().setSelectionProvider(resultViewer
);
124 // register context menu
125 MenuManager menuMgr
= new MenuManager();
126 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
127 getSite().registerContextMenu(menuMgr
, resultViewer
);
129 Control control
= resultViewer
.getControl();
130 Menu menu
= menuMgr
.createContextMenu(control
);
131 control
.setMenu(menu
);
134 private Composite
createInfoComposite(Composite parent
){
135 Composite composite
= new Composite(parent
, SWT
.NULL
);
137 composite
.setLayout(new GridLayout(2, false));
139 Label searchStringLabel
= new Label(composite
, SWT
.NULL
);
140 searchStringLabel
.setText("Search String:");
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(" ");
147 Label configurationDescriptionLabel
= new Label(composite
, SWT
.NULL
);
148 configurationDescriptionLabel
.setText("Search for:");
150 configurationLabel
= new Text(composite
, SWT
.WRAP
);
151 configurationLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
152 configurationLabel
.setEditable(false);
154 Label statusLabel
= new Label(composite
, SWT
.NULL
);
155 statusLabel
.setText("Status:");
157 status
= new Text(composite
, SWT
.NULL
);
158 status
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
159 status
.setEditable(false);
165 * <p>performSearch</p>
167 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
169 public void performSearch(ITaxonServiceConfigurator configurator
){
170 setPartName("Search: '" + configurator
.getTitleSearchString() + "'");
172 searchString
.setText(configurator
.getTitleSearchString());
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());
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
+= ", ";
193 configurationLabel
.setText(includedEntitiesString
);
195 status
.setText("Searching...");
197 searchJob
= new SearchJob(Display
.getCurrent(), configurator
);
198 searchJob
.schedule();
203 * <p>displaySearchResult</p>
205 * @param result a {@link java.util.List} object.
207 protected void displaySearchResult(List
<UuidAndTitleCache
<TaxonBase
>> result
) {
208 if(result
.size() > 0){
209 resultViewer
.setInput(result
);
210 status
.setText(result
.size() + " entities found");
212 resultViewer
.setInput(EMPTY
);
213 status
.setText("Search returned no results");
218 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
222 public void setFocus() {
223 //logger.warn("Setting focus to search result viewer");
225 // pass focus to resultViewer
226 resultViewer
.getControl().setFocus();
230 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
233 * <p>getConversationHolder</p>
235 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
237 public ConversationHolder
getConversationHolder() {
238 return this.conversation
;
242 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
245 public void update(CdmDataChangeMap changeEvents
) {
246 // TODO Auto-generated method stub
251 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
255 public void dispose() {
257 conversation
.close();
258 if(searchJob
!= null)
260 CdmStore
.getContextManager().removeContextListener(contextListener
);
266 * @created Feb 2, 2010
269 class SearchJob
extends Job
{
271 private ITaxonServiceConfigurator configurator
;
273 private Display display
;
278 public SearchJob(Display display
, ITaxonServiceConfigurator configurator
) {
279 super("Performing Search");
280 this.display
= display
;
281 this.configurator
= configurator
;
285 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
288 protected IStatus
run(IProgressMonitor monitor
) {
289 monitor
.beginTask("", 100);
292 final List
<UuidAndTitleCache
<TaxonBase
>> searchResult
= CdmStore
.getSearchManager().findTaxaAndNames(configurator
);
295 if(! monitor
.isCanceled()){
296 display
.asyncExec(new Runnable() {
298 displaySearchResult(searchResult
);
302 display
.asyncExec(new Runnable() {
304 status
.setText("Cancelled");
309 return Status
.OK_STATUS
;