Merge branch 'hotfix/5.45.1'
[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;
18 import org.apache.logging.log4j.Logger;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.e4.ui.di.Focus;
21 import org.eclipse.jface.viewers.CheckboxTableViewer;
22 import org.eclipse.jface.wizard.IWizard;
23 import org.eclipse.jface.wizard.WizardDialog;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.swt.widgets.Event;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Listener;
35 import org.eclipse.swt.widgets.Table;
36 import org.eclipse.swt.widgets.TableItem;
37 import org.eclipse.swt.widgets.Text;
38 import org.eclipse.ui.IMemento;
39 import org.eclipse.ui.forms.widgets.FormToolkit;
40
41 import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
42 import eu.etaxonomy.taxeditor.databaseAdmin.wizard.ImportPreferencesWizard;
43 import eu.etaxonomy.taxeditor.model.IContextListener;
44 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
45 import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
46 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
47 import eu.etaxonomy.taxeditor.store.CdmStore;
48 import eu.etaxonomy.taxeditor.workbench.part.IEditorAreaPart;
49
50 /**
51 * View which shows a list of "data" that can be imported into the CDM
52 *
53 * @author pplitzner
54 * @date 22.08.2017
55 *
56 * @param <T> the CDM type that will be handled by this view
57 */
58 public abstract class DataImportView<T> implements IPartContentHasFactualData, IPartContentHasDetails, IPartContentHasSupplementalData, IContextListener, Listener, IEditorAreaPart{
59
60 protected final Logger logger = LogManager.getLogger();
61
62 protected Collection<T> results = new ArrayList<>();
63
64 protected boolean updated = false;
65
66 protected OccurenceQuery query;
67
68 private SaveImportedSpecimenAction saveImportedSpecimenAction;
69
70 protected Text textReferenceString;
71
72 private Button selectAllButton;
73
74 private Button importButton;
75
76 private boolean state;
77
78 private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
79
80 private Table table;
81 private Composite composite_3;
82
83 /**
84 * Constructs a new DataImportEditor and registers it to listen to context changes
85 */
86 public DataImportView() {
87 CdmStore.getContextManager().addContextListener(this);
88 }
89
90 /**
91 * Create contents of the view part.
92 * @param parent
93 */
94 @PostConstruct
95 public void createPartControl(Composite parent) {
96
97 parent.setLayout(new GridLayout(1, false));
98 Composite composite = new Composite(parent, SWT.NONE);
99 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
100 // composite.setBounds(0, 0, 0, 298);
101 composite.setLayout(new GridLayout(2, false));
102
103 Composite composite_1 = new Composite(composite, SWT.NONE);
104 // gd_composite_1.widthHint = 280;
105 composite_1.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
106 composite_1.setLayout(new GridLayout(3, false));
107
108 // Label label = new Label(composite_1, SWT.TOP);
109 // label.setText("Classification");
110 // label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
111 // textClassification = new Text(composite_1, SWT.BORDER);
112 // textClassification.setEnabled(false);
113 // GridData gd_textClassification = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
114 //// gd_textClassification.widthHint = 118;
115 // textClassification.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, true));
116 // btnBrowseClassification = new Button(composite_1, SWT.NONE);
117 // btnBrowseClassification.setImage(ResourceManager.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/open.gif"));
118 // btnBrowseClassification.addListener(SWT.Selection, this);
119 // btnClear = new Button(composite_1, SWT.NONE);
120 // btnClear.setImage(ResourceManager.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/trash.gif"));
121 // btnClear.addListener(SWT.Selection, this);
122 //source reference
123
124 // Label labelRef = new Label(composite_1, SWT.NONE);
125 // labelRef.setText("Import souce reference");
126 // labelRef.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
127 // textReferenceString = new Text(composite_1, SWT.NONE);
128 // textReferenceString.setEnabled(true);
129 // GridData gd_textReferenceString = new GridData(SWT.LEFT, SWT.CENTER, true, true, 3, 1);
130 // gd_textReferenceString.widthHint = 229;
131 // textReferenceString.setLayoutData(gd_textReferenceString);
132 // DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
133 // LocalDate localDate = LocalDate.now();
134 //
135 // textReferenceString.setText("Specimen Import " + localDate.toString());
136 //open configuration
137 //
138 // PreferencesUtil.getLocalAbcdImportConfigurator(false);
139 //
140 // openConfigurator = new Button(composite_1, SWT.PUSH);
141 // openConfigurator.setText("Configuration");
142 // new Label(composite_1, SWT.NONE);
143 // new Label(composite_1, SWT.NONE);
144 //
145 // openConfigurator.addSelectionListener(new SelectionAdapter(){
146 // @Override
147 // public void widgetSelected(SelectionEvent e) {
148 //
149 // IWizard wizard = new ImportPreferencesWizard();
150 // WizardDialog dialog = new WizardDialog(e.display.getActiveShell(), wizard);
151 // dialog.open();
152 //
153 // }
154 // });
155
156 //checkbox table for result
157 Composite composite_2 = new Composite(composite, SWT.NONE);
158 composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
159 composite_2.setLayout(new GridLayout(2, false));
160
161
162 CheckboxTableViewer checkboxTableViewer = CheckboxTableViewer.newCheckList(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
163
164 table = checkboxTableViewer.getTable();
165 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
166 table.addListener(SWT.Selection, this);
167 toolkit.paintBordersFor(table);
168
169 composite_3 = new Composite(composite_2, SWT.NONE);
170 // gd_composite_3.heightHint = 48;
171 composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
172 toolkit.paintBordersFor(composite_3);
173 composite_3.setLayout(new GridLayout(1, false));
174 //toggle button
175 selectAllButton = new Button(composite_3, SWT.PUSH);
176 selectAllButton.setText("Select all/none");
177 new Label(composite_2, SWT.NONE);
178
179 // Composite composite_4 = new Composite(composite_2, SWT.NONE);
180 // composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
181 // composite_4.setBackground(composite.getBackground());
182 // toolkit.adapt(composite_4);
183 // toolkit.paintBordersFor(composite_4);
184
185 importButton = new Button(composite_2, SWT.PUSH);
186 importButton.setSize(48, 25);
187 importButton.setText("Import");
188 importButton.addSelectionListener(new SelectionAdapter(){
189 @Override
190 public void widgetSelected(SelectionEvent e) {
191 IWizard wizard = new ImportPreferencesWizard();
192 WizardDialog dialog = new WizardDialog(e.display.getActiveShell(), wizard);
193 int status = dialog.open();
194 if (status == 0){
195 saveImportedSpecimenAction.run();
196 }
197 }
198 });
199 // new Label(composite_2, SWT.NONE);
200 // new Label(composite_2, SWT.NONE);
201 // new Label(composite_2, SWT.NONE);
202 // new Label(composite_2, SWT.NONE);
203 // new Label(composite_2, SWT.NONE);
204 // new Label(composite_2, SWT.NONE);
205 selectAllButton.addSelectionListener(new SelectionAdapter(){
206 @Override
207 public void widgetSelected(SelectionEvent e) {
208 state = state ? false : true;
209 for (TableItem item: getTable().getItems()){
210 item.setChecked(state);
211 }
212
213 }
214 });
215 //import button
216
217 saveImportedSpecimenAction = new SaveImportedSpecimenAction(this);
218 initializeToolBar();
219 }
220
221 /**
222 * Initialize the toolbar.
223 */
224 private void initializeToolBar() {
225 //FIXME E4 migrate
226 // IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
227 // tbm.add(saveImportedSpecimenAction);
228 }
229
230 public Table getTable() {
231 return table;
232 }
233
234 public void setQuery(OccurenceQuery query) {
235 this.query = query;
236 }
237
238 public void setResults(Collection<T> results) {
239 this.results = results;
240 updated=false;
241
242 }
243
244 @PreDestroy
245 public void dispose() {
246 CdmStore.getContextManager().removeContextListener(this);
247 }
248
249 @Override
250 public void contextRefresh(IProgressMonitor monitor) {
251 refresh();
252 }
253
254 protected void refresh(){
255 if (!updated){
256 if (getTable() != null){
257 getTable().removeAll();
258 }
259 if(results!=null){
260 for(T item:results){
261 TableItem tableItem = new TableItem(getTable(), SWT.NONE);
262 tableItem.setText(getTextForTableItem(item));
263 tableItem.setData(item);
264 }
265 }
266 updated = true;
267 }
268 }
269
270 public abstract void query();
271
272 /**
273 * Returns the text label of the given item.
274 * @param item the item for which the label should be returns
275 * @return the label of the item
276 */
277 protected abstract String getTextForTableItem(T item);
278
279 @Focus
280 public void setFocus() {
281 if(getTable()!=null){
282 getTable().setFocus();
283 }
284 }
285
286 @Override
287 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
288 }
289
290 @Override
291 public void contextStop(IMemento memento, IProgressMonitor monitor) {
292 }
293
294 @Override
295 public void contextStart(IMemento memento, IProgressMonitor monitor) {
296
297 }
298
299 @Override
300 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
301 }
302
303 @Override
304 public void handleEvent(Event event) {
305 // if(event.widget==btnBrowseClassification){
306 // classification = SelectionDialogFactory.getSelectionFromDialog(Classification.class, event.widget.getDisplay().getActiveShell(), null, null);
307 // if(classification!=null){
308 // textClassification.setText(classification.getTitleCache());
309 // }
310 // }
311 // else if(event.widget==btnClear){
312 // classification = null;
313 // textClassification.setText("");
314 // }
315
316 // else {
317 if(event.widget == table){
318 TableItem[] item = table.getSelection();
319 if (item.length == 1){
320 Object o = item[0].getData();
321 }
322 }
323 // }
324 }
325
326 public String getReferenceString() {
327 return textReferenceString.getText();
328 }
329 }