Project

General

Profile

Download (9.47 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2013 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.taxeditor.view.dataimport;
11

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

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

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

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

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

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

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

    
64
    protected OccurenceQuery query;
65

    
66
    private static ConversationHolder conversationHolder;
67

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

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

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

    
90
    private Table table;
91

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
202
    public abstract void query();
203

    
204

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

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

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

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

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

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

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

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

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