Project

General

Profile

Download (13 KB) Statistics
| Branch: | Tag: | Revision:
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
import eu.etaxonomy.taxeditor.workbench.part.IEditorAreaPart;
56

    
57
/**
58
 * View which shows a list of "data" that can be imported into the CDM
59
 * @author pplitzner
60
 * @date 22.08.2017
61
 *
62
 * @param <T> the CDM type that will be handled by this view
63
 */
64
public abstract class DataImportViewE4<T> implements IPartContentHasFactualData,
65
IConversationEnabled, IPartContentHasDetails, IPartContentHasSupplementalData, IContextListener, Listener, IEditorAreaPart{
66

    
67
    protected final Logger logger = Logger.getLogger(DataImportViewE4.class);
68

    
69
    protected Collection<T> results = new ArrayList<T>();
70

    
71
    protected boolean updated = false;
72

    
73
    protected OccurenceQuery query;
74

    
75
    private static ConversationHolder conversationHolder;
76

    
77
    private SaveImportedSpecimenActionE4 saveImportedSpecimenAction;
78

    
79
    private Text textClassification;
80

    
81
    private Classification classification;
82

    
83
    protected Text textReferenceString;
84

    
85
    private Button selectAllButton;
86
    //private Button selectNoneButton;
87

    
88
    private Button importButton;
89

    
90
    private boolean state;
91

    
92
    private Button openConfigurator;
93

    
94
    private Abcd206ImportConfigurator config;
95

    
96
    public Classification getClassification() {
97
		return classification;
98
	}
99

    
100
	public void setClassification(Classification classification) {
101
		this.classification = classification;
102
	}
103

    
104
	private Button btnBrowseClassification;
105

    
106
	private Button btnClear;
107

    
108
    private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
109

    
110
    private Table table;
111
    private Composite composite_3;
112

    
113
    /**
114
     * Constructs a new DataImportEditor and registers it to listen to context changes
115
     */
116
    public DataImportViewE4() {
117
        CdmStore.getContextManager().addContextListener(this);
118
    }
119

    
120
    /**
121
     * Create contents of the view part.
122
     * @param parent
123
     */
124
    @PostConstruct
125
    public void createPartControl(Composite parent) {
126
        if(CdmStore.isActive()){
127
            initConversation();
128
        }
129
        else{
130
            return;
131
        }
132
        parent.setLayout(new GridLayout(1, false));
133
        Composite composite = new Composite(parent, SWT.NONE);
134
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
135
//        composite.setBounds(0, 0, 0, 298);
136
        composite.setLayout(new GridLayout(2, false));
137

    
138
        Composite composite_1 = new Composite(composite, SWT.NONE);
139
//        gd_composite_1.widthHint = 280;
140
        composite_1.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
141
        composite_1.setLayout(new GridLayout(3, false));
142

    
143
        Label label = new Label(composite_1, SWT.TOP);
144
        label.setText("Classification");
145
        label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
146
        textClassification = new Text(composite_1, SWT.BORDER);
147
        textClassification.setEnabled(false);
148
        GridData gd_textClassification = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
149
//        gd_textClassification.widthHint = 118;
150
        textClassification.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, true));
151
        btnBrowseClassification = new Button(composite_1, SWT.NONE);
