ref #6933: if taxonnode filter is selected classification combo not visible
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / wizard / ExportToFileDestinationWizardPage.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The content 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
10 package eu.etaxonomy.taxeditor.io.wizard;
11
12 import java.awt.Checkbox;
13 import java.text.SimpleDateFormat;
14 import java.util.Calendar;
15 import java.util.Collections;
16 import java.util.Comparator;
17 import java.util.List;
18 import java.util.UUID;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.eclipse.jface.wizard.WizardPage;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.events.SelectionListener;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Combo;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.DirectoryDialog;
33 import org.eclipse.swt.widgets.Event;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Listener;
36 import org.eclipse.swt.widgets.Text;
37
38 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
39 import eu.etaxonomy.cdm.api.service.IClassificationService;
40 import eu.etaxonomy.cdm.io.cdmLight.CdmLightExportConfigurator;
41 import eu.etaxonomy.cdm.io.common.ExportConfiguratorBase;
42 import eu.etaxonomy.cdm.io.csv.caryophyllales.out.CsvNameExportConfigurator;
43 import eu.etaxonomy.cdm.io.csv.redlist.demo.CsvDemoExportConfigurator;
44 import eu.etaxonomy.cdm.io.csv.redlist.out.CsvTaxExportConfiguratorRedlist;
45 import eu.etaxonomy.cdm.io.dwca.out.DwcaTaxExportConfigurator;
46 import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
47 import eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator;
48 import eu.etaxonomy.cdm.io.tcsxml.out.TcsXmlExportConfigurator;
49 import eu.etaxonomy.cdm.model.taxon.Classification;
50 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
51 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
52 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
53 import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
54 import eu.etaxonomy.taxeditor.store.CdmStore;
55
56 /**
57 * <p>
58 * ExportToFileDestinationWizardPage class.
59 * </p>
60 *
61 * @author n.hoffmann
62 * @created 15.06.2009
63 * @version 1.0
64 */
65 public class ExportToFileDestinationWizardPage extends WizardPage implements SelectionListener {
66
67 /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
68 public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
69
70 /** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
71 public static final String JAXB_EXPORT = "JAXB_EXPORT";
72
73 /** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
74 public static final String TCS_EXPORT = "TCS_EXPORT";
75
76 /** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
77 public static final String SDD_EXPORT = "SDD_EXPORT";
78
79 public static final String DWCA_EXPORT = "DWCA_EXPORT";
80 public static final String OUTPUT_MODEL_EXPORT = "CDM_LIGHT(CSV)";
81 public static final String CSV_EXPORT = "CSV_EXPORT";
82 public static final String CSV_NAME_EXPORT = "CSV_NAME_EXPORT";
83 private static final String CSV_PRINT_EXPORT ="CSV_PRINT_EXPORT";
84 private static final String EXPORT_FOLDER ="EXPORT_FOLDER";
85 public static final String XML = "xml";
86
87 public static final String CSV = "csv";
88
89
90
91 public static final String ZIP = "zip";
92
93 private boolean csvExport = false;
94
95 private boolean csvNameExport = false;
96
97
98
99 private boolean csvPrintExport = false;
100
101 private boolean outputModelExport = false;
102 private boolean dwcaExport = false;
103
104 private DirectoryDialog folderDialog;
105 private Text text_exportFileName;
106
107 private Text text_folder;
108
109 private final String type;
110
111 private final String extension;
112
113 private Combo classificationSelectionCombo;
114
115 private List<Classification> classifications;
116
117 private Classification selectedClassification;
118 private Label classificationLabel;
119
120 private ExportConfiguratorBase configurator;
121 private Button checkUseSelectedtaxonNode;
122
123 private Button checkUseSelectedClassification;
124
125 /**
126 * @param pageName
127 * @param selection
128 */
129 protected ExportToFileDestinationWizardPage(String pageName, String type,
130 String title, String description, String extension, ExportConfiguratorBase configurator) {
131 super(pageName);
132 this.configurator = configurator;
133 this.type = type;
134 switch(type) {
135 case CSV_EXPORT :
136 csvExport = true;
137 break;
138 case CSV_NAME_EXPORT:
139 csvNameExport = true;
140 break;
141 case OUTPUT_MODEL_EXPORT:
142 outputModelExport = true;
143 break;
144 case CSV_PRINT_EXPORT:
145 csvPrintExport = true;
146 break;
147 case DWCA_EXPORT:
148 dwcaExport = true;
149 break;
150
151 }
152 this.extension = extension;
153 this.setTitle(title);
154 this.setDescription(description);
155 }
156
157 /**
158 * <p>
159 * Jaxb
160 * </p>
161 *
162 * @return a
163 * {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
164 * object.
165 */
166 public static ExportToFileDestinationWizardPage Jaxb(JaxbExportConfigurator configurator) {
167 return new ExportToFileDestinationWizardPage(
168 JAXB_EXPORT,
169 "jaxb",
170 "JAXB Export",
171 "Exports the content of the currently selected database into the cdm jaxb format.",
172 XML, configurator);
173 }
174
175 /**
176 * <p>
177 * Tcs
178 * </p>
179 *
180 * @return a
181 * {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
182 * object.
183 */
184 public static ExportToFileDestinationWizardPage Tcs(TcsXmlExportConfigurator config) {
185 return new ExportToFileDestinationWizardPage(
186 TCS_EXPORT,
187 "tcs",
188 "Tcs Export",
189 "Export the content of the currently selected database into TCS format.",
190 XML, config);
191 }
192
193 /**
194 * <p>
195 * Sdd
196 * </p>
197 *
198 * @return a
199 * {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
200 * object.
201 */
202 public static ExportToFileDestinationWizardPage Sdd(SDDExportConfigurator config) {
203 return new ExportToFileDestinationWizardPage(
204 SDD_EXPORT,
205 "sdd",
206 "Sdd Export",
207 "Export the content of the currently selected database into SDD format.",
208 XML, config);
209 }
210
211 /**
212 * @return
213 */
214 public static ExportToFileDestinationWizardPage Dwca(DwcaTaxExportConfigurator config) {
215 return new ExportToFileDestinationWizardPage(
216 DWCA_EXPORT,
217 DWCA_EXPORT,
218 "DwC-Archive Export",
219 "Export the content of the currently selected database into Darwin Core Archive format.",
220 ZIP, config);
221 }
222
223 /**
224 * @return
225 */
226 public static ExportToFileDestinationWizardPage OutputModel(CdmLightExportConfigurator config) {
227
228 return new ExportToFileDestinationWizardPage(
229 OUTPUT_MODEL_EXPORT,
230 OUTPUT_MODEL_EXPORT,
231 "Cdm Light Export (csv)",
232 "Export the content of the currently selected database into the cdm light (csv) format.",
233 CSV,config);
234 }
235
236
237 /**
238 * @return
239 */
240 public static ExportToFileDestinationWizardPage Csv(CsvDemoExportConfigurator config) {
241
242 return new ExportToFileDestinationWizardPage(
243 CSV_EXPORT,
244 CSV_EXPORT,
245 "CSV Export",
246 "Export the content of the currently selected database into Comma Separated Value format.",
247 CSV, config);
248 }
249
250 /**
251 * @return
252 */
253 public static ExportToFileDestinationWizardPage CsvNames(CsvNameExportConfigurator config) {
254
255 return new ExportToFileDestinationWizardPage(
256 CSV_NAME_EXPORT,
257 CSV_NAME_EXPORT,
258 "CSV Name Export",
259 "Export the names of the currently selected database into Semicolon Separated Value format.",
260 CSV, config);
261 }
262
263 /**
264 * @return
265 */
266 public static ExportToFileDestinationWizardPage CsvPrint(CsvNameExportConfigurator config) {
267
268 return new ExportToFileDestinationWizardPage(
269 CSV_PRINT_EXPORT,
270 CSV_NAME_EXPORT,
271 "CSV Print Export",
272 "Export the content of the currently selected database into Semicolon Separated Value format.",
273 CSV, config);
274 }
275
276 /*
277 * (non-Javadoc)
278 *
279 * @see
280 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
281 * .Composite)
282 */
283 /** {@inheritDoc} */
284 @Override
285 public void createControl(Composite parent) {
286
287 setPageComplete(false);
288
289 Composite composite = new Composite(parent, SWT.NONE);
290 GridLayout gridLayout = new GridLayout();
291
292 gridLayout.numColumns = 2;
293
294
295 composite.setLayout(gridLayout);
296 if(classifications == null){
297 classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
298 Collections.sort(classifications, new Comparator<Classification>() {
299
300 @Override
301 public int compare(Classification o1, Classification o2) {
302 if (o1.equals(o2)){
303 return 0;
304 }
305 int result = o1.getTitleCache().compareTo(o2.getTitleCache());
306 if (result == 0){
307 return o1.getUuid().compareTo(o2.getUuid());
308 }
309 return result;
310 }
311 });
312 if (!configurator.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
313 selectedClassification = CdmStore.getService(IClassificationService.class).load(configurator.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
314 }else{
315 selectedClassification = classifications.iterator().next();
316 }
317 }
318
319
320 if(csvExport || csvPrintExport || outputModelExport|| csvNameExport || dwcaExport){
321 // Label comboBoxLabel = new Label(composite, SWT.NONE);
322 // comboBoxLabel.setText("Classification");
323
324 if ((outputModelExport || dwcaExport)&& !configurator.getTaxonNodeFilter().getSubtreeFilter().isEmpty()){
325 Composite selectNodeOrClassification = new Composite(composite, SWT.NONE);
326 GridLayout grid = new GridLayout();
327 grid.numColumns = 1;
328 selectNodeOrClassification.setLayout(grid);
329 checkUseSelectedtaxonNode= new Button(selectNodeOrClassification, SWT.RADIO);
330 String taxonStr = "";
331 TaxonNode node = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(configurator.getTaxonNodeFilter().getSubtreeFilter().get(0).getUuid());
332 if (node.hasTaxon()){
333 taxonStr = node.getTaxon().getName().getTitleCache();
334 }
335 checkUseSelectedtaxonNode.setText("Export selected subtree ("+ taxonStr+")");
336 checkUseSelectedtaxonNode.addListener(SWT.Selection, new Listener() {
337 @Override
338 public void handleEvent(Event e) {
339 Button b = (Button) e.widget;
340 GridData data = (GridData) classificationSelectionCombo.getLayoutData();
341 data.exclude = b.getSelection();
342 classificationSelectionCombo.setVisible(!data.exclude);
343 classificationLabel.setVisible(!data.exclude);
344 }
345 });
346 checkUseSelectedtaxonNode.setSelection(true);
347 checkUseSelectedClassification= new Button(selectNodeOrClassification, SWT.RADIO);
348 checkUseSelectedClassification.setText("Export selected classification");
349 checkUseSelectedClassification.addListener(SWT.Selection, new Listener() {
350 @Override
351 public void handleEvent(Event e) {
352 Button b = (Button) e.widget;
353 GridData data = (GridData) classificationSelectionCombo.getLayoutData();
354 data.exclude = b.getSelection();
355 classificationSelectionCombo.setVisible(data.exclude);
356 classificationLabel.setVisible(data.exclude);
357 }
358 });
359 }
360
361 createClassificationSelectionCombo(composite);
362 classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
363 true, false, 2, 1));
364 if (checkUseSelectedtaxonNode.getSelection()){
365 classificationSelectionCombo.setVisible(false);
366 classificationLabel.setVisible(false);
367 }
368
369
370 }
371
372
373 Label folderLabel = new Label(composite, SWT.NONE);
374 folderLabel.setText("Select Folder for exported files");
375 folderLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
376 true, false, 2, 1));
377 folderDialog = new DirectoryDialog(parent.getShell());
378 folderDialog.setFilterPath(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
379
380 text_folder = new Text(composite, SWT.BORDER);
381 text_folder.setEditable(false);
382 text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
383 false));
384 if (PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER) != null){
385 text_folder.setText(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
386 setPageComplete(true);
387 }
388
389 Button button = new Button(composite, SWT.PUSH);
390 button.setText("Browse...");
391
392 button.addSelectionListener(new SelectionAdapter() {
393 /*
394 * (non-Javadoc)
395 *
396 * @see
397 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
398 * .swt.events.SelectionEvent)
399 */
400 @Override
401 public void widgetSelected(SelectionEvent e) {
402 super.widgetSelected(e);
403
404 String path = folderDialog.open();
405 if (path != null) { // a folder was selected
406 text_folder.setText(path);
407 PreferencesUtil.getPreferenceStore().setValue(EXPORT_FOLDER, path);
408 setPageComplete(true);
409 }
410 }
411 });
412 if (!outputModelExport){
413
414 Label fileLabel = new Label(composite, SWT.NONE);
415 fileLabel.setText("File");
416 text_exportFileName = new Text(composite, SWT.BORDER);
417 text_exportFileName.setText(generateFilename());
418 text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
419 true, false));
420 }
421 // make the composite the wizard pages control
422 setControl(composite);
423 }
424
425 protected String generateFilename() {
426 StringBuffer buffer = new StringBuffer();
427
428 Calendar cal = Calendar.getInstance();
429 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
430 buffer.append(sdf.format(cal.getTime()));
431
432 buffer.append("-");
433
434 buffer.append(type + "_export-");
435 try {
436 buffer.append(CdmDataSourceRepository.getCurrentCdmSource());
437 } catch (CdmRemoteSourceException e) {
438 buffer.append("Unknown");
439 }
440
441 buffer.append(".");
442 buffer.append(extension);
443
444 return buffer.toString();
445 }
446
447 /**
448 * <p>
449 * getExportFileName
450 * </p>
451 *
452 * @return the exportFileName
453 */
454 public String getExportFileName() {
455 return text_exportFileName.getText();
456 }
457
458 /**
459 * <p>
460 * getFolderText
461 * </p>
462 *
463 * @return the folderText
464 */
465 public String getFolderText() {
466 return text_folder.getText();
467 }
468
469 /**
470 * <p>
471 * getFolderComposite
472 * </p>
473 *
474 * @return the folderText
475 */
476 public Text getFolderComposite() {
477 return text_folder;
478 }
479
480 private void createClassificationSelectionCombo(Composite parent){
481 // classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
482
483 Composite classificationSelection = new Composite(parent, SWT.NULL);
484 classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));
485
486 GridLayout layout = new GridLayout();
487 classificationSelection.setLayout(layout);
488
489 classificationLabel = new Label(classificationSelection, SWT.NULL);
490 // TODO not working is not really true but leave it here to remind everyone that this is under construction
491 classificationLabel.setText("Export complete classification");
492
493 classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
494 classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 2,1));
495
496 for(Classification tree : classifications){
497 classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
498
499 }
500
501 classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
502
503 // TODO remember last selection
504 classificationSelectionCombo.addSelectionListener(this);
505
506
507
508 //return classificationSelection;
509 }
510
511 public Combo getCombo(){
512 return classificationSelectionCombo;
513 }
514
515 @Override
516 public void widgetSelected(SelectionEvent e) {
517 selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
518
519 }
520
521 @Override
522 public void widgetDefaultSelected(SelectionEvent e) {
523 //not needed here
524
525 }
526
527 public UUID getSelectedClassificationUUID() {
528
529 return selectedClassification.getUuid();
530 }
531
532 public boolean getCheckUseSelectedTaxonNode() {
533 if (checkUseSelectedtaxonNode== null){
534 return false;
535 }
536 return checkUseSelectedtaxonNode.getSelection();
537 }
538
539
540 @Override
541 public boolean canFlipToNextPage() {
542 return getFolderText() != null;
543 }
544
545
546
547
548 }