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
.ISelection
;
25 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
26 import org
.eclipse
.jface
.viewers
.TableViewer
;
27 import org
.eclipse
.swt
.SWT
;
28 import org
.eclipse
.swt
.layout
.GridData
;
29 import org
.eclipse
.swt
.layout
.GridLayout
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.Control
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.swt
.widgets
.Label
;
34 import org
.eclipse
.swt
.widgets
.Menu
;
35 import org
.eclipse
.swt
.widgets
.Text
;
36 import org
.eclipse
.ui
.IMemento
;
37 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
38 import org
.eclipse
.ui
.part
.ViewPart
;
40 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
41 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
42 import eu
.etaxonomy
.cdm
.api
.service
.config
.IFindTaxaAndNamesConfigurator
;
43 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
44 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
45 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
46 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
47 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
48 import eu
.etaxonomy
.taxeditor
.model
.ContextListenerAdapter
;
49 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
50 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
51 import eu
.etaxonomy
.taxeditor
.navigation
.search
.SearchBar
.SearchOption
;
52 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
55 * <p>SearchResultView class.</p>
62 public class SearchResultView
extends ViewPart
implements IConversationEnabled
{
64 private static Object
[] EMPTY
= new Object
[0];
66 private class ContextListener
extends ContextListenerAdapter
{
68 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
71 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
72 monitor
.subTask("Getting rid of search results");
73 AbstractUtility
.hideView(SearchResultView
.this);
77 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.searc"{trunked}</code> */
78 public static final String ID
=
79 "eu.etaxonomy.taxeditor.navigation.search.searchResultView"; //$NON-NLS-1$
81 private TableViewer resultViewer
;
83 private ConversationHolder conversation
;
85 private Text searchString
;
87 private Text configurationLabel
;
91 private SearchJob searchJob
;
93 private IContextListener contextListener
;
96 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
100 public void createPartControl(Composite parent
) {
102 conversation
= CdmStore
.createConversation();
103 contextListener
= new ContextListener();
104 CdmStore
.getContextManager().addContextListener(contextListener
);
106 GridLayout layout
= new GridLayout();
107 layout
.marginWidth
= 0;
108 layout
.marginHeight
= 0;
110 parent
.setLayout(layout
);
112 Composite infoComposite
= createInfoComposite(parent
);
113 infoComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
115 resultViewer
= new TableViewer(parent
, SWT
.NONE
);
116 resultViewer
.setContentProvider(new ArrayContentProvider());
117 resultViewer
.setLabelProvider(new SearchResultLabelProvider());
118 resultViewer
.addDoubleClickListener(new IDoubleClickListener() {
120 public void doubleClick(DoubleClickEvent event
) {
121 ISelection selection
= event
.getSelection();
122 if(selection
instanceof IStructuredSelection
){
123 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
124 if(firstElement
instanceof UuidAndTitleCache
){
125 NavigationUtil
.openEditor((UuidAndTitleCache
) firstElement
);
131 resultViewer
.getControl().setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
133 getSite().setSelectionProvider(resultViewer
);
135 // register context menu
136 MenuManager menuMgr
= new MenuManager();
137 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
138 getSite().registerContextMenu(menuMgr
, resultViewer
);
140 Control control
= resultViewer
.getControl();
141 Menu menu
= menuMgr
.createContextMenu(control
);
142 control
.setMenu(menu
);
145 private Composite
createInfoComposite(Composite parent
){
146 Composite composite
= new Composite(parent
, SWT
.NULL
);
148 composite
.setLayout(new GridLayout(2, false));
150 Label searchStringLabel
= new Label(composite
, SWT
.NULL
);
151 searchStringLabel
.setText("Search String:");
153 searchString
= new Text(composite
, SWT
.NULL
);
154 searchString
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
155 searchString
.setEditable(false);
156 // searchString.setText(" ");
158 Label configurationDescriptionLabel
= new Label(composite
, SWT
.NULL
);
159 configurationDescriptionLabel
.setText("Search for:");
161 configurationLabel
= new Text(composite
, SWT
.WRAP
);
162 configurationLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
163 configurationLabel
.setEditable(false);
165 Label statusLabel
= new Label(composite
, SWT
.NULL
);
166 statusLabel
.setText("Status:");
168 status
= new Text(composite
, SWT
.NULL
);
169 status
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
170 status
.setEditable(false);
176 * <p>performSearch</p>
178 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
180 public void performSearch(IFindTaxaAndNamesConfigurator configurator
){
181 setPartName("Search: '" + configurator
.getTitleSearchString() + "'");
183 searchString
.setText(configurator
.getTitleSearchString());
185 List
<String
> includedEntities
= new ArrayList
<String
>();
186 if(configurator
.isDoTaxa()) {
187 includedEntities
.add(SearchOption
.TAXON
.getLabel());
189 if(configurator
.isDoSynonyms()) {
190 includedEntities
.add(SearchOption
.SYNONYM
.getLabel());
192 if(configurator
.isDoNamesWithoutTaxa()) {
193 includedEntities
.add(SearchOption
.NAME
.getLabel());
195 if(configurator
.isDoTaxaByCommonNames()){
196 includedEntities
.add(SearchOption
.COMMON_NAME
.getLabel());
198 configurator
.setMatchMode(MatchMode
.LIKE
);
200 String includedEntitiesString
= "";
201 for (int i
= 0; i
< includedEntities
.size(); i
++){
202 includedEntitiesString
+= includedEntities
.get(i
);
203 if(i
< includedEntities
.size() -1){
204 includedEntitiesString
+= ", ";
208 configurationLabel
.setText(includedEntitiesString
);
210 status
.setText("Searching...");
212 searchJob
= new SearchJob(Display
.getCurrent(), configurator
);
213 searchJob
.schedule();
218 * <p>displaySearchResult</p>
220 * @param result a {@link java.util.List} object.
222 protected void displaySearchResult(List
<UuidAndTitleCache
<IdentifiableEntity
>> result
) {
223 if(result
.size() > 0){
224 resultViewer
.setInput(result
);
225 status
.setText(result
.size() + " entities found");
227 resultViewer
.setInput(EMPTY
);
228 status
.setText("Search returned no results");
233 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
237 public void setFocus() {
238 //logger.warn("Setting focus to search result viewer");
240 // pass focus to resultViewer
241 resultViewer
.getControl().setFocus();
245 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
248 * <p>getConversationHolder</p>
250 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
253 public ConversationHolder
getConversationHolder() {
254 return this.conversation
;
258 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
262 public void update(CdmDataChangeMap changeEvents
) {
263 // TODO Auto-generated method stub
268 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
272 public void dispose() {
274 conversation
.close();
275 if(searchJob
!= null) {
278 CdmStore
.getContextManager().removeContextListener(contextListener
);
284 * @created Feb 2, 2010
287 class SearchJob
extends Job
{
289 private final IFindTaxaAndNamesConfigurator configurator
;
291 private final Display display
;
296 public SearchJob(Display display
, IFindTaxaAndNamesConfigurator configurator
) {
297 super("Performing Search");
298 this.display
= display
;
299 this.configurator
= configurator
;
303 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
306 protected IStatus
run(IProgressMonitor monitor
) {
307 monitor
.beginTask("", 100);
310 final List
<UuidAndTitleCache
<IdentifiableEntity
>> searchResult
= CdmStore
.getSearchManager().findTaxaAndNames(configurator
);
313 if(! monitor
.isCanceled()){
314 display
.asyncExec(new Runnable() {
317 displaySearchResult(searchResult
);
321 display
.asyncExec(new Runnable() {
324 status
.setText("Cancelled");
329 return Status
.OK_STATUS
;