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
.l10n
.Messages
;
52 import eu
.etaxonomy
.taxeditor
.navigation
.search
.SearchBar
.SearchOption
;
53 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
56 * <p>SearchResultView class.</p>
63 public class SearchResultView
extends ViewPart
implements IConversationEnabled
{
65 private static Object
[] EMPTY
= new Object
[0];
67 private class ContextListener
extends ContextListenerAdapter
{
69 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
72 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
73 monitor
.subTask(Messages
.SearchResultView_REMOVE_SEARCH_RESULTS
);
74 AbstractUtility
.hideView(SearchResultView
.this);
78 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.searc"{trunked}</code> */
79 public static final String ID
=
80 "eu.etaxonomy.taxeditor.navigation.search.searchResultView"; //$NON-NLS-1$
82 private TableViewer resultViewer
;
84 private ConversationHolder conversation
;
86 private Text searchString
;
88 private Text configurationLabel
;
92 private SearchJob searchJob
;
94 private IContextListener contextListener
;
97 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
101 public void createPartControl(Composite parent
) {
103 conversation
= CdmStore
.createConversation();
104 contextListener
= new ContextListener();
105 CdmStore
.getContextManager().addContextListener(contextListener
);
107 GridLayout layout
= new GridLayout();
108 layout
.marginWidth
= 0;
109 layout
.marginHeight
= 0;
111 parent
.setLayout(layout
);
113 Composite infoComposite
= createInfoComposite(parent
);
114 infoComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
116 resultViewer
= new TableViewer(parent
, SWT
.NONE
);
117 resultViewer
.setContentProvider(new ArrayContentProvider());
118 resultViewer
.setLabelProvider(new SearchResultLabelProvider());
119 resultViewer
.addDoubleClickListener(new IDoubleClickListener() {
121 public void doubleClick(DoubleClickEvent event
) {
122 ISelection selection
= event
.getSelection();
123 if(selection
instanceof IStructuredSelection
){
124 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
125 if(firstElement
instanceof UuidAndTitleCache
){
126 NavigationUtil
.openEditor((UuidAndTitleCache
) firstElement
);
132 resultViewer
.getControl().setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
134 getSite().setSelectionProvider(resultViewer
);
136 // register context menu
137 MenuManager menuMgr
= new MenuManager();
138 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
139 getSite().registerContextMenu(menuMgr
, resultViewer
);
141 Control control
= resultViewer
.getControl();
142 Menu menu
= menuMgr
.createContextMenu(control
);
143 control
.setMenu(menu
);
146 private Composite
createInfoComposite(Composite parent
){
147 Composite composite
= new Composite(parent
, SWT
.NULL
);
149 composite
.setLayout(new GridLayout(2, false));
151 Label searchStringLabel
= new Label(composite
, SWT
.NULL
);
152 searchStringLabel
.setText(Messages
.SearchResultView_SEARCH_STRING
);
154 searchString
= new Text(composite
, SWT
.NULL
);
155 searchString
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
156 searchString
.setEditable(false);
157 // searchString.setText(" ");
159 Label configurationDescriptionLabel
= new Label(composite
, SWT
.NULL
);
160 configurationDescriptionLabel
.setText(Messages
.SearchResultView_SEARCH_FOR
);
162 configurationLabel
= new Text(composite
, SWT
.WRAP
);
163 configurationLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
164 configurationLabel
.setEditable(false);
166 Label statusLabel
= new Label(composite
, SWT
.NULL
);
167 statusLabel
.setText(Messages
.SearchResultView_STATUS
);
169 status
= new Text(composite
, SWT
.NULL
);
170 status
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
171 status
.setEditable(false);
177 * <p>performSearch</p>
179 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
181 public void performSearch(IFindTaxaAndNamesConfigurator configurator
){
182 setPartName(String
.format(Messages
.SearchResultView_SEARCH
, configurator
.getTitleSearchString()));
184 searchString
.setText(configurator
.getTitleSearchString());
186 List
<String
> includedEntities
= new ArrayList
<String
>();
187 if(configurator
.isDoTaxa()) {
188 includedEntities
.add(SearchOption
.TAXON
.getLabel());
190 if(configurator
.isDoSynonyms()) {
191 includedEntities
.add(SearchOption
.SYNONYM
.getLabel());
193 if(configurator
.isDoNamesWithoutTaxa()) {
194 includedEntities
.add(SearchOption
.NAME
.getLabel());
196 if(configurator
.isDoTaxaByCommonNames()){
197 includedEntities
.add(SearchOption
.COMMON_NAME
.getLabel());
199 configurator
.setMatchMode(MatchMode
.LIKE
);
201 String includedEntitiesString
= ""; //$NON-NLS-1$
202 for (int i
= 0; i
< includedEntities
.size(); i
++){
203 includedEntitiesString
+= includedEntities
.get(i
);
204 if(i
< includedEntities
.size() -1){
205 includedEntitiesString
+= ", "; //$NON-NLS-1$
209 configurationLabel
.setText(includedEntitiesString
);
211 status
.setText(Messages
.SearchResultView_SEARCHING
);
213 searchJob
= new SearchJob(Display
.getCurrent(), configurator
);
214 searchJob
.schedule();
219 * <p>displaySearchResult</p>
221 * @param result a {@link java.util.List} object.
223 protected void displaySearchResult(List
<UuidAndTitleCache
<IdentifiableEntity
>> result
) {
224 if(result
.size() > 0){
225 resultViewer
.setInput(result
);
226 status
.setText(String
.format(Messages
.SearchResultView_CNT_ENTITIES_FOUND
, result
.size()));
228 resultViewer
.setInput(EMPTY
);
229 status
.setText(Messages
.SearchResultView_NO_RESULTS
);
234 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
238 public void setFocus() {
239 //logger.warn("Setting focus to search result viewer");
241 // pass focus to resultViewer
242 resultViewer
.getControl().setFocus();
246 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
249 * <p>getConversationHolder</p>
251 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
254 public ConversationHolder
getConversationHolder() {
255 return this.conversation
;
259 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
263 public void update(CdmDataChangeMap changeEvents
) {
264 // TODO Auto-generated method stub
269 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
273 public void dispose() {
275 conversation
.close();
276 if(searchJob
!= null) {
279 CdmStore
.getContextManager().removeContextListener(contextListener
);
285 * @created Feb 2, 2010
288 class SearchJob
extends Job
{
290 private final IFindTaxaAndNamesConfigurator configurator
;
292 private final Display display
;
297 public SearchJob(Display display
, IFindTaxaAndNamesConfigurator configurator
) {
298 super(Messages
.SearchResultView_PERFORMING_SEARCH
);
299 this.display
= display
;
300 this.configurator
= configurator
;
304 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
307 protected IStatus
run(IProgressMonitor monitor
) {
308 monitor
.beginTask("", 100); //$NON-NLS-1$
311 final List
<UuidAndTitleCache
<IdentifiableEntity
>> searchResult
= CdmStore
.getSearchManager().findTaxaAndNames(configurator
);
314 if(! monitor
.isCanceled()){
315 display
.asyncExec(new Runnable() {
318 displaySearchResult(searchResult
);
322 display
.asyncExec(new Runnable() {
325 status
.setText(Messages
.SearchResultView_CANCELLED
);
330 return Status
.OK_STATUS
;