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
.UuidAndTitleCache
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
43 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
44 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
45 import eu
.etaxonomy
.taxeditor
.model
.ContextListenerAdapter
;
46 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
47 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
48 import eu
.etaxonomy
.taxeditor
.navigation
.search
.SearchBar
.SearchOption
;
49 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
52 * <p>SearchResultView class.</p>
59 public class SearchResultView
extends ViewPart
implements IConversationEnabled
{
61 private static Object
[] EMPTY
= new Object
[0];
63 private class ContextListener
extends ContextListenerAdapter
{
65 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
68 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
69 monitor
.subTask("Getting rid of search results");
70 AbstractUtility
.hideView(SearchResultView
.this);
74 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.searc"{trunked}</code> */
75 public static final String ID
=
76 "eu.etaxonomy.taxeditor.navigation.search.searchResultView"; //$NON-NLS-1$
78 private TableViewer resultViewer
;
80 private ConversationHolder conversation
;
82 private Text searchString
;
84 private Text configurationLabel
;
88 private SearchJob searchJob
;
90 private IContextListener contextListener
;
93 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
97 public void createPartControl(Composite parent
) {
99 conversation
= CdmStore
.createConversation();
100 contextListener
= new ContextListener();
101 CdmStore
.getContextManager().addContextListener(contextListener
);
103 GridLayout layout
= new GridLayout();
104 layout
.marginWidth
= 0;
105 layout
.marginHeight
= 0;
107 parent
.setLayout(layout
);
109 Composite infoComposite
= createInfoComposite(parent
);
110 infoComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
112 resultViewer
= new TableViewer(parent
, SWT
.NONE
);
113 resultViewer
.setContentProvider(new ArrayContentProvider());
114 resultViewer
.setLabelProvider(new SearchResultLabelProvider());
115 resultViewer
.addDoubleClickListener(new IDoubleClickListener() {
117 public void doubleClick(DoubleClickEvent event
) {
118 NavigationUtil
.executeEditHandler();
122 resultViewer
.getControl().setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
124 getSite().setSelectionProvider(resultViewer
);
126 // register context menu
127 MenuManager menuMgr
= new MenuManager();
128 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
129 getSite().registerContextMenu(menuMgr
, resultViewer
);
131 Control control
= resultViewer
.getControl();
132 Menu menu
= menuMgr
.createContextMenu(control
);
133 control
.setMenu(menu
);
136 private Composite
createInfoComposite(Composite parent
){
137 Composite composite
= new Composite(parent
, SWT
.NULL
);
139 composite
.setLayout(new GridLayout(2, false));
141 Label searchStringLabel
= new Label(composite
, SWT
.NULL
);
142 searchStringLabel
.setText("Search String:");
144 searchString
= new Text(composite
, SWT
.NULL
);
145 searchString
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
146 searchString
.setEditable(false);
147 // searchString.setText(" ");
149 Label configurationDescriptionLabel
= new Label(composite
, SWT
.NULL
);
150 configurationDescriptionLabel
.setText("Search for:");
152 configurationLabel
= new Text(composite
, SWT
.WRAP
);
153 configurationLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
154 configurationLabel
.setEditable(false);
156 Label statusLabel
= new Label(composite
, SWT
.NULL
);
157 statusLabel
.setText("Status:");
159 status
= new Text(composite
, SWT
.NULL
);
160 status
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
161 status
.setEditable(false);
167 * <p>performSearch</p>
169 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
171 public void performSearch(IFindTaxaAndNamesConfigurator configurator
){
172 setPartName("Search: '" + configurator
.getTitleSearchString() + "'");
174 searchString
.setText(configurator
.getTitleSearchString());
176 List
<String
> includedEntities
= new ArrayList
<String
>();
177 if(configurator
.isDoTaxa()) {
178 includedEntities
.add(SearchOption
.TAXON
.getLabel());
180 if(configurator
.isDoSynonyms()) {
181 includedEntities
.add(SearchOption
.SYNONYM
.getLabel());
183 if(configurator
.isDoNamesWithoutTaxa()) {
184 includedEntities
.add(SearchOption
.NAME
.getLabel());
186 if(configurator
.isDoTaxaByCommonNames()){
187 includedEntities
.add(SearchOption
.COMMON_NAME
.getLabel());
190 String includedEntitiesString
= "";
191 for (int i
= 0; i
< includedEntities
.size(); i
++){
192 includedEntitiesString
+= includedEntities
.get(i
);
193 if(i
< includedEntities
.size() -1){
194 includedEntitiesString
+= ", ";
198 configurationLabel
.setText(includedEntitiesString
);
200 status
.setText("Searching...");
202 searchJob
= new SearchJob(Display
.getCurrent(), configurator
);
203 searchJob
.schedule();
208 * <p>displaySearchResult</p>
210 * @param result a {@link java.util.List} object.
212 protected void displaySearchResult(List
<UuidAndTitleCache
<TaxonBase
>> result
) {
213 if(result
.size() > 0){
214 resultViewer
.setInput(result
);
215 status
.setText(result
.size() + " entities found");
217 resultViewer
.setInput(EMPTY
);
218 status
.setText("Search returned no results");
223 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
227 public void setFocus() {
228 //logger.warn("Setting focus to search result viewer");
230 // pass focus to resultViewer
231 resultViewer
.getControl().setFocus();
235 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
238 * <p>getConversationHolder</p>
240 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
243 public ConversationHolder
getConversationHolder() {
244 return this.conversation
;
248 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
252 public void update(CdmDataChangeMap changeEvents
) {
253 // TODO Auto-generated method stub
258 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
262 public void dispose() {
264 conversation
.close();
265 if(searchJob
!= null) {
268 CdmStore
.getContextManager().removeContextListener(contextListener
);
274 * @created Feb 2, 2010
277 class SearchJob
extends Job
{
279 private final IFindTaxaAndNamesConfigurator configurator
;
281 private final Display display
;
286 public SearchJob(Display display
, IFindTaxaAndNamesConfigurator configurator
) {
287 super("Performing Search");
288 this.display
= display
;
289 this.configurator
= configurator
;
293 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
296 protected IStatus
run(IProgressMonitor monitor
) {
297 monitor
.beginTask("", 100);
300 final List
<UuidAndTitleCache
<TaxonBase
>> searchResult
= CdmStore
.getSearchManager().findTaxaAndNames(configurator
);
303 if(! monitor
.isCanceled()){
304 display
.asyncExec(new Runnable() {
307 displaySearchResult(searchResult
);
311 display
.asyncExec(new Runnable() {
314 status
.setText("Cancelled");
319 return Status
.OK_STATUS
;