changes in specimen import view
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / dataimport / DataImportView.java
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.jface.wizard.IWizard;
20 import org.eclipse.jface.wizard.WizardDialog;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Event;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Listener;
32 import org.eclipse.swt.widgets.Table;
33 import org.eclipse.swt.widgets.TableItem;
34 import org.eclipse.swt.widgets.Text;
35 import org.eclipse.ui.IMemento;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.forms.widgets.FormToolkit;
38 import org.eclipse.ui.part.ViewPart;
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 Sep 3, 2014
58 *
59 * @param <T> the CDM type that will be handled by this view
60 */
61
62 public abstract class DataImportView<T> extends ViewPart implements IPartContentHasFactualData,
63 IConversationEnabled, IPartContentHasDetails, IPartContentHasSupplementalData, IContextListener, Listener{
64
65 protected final Logger logger = Logger.getLogger(DataImportView.class);
66
67 protected Collection<T> results = new ArrayList<T>();
68
69 protected boolean updated = false;
70
71 protected OccurenceQuery query;
72
73 private static ConversationHolder conversationHolder;
74
75 private SaveImportedSpecimenAction saveImportedSpecimenAction;
76
77 private Text textClassification;
78 private Classification classification;
79
80 private Text textReferenceString;
81
82 private Button toggleButton;
83 private boolean state;
84
85 private Button openConfigurator;
86 /**
87 * @return the classification
88 */
89 public Classification getClassification() {
90 return classification;
91 }
92
93 /**
94 * @param classification the classification to set
95 */
96 public void setClassification(Classification classification) {
97 this.classification = classification;
98 }
99 private Button btnBrowseClassification;
100 private Button btnClear;
101
102 private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
103
104 private Table table;
105
106 /**
107 * Constructs a new DataImportEditor and registers it to listen to context changes
108 */
109 public DataImportView() {
110 CdmStore.getContextManager().addContextListener(this);
111 if(CdmStore.isActive()){
112 initConversation();
113 }
114 }
115
116 /**
117 * Create contents of the view part.
118 * @param parent
119 */
120 @Override
121 public void createPartControl(Composite parent) {
122 // final Composite composite = new Composite(parent, SWT.NONE);
123 //
124 // GridLayout gridLayout = new GridLayout();
125 // gridLayout.numColumns = 3;
126 // composite.setLayout(gridLayout);
127 // composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
128 Composite composite = new Composite(parent, SWT.NONE);
129 composite.setLayout(new GridLayout(2, false));
130
131 Composite composite_1 = new Composite(composite, SWT.NONE);
132 GridData gd_composite_1 = new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1);
133 gd_composite_1.widthHint = 280;
134 composite_1.setLayoutData(gd_composite_1);
135 composite_1.setLayout(new GridLayout(3, false));
136
137 Label label = new Label(composite_1, SWT.TOP);
138 label.setText("Classification");
139 label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
140 textClassification = new Text(composite_1, SWT.BORDER);
141 textClassification.setEnabled(false);
142 GridData gd_textClassification = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
143 gd_textClassification.widthHint = 118;
144 textClassification.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, true));
145 btnBrowseClassification = new Button(composite_1, SWT.NONE);
146 btnBrowseClassification.setImage(ResourceManager.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/open.gif"));
147 btnBrowseClassification.addListener(SWT.Selection, this);
148 btnClear = new Button(composite_1, SWT.NONE);
149 btnClear.setImage(ResourceManager.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/trash.gif"));
150 btnClear.addListener(SWT.Selection, this);
151 //source reference
152
153 Label labelRef = new Label(composite_1, SWT.NONE);
154 labelRef.setText("Default import souce reference");
155 labelRef.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
156 textReferenceString = new Text(composite_1, SWT.NONE);
157 textReferenceString.setEnabled(true);
158 GridData gd_textReferenceString = new GridData(SWT.LEFT, SWT.CENTER, true, true, 3, 1);
159 gd_textReferenceString.widthHint = 229;
160 textReferenceString.setLayoutData(gd_textReferenceString);
161
162 //open configuration
163 openConfigurator = new Button(composite_1, SWT.PUSH);
164 openConfigurator.setText("Configuration");
165 new Label(composite_1, SWT.NONE);
166 new Label(composite_1, SWT.NONE);
167 openConfigurator.addSelectionListener(new SelectionAdapter(){
168 @Override
169 public void widgetSelected(SelectionEvent e) {
170
171 IWizard wizard = new ImportPreferencesWizard();
172 WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), wizard);
173 dialog.open();
174 }
175 });
176
177 //checkbox table for result
178 Composite composite_2 = new Composite(composite, SWT.NONE);
179 GridData gd_composite_2 = new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1);
180 gd_composite_2.heightHint = 454;
181 gd_composite_2.widthHint = 403;
182 composite_2.setLayoutData(gd_composite_2);
183 composite_2.setLayout(new GridLayout(2, false));
184
185
186 CheckboxTableViewer checkboxTableViewer = CheckboxTableViewer.newCheckList(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
187
188
189 table = checkboxTableViewer.getTable();
190 GridData gd_table = new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1);
191 gd_table.heightHint = 444;
192 gd_table.widthHint = 312;
193 table.setLayoutData(gd_table);
194 toolkit.paintBordersFor(table);
195 //toggle button
196 toggleButton = new Button(composite_2, SWT.PUSH);
197 GridData gd_toggleButton = new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1);
198 gd_toggleButton.widthHint = 56;
199 toggleButton.setLayoutData(gd_toggleButton);
200 toggleButton.setText("Toggle");
201 toggleButton.addSelectionListener(new SelectionAdapter(){
202 @Override
203 public void widgetSelected(SelectionEvent e) {
204 state = state ? false : true;
205 for (TableItem item: getTable().getItems()){
206 item.setChecked(state);
207 }
208
209 }
210 });
211 createActions();
212 initializeToolBar();
213 initializeMenu();
214 }
215
216 /**
217 * Create the actions.
218 */
219 private void createActions() {
220 saveImportedSpecimenAction = new SaveImportedSpecimenAction();
221 }
222
223 /**
224 * Initialize the toolbar.
225 */
226 private void initializeToolBar() {
227 IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
228 tbm.add(saveImportedSpecimenAction);
229 }
230
231 /**
232 * Initialize the menu.
233 */
234 private void initializeMenu() {
235 IMenuManager manager = getViewSite().getActionBars().getMenuManager();
236 }
237
238 public Table getTable() {
239 return table;
240 }
241
242 /**
243 * @param query the query to set
244 */
245 public void setQuery(OccurenceQuery query) {
246 this.query = query;
247 }
248
249 /**
250 * @param results the results to set
251 */
252 public void setResults(Collection<T> results) {
253 this.results = results;
254 updated=false;
255 }
256
257 /* (non-Javadoc)
258 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#dispose()
259 */
260 @Override
261 public void dispose() {
262 super.dispose();
263 CdmStore.getContextManager().removeContextListener(this);
264 }
265
266 /* (non-Javadoc)
267 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextRefresh(org.eclipse.core.runtime.IProgressMonitor)
268 */
269 @Override
270 public void contextRefresh(IProgressMonitor monitor) {
271 refresh();
272 }
273
274 protected void refresh(){
275
276 if (!updated){
277 if (getTable() != null){
278 getTable().removeAll();
279 }
280 for(T item:results){
281 TableItem tableItem = new TableItem(getTable(), SWT.NONE);
282 tableItem.setText(getTextForTableItem(item));
283 tableItem.setData(item);
284 }
285 updated = true;
286 }
287 }
288
289 public abstract void query();
290
291
292 /* (non-Javadoc)
293 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
294 */
295 @Override
296 public void update(CdmDataChangeMap changeEvents) {
297 //nothing
298 }
299
300 /**
301 * Returns the text label of the given item.
302 * @param item the item for which the label should be returns
303 * @return the label of the item
304 */
305 protected abstract String getTextForTableItem(T item);
306
307 /* (non-Javadoc)
308 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
309 */
310 @Override
311 public void setFocus() {
312 getTable().setFocus();
313 //make sure to bind again if maybe in another view the conversation was unbound
314 if(getConversationHolder()!=null && !getConversationHolder().isBound()){
315 getConversationHolder().bind();
316 }
317 }
318
319 /* (non-Javadoc)
320 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
321 */
322 @Override
323 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
324 }
325
326 /* (non-Javadoc)
327 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
328 */
329 @Override
330 public void contextStop(IMemento memento, IProgressMonitor monitor) {
331 // derivateSearchCompositeController.setEnabled(false);
332 // derivateSearchCompositeController.reset();
333 }
334
335 /* (non-Javadoc)
336 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
337 */
338 @Override
339 public void contextStart(IMemento memento, IProgressMonitor monitor) {
340 initConversation();
341 // derivateSearchCompositeController.setEnabled(true);
342 }
343
344 private void initConversation(){
345 if(conversationHolder==null){
346 conversationHolder = CdmStore.createConversation();
347 }
348 }
349
350 /* (non-Javadoc)
351 * @see eu.etaxonomy.taxeditor.model.IContextListener#workbenchShutdown(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
352 */
353 @Override
354 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
355 if(getConversationHolder()!=null && getConversationHolder().isBound() && !getConversationHolder().isClosed()) {
356 getConversationHolder().close();
357 }
358 }
359
360 /**
361 * @return the conversationHolder
362 */
363 @Override
364 public ConversationHolder getConversationHolder() {
365 if(CdmStore.isActive() && conversationHolder==null){
366 initConversation();
367 }
368 return conversationHolder;
369 }
370 @Override
371 public void handleEvent(Event event) {
372 if(event.widget==btnBrowseClassification){
373 classification = SelectionDialogFactory.getSelectionFromDialog(Classification.class, getSite().getShell(), null, null);
374 if(classification!=null){
375 textClassification.setText(classification.getTitleCache());
376 }
377 }
378 else if(event.widget==btnClear){
379 classification = null;
380 textClassification.setText("");
381 }
382 }
383
384 /**
385 * @return
386 */
387 public String getReferenceString() {
388
389 return textReferenceString.getText();
390 }
391 }