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
;
16 import javax
.inject
.Inject
;
17 import javax
.inject
.Named
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.e4
.ui
.di
.Focus
;
22 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
23 import org
.eclipse
.jface
.viewers
.CheckboxTableViewer
;
24 import org
.eclipse
.jface
.wizard
.IWizard
;
25 import org
.eclipse
.jface
.wizard
.WizardDialog
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.events
.SelectionAdapter
;
28 import org
.eclipse
.swt
.events
.SelectionEvent
;
29 import org
.eclipse
.swt
.layout
.GridData
;
30 import org
.eclipse
.swt
.layout
.GridLayout
;
31 import org
.eclipse
.swt
.widgets
.Button
;
32 import org
.eclipse
.swt
.widgets
.Composite
;
33 import org
.eclipse
.swt
.widgets
.Display
;
34 import org
.eclipse
.swt
.widgets
.Event
;
35 import org
.eclipse
.swt
.widgets
.Label
;
36 import org
.eclipse
.swt
.widgets
.Listener
;
37 import org
.eclipse
.swt
.widgets
.Shell
;
38 import org
.eclipse
.swt
.widgets
.Table
;
39 import org
.eclipse
.swt
.widgets
.TableItem
;
40 import org
.eclipse
.swt
.widgets
.Text
;
41 import org
.eclipse
.ui
.IMemento
;
42 import org
.eclipse
.ui
.PlatformUI
;
43 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
44 import org
.eclipse
.wb
.swt
.ResourceManager
;
46 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
47 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
48 import eu
.etaxonomy
.cdm
.ext
.occurrence
.OccurenceQuery
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
50 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
51 import eu
.etaxonomy
.taxeditor
.databaseAdmin
.wizard
.ImportPreferencesWizard
;
52 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
53 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
54 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
55 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
56 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
57 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
58 import eu
.etaxonomy
.taxeditor
.view
.dataimport
.SaveImportedSpecimenAction
;
61 * View which shows a list of "data" that can be imported into the CDM
65 * @param <T> the CDM type that will be handled by this view
67 public abstract class DataImportViewE4
<T
> implements IPartContentHasFactualData
,
68 IConversationEnabled
, IPartContentHasDetails
, IPartContentHasSupplementalData
, IContextListener
, Listener
{
70 protected final Logger logger
= Logger
.getLogger(DataImportViewE4
.class);
75 protected Collection
<T
> results
= new ArrayList
<T
>();
77 protected boolean updated
= false;
79 protected OccurenceQuery query
;
81 private static ConversationHolder conversationHolder
;
83 private SaveImportedSpecimenAction saveImportedSpecimenAction
;
85 private Text textClassification
;
87 private Classification classification
;
89 private Text textReferenceString
;
91 private Button toggleButton
;
93 private boolean state
;
95 private Button openConfigurator
;
97 public Classification
getClassification() {
98 return classification
;
101 public void setClassification(Classification classification
) {
102 this.classification
= classification
;
105 private Button btnBrowseClassification
;
107 private Button btnClear
;
109 private final FormToolkit toolkit
= new FormToolkit(Display
.getCurrent());
114 * Constructs a new DataImportEditor and registers it to listen to context changes
116 public DataImportViewE4() {
117 CdmStore
.getContextManager().addContextListener(this);
118 if(CdmStore
.isActive()){
124 * Create contents of the view part.
128 public void createPartControl(Composite parent
, @Named(IServiceConstants
.ACTIVE_SHELL
)Shell shell
) {
130 Composite composite
= new Composite(parent
, SWT
.NONE
);
131 composite
.setLayout(new GridLayout(2, false));
133 Composite composite_1
= new Composite(composite
, SWT
.NONE
);
134 GridData gd_composite_1
= new GridData(SWT
.LEFT
, SWT
.TOP
, false, false, 1, 1);
135 gd_composite_1
.widthHint
= 280;
136 composite_1
.setLayoutData(gd_composite_1
);
137 composite_1
.setLayout(new GridLayout(3, false));
139 Label label
= new Label(composite_1
, SWT
.TOP
);
140 label
.setText("Classification");
141 label
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 3, 1));
142 textClassification
= new Text(composite_1
, SWT
.BORDER
);
143 textClassification
.setEnabled(false);
144 GridData gd_textClassification
= new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 1, 1);
145 gd_textClassification
.widthHint
= 118;
146 textClassification
.setLayoutData(new GridData(SWT
.FILL
, SWT
.LEFT
, true, true));
147 btnBrowseClassification
= new Button(composite_1
, SWT
.NONE
);
148 btnBrowseClassification
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/open.gif"));
149 btnBrowseClassification
.addListener(SWT
.Selection
, this);
150 btnClear
= new Button(composite_1
, SWT
.NONE
);
151 btnClear
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/trash.gif"));
152 btnClear
.addListener(SWT
.Selection
, this);
155 Label labelRef
= new Label(composite_1
, SWT
.NONE
);
156 labelRef
.setText("Default import souce reference");
157 labelRef
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 3, 1));
158 textReferenceString
= new Text(composite_1
, SWT
.NONE
);
159 textReferenceString
.setEnabled(true);
160 GridData gd_textReferenceString
= new GridData(SWT
.LEFT
, SWT
.CENTER
, true, true, 3, 1);
161 gd_textReferenceString
.widthHint
= 229;
162 textReferenceString
.setLayoutData(gd_textReferenceString
);
165 openConfigurator
= new Button(composite_1
, SWT
.PUSH
);
166 openConfigurator
.setText("Configuration");
167 new Label(composite_1
, SWT
.NONE
);
168 new Label(composite_1
, SWT
.NONE
);
169 openConfigurator
.addSelectionListener(new SelectionAdapter(){
171 public void widgetSelected(SelectionEvent e
) {
173 IWizard wizard
= new ImportPreferencesWizard();
174 WizardDialog dialog
= new WizardDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), wizard
);
179 //checkbox table for result
180 Composite composite_2
= new Composite(composite
, SWT
.NONE
);
181 GridData gd_composite_2
= new GridData(SWT
.LEFT
, SWT
.TOP
, true, true, 1, 1);
182 gd_composite_2
.heightHint
= 454;
183 gd_composite_2
.widthHint
= 403;
184 composite_2
.setLayoutData(gd_composite_2
);
185 composite_2
.setLayout(new GridLayout(2, false));
188 CheckboxTableViewer checkboxTableViewer
= CheckboxTableViewer
.newCheckList(composite_2
, SWT
.BORDER
| SWT
.FULL_SELECTION
);
191 table
= checkboxTableViewer
.getTable();
192 GridData gd_table
= new GridData(SWT
.LEFT
, SWT
.TOP
, true, true, 1, 1);
193 gd_table
.heightHint
= 444;
194 gd_table
.widthHint
= 312;
195 table
.setLayoutData(gd_table
);
196 toolkit
.paintBordersFor(table
);
198 toggleButton
= new Button(composite_2
, SWT
.PUSH
);
199 GridData gd_toggleButton
= new GridData(SWT
.LEFT
, SWT
.TOP
, false, false, 1, 1);
200 gd_toggleButton
.widthHint
= 56;
201 toggleButton
.setLayoutData(gd_toggleButton
);
202 toggleButton
.setText("Toggle");
203 toggleButton
.addSelectionListener(new SelectionAdapter(){
205 public void widgetSelected(SelectionEvent e
) {
206 state
= state ?
false : true;
207 for (TableItem item
: getTable().getItems()){
208 item
.setChecked(state
);
218 * Create the actions.
220 private void createActions() {
221 saveImportedSpecimenAction
= new SaveImportedSpecimenAction();
225 * Initialize the toolbar.
227 private void initializeToolBar() {
229 // IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
230 // tbm.add(saveImportedSpecimenAction);
233 public Table
getTable() {
237 public void setQuery(OccurenceQuery query
) {
241 public void setResults(Collection
<T
> results
) {
242 this.results
= results
;
247 public void dispose() {
248 CdmStore
.getContextManager().removeContextListener(this);
252 public void contextRefresh(IProgressMonitor monitor
) {
256 protected void refresh(){
258 if (getTable() != null){
259 getTable().removeAll();
262 TableItem tableItem
= new TableItem(getTable(), SWT
.NONE
);
263 tableItem
.setText(getTextForTableItem(item
));
264 tableItem
.setData(item
);
270 public abstract void query();
273 public void update(CdmDataChangeMap changeEvents
) {
278 * Returns the text label of the given item.
279 * @param item the item for which the label should be returns
280 * @return the label of the item
282 protected abstract String
getTextForTableItem(T item
);
285 public void setFocus() {
286 getTable().setFocus();
287 //make sure to bind again if maybe in another view the conversation was unbound
288 if(getConversationHolder()!=null && !getConversationHolder().isBound()){
289 getConversationHolder().bind();
294 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
298 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
302 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
306 private void initConversation(){
307 if(conversationHolder
==null){
308 conversationHolder
= CdmStore
.createConversation();
313 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {
314 if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
315 getConversationHolder().close();
320 * @return the conversationHolder
323 public ConversationHolder
getConversationHolder() {
324 if(CdmStore
.isActive() && conversationHolder
==null){
327 return conversationHolder
;
331 public void handleEvent(Event event
) {
332 if(event
.widget
==btnBrowseClassification
){
333 classification
= SelectionDialogFactory
.getSelectionFromDialog(Classification
.class, shell
, null, null);
334 if(classification
!=null){
335 textClassification
.setText(classification
.getTitleCache());
338 else if(event
.widget
==btnClear
){
339 classification
= null;
340 textClassification
.setText("");
344 public String
getReferenceString() {
345 return textReferenceString
.getText();