2 * Copyright (C) 2013 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.
9 package eu
.etaxonomy
.taxeditor
.view
.dataimport
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.jface
.action
.IMenuManager
;
17 import org
.eclipse
.jface
.action
.IToolBarManager
;
18 import org
.eclipse
.jface
.viewers
.CheckboxTableViewer
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.layout
.GridData
;
21 import org
.eclipse
.swt
.layout
.GridLayout
;
22 import org
.eclipse
.swt
.widgets
.Button
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Display
;
25 import org
.eclipse
.swt
.widgets
.Event
;
26 import org
.eclipse
.swt
.widgets
.Label
;
27 import org
.eclipse
.swt
.widgets
.Listener
;
28 import org
.eclipse
.swt
.widgets
.Table
;
29 import org
.eclipse
.swt
.widgets
.TableItem
;
30 import org
.eclipse
.swt
.widgets
.Text
;
31 import org
.eclipse
.ui
.IMemento
;
32 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
33 import org
.eclipse
.ui
.part
.ViewPart
;
34 import org
.eclipse
.wb
.swt
.ResourceManager
;
36 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
37 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
38 import eu
.etaxonomy
.cdm
.ext
.occurrence
.OccurenceQuery
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
40 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
41 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
42 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
43 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
44 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
45 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
46 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
49 * View which shows a list of "data" that can be imported into the CDM
53 * @param <T> the CDM type that will be handled by this view
56 public abstract class DataImportView
<T
> extends ViewPart
implements IPartContentHasFactualData
,
57 IConversationEnabled
, IPartContentHasDetails
, IPartContentHasSupplementalData
, IContextListener
, Listener
{
59 protected final Logger logger
= Logger
.getLogger(DataImportView
.class);
61 protected Collection
<T
> results
= new ArrayList
<T
>();
63 protected OccurenceQuery query
;
65 private static ConversationHolder conversationHolder
;
67 private SaveImportedSpecimenAction saveImportedSpecimenAction
;
69 private Text textClassification
;
70 private Classification classification
;
72 * @return the classification
74 public Classification
getClassification() {
75 return classification
;
79 * @param classification the classification to set
81 public void setClassification(Classification classification
) {
82 this.classification
= classification
;
84 private Button btnBrowseClassification
;
85 private Button btnClear
;
87 private final FormToolkit toolkit
= new FormToolkit(Display
.getCurrent());
92 * Constructs a new DataImportEditor and registers it to listen to context changes
94 public DataImportView() {
95 CdmStore
.getContextManager().addContextListener(this);
96 if(CdmStore
.isActive()){
102 * Create contents of the view part.
106 public void createPartControl(Composite parent
) {
107 final Composite composite
= new Composite(parent
, SWT
.NULL
);
109 GridLayout gridLayout
= new GridLayout();
110 gridLayout
.numColumns
= 4;
111 composite
.setLayout(gridLayout
);
112 Label label
= new Label(composite
, SWT
.NONE
);
113 label
.setText("Classification");
114 textClassification
= new Text(composite
, SWT
.NONE
);
115 textClassification
.setEnabled(false);
116 textClassification
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
117 btnBrowseClassification
= new Button(composite
, SWT
.NONE
);
118 btnBrowseClassification
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/open.gif"));
119 btnBrowseClassification
.addListener(SWT
.Selection
, this);
120 btnClear
= new Button(composite
, SWT
.NONE
);
121 btnClear
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/trash.gif"));
122 btnClear
.addListener(SWT
.Selection
, this);
124 CheckboxTableViewer checkboxTableViewer
= CheckboxTableViewer
.newCheckList(parent
, SWT
.BORDER
| SWT
.FULL_SELECTION
);
125 table
= checkboxTableViewer
.getTable();
126 toolkit
.paintBordersFor(table
);
136 * Create the actions.
138 private void createActions() {
139 saveImportedSpecimenAction
= new SaveImportedSpecimenAction();
143 * Initialize the toolbar.
145 private void initializeToolBar() {
146 IToolBarManager tbm
= getViewSite().getActionBars().getToolBarManager();
147 tbm
.add(saveImportedSpecimenAction
);
151 * Initialize the menu.
153 private void initializeMenu() {
154 IMenuManager manager
= getViewSite().getActionBars().getMenuManager();
157 public Table
getTable() {
162 * @param query the query to set
164 public void setQuery(OccurenceQuery query
) {
169 * @param results the results to set
171 public void setResults(Collection
<T
> results
) {
172 this.results
= results
;
176 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#dispose()
179 public void dispose() {
181 CdmStore
.getContextManager().removeContextListener(this);
185 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextRefresh(org.eclipse.core.runtime.IProgressMonitor)
188 public void contextRefresh(IProgressMonitor monitor
) {
192 protected void refresh(){
193 // getTable().removeAll();
195 TableItem tableItem
= new TableItem(getTable(), SWT
.NONE
);
196 tableItem
.setText(getTextForTableItem(item
));
197 tableItem
.setData(item
);
201 public abstract void query();
205 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
208 public void update(CdmDataChangeMap changeEvents
) {
213 * Returns the text label of the given item.
214 * @param item the item for which the label should be returns
215 * @return the label of the item
217 protected abstract String
getTextForTableItem(T item
);
220 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
223 public void setFocus() {
224 getTable().setFocus();
225 //make sure to bind again if maybe in another view the conversation was unbound
226 if(getConversationHolder()!=null && !getConversationHolder().isBound()){
227 getConversationHolder().bind();
232 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
235 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
239 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
242 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
243 // derivateSearchCompositeController.setEnabled(false);
244 // derivateSearchCompositeController.reset();
248 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
251 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
253 // derivateSearchCompositeController.setEnabled(true);
256 private void initConversation(){
257 if(conversationHolder
==null){
258 conversationHolder
= CdmStore
.createConversation();
263 * @see eu.etaxonomy.taxeditor.model.IContextListener#workbenchShutdown(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
266 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {
267 if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
268 getConversationHolder().close();
273 * @return the conversationHolder
276 public ConversationHolder
getConversationHolder() {
277 if(CdmStore
.isActive() && conversationHolder
==null){
280 return conversationHolder
;
283 public void handleEvent(Event event
) {
284 if(event
.widget
==btnBrowseClassification
){
285 classification
= SelectionDialogFactory
.getSelectionFromDialog(Classification
.class, getSite().getShell(), null, null);
286 if(classification
!=null){
287 textClassification
.setText(classification
.getTitleCache());
290 else if(event
.widget
==btnClear
){
291 classification
= null;
292 textClassification
.setText("");