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
.apache
.log4j
.Logger
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.core
.runtime
.jobs
.Job
;
20 import org
.eclipse
.jface
.action
.GroupMarker
;
21 import org
.eclipse
.jface
.action
.MenuManager
;
22 import org
.eclipse
.jface
.viewers
.ArrayContentProvider
;
23 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
24 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
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
.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
.IdentifiableEntity
;
42 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
43 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
44 import eu
.etaxonomy
.taxeditor
.navigation
.search
.SearchBar
.SearchOption
;
45 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
53 public class SearchResultView
extends ViewPart
implements IConversationEnabled
{
54 private static final Logger logger
= Logger
55 .getLogger(SearchResultView
.class);
57 public static final String ID
=
58 "eu.etaxonomy.taxeditor.navigation.search.searchResultView"; //$NON-NLS-1$
60 private TableViewer resultViewer
;
62 private ConversationHolder conversation
;
64 private Text searchString
;
66 private Text configurationLabel
;
70 private SearchJob searchJob
;
74 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
77 public void createPartControl(Composite parent
) {
79 conversation
= CdmStore
.createConversation();
81 GridLayout layout
= new GridLayout();
82 layout
.marginWidth
= 0;
83 layout
.marginHeight
= 0;
85 parent
.setLayout(layout
);
87 Composite infoComposite
= createInfoComposite(parent
);
88 infoComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
90 resultViewer
= new TableViewer(parent
, SWT
.NONE
);
91 resultViewer
.setContentProvider(new ArrayContentProvider());
92 resultViewer
.setLabelProvider(new SearchResultLabelProvider());
93 resultViewer
.addDoubleClickListener(new IDoubleClickListener() {
94 public void doubleClick(DoubleClickEvent event
) {
95 NavigationUtil
.executeEditHandler();
99 resultViewer
.getControl().setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
101 getSite().setSelectionProvider(resultViewer
);
103 // register context menu
104 MenuManager menuMgr
= new MenuManager();
105 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
106 getSite().registerContextMenu(menuMgr
, resultViewer
);
108 Control control
= resultViewer
.getControl();
109 Menu menu
= menuMgr
.createContextMenu(control
);
110 control
.setMenu(menu
);
113 private Composite
createInfoComposite(Composite parent
){
114 Composite composite
= new Composite(parent
, SWT
.NULL
);
116 composite
.setLayout(new GridLayout(2, false));
118 Label searchStringLabel
= new Label(composite
, SWT
.NULL
);
119 searchStringLabel
.setText("Search String:");
121 searchString
= new Text(composite
, SWT
.NULL
);
122 searchString
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
123 searchString
.setEditable(false);
124 // searchString.setText(" ");
126 Label configurationDescriptionLabel
= new Label(composite
, SWT
.NULL
);
127 configurationDescriptionLabel
.setText("Search for:");
129 configurationLabel
= new Text(composite
, SWT
.WRAP
);
130 configurationLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
131 configurationLabel
.setEditable(false);
133 Label statusLabel
= new Label(composite
, SWT
.NULL
);
134 statusLabel
.setText("Status:");
136 status
= new Text(composite
, SWT
.NULL
);
137 status
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
138 status
.setEditable(false);
143 public void performSearch(ITaxonServiceConfigurator configurator
){
144 setPartName("Search: '" + configurator
.getSearchString() + "'");
146 searchString
.setText(configurator
.getSearchString());
148 List
<String
> includedEntities
= new ArrayList
<String
>();
149 if(configurator
.isDoTaxa())
150 includedEntities
.add(SearchOption
.TAXON
.getLabel());
151 if(configurator
.isDoSynonyms())
152 includedEntities
.add(SearchOption
.SYNONYM
.getLabel());
153 if(configurator
.isDoNamesWithoutTaxa())
154 includedEntities
.add(SearchOption
.NAME
.getLabel());
155 if(configurator
.isDoTaxaByCommonNames()){
156 includedEntities
.add(SearchOption
.COMMON_NAME
.getLabel());
159 String includedEntitiesString
= "";
160 for (int i
= 0; i
< includedEntities
.size(); i
++){
161 includedEntitiesString
+= includedEntities
.get(i
);
162 if(i
< includedEntities
.size() -1){
163 includedEntitiesString
+= ", ";
167 configurationLabel
.setText(includedEntitiesString
);
169 status
.setText("Searching...");
171 searchJob
= new SearchJob(Display
.getCurrent(), configurator
);
172 searchJob
.schedule();
175 protected void displaySearchResult(List
<IdentifiableEntity
> result
) {
176 if(result
.size() > 0){
177 resultViewer
.setInput(result
);
178 status
.setText(result
.size() + " entities found");
180 status
.setText("Search returned no results");
185 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
188 public void setFocus() {
189 //logger.warn("Setting focus to search result viewer");
191 // pass focus to resultViewer
192 resultViewer
.getControl().setFocus();
196 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
198 public ConversationHolder
getConversationHolder() {
199 return this.conversation
;
203 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
205 public void update(CdmDataChangeMap changeEvents
) {
206 // TODO Auto-generated method stub
211 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
214 public void dispose() {
216 conversation
.close();
217 if(searchJob
!= null)
224 * @created Feb 2, 2010
227 class SearchJob
extends Job
{
229 private ITaxonServiceConfigurator configurator
;
231 private Display display
;
236 public SearchJob(Display display
, ITaxonServiceConfigurator configurator
) {
238 this.display
= display
;
239 this.configurator
= configurator
;
243 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
246 protected IStatus
run(IProgressMonitor monitor
) {
247 // monitor.beginTask("Performing Search", 100);
248 // monitor.worked(20);
250 final List
<IdentifiableEntity
> searchResult
= CdmStore
.findTaxaAndNames(configurator
);
251 // monitor.worked(40);
252 display
.asyncExec(new Runnable() {
255 displaySearchResult(searchResult
);
259 return Status
.OK_STATUS
;