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
.e4
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
14 import javax
.annotation
.PostConstruct
;
15 import javax
.annotation
.PreDestroy
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.e4
.ui
.di
.Focus
;
20 import org
.eclipse
.jface
.viewers
.CheckboxTableViewer
;
21 import org
.eclipse
.jface
.wizard
.IWizard
;
22 import org
.eclipse
.jface
.wizard
.WizardDialog
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.events
.SelectionAdapter
;
25 import org
.eclipse
.swt
.events
.SelectionEvent
;
26 import org
.eclipse
.swt
.layout
.GridData
;
27 import org
.eclipse
.swt
.layout
.GridLayout
;
28 import org
.eclipse
.swt
.widgets
.Button
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.swt
.widgets
.Event
;
32 import org
.eclipse
.swt
.widgets
.Label
;
33 import org
.eclipse
.swt
.widgets
.Listener
;
34 import org
.eclipse
.swt
.widgets
.Table
;
35 import org
.eclipse
.swt
.widgets
.TableItem
;
36 import org
.eclipse
.swt
.widgets
.Text
;
37 import org
.eclipse
.ui
.IMemento
;
38 import org
.eclipse
.ui
.PlatformUI
;
39 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
40 import org
.eclipse
.wb
.swt
.ResourceManager
;
42 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
43 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
44 import eu
.etaxonomy
.cdm
.ext
.occurrence
.OccurenceQuery
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
46 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
47 import eu
.etaxonomy
.taxeditor
.databaseAdmin
.wizard
.ImportPreferencesWizard
;
48 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
49 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
50 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
51 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
52 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
53 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
54 import eu
.etaxonomy
.taxeditor
.view
.dataimport
.SaveImportedSpecimenAction
;
57 * View which shows a list of "data" that can be imported into the CDM
61 * @param <T> the CDM type that will be handled by this view
63 public abstract class DataImportViewE4
<T
> implements IPartContentHasFactualData
,
64 IConversationEnabled
, IPartContentHasDetails
, IPartContentHasSupplementalData
, IContextListener
, Listener
{
66 protected final Logger logger
= Logger
.getLogger(DataImportViewE4
.class);
68 protected Collection
<T
> results
= new ArrayList
<T
>();
70 protected boolean updated
= false;
72 protected OccurenceQuery query
;
74 private static ConversationHolder conversationHolder
;
76 private SaveImportedSpecimenAction saveImportedSpecimenAction
;
78 private Text textClassification
;
80 private Classification classification
;
82 private Text textReferenceString
;
84 private Button toggleButton
;
86 private boolean state
;
88 private Button openConfigurator
;
90 public Classification
getClassification() {
91 return classification
;
94 public void setClassification(Classification classification
) {
95 this.classification
= classification
;
98 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 DataImportViewE4() {
110 CdmStore
.getContextManager().addContextListener(this);
115 * Create contents of the view part.
119 public void createPartControl(Composite parent
) {
120 if(CdmStore
.isActive()){
126 Composite composite
= new Composite(parent
, SWT
.NONE
);
127 composite
.setLayout(new GridLayout(2, false));
129 Composite composite_1
= new Composite(composite
, SWT
.NONE
);
130 GridData gd_composite_1
= new GridData(SWT
.LEFT
, SWT
.TOP
, false, false, 1, 1);
131 gd_composite_1
.widthHint
= 280;
132 composite_1
.setLayoutData(gd_composite_1
);
133 composite_1
.setLayout(new GridLayout(3, false));
135 Label label
= new Label(composite_1
, SWT
.TOP
);
136 label
.setText("Classification");
137 label
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 3, 1));
138 textClassification
= new Text(composite_1
, SWT
.BORDER
);
139 textClassification
.setEnabled(false);
140 GridData gd_textClassification
= new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 1, 1);
141 gd_textClassification
.widthHint
= 118;
142 textClassification
.setLayoutData(new GridData(SWT
.FILL
, SWT
.LEFT
, true, true));
143 btnBrowseClassification
= new Button(composite_1
, SWT
.NONE
);
144 btnBrowseClassification
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/open.gif"));
145 btnBrowseClassification
.addListener(SWT
.Selection
, this);
146 btnClear
= new Button(composite_1
, SWT
.NONE
);
147 btnClear
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/trash.gif"));
148 btnClear
.addListener(SWT
.Selection
, this);
151 Label labelRef
= new Label(composite_1
, SWT
.NONE
);
152 labelRef
.setText("Default import souce reference");
153 labelRef
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 3, 1));
154 textReferenceString
= new Text(composite_1
, SWT
.NONE
);
155 textReferenceString
.setEnabled(true);
156 GridData gd_textReferenceString
= new GridData(SWT
.LEFT
, SWT
.CENTER
, true, true, 3, 1);
157 gd_textReferenceString
.widthHint
= 229;
158 textReferenceString
.setLayoutData(gd_textReferenceString
);
161 openConfigurator
= new Button(composite_1
, SWT
.PUSH
);
162 openConfigurator
.setText("Configuration");
163 new Label(composite_1
, SWT
.NONE
);
164 new Label(composite_1
, SWT
.NONE
);
165 openConfigurator
.addSelectionListener(new SelectionAdapter(){
167 public void widgetSelected(SelectionEvent e
) {
169 IWizard wizard
= new ImportPreferencesWizard();
170 WizardDialog dialog
= new WizardDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), wizard
);
175 //checkbox table for result
176 Composite composite_2
= new Composite(composite
, SWT
.NONE
);
177 GridData gd_composite_2
= new GridData(SWT
.LEFT
, SWT
.TOP
, true, true, 1, 1);
178 gd_composite_2
.heightHint
= 454;
179 gd_composite_2
.widthHint
= 403;
180 composite_2
.setLayoutData(gd_composite_2
);
181 composite_2
.setLayout(new GridLayout(2, false));
184 CheckboxTableViewer checkboxTableViewer
= CheckboxTableViewer
.newCheckList(composite_2
, SWT
.BORDER
| SWT
.FULL_SELECTION
);
187 table
= checkboxTableViewer
.getTable();
188 GridData gd_table
= new GridData(SWT
.LEFT
, SWT
.TOP
, true, true, 1, 1);
189 gd_table
.heightHint
= 444;
190 gd_table
.widthHint
= 312;
191 table
.setLayoutData(gd_table
);
192 toolkit
.paintBordersFor(table
);
194 toggleButton
= new Button(composite_2
, SWT
.PUSH
);
195 GridData gd_toggleButton
= new GridData(SWT
.LEFT
, SWT
.TOP
, false, false, 1, 1);
196 gd_toggleButton
.widthHint
= 56;
197 toggleButton
.setLayoutData(gd_toggleButton
);
198 toggleButton
.setText("Toggle");
199 toggleButton
.addSelectionListener(new SelectionAdapter(){
201 public void widgetSelected(SelectionEvent e
) {
202 state
= state ?
false : true;
203 for (TableItem item
: getTable().getItems()){
204 item
.setChecked(state
);
214 * Create the actions.
216 private void createActions() {
217 saveImportedSpecimenAction
= new SaveImportedSpecimenAction();
221 * Initialize the toolbar.
223 private void initializeToolBar() {
225 // IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
226 // tbm.add(saveImportedSpecimenAction);
229 public Table
getTable() {
233 public void setQuery(OccurenceQuery query
) {
237 public void setResults(Collection
<T
> results
) {
238 this.results
= results
;
243 public void dispose() {
244 CdmStore
.getContextManager().removeContextListener(this);
248 public void contextRefresh(IProgressMonitor monitor
) {
252 protected void refresh(){
254 if (getTable() != null){
255 getTable().removeAll();
258 TableItem tableItem
= new TableItem(getTable(), SWT
.NONE
);
259 tableItem
.setText(getTextForTableItem(item
));
260 tableItem
.setData(item
);
266 public abstract void query();
269 public void update(CdmDataChangeMap changeEvents
) {
274 * Returns the text label of the given item.
275 * @param item the item for which the label should be returns
276 * @return the label of the item
278 protected abstract String
getTextForTableItem(T item
);
281 public void setFocus() {
282 getTable().setFocus();
283 //make sure to bind again if maybe in another view the conversation was unbound
284 if(getConversationHolder()!=null && !getConversationHolder().isBound()){
285 getConversationHolder().bind();
290 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
294 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
298 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
302 private void initConversation(){
303 if(conversationHolder
==null){
304 conversationHolder
= CdmStore
.createConversation();
309 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {
310 if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
311 getConversationHolder().close();
316 * @return the conversationHolder
319 public ConversationHolder
getConversationHolder() {
320 if(CdmStore
.isActive() && conversationHolder
==null){
323 return conversationHolder
;
327 public void handleEvent(Event event
) {
328 if(event
.widget
==btnBrowseClassification
){
329 classification
= SelectionDialogFactory
.getSelectionFromDialog(Classification
.class, event
.widget
.getDisplay().getActiveShell(), null, null);
330 if(classification
!=null){
331 textClassification
.setText(classification
.getTitleCache());
334 else if(event
.widget
==btnClear
){
335 classification
= null;
336 textClassification
.setText("");
340 public String
getReferenceString() {
341 return textReferenceString
.getText();