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 class ContextListener
extends ContextListenerAdapter
{
62 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
65 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
66 monitor
.subTask("Getting rid of search results");
67 NavigationUtil
.hideView(SearchResultView
.this);
71 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.searc"{trunked}</code> */
72 public static final String ID
=
73 "eu.etaxonomy.taxeditor.navigation.search.searchResultView"; //$NON-NLS-1$
75 private TableViewer resultViewer
;
77 private ConversationHolder conversation
;
79 private Text searchString
;
81 private Text configurationLabel
;
85 private SearchJob searchJob
;
87 private IContextListener contextListener
;
90 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
94 public void createPartControl(Composite parent
) {
96 conversation
= CdmStore
.createConversation();
97 contextListener
= new ContextListener();
98 CdmStore
.getContextManager().addContextListener(contextListener
);
100 GridLayout layout
= new GridLayout();
101 layout
.marginWidth
= 0;
102 layout
.marginHeight
= 0;
104 parent
.setLayout(layout
);
106 Composite infoComposite
= createInfoComposite(parent
);
107 infoComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
109 resultViewer
= new TableViewer(parent
, SWT
.NONE
);
110 resultViewer
.setContentProvider(new ArrayContentProvider());
111 resultViewer
.setLabelProvider(new SearchResultLabelProvider());
112 resultViewer
.addDoubleClickListener(new IDoubleClickListener() {
113 public void doubleClick(DoubleClickEvent event
) {
114 NavigationUtil
.executeEditHandler();
118 resultViewer
.getControl().setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
120 getSite().setSelectionProvider(resultViewer
);
122 // register context menu
123 MenuManager menuMgr
= new MenuManager();
124 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
125 getSite().registerContextMenu(menuMgr
, resultViewer
);
127 Control control
= resultViewer
.getControl();
128 Menu menu
= menuMgr
.createContextMenu(control
);
129 control
.setMenu(menu
);
132 private Composite
createInfoComposite(Composite parent
){
133 Composite composite
= new Composite(parent
, SWT
.NULL
);
135 composite
.setLayout(new GridLayout(2, false));
137 Label searchStringLabel
= new Label(composite
, SWT
.NULL
);
138 searchStringLabel
.setText("Search String:");
140 searchString
= new Text(composite
, SWT
.NULL
);
141 searchString
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
142 searchString
.setEditable(false);
143 // searchString.setText(" ");
145 Label configurationDescriptionLabel
= new Label(composite
, SWT
.NULL
);
146 configurationDescriptionLabel
.setText("Search for:");
148 configurationLabel
= new Text(composite
, SWT
.WRAP
);
149 configurationLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
150 configurationLabel
.setEditable(false);
152 Label statusLabel
= new Label(composite
, SWT
.NULL
);
153 statusLabel
.setText("Status:");
155 status
= new Text(composite
, SWT
.NULL
);
156 status
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
157 status
.setEditable(false);
163 * <p>performSearch</p>
165 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
167 public void performSearch(ITaxonServiceConfigurator configurator
){
168 setPartName("Search: '" + configurator
.getSearchString() + "'");
170 searchString
.setText(configurator
.getSearchString());
172 List
<String
> includedEntities
= new ArrayList
<String
>();
173 if(configurator
.isDoTaxa())
174 includedEntities
.add(SearchOption
.TAXON
.getLabel());
175 if(configurator
.isDoSynonyms())
176 includedEntities
.add(SearchOption
.SYNONYM
.getLabel());
177 if(configurator
.isDoNamesWithoutTaxa())
178 includedEntities
.add(SearchOption
.NAME
.getLabel());
179 if(configurator
.isDoTaxaByCommonNames()){
180 includedEntities
.add(SearchOption
.COMMON_NAME
.getLabel());
183 String includedEntitiesString
= "";
184 for (int i
= 0; i
< includedEntities
.size(); i
++){
185 includedEntitiesString
+= includedEntities
.get(i
);
186 if(i
< includedEntities
.size() -1){
187 includedEntitiesString
+= ", ";
191 configurationLabel
.setText(includedEntitiesString
);
193 status
.setText("Searching...");
195 searchJob
= new SearchJob(Display
.getCurrent(), configurator
);
196 searchJob
.schedule();
201 * <p>displaySearchResult</p>
203 * @param result a {@link java.util.List} object.
205 protected void displaySearchResult(List
<UuidAndTitleCache
<TaxonBase
>> result
) {
206 if(result
.size() > 0){
207 resultViewer
.setInput(result
);
208 status
.setText(result
.size() + " entities found");
210 status
.setText("Search returned no results");
215 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
219 public void setFocus() {
220 //logger.warn("Setting focus to search result viewer");
222 // pass focus to resultViewer
223 resultViewer
.getControl().setFocus();
227 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
230 * <p>getConversationHolder</p>
232 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
234 public ConversationHolder
getConversationHolder() {
235 return this.conversation
;
239 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
242 public void update(CdmDataChangeMap changeEvents
) {
243 // TODO Auto-generated method stub
248 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
252 public void dispose() {
254 conversation
.close();
255 if(searchJob
!= null)
257 CdmStore
.getContextManager().removeContextListener(contextListener
);
263 * @created Feb 2, 2010
266 class SearchJob
extends Job
{
268 private ITaxonServiceConfigurator configurator
;
270 private Display display
;
275 public SearchJob(Display display
, ITaxonServiceConfigurator configurator
) {
276 super("Performing Search");
277 this.display
= display
;
278 this.configurator
= configurator
;
282 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
285 protected IStatus
run(IProgressMonitor monitor
) {
286 monitor
.beginTask("", 100);
289 final List
<UuidAndTitleCache
<TaxonBase
>> searchResult
= CdmStore
.getSearchManager().findTaxaAndNames(configurator
);
292 if(! monitor
.isCanceled()){
293 display
.asyncExec(new Runnable() {
295 displaySearchResult(searchResult
);
299 display
.asyncExec(new Runnable() {
301 status
.setText("Cancelled");
306 return Status
.OK_STATUS
;