Project

General

Profile

Download (11.8 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.model.taxon.Classification;
45
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
46
import eu.etaxonomy.taxeditor.databaseAdmin.wizard.ImportPreferencesWizard;
47
import eu.etaxonomy.taxeditor.model.IContextListener;
48
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
49
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
50
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
51
import eu.etaxonomy.taxeditor.store.CdmStore;
52
import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
53

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

    
64
    protected final Logger logger = Logger.getLogger(DataImportViewE4.class);
65

    
66
    protected Collection<T> results = new ArrayList<T>();
67

    
68
    protected boolean updated = false;
69

    
70
    protected OccurenceQuery query;
71

    
72
    private static ConversationHolder conversationHolder;
73

    
74
    private SaveImportedSpecimenActionE4 saveImportedSpecimenAction;
75

    
76
    private Text textClassification;
77

    
78
    private Classification classification;
79

    
80
    private Text textReferenceString;
81

    
82
    private Button toggleButton;
83

    
84
    private Button importButton;
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
    private Composite composite_3;
106

    
107
    /**
108
     * Constructs a new DataImportEditor and registers it to listen to context changes
109
     */
110
    public DataImportViewE4() {
111
        CdmStore.getContextManager().addContextListener(this);
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(e.display.getActiveShell(), 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

    
194
        composite_3 = new Composite(composite_2, SWT.NONE);
195
        composite_3.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
196
        toolkit.paintBordersFor(composite_3);
197
        composite_3.setLayout(new GridLayout(1, false));
198
        //toggle button
199
        toggleButton = new Button(composite_3, SWT.PUSH);
200
        toggleButton.setText("Toggle");
201

    
202
                importButton = new Button(composite_3, SWT.PUSH);
203
                importButton.setText("Import");
204
                importButton.addSelectionListener(new SelectionAdapter(){
205
                    @Override
206
                    public void widgetSelected(SelectionEvent e) {
207
                        saveImportedSpecimenAction.run();
208
                    }
209
                });
210
        toggleButton.addSelectionListener(new SelectionAdapter(){
211
            @Override
212
            public void widgetSelected(SelectionEvent e) {
213
                state = state ? false : true;
214
                for (TableItem item: getTable().getItems()){
215
                    item.setChecked(state);
216
                }
217

    
218
            }
219
        });
220
        //import button
221
        saveImportedSpecimenAction = new SaveImportedSpecimenActionE4(this);
222
        initializeToolBar();
223
    }
224

    
225
    /**
226
     * Initialize the toolbar.
227
     */
228
    private void initializeToolBar() {
229
        //FIXME E4 migrate
230
//        IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
231
//        tbm.add(saveImportedSpecimenAction);
232
    }
233

    
234
    public Table getTable() {
235
        return table;
236
    }
237

    
238
    public void setQuery(OccurenceQuery query) {
239
        this.query = query;
240
    }
241

    
242
    public void setResults(Collection<T> results) {
243
        this.results = results;
244
        updated=false;
245
    }
246

    
247
    @PreDestroy
248
    public void dispose() {
249
        CdmStore.getContextManager().removeContextListener(this);
250
    }
251

    
252
    @Override
253
    public void contextRefresh(IProgressMonitor monitor) {
254
        refresh();
255
    }
256

    
257
    protected void refresh(){
258
        if (!updated){
259
            if (getTable() != null){
260
                getTable().removeAll();
261
            }
262
            if(results!=null){
263
                for(T item:results){
264
                    TableItem tableItem = new TableItem(getTable(), SWT.NONE);
265
                    tableItem.setText(getTextForTableItem(item));
266
                    tableItem.setData(item);
267
                }
268
            }
269
            updated = true;
270
        }
271
    }
272

    
273
    public abstract void query();
274

    
275
    @Override
276
    public void update(CdmDataChangeMap changeEvents) {
277
        //nothing
278
    }
279

    
280
    /**
281
     * Returns the text label of the given item.
282
     * @param item the item for which the label should be returns
283
     * @return the label of the item
284
     */
285
    protected abstract String getTextForTableItem(T item);
286

    
287
    @Focus
288
    public void setFocus() {
289
        if(getTable()!=null){
290
            getTable().setFocus();
291
        }
292
        //make sure to bind again if maybe in another view the conversation was unbound
293
        if(getConversationHolder()!=null && !getConversationHolder().isBound()){
294
            getConversationHolder().bind();
295
        }
296
    }
297

    
298
    @Override
299
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
300
    }
301

    
302
    @Override
303
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
304
    }
305

    
306
    @Override
307
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
308
        initConversation();
309
    }
310

    
311
    private void initConversation(){
312
        if(conversationHolder==null){
313
            conversationHolder = CdmStore.createConversation();
314
        }
315
    }
316

    
317
    @Override
318
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
319
        if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
320
            getConversationHolder().close();
321
        }
322
    }
323

    
324
    /**
325
     * @return the conversationHolder
326
     */
327
    @Override
328
    public ConversationHolder getConversationHolder() {
329
        if(CdmStore.isActive() && conversationHolder==null){
330
            initConversation();
331
        }
332
        return conversationHolder;
333
    }
334

    
335
    @Override
336
	public void handleEvent(Event event) {
337
	    if(event.widget==btnBrowseClassification){
338
	        classification = SelectionDialogFactory.getSelectionFromDialog(Classification.class, event.widget.getDisplay().getActiveShell(), null, null);
339
	        if(classification!=null){
340
	            textClassification.setText(classification.getTitleCache());
341
	        }
342
	    }
343
	    else if(event.widget==btnClear){
344
	        classification = null;
345
	        textClassification.setText("");
346
	    }
347
	}
348

    
349
    public String getReferenceString() {
350
        return textReferenceString.getText();
351
    }
352
}
(1-1/7)