Project

General

Profile

Download (9.64 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;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13

    
14
import org.apache.log4j.Logger;
15
import org.eclipse.core.runtime.IProgressMonitor;
16
import org.eclipse.jface.action.IMenuManager;
17
import org.eclipse.jface.action.IToolBarManager;
18
import org.eclipse.jface.viewers.CheckboxTableViewer;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.layout.GridData;
21
import org.eclipse.swt.layout.GridLayout;
22
import org.eclipse.swt.widgets.Button;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Display;
25
import org.eclipse.swt.widgets.Event;
26
import org.eclipse.swt.widgets.Label;
27
import org.eclipse.swt.widgets.Listener;
28
import org.eclipse.swt.widgets.Table;
29
import org.eclipse.swt.widgets.TableItem;
30
import org.eclipse.swt.widgets.Text;
31
import org.eclipse.ui.IMemento;
32
import org.eclipse.ui.forms.widgets.FormToolkit;
33
import org.eclipse.ui.part.ViewPart;
34
import org.eclipse.wb.swt.ResourceManager;
35

    
36
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
37
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
38
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
39
import eu.etaxonomy.cdm.model.taxon.Classification;
40
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
41
import eu.etaxonomy.taxeditor.model.IContextListener;
42
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
43
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
44
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
45
import eu.etaxonomy.taxeditor.store.CdmStore;
46
import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
47

    
48
/**
49
 * View which shows a list of "data" that can be imported into the CDM
50
 * @author pplitzner
51
 * @date Sep 3, 2014
52
 *
53
 * @param <T> the CDM type that will be handled by this view
54
 */
55

    
56
public abstract class DataImportView<T> extends ViewPart implements IPartContentHasFactualData,
57
IConversationEnabled, IPartContentHasDetails, IPartContentHasSupplementalData, IContextListener, Listener{
58

    
59
    protected final Logger logger = Logger.getLogger(DataImportView.class);
60

    
61
    protected Collection<T> results = new ArrayList<T>();
62

    
63
    protected boolean updated = false;
64

    
65
    protected OccurenceQuery query;
66

    
67
    private static ConversationHolder conversationHolder;
68

    
69
    private SaveImportedSpecimenAction saveImportedSpecimenAction;
70

    
71
    private Text textClassification;
72
    private Classification classification;
73
    /**
74
	 * @return the classification
75
	 */
76
	public Classification getClassification() {
77
		return classification;
78
	}
79

    
80
	/**
81
	 * @param classification the classification to set
82
	 */
83
	public void setClassification(Classification classification) {
84
		this.classification = classification;
85
	}
86
	private Button btnBrowseClassification;
87
    private Button btnClear;
88

    
89
    private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
90

    
91
    private Table table;
92

    
93
    /**
94
     * Constructs a new DataImportEditor and registers it to listen to context changes
95
     */
96
    public DataImportView() {
97
        CdmStore.getContextManager().addContextListener(this);
98
        if(CdmStore.isActive()){
99
            initConversation();
100
        }
101
    }
102

    
103
    /**
104
     * Create contents of the view part.
105
     * @param parent
106
     */
107
    @Override
108
    public void createPartControl(Composite parent) {
109
    	final Composite composite = new Composite(parent, SWT.NULL);
110

    
111
		GridLayout gridLayout = new GridLayout();
112
		gridLayout.numColumns = 4;
113
		composite.setLayout(gridLayout);
114
    	Label label = new Label(composite, SWT.NONE);
115
		label.setText("Classification");
116
		textClassification = new Text(composite, SWT.NONE);
117
		textClassification.setEnabled(false);
118
		textClassification.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
119
		btnBrowseClassification = new Button(composite, SWT.NONE);
120
		btnBrowseClassification.setImage(ResourceManager.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/open.gif"));
121
		btnBrowseClassification.addListener(SWT.Selection, this);
122
		btnClear = new Button(composite, SWT.NONE);
123
		btnClear.setImage(ResourceManager.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/trash.gif"));
124
		btnClear.addListener(SWT.Selection, this);
125

    
126
        CheckboxTableViewer checkboxTableViewer = CheckboxTableViewer.newCheckList(parent, SWT.BORDER | SWT.FULL_SELECTION);
127
        table = checkboxTableViewer.getTable();
128
        toolkit.paintBordersFor(table);
129

    
130

    
131

    
132
        createActions();
133
        initializeToolBar();
134
        initializeMenu();
135
    }
136

    
137
    /**
138
     * Create the actions.
139
     */
140
    private void createActions() {
141
        saveImportedSpecimenAction = new SaveImportedSpecimenAction();
142
    }
143

    
144
    /**
145
     * Initialize the toolbar.
146
     */
147
    private void initializeToolBar() {
148
        IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
149
        tbm.add(saveImportedSpecimenAction);
150
    }
151

    
152
    /**
153
     * Initialize the menu.
154
     */
155
    private void initializeMenu() {
156
        IMenuManager manager = getViewSite().getActionBars().getMenuManager();
157
    }
158

    
159
    public Table getTable() {
160
        return table;
161
    }
162

    
163
    /**
164
     * @param query the query to set
165
     */
166
    public void setQuery(OccurenceQuery query) {
167
        this.query = query;
168
    }
169

    
170
    /**
171
     * @param results the results to set
172
     */
173
    public void setResults(Collection<T> results) {
174
        this.results = results;
175
        updated=false;
176
    }
177

    
178
    /* (non-Javadoc)
179
     * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#dispose()
180
     */
181
    @Override
182
    public void dispose() {
183
        super.dispose();
184
        CdmStore.getContextManager().removeContextListener(this);
185
    }
186

    
187
    /* (non-Javadoc)
188
     * @see eu.etaxonomy.taxeditor.model.IContextListener#contextRefresh(org.eclipse.core.runtime.IProgressMonitor)
189
     */
190
    @Override
191
    public void contextRefresh(IProgressMonitor monitor) {
192
        refresh();
193
    }
194

    
195
    protected void refresh(){
196

    
197
        if (!updated){
198
            if (getTable() != null){
199
                getTable().removeAll();
200
            }
201
            for(T item:results){
202
                TableItem tableItem = new TableItem(getTable(), SWT.NONE);
203
                tableItem.setText(getTextForTableItem(item));
204
                tableItem.setData(item);
205
            }
206
            updated = true;
207
        }
208
    }
209

    
210
    public abstract void query();
211

    
212

    
213
    /* (non-Javadoc)
214
     * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
215
     */
216
    @Override
217
    public void update(CdmDataChangeMap changeEvents) {
218
        //nothing
219
    }
220

    
221
    /**
222
     * Returns the text label of the given item.
223
     * @param item the item for which the label should be returns
224
     * @return the label of the item
225
     */
226
    protected abstract String getTextForTableItem(T item);
227

    
228
    /* (non-Javadoc)
229
     * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
230
     */
231
    @Override
232
    public void setFocus() {
233
        getTable().setFocus();
234
        //make sure to bind again if maybe in another view the conversation was unbound
235
        if(getConversationHolder()!=null && !getConversationHolder().isBound()){
236
            getConversationHolder().bind();
237
        }
238
    }
239

    
240
    /* (non-Javadoc)
241
     * @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
242
     */
243
    @Override
244
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
245
    }
246

    
247
    /* (non-Javadoc)
248
     * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
249
     */
250
    @Override
251
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
252
//        derivateSearchCompositeController.setEnabled(false);
253
//        derivateSearchCompositeController.reset();
254
    }
255

    
256
    /* (non-Javadoc)
257
     * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
258
     */
259
    @Override
260
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
261
        initConversation();
262
//        derivateSearchCompositeController.setEnabled(true);
263
    }
264

    
265
    private void initConversation(){
266
        if(conversationHolder==null){
267
            conversationHolder = CdmStore.createConversation();
268
        }
269
    }
270

    
271
    /* (non-Javadoc)
272
     * @see eu.etaxonomy.taxeditor.model.IContextListener#workbenchShutdown(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
273
     */
274
    @Override
275
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
276
        if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
277
            getConversationHolder().close();
278
        }
279
    }
280

    
281
    /**
282
     * @return the conversationHolder
283
     */
284
    @Override
285
    public ConversationHolder getConversationHolder() {
286
        if(CdmStore.isActive() && conversationHolder==null){
287
            initConversation();
288
        }
289
        return conversationHolder;
290
    }
291
    @Override
292
	public void handleEvent(Event event) {
293
	    if(event.widget==btnBrowseClassification){
294
	        classification = SelectionDialogFactory.getSelectionFromDialog(Classification.class, getSite().getShell(), null, null);
295
	        if(classification!=null){
296
	            textClassification.setText(classification.getTitleCache());
297
	        }
298
	    }
299
	    else if(event.widget==btnClear){
300
	        classification = null;
301
	        textClassification.setText("");
302
	    }
303
	}
304
}
(5-5/16)