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
.IStructuredSelection
;
25 import org
.eclipse
.jface
.viewers
.TableViewer
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Control
;
31 import org
.eclipse
.swt
.widgets
.Display
;
32 import org
.eclipse
.swt
.widgets
.Label
;
33 import org
.eclipse
.swt
.widgets
.Menu
;
34 import org
.eclipse
.swt
.widgets
.Text
;
35 import org
.eclipse
.ui
.IMemento
;
36 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
37 import org
.eclipse
.ui
.part
.ViewPart
;
39 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
40 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
41 import eu
.etaxonomy
.cdm
.api
.service
.config
.IFindTaxaAndNamesConfigurator
;
42 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
43 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
44 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
45 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
46 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
47 import eu
.etaxonomy
.taxeditor
.model
.ContextListenerAdapter
;
48 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
49 import eu
.etaxonomy
.taxeditor
.navigation
.search
.SearchBar
.SearchOption
;
50 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
51 import eu
.etaxonomy
.taxeditor
.view
.CdmViewerChooser
;
54 * <p>SearchResultView class.</p>
61 public class SearchResultView
extends ViewPart
implements IConversationEnabled
{
63 private static Object
[] EMPTY
= new Object
[0];
65 private class ContextListener
extends ContextListenerAdapter
{
67 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
70 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
71 monitor
.subTask("Getting rid of search results");
72 AbstractUtility
.hideView(SearchResultView
.this);
76 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.searc"{trunked}</code> */
77 public static final String ID
=
78 "eu.etaxonomy.taxeditor.navigation.search.searchResultView"; //$NON-NLS-1$
80 private TableViewer resultViewer
;
82 private ConversationHolder conversation
;
84 private Text searchString
;
86 private Text configurationLabel
;
90 private SearchJob searchJob
;
92 private IContextListener contextListener
;
95 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
99 public void createPartControl(Composite parent
) {
101 conversation
= CdmStore
.createConversation();
102 contextListener
= new ContextListener();
103 CdmStore
.getContextManager().addContextListener(contextListener
);
105 GridLayout layout
= new GridLayout();
106 layout
.marginWidth
= 0;
107 layout
.marginHeight
= 0;
109 parent
.setLayout(layout
);
111 Composite infoComposite
= createInfoComposite(parent
);
112 infoComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
114 resultViewer
= new TableViewer(parent
, SWT
.NONE
);
115 resultViewer
.setContentProvider(new ArrayContentProvider());
116 resultViewer
.setLabelProvider(new SearchResultLabelProvider());
117 resultViewer
.addDoubleClickListener(new IDoubleClickListener() {
119 public void doubleClick(DoubleClickEvent event
) {
120 if(event
.getSelection() instanceof IStructuredSelection
){
121 Object selectedObject
= ((IStructuredSelection
) event
.getSelection()).getFirstElement();
122 CdmViewerChooser chooser
= new CdmViewerChooser(getSite().getShell());
123 chooser
.chooseViewer(selectedObject
);
128 resultViewer
.getControl().setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
130 getSite().setSelectionProvider(resultViewer
);
132 // register context menu
133 MenuManager menuMgr
= new MenuManager();
134 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
135 getSite().registerContextMenu(menuMgr
, resultViewer
);
137 Control control
= resultViewer
.getControl();
138 Menu menu
= menuMgr
.createContextMenu(control
);
139 control
.setMenu(menu
);
142 private Composite
createInfoComposite(Composite parent
){
143 Composite composite
= new Composite(parent
, SWT
.NULL
);
145 composite
.setLayout(new GridLayout(2, false));
147 Label searchStringLabel
= new Label(composite
, SWT
.NULL
);
148 searchStringLabel
.setText("Search String:");
150 searchString
= new Text(composite
, SWT
.NULL
);
151 searchString
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
152 searchString
.setEditable(false);
153 // searchString.setText(" ");
155 Label configurationDescriptionLabel
= new Label(composite
, SWT
.NULL
);
156 configurationDescriptionLabel
.setText("Search for:");
158 configurationLabel
= new Text(composite
, SWT
.WRAP
);
159 configurationLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
160 configurationLabel
.setEditable(false);
162 Label statusLabel
= new Label(composite
, SWT
.NULL
);
163 statusLabel
.setText("Status:");
165 status
= new Text(composite
, SWT
.NULL
);
166 status
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
167 status
.setEditable(false);
173 * <p>performSearch</p>
175 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
177 public void performSearch(IFindTaxaAndNamesConfigurator configurator
){
178 setPartName("Search: '" + configurator
.getTitleSearchString() + "'");
180 searchString
.setText(configurator
.getTitleSearchString());
182 List
<String
> includedEntities
= new ArrayList
<String
>();
183 if(configurator
.isDoTaxa()) {
184 includedEntities
.add(SearchOption
.TAXON
.getLabel());
186 if(configurator
.isDoSynonyms()) {
187 includedEntities
.add(SearchOption
.SYNONYM
.getLabel());
189 if(configurator
.isDoNamesWithoutTaxa()) {
190 includedEntities
.add(SearchOption
.NAME
.getLabel());
192 if(configurator
.isDoTaxaByCommonNames()){
193 includedEntities
.add(SearchOption
.COMMON_NAME
.getLabel());
195 configurator
.setMatchMode(MatchMode
.LIKE
);
197 String includedEntitiesString
= "";
198 for (int i
= 0; i
< includedEntities
.size(); i
++){
199 includedEntitiesString
+= includedEntities
.get(i
);
200 if(i
< includedEntities
.size() -1){
201 includedEntitiesString
+= ", ";
205 configurationLabel
.setText(includedEntitiesString
);
207 status
.setText("Searching...");
209 searchJob
= new SearchJob(Display
.getCurrent(), configurator
);
210 searchJob
.schedule();
215 * <p>displaySearchResult</p>
217 * @param result a {@link java.util.List} object.
219 protected void displaySearchResult(List
<UuidAndTitleCache
<IdentifiableEntity
>> result
) {
220 if(result
.size() > 0){
221 resultViewer
.setInput(result
);
222 status
.setText(result
.size() + " entities found");
224 resultViewer
.setInput(EMPTY
);
225 status
.setText("Search returned no results");
230 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
234 public void setFocus() {
235 //logger.warn("Setting focus to search result viewer");
237 // pass focus to resultViewer
238 resultViewer
.getControl().setFocus();
242 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
245 * <p>getConversationHolder</p>
247 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
250 public ConversationHolder
getConversationHolder() {
251 return this.conversation
;
255 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
259 public void update(CdmDataChangeMap changeEvents
) {
260 // TODO Auto-generated method stub
265 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
269 public void dispose() {
271 conversation
.close();
272 if(searchJob
!= null) {
275 CdmStore
.getContextManager().removeContextListener(contextListener
);
281 * @created Feb 2, 2010
284 class SearchJob
extends Job
{
286 private final IFindTaxaAndNamesConfigurator configurator
;
288 private final Display display
;
293 public SearchJob(Display display
, IFindTaxaAndNamesConfigurator configurator
) {
294 super("Performing Search");
295 this.display
= display
;
296 this.configurator
= configurator
;
300 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
303 protected IStatus
run(IProgressMonitor monitor
) {
304 monitor
.beginTask("", 100);
307 final List
<UuidAndTitleCache
<IdentifiableEntity
>> searchResult
= CdmStore
.getSearchManager().findTaxaAndNames(configurator
);
310 if(! monitor
.isCanceled()){
311 display
.asyncExec(new Runnable() {
314 displaySearchResult(searchResult
);
318 display
.asyncExec(new Runnable() {
321 status
.setText("Cancelled");
326 return Status
.OK_STATUS
;