Project

General

Profile

Download (9.46 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 OccurenceQuery query;
64

    
65
    private static ConversationHolder conversationHolder;
66

    
67
    private SaveImportedSpecimenAction saveImportedSpecimenAction;
68
    
69
    private Text textClassification;
70
    private Classification classification;
71
    /**
72
	 * @return the classification
73
	 */
74
	public Classification getClassification() {
75
		return classification;
76
	}
77

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

    
87
    private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
88

    
89
    private Table table;
90

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

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

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

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

    
128
        
129
        
130
        createActions();
131
        initializeToolBar();
132
        initializeMenu();
133
    }
134

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

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

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

    
157
    public Table getTable() {
158
        return table;
159
    }
160

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

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

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

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

    
192
    protected void refresh(){
193
       // getTable().removeAll();
194
        for(T item:results){
195
            TableItem tableItem = new TableItem(getTable(), SWT.NONE);
196
            tableItem.setText(getTextForTableItem(item));
197
            tableItem.setData(item);
198
        }
199
    }
200

    
201
    public abstract void query();
202

    
203

    
204
    /* (non-Javadoc)
205
     * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
206
     */
207
    @Override
208
    public void update(CdmDataChangeMap changeEvents) {
209
        //nothing
210
    }
211

    
212
    /**
213
     * Returns the text label of the given item.
214
     * @param item the item for which the label should be returns
215
     * @return the label of the item
216
     */
217
    protected abstract String getTextForTableItem(T item);
218

    
219
    /* (non-Javadoc)
220
     * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
221
     */
222
    @Override
223
    public void setFocus() {
224
        getTable().setFocus();
225
        //make sure to bind again if maybe in another view the conversation was unbound
226
        if(getConversationHolder()!=null && !getConversationHolder().isBound()){
227
            getConversationHolder().bind();
228
        }
229
    }
230

    
231
    /* (non-Javadoc)
232
     * @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
233
     */
234
    @Override
235
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
236
    }
237

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

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

    
256
    private void initConversation(){
257
        if(conversationHolder==null){
258
            conversationHolder = CdmStore.createConversation();
259
        }
260
    }
261

    
262
    /* (non-Javadoc)
263
     * @see eu.etaxonomy.taxeditor.model.IContextListener#workbenchShutdown(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
264
     */
265
    @Override
266
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
267
        if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
268
            getConversationHolder().close();
269
        }
270
    }
271

    
272
    /**
273
     * @return the conversationHolder
274
     */
275
    @Override
276
    public ConversationHolder getConversationHolder() {
277
        if(CdmStore.isActive() && conversationHolder==null){
278
            initConversation();
279
        }
280
        return conversationHolder;
281
    }
282
    @Override
283
	public void handleEvent(Event event) {
284
	    if(event.widget==btnBrowseClassification){
285
	        classification = SelectionDialogFactory.getSelectionFromDialog(Classification.class, getSite().getShell(), null, null);
286
	        if(classification!=null){
287
	            textClassification.setText(classification.getTitleCache());
288
	        }
289
	    }
290
	    else if(event.widget==btnClear){
291
	        classification = null;
292
	        textClassification.setText("");
293
	    }
294
	}
295
}
(5-5/16)