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
.jface
.wizard
.IWizard
;
20 import org
.eclipse
.jface
.wizard
.WizardDialog
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.events
.SelectionAdapter
;
23 import org
.eclipse
.swt
.events
.SelectionEvent
;
24 import org
.eclipse
.swt
.layout
.GridData
;
25 import org
.eclipse
.swt
.layout
.GridLayout
;
26 import org
.eclipse
.swt
.widgets
.Button
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.swt
.widgets
.Display
;
29 import org
.eclipse
.swt
.widgets
.Event
;
30 import org
.eclipse
.swt
.widgets
.Label
;
31 import org
.eclipse
.swt
.widgets
.Listener
;
32 import org
.eclipse
.swt
.widgets
.Table
;
33 import org
.eclipse
.swt
.widgets
.TableItem
;
34 import org
.eclipse
.swt
.widgets
.Text
;
35 import org
.eclipse
.ui
.IMemento
;
36 import org
.eclipse
.ui
.PlatformUI
;
37 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
38 import org
.eclipse
.ui
.part
.ViewPart
;
39 import org
.eclipse
.wb
.swt
.ResourceManager
;
41 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
42 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
43 import eu
.etaxonomy
.cdm
.ext
.occurrence
.OccurenceQuery
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
45 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
46 import eu
.etaxonomy
.taxeditor
.databaseAdmin
.wizard
.ImportPreferencesWizard
;
47 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
48 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
49 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
50 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
51 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
52 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
55 * View which shows a list of "data" that can be imported into the CDM
59 * @param <T> the CDM type that will be handled by this view
62 public abstract class DataImportView
<T
> extends ViewPart
implements IPartContentHasFactualData
,
63 IConversationEnabled
, IPartContentHasDetails
, IPartContentHasSupplementalData
, IContextListener
, Listener
{
65 protected final Logger logger
= Logger
.getLogger(DataImportView
.class);
67 protected Collection
<T
> results
= new ArrayList
<T
>();
69 protected boolean updated
= false;
71 protected OccurenceQuery query
;
73 private static ConversationHolder conversationHolder
;
75 private SaveImportedSpecimenAction saveImportedSpecimenAction
;
77 private Text textClassification
;
78 private Classification classification
;
80 private Text textReferenceString
;
82 private Button toggleButton
;
83 private boolean state
;
85 private Button openConfigurator
;
87 * @return the classification
89 public Classification
getClassification() {
90 return classification
;
94 * @param classification the classification to set
96 public void setClassification(Classification classification
) {
97 this.classification
= classification
;
99 private Button btnBrowseClassification
;
100 private Button btnClear
;
102 private final FormToolkit toolkit
= new FormToolkit(Display
.getCurrent());
107 * Constructs a new DataImportEditor and registers it to listen to context changes
109 public DataImportView() {
110 CdmStore
.getContextManager().addContextListener(this);
111 if(CdmStore
.isActive()){
117 * Create contents of the view part.
121 public void createPartControl(Composite parent
) {
122 // final Composite composite = new Composite(parent, SWT.NONE);
124 // GridLayout gridLayout = new GridLayout();
125 // gridLayout.numColumns = 3;
126 // composite.setLayout(gridLayout);
127 // composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
128 Composite composite
= new Composite(parent
, SWT
.NONE
);
129 composite
.setLayout(new GridLayout(2, false));
131 Composite composite_1
= new Composite(composite
, SWT
.NONE
);
132 GridData gd_composite_1
= new GridData(SWT
.LEFT
, SWT
.TOP
, false, false, 1, 1);
133 gd_composite_1
.widthHint
= 280;
134 composite_1
.setLayoutData(gd_composite_1
);
135 composite_1
.setLayout(new GridLayout(3, false));
137 Label label
= new Label(composite_1
, SWT
.TOP
);
138 label
.setText("Classification");
139 label
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 3, 1));
140 textClassification
= new Text(composite_1
, SWT
.BORDER
);
141 textClassification
.setEnabled(false);
142 GridData gd_textClassification
= new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 1, 1);
143 gd_textClassification
.widthHint
= 118;
144 textClassification
.setLayoutData(new GridData(SWT
.FILL
, SWT
.LEFT
, true, true));
145 btnBrowseClassification
= new Button(composite_1
, SWT
.NONE
);
146 btnBrowseClassification
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/open.gif"));
147 btnBrowseClassification
.addListener(SWT
.Selection
, this);
148 btnClear
= new Button(composite_1
, SWT
.NONE
);
149 btnClear
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/trash.gif"));
150 btnClear
.addListener(SWT
.Selection
, this);
153 Label labelRef
= new Label(composite_1
, SWT
.NONE
);
154 labelRef
.setText("Default import souce reference");
155 labelRef
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 3, 1));
156 textReferenceString
= new Text(composite_1
, SWT
.NONE
);
157 textReferenceString
.setEnabled(true);
158 GridData gd_textReferenceString
= new GridData(SWT
.LEFT
, SWT
.CENTER
, true, true, 3, 1);
159 gd_textReferenceString
.widthHint
= 229;
160 textReferenceString
.setLayoutData(gd_textReferenceString
);
163 openConfigurator
= new Button(composite_1
, SWT
.PUSH
);
164 openConfigurator
.setText("Configuration");
165 new Label(composite_1
, SWT
.NONE
);
166 new Label(composite_1
, SWT
.NONE
);
167 openConfigurator
.addSelectionListener(new SelectionAdapter(){
169 public void widgetSelected(SelectionEvent e
) {
171 IWizard wizard
= new ImportPreferencesWizard();
172 WizardDialog dialog
= new WizardDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), wizard
);
177 //checkbox table for result
178 Composite composite_2
= new Composite(composite
, SWT
.NONE
);
179 GridData gd_composite_2
= new GridData(SWT
.LEFT
, SWT
.TOP
, true, true, 1, 1);
180 gd_composite_2
.heightHint
= 454;
181 gd_composite_2
.widthHint
= 403;
182 composite_2
.setLayoutData(gd_composite_2
);
183 composite_2
.setLayout(new GridLayout(2, false));
186 CheckboxTableViewer checkboxTableViewer
= CheckboxTableViewer
.newCheckList(composite_2
, SWT
.BORDER
| SWT
.FULL_SELECTION
);
189 table
= checkboxTableViewer
.getTable();
190 GridData gd_table
= new GridData(SWT
.LEFT
, SWT
.TOP
, true, true, 1, 1);
191 gd_table
.heightHint
= 444;
192 gd_table
.widthHint
= 312;
193 table
.setLayoutData(gd_table
);
194 toolkit
.paintBordersFor(table
);
196 toggleButton
= new Button(composite_2
, SWT
.PUSH
);
197 GridData gd_toggleButton
= new GridData(SWT
.LEFT
, SWT
.TOP
, false, false, 1, 1);
198 gd_toggleButton
.widthHint
= 56;
199 toggleButton
.setLayoutData(gd_toggleButton
);
200 toggleButton
.setText("Toggle");
201 toggleButton
.addSelectionListener(new SelectionAdapter(){
203 public void widgetSelected(SelectionEvent e
) {
204 state
= state ?
false : true;
205 for (TableItem item
: getTable().getItems()){
206 item
.setChecked(state
);
217 * Create the actions.
219 private void createActions() {
220 saveImportedSpecimenAction
= new SaveImportedSpecimenAction();
224 * Initialize the toolbar.
226 private void initializeToolBar() {
227 IToolBarManager tbm
= getViewSite().getActionBars().getToolBarManager();
228 tbm
.add(saveImportedSpecimenAction
);
232 * Initialize the menu.
234 private void initializeMenu() {
235 IMenuManager manager
= getViewSite().getActionBars().getMenuManager();
238 public Table
getTable() {
243 * @param query the query to set
245 public void setQuery(OccurenceQuery query
) {
250 * @param results the results to set
252 public void setResults(Collection
<T
> results
) {
253 this.results
= results
;
258 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#dispose()
261 public void dispose() {
263 CdmStore
.getContextManager().removeContextListener(this);
267 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextRefresh(org.eclipse.core.runtime.IProgressMonitor)
270 public void contextRefresh(IProgressMonitor monitor
) {
274 protected void refresh(){
277 if (getTable() != null){
278 getTable().removeAll();
281 TableItem tableItem
= new TableItem(getTable(), SWT
.NONE
);
282 tableItem
.setText(getTextForTableItem(item
));
283 tableItem
.setData(item
);
289 public abstract void query();
293 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
296 public void update(CdmDataChangeMap changeEvents
) {
301 * Returns the text label of the given item.
302 * @param item the item for which the label should be returns
303 * @return the label of the item
305 protected abstract String
getTextForTableItem(T item
);
308 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
311 public void setFocus() {
312 getTable().setFocus();
313 //make sure to bind again if maybe in another view the conversation was unbound
314 if(getConversationHolder()!=null && !getConversationHolder().isBound()){
315 getConversationHolder().bind();
320 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
323 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
327 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
330 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
331 // derivateSearchCompositeController.setEnabled(false);
332 // derivateSearchCompositeController.reset();
336 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
339 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
341 // derivateSearchCompositeController.setEnabled(true);
344 private void initConversation(){
345 if(conversationHolder
==null){
346 conversationHolder
= CdmStore
.createConversation();
351 * @see eu.etaxonomy.taxeditor.model.IContextListener#workbenchShutdown(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
354 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {
355 if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
356 getConversationHolder().close();
361 * @return the conversationHolder
364 public ConversationHolder
getConversationHolder() {
365 if(CdmStore
.isActive() && conversationHolder
==null){
368 return conversationHolder
;
371 public void handleEvent(Event event
) {
372 if(event
.widget
==btnBrowseClassification
){
373 classification
= SelectionDialogFactory
.getSelectionFromDialog(Classification
.class, getSite().getShell(), null, null);
374 if(classification
!=null){
375 textClassification
.setText(classification
.getTitleCache());
378 else if(event
.widget
==btnClear
){
379 classification
= null;
380 textClassification
.setText("");
387 public String
getReferenceString() {
389 return textReferenceString
.getText();