152
        btnBrowseClassification.setImage(ResourceManager.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/open.gif"));
153
        btnBrowseClassification.addListener(SWT.Selection, this);
154
        btnClear = new Button(composite_1, SWT.NONE);
155
        btnClear.setImage(ResourceManager.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/trash.gif"));
156
        btnClear.addListener(SWT.Selection, this);
157
        //source reference
158

    
159
        Label labelRef = new Label(composite_1, SWT.NONE);
160
        labelRef.setText("Import souce reference");
161
        labelRef.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
162
        textReferenceString = new Text(composite_1, SWT.NONE);
163
        textReferenceString.setEnabled(true);
164
        GridData gd_textReferenceString = new GridData(SWT.LEFT, SWT.CENTER, true, true, 3, 1);
165
        gd_textReferenceString.widthHint = 229;
166
        textReferenceString.setLayoutData(gd_textReferenceString);
167

    
168
        //open configuration
169

    
170
        PreferencesUtil.getLocalAbcdImportConfigurator(false);
171

    
172
        openConfigurator = new Button(composite_1, SWT.PUSH);
173
        openConfigurator.setText("Configuration");
174
        new Label(composite_1, SWT.NONE);
175
        new Label(composite_1, SWT.NONE);
176

    
177
        openConfigurator.addSelectionListener(new SelectionAdapter(){
178
            @Override
179
            public void widgetSelected(SelectionEvent e) {
180

    
181
                IWizard wizard = new ImportPreferencesWizard();
182
                WizardDialog dialog = new WizardDialog(e.display.getActiveShell(), wizard);
183
                dialog.open();
184

    
185
            }
186
        });
187

    
188
        //checkbox table for result
189
        Composite composite_2 = new Composite(composite, SWT.NONE);
190
        composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
191
        composite_2.setLayout(new GridLayout(2, false));
192

    
193

    
194
        CheckboxTableViewer checkboxTableViewer = CheckboxTableViewer.newCheckList(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
195

    
196

    
197
        table = checkboxTableViewer.getTable();
198
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
199
        table.addListener(SWT.Selection, this);
200
        toolkit.paintBordersFor(table);
201

    
202
        composite_3 = new Composite(composite_2, SWT.NONE);
203
//        gd_composite_3.heightHint = 48;
204
        composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
205
        toolkit.paintBordersFor(composite_3);
206
        composite_3.setLayout(new GridLayout(1, false));
207
        //toggle button
208
        selectAllButton = new Button(composite_3, SWT.PUSH);
209
        selectAllButton.setText("Select all/none");
210
        new Label(composite_2, SWT.NONE);
211

    
212
//        Composite composite_4 = new Composite(composite_2, SWT.NONE);
213
//        composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
214
//        composite_4.setBackground(composite.getBackground());
215
//        toolkit.adapt(composite_4);
216
//        toolkit.paintBordersFor(composite_4);
217

    
218
        importButton = new Button(composite_2, SWT.PUSH);
219
        importButton.setSize(48, 25);
220
        importButton.setText("Import");
221
        importButton.addSelectionListener(new SelectionAdapter(){
222
            @Override
223
            public void widgetSelected(SelectionEvent e) {
224
                saveImportedSpecimenAction.run();
225
            }
226
        });
227
//        new Label(composite_2, SWT.NONE);
228
//        new Label(composite_2, SWT.NONE);
229
//        new Label(composite_2, SWT.NONE);
230
//        new Label(composite_2, SWT.NONE);
231
//        new Label(composite_2, SWT.NONE);
232
//        new Label(composite_2, SWT.NONE);
233
        selectAllButton.addSelectionListener(new SelectionAdapter(){
234
            @Override
235
            public void widgetSelected(SelectionEvent e) {
236
                state = state ? false : true;
237
                for (TableItem item: getTable().getItems()){
238
                    item.setChecked(state);
239
                }
240

    
241
            }
242
        });
243
        //import button
244

    
245
        saveImportedSpecimenAction = new SaveImportedSpecimenActionE4(this);
246
        initializeToolBar();
247
    }
248

    
249
    /**
250
     * Initialize the toolbar.
251
     */
252
    private void initializeToolBar() {
253
        //FIXME E4 migrate
254
//        IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
255
//        tbm.add(saveImportedSpecimenAction);
256
    }
257

    
258
    public Table getTable() {
259
        return table;
260
    }
261

    
262
    public void setQuery(OccurenceQuery query) {
263
        this.query = query;
264
    }
265

    
266
    public void setResults(Collection<T> results) {
267
        this.results = results;
268
        updated=false;
269
    }
270

    
271
    @PreDestroy
272
    public void dispose() {
273
        CdmStore.getContextManager().removeContextListener(this);
274
    }
275

    
276
    @Override
277
    public void contextRefresh(IProgressMonitor monitor) {
278
        refresh();
279
    }
280

    
281
    protected void refresh(){
282
        if (!updated){
283
            if (getTable() != null){
284
                getTable().removeAll();
285
            }
286
            if(results!=null){
287
                for(T item:results){
288
                    TableItem tableItem = new TableItem(getTable(), SWT.NONE);
289
                    tableItem.setText(getTextForTableItem(item));
290
                    tableItem.setData(item);
291
                }
292
            }
293
            updated = true;
294
        }
295
    }
296

    
297
    public abstract void query();
298

    
299
    @Override
300
    public void update(CdmDataChangeMap changeEvents) {
301
        //nothing
302
    }
303

    
304
    /**
305
     * Returns the text label of the given item.
306
     * @param item the item for which the label should be returns
307
     * @return the label of the item
308
     */
309
    protected abstract String getTextForTableItem(T item);
310

    
311
    @Focus
312
    public void setFocus() {
313
        if(getTable()!=null){
314
            getTable().setFocus();
315
        }
316
        //make sure to bind again if maybe in another view the conversation was unbound
317
        if(getConversationHolder()!=null && !getConversationHolder().isBound()){
318
            getConversationHolder().bind();
319
        }
320
    }
321

    
322
    @Override
323
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
324
    }
325

    
326
    @Override
327
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
328
    }
329

    
330
    @Override
331
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
332
        initConversation();
333
    }
334

    
335
    private void initConversation(){
336
        if(conversationHolder==null){
337
            conversationHolder = CdmStore.createConversation();
338
        }
339
    }
340

    
341
    @Override
342
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
343
        if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
344
            getConversationHolder().close();
345
        }
346
    }
347

    
348
    /**
349
     * @return the conversationHolder
350
     */
351
    @Override
352
    public ConversationHolder getConversationHolder() {
353
        if(CdmStore.isActive() && conversationHolder==null){
354
            initConversation();
355
        }
356
        return conversationHolder;
357
    }
358

    
359
    @Override
360
	public void handleEvent(Event event) {
361
	    if(event.widget==btnBrowseClassification){
362
	        classification = SelectionDialogFactory.getSelectionFromDialog(Classification.class, event.widget.getDisplay().getActiveShell(), null, null);
363
	        if(classification!=null){
364
	            textClassification.setText(classification.getTitleCache());
365
	        }
366
	    }
367
	    else if(event.widget==btnClear){
368
	        classification = null;
369
	        textClassification.setText("");
370
	    }
371

    
372
	    else {
373
	        if(event.widget == table){
374
	            TableItem[] item = table.getSelection();
375
	            if (item.length == 1){
376
	                Object o = item[0].getData();
377
	                System.err.println(o.toString());
378
	            }
379
	        }
380
	    }
381
	}
382

    
383
    public String getReferenceString() {
384
        return textReferenceString.getText();
385
    }
386
}
(1-1/7)