use last selected abcd provider for import
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / dataimport / e4 / DataImportViewE4.java
1 /**
2 * Copyright (C) 2013 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.view.dataimport.e4;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13
14 import javax.annotation.PostConstruct;
15 import javax.annotation.PreDestroy;
16
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.forms.widgets.FormToolkit;
39 import org.eclipse.wb.swt.ResourceManager;
40
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.io.specimen.abcd206.in.Abcd206ImportConfigurator;
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.preference.PreferencesUtil;
53 import eu.etaxonomy.taxeditor.store.CdmStore;
54 import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
55
56 /**
57 * View which shows a list of "data" that can be imported into the CDM
58 * @author pplitzner
59 * @date 22.08.2017
60 *
61 * @param <T> the CDM type that will be handled by this view
62 */
63 public abstract class DataImportViewE4<T> implements IPartContentHasFactualData,
64 IConversationEnabled, IPartContentHasDetails, IPartContentHasSupplementalData, IContextListener, Listener{
65
66 protected final Logger logger = Logger.getLogger(DataImportViewE4.class);
67
68 protected Collection<T> results = new ArrayList<T>();
69
70 protected boolean updated = false;
71
72 protected OccurenceQuery query;
73
74 private static ConversationHolder conversationHolder;
75
76 private SaveImportedSpecimenActionE4 saveImportedSpecimenAction;
77
78 private Text textClassification;
79
80 private Classification classification;
81
82 private Text textReferenceString;
83
84 private Button toggleButton;
85
86 private Button importButton;
87
88 private boolean state;
89
90 private Button openConfigurator;
91
92 private Abcd206ImportConfigurator config;
93
94 public Classification getClassification() {
95 return classification;
96 }
97
98 public void setClassification(Classification classification) {
99 this.classification = classification;
100 }
101
102 private Button btnBrowseClassification;
103
104 private Button btnClear;
105
106 private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
107
108 private Table table;
109 private Composite composite_3;
110
111 /**
112 * Constructs a new DataImportEditor and registers it to listen to context changes
113 */
114 public DataImportViewE4() {
115 CdmStore.getContextManager().addContextListener(this);
116 }
117
118 /**
119 * Create contents of the view part.
120 * @param parent
121 */
122 @PostConstruct
123 public void createPartControl(Composite parent) {
124 if(CdmStore.isActive()){
125 initConversation();
126 }
127 else{
128 return;
129 }
130 parent.setLayout(new GridLayout(1, false));
131 Composite composite = new Composite(parent, SWT.NONE);
132 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
133 // composite.setBounds(0, 0, 0, 298);
134 composite.setLayout(new GridLayout(2, false));
135
136 Composite composite_1 = new Composite(composite, SWT.NONE);
137 // gd_composite_1.widthHint = 280;
138 composite_1.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
139 composite_1.setLayout(new GridLayout(3, false));
140
141 Label label = new Label(composite_1, SWT.TOP);
142 label.setText("Classification");
143 label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
144 textClassification = new Text(composite_1, SWT.BORDER);
145 textClassification.setEnabled(false);
146 GridData gd_textClassification = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
147 // gd_textClassification.widthHint = 118;
148 textClassification.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, true));
149 btnBrowseClassification = new Button(composite_1, SWT.NONE);
150 btnBrowseClassification.setImage(ResourceManager.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/open.gif"));
151 btnBrowseClassification.addListener(SWT.Selection, this);
152 btnClear = new Button(composite_1, SWT.NONE);
153 btnClear.setImage(ResourceManager.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/trash.gif"));
154 btnClear.addListener(SWT.Selection, this);
155 //source reference
156
157 Label labelRef = new Label(composite_1, SWT.NONE);
158 labelRef.setText("Default import souce reference");
159 labelRef.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
160 textReferenceString = new Text(composite_1, SWT.NONE);
161 textReferenceString.setEnabled(true);
162 GridData gd_textReferenceString = new GridData(SWT.LEFT, SWT.CENTER, true, true, 3, 1);
163 gd_textReferenceString.widthHint = 229;
164 textReferenceString.setLayoutData(gd_textReferenceString);
165
166 //open configuration
167
168 PreferencesUtil.getLocalAbcdImportConfigurator(false);
169
170 openConfigurator = new Button(composite_1, SWT.PUSH);
171 openConfigurator.setText("Configuration");
172 new Label(composite_1, SWT.NONE);
173 new Label(composite_1, SWT.NONE);
174
175 openConfigurator.addSelectionListener(new SelectionAdapter(){
176 @Override
177 public void widgetSelected(SelectionEvent e) {
178
179 IWizard wizard = new ImportPreferencesWizard();
180 WizardDialog dialog = new WizardDialog(e.display.getActiveShell(), wizard);
181 dialog.open();
182
183 }
184 });
185
186 //checkbox table for result
187 Composite composite_2 = new Composite(composite, SWT.NONE);
188 composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
189 composite_2.setLayout(new GridLayout(2, false));
190
191
192 CheckboxTableViewer checkboxTableViewer = CheckboxTableViewer.newCheckList(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
193
194
195 table = checkboxTableViewer.getTable();
196 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
197 toolkit.paintBordersFor(table);
198
199 composite_3 = new Composite(composite_2, SWT.NONE);
200 // gd_composite_3.heightHint = 48;
201 composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
202 toolkit.paintBordersFor(composite_3);
203 composite_3.setLayout(new GridLayout(1, false));
204 //toggle button
205 toggleButton = new Button(composite_3, SWT.PUSH);
206 toggleButton.setText("Toggle");
207 new Label(composite_2, SWT.NONE);
208
209 Composite composite_4 = new Composite(composite_2, SWT.NONE);
210 composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
211 toolkit.adapt(composite_4);
212 toolkit.paintBordersFor(composite_4);
213
214 importButton = new Button(composite_4, SWT.PUSH);
215 importButton.setSize(48, 25);
216 importButton.setText("Import");
217 importButton.addSelectionListener(new SelectionAdapter(){
218 @Override
219 public void widgetSelected(SelectionEvent e) {
220 saveImportedSpecimenAction.run();
221 }
222 });
223 new Label(composite_2, SWT.NONE);
224 new Label(composite_2, SWT.NONE);
225 new Label(composite_2, SWT.NONE);
226 new Label(composite_2, SWT.NONE);
227 new Label(composite_2, SWT.NONE);
228 new Label(composite_2, SWT.NONE);
229 toggleButton.addSelectionListener(new SelectionAdapter(){
230 @Override
231 public void widgetSelected(SelectionEvent e) {
232 state = state ? false : true;
233 for (TableItem item: getTable().getItems()){
234 item.setChecked(state);
235 }
236
237 }
238 });
239 //import button
240
241 saveImportedSpecimenAction = new SaveImportedSpecimenActionE4(this);
242 initializeToolBar();
243 }
244
245 /**
246 * Initialize the toolbar.
247 */
248 private void initializeToolBar() {
249 //FIXME E4 migrate
250 // IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
251 // tbm.add(saveImportedSpecimenAction);
252 }
253
254 public Table getTable() {
255 return table;
256 }
257
258 public void setQuery(OccurenceQuery query) {
259 this.query = query;
260 }
261
262 public void setResults(Collection<T> results) {
263 this.results = results;
264 updated=false;
265 }
266
267 @PreDestroy
268 public void dispose() {
269 CdmStore.getContextManager().removeContextListener(this);
270 }
271
272 @Override
273 public void contextRefresh(IProgressMonitor monitor) {
274 refresh();
275 }
276
277 protected void refresh(){
278 if (!updated){
279 if (getTable() != null){
280 getTable().removeAll();
281 }
282 if(results!=null){
283 for(T item:results){
284 TableItem tableItem = new TableItem(getTable(), SWT.NONE);
285 tableItem.setText(getTextForTableItem(item));
286 tableItem.setData(item);
287 }
288 }
289 updated = true;
290 }
291 }
292
293 public abstract void query();
294
295 @Override
296 public void update(CdmDataChangeMap changeEvents) {
297 //nothing
298 }
299
300 /**
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
304 */
305 protected abstract String getTextForTableItem(T item);
306
307 @Focus
308 public void setFocus() {
309 if(getTable()!=null){
310 getTable().setFocus();
311 }
312 //make sure to bind again if maybe in another view the conversation was unbound
313 if(getConversationHolder()!=null && !getConversationHolder().isBound()){
314 getConversationHolder().bind();
315 }
316 }
317
318 @Override
319 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
320 }
321
322 @Override
323 public void contextStop(IMemento memento, IProgressMonitor monitor) {
324 }
325
326 @Override
327 public void contextStart(IMemento memento, IProgressMonitor monitor) {
328 initConversation();
329 }
330
331 private void initConversation(){
332 if(conversationHolder==null){
333 conversationHolder = CdmStore.createConversation();
334 }
335 }
336
337 @Override
338 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
339 if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
340 getConversationHolder().close();
341 }
342 }
343
344 /**
345 * @return the conversationHolder
346 */
347 @Override
348 public ConversationHolder getConversationHolder() {
349 if(CdmStore.isActive() && conversationHolder==null){
350 initConversation();
351 }
352 return conversationHolder;
353 }
354
355 @Override
356 public void handleEvent(Event event) {
357 if(event.widget==btnBrowseClassification){
358 classification = SelectionDialogFactory.getSelectionFromDialog(Classification.class, event.widget.getDisplay().getActiveShell(), null, null);
359 if(classification!=null){
360 textClassification.setText(classification.getTitleCache());
361 }
362 }
363 else if(event.widget==btnClear){
364 classification = null;
365 textClassification.setText("");
366 }
367 }
368
369 public String getReferenceString() {
370 return textReferenceString.getText();
371 }
372 }