ref #6908 Migrate polytomous key list and graph editor
[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.PlatformUI;
39 import org.eclipse.ui.forms.widgets.FormToolkit;
40 import org.eclipse.wb.swt.ResourceManager;
41
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;
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 SaveImportedSpecimenAction saveImportedSpecimenAction;
77
78 private Text textClassification;
79
80 private Classification classification;
81
82 private Text textReferenceString;
83
84 private Button toggleButton;
85
86 private boolean state;
87
88 private Button openConfigurator;
89
90 public Classification getClassification() {
91 return classification;
92 }
93
94 public void setClassification(Classification classification) {
95 this.classification = classification;
96 }
97
98 private Button btnBrowseClassification;
99
100 private Button btnClear;
101
102 private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
103
104 private Table table;
105
106 /**
107 * Constructs a new DataImportEditor and registers it to listen to context changes
108 */
109 public DataImportViewE4() {
110 CdmStore.getContextManager().addContextListener(this);
111
112 }
113
114 /**
115 * Create contents of the view part.
116 * @param parent
117 */
118 @PostConstruct
119 public void createPartControl(Composite parent) {
120 if(CdmStore.isActive()){
121 initConversation();
122 }
123 else{
124 return;
125 }
126 Composite composite = new Composite(parent, SWT.NONE);
127 composite.setLayout(new GridLayout(2, false));
128
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));
134
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);
149 //source reference
150
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);
159
160 //open configuration
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(){
166 @Override
167 public void widgetSelected(SelectionEvent e) {
168
169 IWizard wizard = new ImportPreferencesWizard();
170 WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), wizard);
171 dialog.open();
172 }
173 });
174
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));
182
183
184 CheckboxTableViewer checkboxTableViewer = CheckboxTableViewer.newCheckList(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
185
186
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);
193 //toggle button
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(){
200 @Override
201 public void widgetSelected(SelectionEvent e) {
202 state = state ? false : true;
203 for (TableItem item: getTable().getItems()){
204 item.setChecked(state);
205 }
206
207 }
208 });
209 createActions();
210 initializeToolBar();
211 }
212
213 /**
214 * Create the actions.
215 */
216 private void createActions() {
217 saveImportedSpecimenAction = new SaveImportedSpecimenAction();
218 }
219
220 /**
221 * Initialize the toolbar.
222 */
223 private void initializeToolBar() {
224 //FIXME E4 migrate
225 // IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
226 // tbm.add(saveImportedSpecimenAction);
227 }
228
229 public Table getTable() {
230 return table;
231 }
232
233 public void setQuery(OccurenceQuery query) {
234 this.query = query;
235 }
236
237 public void setResults(Collection<T> results) {
238 this.results = results;
239 updated=false;
240 }
241
242 @PreDestroy
243 public void dispose() {
244 CdmStore.getContextManager().removeContextListener(this);
245 }
246
247 @Override
248 public void contextRefresh(IProgressMonitor monitor) {
249 refresh();
250 }
251
252 protected void refresh(){
253 if (!updated){
254 if (getTable() != null){
255 getTable().removeAll();
256 }
257 for(T item:results){
258 TableItem tableItem = new TableItem(getTable(), SWT.NONE);
259 tableItem.setText(getTextForTableItem(item));
260 tableItem.setData(item);
261 }
262 updated = true;
263 }
264 }
265
266 public abstract void query();
267
268 @Override
269 public void update(CdmDataChangeMap changeEvents) {
270 //nothing
271 }
272
273 /**
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
277 */
278 protected abstract String getTextForTableItem(T item);
279
280 @Focus
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();
286 }
287 }
288
289 @Override
290 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
291 }
292
293 @Override
294 public void contextStop(IMemento memento, IProgressMonitor monitor) {
295 }
296
297 @Override
298 public void contextStart(IMemento memento, IProgressMonitor monitor) {
299 initConversation();
300 }
301
302 private void initConversation(){
303 if(conversationHolder==null){
304 conversationHolder = CdmStore.createConversation();
305 }
306 }
307
308 @Override
309 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
310 if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
311 getConversationHolder().close();
312 }
313 }
314
315 /**
316 * @return the conversationHolder
317 */
318 @Override
319 public ConversationHolder getConversationHolder() {
320 if(CdmStore.isActive() && conversationHolder==null){
321 initConversation();
322 }
323 return conversationHolder;
324 }
325
326 @Override
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());
332 }
333 }
334 else if(event.widget==btnClear){
335 classification = null;
336 textClassification.setText("");
337 }
338 }
339
340 public String getReferenceString() {
341 return textReferenceString.getText();
342 }
343 }