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