ref #9359 upgrade TaxEditor to log4j2
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / dataimport / e4 / DataImportView.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.logging.log4j.LogManager;import org.apache.logging.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
40 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
41 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
42 import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
43 import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
44 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
45 import eu.etaxonomy.taxeditor.databaseAdmin.wizard.ImportPreferencesWizard;
46 import eu.etaxonomy.taxeditor.model.IContextListener;
47 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
48 import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
49 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
50 import eu.etaxonomy.taxeditor.store.CdmStore;
51 import eu.etaxonomy.taxeditor.workbench.part.IEditorAreaPart;
52
53 /**
54 * View which shows a list of "data" that can be imported into the CDM
55 * @author pplitzner
56 * @date 22.08.2017
57 *
58 * @param <T> the CDM type that will be handled by this view
59 */
60 public abstract class DataImportView<T> implements IPartContentHasFactualData,
61 IConversationEnabled, IPartContentHasDetails, IPartContentHasSupplementalData, IContextListener, Listener, IEditorAreaPart{
62
63 protected final Logger logger = LogManager.getLogger(DataImportView.class);
64
65 protected Collection<T> results = new ArrayList<>();
66
67 protected boolean updated = false;
68
69 protected OccurenceQuery query;
70
71 private static ConversationHolder conversationHolder;
72
73 private SaveImportedSpecimenAction saveImportedSpecimenAction;
74
75 // private Text textClassification;
76
77 // private Classification classification;
78
79 protected Text textReferenceString;
80
81 private Button selectAllButton;
82 //private Button selectNoneButton;
83
84 private Button importButton;
85
86 private boolean state;
87
88 private Button openConfigurator;
89
90 private Abcd206ImportConfigurator config;
91
92 // public Classification getClassification() {
93 // return classification;
94 // }
95 //
96 // public void setClassification(Classification classification) {
97 // this.classification = classification;
98 // }
99
100 private Button btnBrowseClassification;
101
102 private Button btnClear;
103
104 private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
105
106 private Table table;
107 private Composite composite_3;
108
109 /**
110 * Constructs a new DataImportEditor and registers it to listen to context changes
111 */
112 public DataImportView() {
113 CdmStore.getContextManager().addContextListener(this);
114 }
115
116 /**
117 * Create contents of the view part.
118 * @param parent
119 */
120 @PostConstruct
121 public void createPartControl(Composite parent) {
122 if(CdmStore.isActive()){
123 initConversation();
124 }
125 else{
126 return;
127 }
128 parent.setLayout(new GridLayout(1, false));
129 Composite composite = new Composite(parent, SWT.NONE);
130 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
131 // composite.setBounds(0, 0, 0, 298);
132 composite.setLayout(new GridLayout(2, false));
133
134 Composite composite_1 = new Composite(composite, SWT.NONE);
135 // gd_composite_1.widthHint = 280;
136 composite_1.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
137 composite_1.setLayout(new GridLayout(3, false));
138
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);
153 //source reference
154
155 // Label labelRef = new Label(composite_1, SWT.NONE);
156 // labelRef.setText("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);
163 // DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
164 // LocalDate localDate = LocalDate.now();
165 //
166 // textReferenceString.setText("Specimen Import " + localDate.toString());
167 //open configuration
168 //
169 // PreferencesUtil.getLocalAbcdImportConfigurator(false);
170 //
171 // openConfigurator = new Button(composite_1, SWT.PUSH);
172 // openConfigurator.setText("Configuration");
173 // new Label(composite_1, SWT.NONE);
174 // new Label(composite_1, SWT.NONE);
175 //
176 // openConfigurator.addSelectionListener(new SelectionAdapter(){
177 // @Override
178 // public void widgetSelected(SelectionEvent e) {
179 //
180 // IWizard wizard = new ImportPreferencesWizard();
181 // WizardDialog dialog = new WizardDialog(e.display.getActiveShell(), wizard);
182 // dialog.open();
183 //
184 // }
185 // });
186
187 //checkbox table for result
188 Composite composite_2 = new Composite(composite, SWT.NONE);
189 composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
190 composite_2.setLayout(new GridLayout(2, false));
191
192
193 CheckboxTableViewer checkboxTableViewer = CheckboxTableViewer.newCheckList(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
194
195
196 table = checkboxTableViewer.getTable();
197 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
198 table.addListener(SWT.Selection, this);
199 toolkit.paintBordersFor(table);
200
201 composite_3 = new Composite(composite_2, SWT.NONE);
202 // gd_composite_3.heightHint = 48;
203 composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
204 toolkit.paintBordersFor(composite_3);
205 composite_3.setLayout(new GridLayout(1, false));
206 //toggle button
207 selectAllButton = new Button(composite_3, SWT.PUSH);
208 selectAllButton.setText("Select all/none");
209 new Label(composite_2, SWT.NONE);
210
211 // Composite composite_4 = new Composite(composite_2, SWT.NONE);
212 // composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
213 // composite_4.setBackground(composite.getBackground());
214 // toolkit.adapt(composite_4);
215 // toolkit.paintBordersFor(composite_4);
216
217 importButton = new Button(composite_2, SWT.PUSH);
218 importButton.setSize(48, 25);
219 importButton.setText("Import");
220 importButton.addSelectionListener(new SelectionAdapter(){
221 @Override
222 public void widgetSelected(SelectionEvent e) {
223 IWizard wizard = new ImportPreferencesWizard();
224 WizardDialog dialog = new WizardDialog(e.display.getActiveShell(), wizard);
225 int status = dialog.open();
226 if (status == 0){
227 saveImportedSpecimenAction.run();
228 }
229 }
230 });
231 // new Label(composite_2, SWT.NONE);
232 // new Label(composite_2, SWT.NONE);
233 // new Label(composite_2, SWT.NONE);
234 // new Label(composite_2, SWT.NONE);
235 // new Label(composite_2, SWT.NONE);
236 // new Label(composite_2, SWT.NONE);
237 selectAllButton.addSelectionListener(new SelectionAdapter(){
238 @Override
239 public void widgetSelected(SelectionEvent e) {
240 state = state ? false : true;
241 for (TableItem item: getTable().getItems()){
242 item.setChecked(state);
243 }
244
245 }
246 });
247 //import button
248
249 saveImportedSpecimenAction = new SaveImportedSpecimenAction(this);
250 initializeToolBar();
251 }
252
253 /**
254 * Initialize the toolbar.
255 */
256 private void initializeToolBar() {
257 //FIXME E4 migrate
258 // IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
259 // tbm.add(saveImportedSpecimenAction);
260 }
261
262 public Table getTable() {
263 return table;
264 }
265
266 public void setQuery(OccurenceQuery query) {
267 this.query = query;
268 }
269
270 public void setResults(Collection<T> results) {
271 this.results = results;
272 updated=false;
273
274 }
275
276 @PreDestroy
277 public void dispose() {
278 CdmStore.getContextManager().removeContextListener(this);
279 }
280
281 @Override
282 public void contextRefresh(IProgressMonitor monitor) {
283 refresh();
284 }
285
286 protected void refresh(){
287 if (!updated){
288 if (getTable() != null){
289 getTable().removeAll();
290 }
291 if(results!=null){
292 for(T item:results){
293 TableItem tableItem = new TableItem(getTable(), SWT.NONE);
294 tableItem.setText(getTextForTableItem(item));
295 tableItem.setData(item);
296 }
297 }
298 updated = true;
299 }
300 }
301
302 public abstract void query();
303
304 @Override
305 public void update(CdmDataChangeMap changeEvents) {
306 //nothing
307 }
308
309 /**
310 * Returns the text label of the given item.
311 * @param item the item for which the label should be returns
312 * @return the label of the item
313 */
314 protected abstract String getTextForTableItem(T item);
315
316 @Focus
317 public void setFocus() {
318 if(getTable()!=null){
319 getTable().setFocus();
320 }
321 //make sure to bind again if maybe in another view the conversation was unbound
322 if(getConversationHolder()!=null && !getConversationHolder().isBound()){
323 getConversationHolder().bind();
324 }
325 }
326
327 @Override
328 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
329 }
330
331 @Override
332 public void contextStop(IMemento memento, IProgressMonitor monitor) {
333 }
334
335 @Override
336 public void contextStart(IMemento memento, IProgressMonitor monitor) {
337 initConversation();
338 }
339
340 private void initConversation(){
341 if(conversationHolder==null){
342 conversationHolder = CdmStore.createConversation();
343 }
344 }
345
346 @Override
347 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
348 if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
349 getConversationHolder().close();
350 }
351 }
352
353 /**
354 * @return the conversationHolder
355 */
356 @Override
357 public ConversationHolder getConversationHolder() {
358 if(CdmStore.isActive() && conversationHolder==null){
359 initConversation();
360 }
361 return conversationHolder;
362 }
363
364 @Override
365 public void handleEvent(Event event) {
366 // if(event.widget==btnBrowseClassification){
367 // classification = SelectionDialogFactory.getSelectionFromDialog(Classification.class, event.widget.getDisplay().getActiveShell(), null, null);
368 // if(classification!=null){
369 // textClassification.setText(classification.getTitleCache());
370 // }
371 // }
372 // else if(event.widget==btnClear){
373 // classification = null;
374 // textClassification.setText("");
375 // }
376
377 // else {
378 if(event.widget == table){
379 TableItem[] item = table.getSelection();
380 if (item.length == 1){
381 Object o = item[0].getData();
382 }
383 }
384 // }
385 }
386
387 public String getReferenceString() {
388 return textReferenceString.getText();
389 }
390 }