2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.io
.wizard
;
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
;
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
;
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
;
58 * ExportToFileDestinationWizardPage class.
65 public class ExportToFileDestinationWizardPage
extends WizardPage
implements SelectionListener
{
67 /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
68 public static final String DATE_FORMAT_NOW
= "yyyyMMddHHmm";
70 /** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
71 public static final String JAXB_EXPORT
= "JAXB_EXPORT";
73 /** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
74 public static final String TCS_EXPORT
= "TCS_EXPORT";
76 /** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
77 public static final String SDD_EXPORT
= "SDD_EXPORT";
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";
87 public static final String CSV
= "csv";
91 public static final String ZIP
= "zip";
93 private boolean csvExport
= false;
95 private boolean csvNameExport
= false;
99 private boolean csvPrintExport
= false;
101 private boolean outputModelExport
= false;
102 private boolean dwcaExport
= false;
104 private DirectoryDialog folderDialog
;
105 private Text text_exportFileName
;
107 private Text text_folder
;
109 private final String type
;
111 private final String extension
;
113 private Combo classificationSelectionCombo
;
115 private List
<Classification
> classifications
;
117 private Classification selectedClassification
;
118 private Label classificationLabel
;
120 private ExportConfiguratorBase configurator
;
121 private Button checkUseSelectedtaxonNode
;
123 private Button checkUseSelectedClassification
;
129 protected ExportToFileDestinationWizardPage(String pageName
, String type
,
130 String title
, String description
, String extension
, ExportConfiguratorBase configurator
) {
132 this.configurator
= configurator
;
138 case CSV_NAME_EXPORT
:
139 csvNameExport
= true;
141 case OUTPUT_MODEL_EXPORT
:
142 outputModelExport
= true;
144 case CSV_PRINT_EXPORT
:
145 csvPrintExport
= true;
152 this.extension
= extension
;
153 this.setTitle(title
);
154 this.setDescription(description
);
163 * {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
166 public static ExportToFileDestinationWizardPage
Jaxb(JaxbExportConfigurator configurator
) {
167 return new ExportToFileDestinationWizardPage(
171 "Exports the content of the currently selected database into the cdm jaxb format.",
181 * {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
184 public static ExportToFileDestinationWizardPage
Tcs(TcsXmlExportConfigurator config
) {
185 return new ExportToFileDestinationWizardPage(
189 "Export the content of the currently selected database into TCS format.",
199 * {@link eu.etaxonomy.taxeditor.io.wizard.ExportToFileDestinationWizardPage}
202 public static ExportToFileDestinationWizardPage
Sdd(SDDExportConfigurator config
) {
203 return new ExportToFileDestinationWizardPage(
207 "Export the content of the currently selected database into SDD format.",
214 public static ExportToFileDestinationWizardPage
Dwca(DwcaTaxExportConfigurator config
) {
215 return new ExportToFileDestinationWizardPage(
218 "DwC-Archive Export",
219 "Export the content of the currently selected database into Darwin Core Archive format.",
226 public static ExportToFileDestinationWizardPage
OutputModel(CdmLightExportConfigurator config
) {
228 return new ExportToFileDestinationWizardPage(
231 "Cdm Light Export (csv)",
232 "Export the content of the currently selected database into the cdm light (csv) format.",
240 public static ExportToFileDestinationWizardPage
Csv(CsvDemoExportConfigurator config
) {
242 return new ExportToFileDestinationWizardPage(
246 "Export the content of the currently selected database into Comma Separated Value format.",
253 public static ExportToFileDestinationWizardPage
CsvNames(CsvNameExportConfigurator config
) {
255 return new ExportToFileDestinationWizardPage(
259 "Export the names of the currently selected database into Semicolon Separated Value format.",
266 public static ExportToFileDestinationWizardPage
CsvPrint(CsvNameExportConfigurator config
) {
268 return new ExportToFileDestinationWizardPage(
272 "Export the content of the currently selected database into Semicolon Separated Value format.",
280 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
285 public void createControl(Composite parent
) {
287 setPageComplete(false);
289 Composite composite
= new Composite(parent
, SWT
.NONE
);
290 GridLayout gridLayout
= new GridLayout();
292 gridLayout
.numColumns
= 2;
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
>() {
301 public int compare(Classification o1
, Classification o2
) {
305 int result
= o1
.getTitleCache().compareTo(o2
.getTitleCache());
307 return o1
.getUuid().compareTo(o2
.getUuid());
312 if (!configurator
.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
313 selectedClassification
= CdmStore
.getService(IClassificationService
.class).load(configurator
.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
315 selectedClassification
= classifications
.iterator().next();
320 if(csvExport
|| csvPrintExport
|| outputModelExport
|| csvNameExport
|| dwcaExport
){
321 // Label comboBoxLabel = new Label(composite, SWT.NONE);
322 // comboBoxLabel.setText("Classification");
324 if ((outputModelExport
|| dwcaExport
)&& !configurator
.getTaxonNodeFilter().getSubtreeFilter().isEmpty()){
325 Composite selectNodeOrClassification
= new Composite(composite
, SWT
.NONE
);
326 GridLayout grid
= new GridLayout();
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();
335 checkUseSelectedtaxonNode
.setText("Export selected subtree ("+ taxonStr
+")");
336 checkUseSelectedtaxonNode
.addListener(SWT
.Selection
, new Listener() {
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
);
346 checkUseSelectedtaxonNode
.setSelection(true);
347 checkUseSelectedClassification
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
348 checkUseSelectedClassification
.setText("Export selected classification");
349 checkUseSelectedClassification
.addListener(SWT
.Selection
, new Listener() {
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
);
361 createClassificationSelectionCombo(composite
);
362 classificationSelectionCombo
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
364 if (checkUseSelectedtaxonNode
!= null){
365 if (checkUseSelectedtaxonNode
.getSelection()){
366 classificationSelectionCombo
.setVisible(false);
367 classificationLabel
.setVisible(false);
375 Label folderLabel
= new Label(composite
, SWT
.NONE
);
376 folderLabel
.setText("Select Folder for exported files");
377 folderLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
379 folderDialog
= new DirectoryDialog(parent
.getShell());
380 folderDialog
.setFilterPath(PreferencesUtil
.getPreferenceStore().getString(EXPORT_FOLDER
));
382 text_folder
= new Text(composite
, SWT
.BORDER
);
383 text_folder
.setEditable(false);
384 text_folder
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true,
386 if (PreferencesUtil
.getPreferenceStore().getString(EXPORT_FOLDER
) != null){
387 text_folder
.setText(PreferencesUtil
.getPreferenceStore().getString(EXPORT_FOLDER
));
388 setPageComplete(true);
391 Button button
= new Button(composite
, SWT
.PUSH
);
392 button
.setText("Browse...");
394 button
.addSelectionListener(new SelectionAdapter() {
399 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse
400 * .swt.events.SelectionEvent)
403 public void widgetSelected(SelectionEvent e
) {
404 super.widgetSelected(e
);
406 String path
= folderDialog
.open();
407 if (path
!= null) { // a folder was selected
408 text_folder
.setText(path
);
409 PreferencesUtil
.getPreferenceStore().setValue(EXPORT_FOLDER
, path
);
410 setPageComplete(true);
414 if (!outputModelExport
){
416 Label fileLabel
= new Label(composite
, SWT
.NONE
);
417 fileLabel
.setText("File");
418 text_exportFileName
= new Text(composite
, SWT
.BORDER
);
419 text_exportFileName
.setText(generateFilename());
420 text_exportFileName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
423 // make the composite the wizard pages control
424 setControl(composite
);
427 protected String
generateFilename() {
428 StringBuffer buffer
= new StringBuffer();
430 Calendar cal
= Calendar
.getInstance();
431 SimpleDateFormat sdf
= new SimpleDateFormat(DATE_FORMAT_NOW
);
432 buffer
.append(sdf
.format(cal
.getTime()));
436 buffer
.append(type
+ "_export-");
438 buffer
.append(CdmDataSourceRepository
.getCurrentCdmSource());
439 } catch (CdmRemoteSourceException e
) {
440 buffer
.append("Unknown");
444 buffer
.append(extension
);
446 return buffer
.toString();
454 * @return the exportFileName
456 public String
getExportFileName() {
457 return text_exportFileName
.getText();
465 * @return the folderText
467 public String
getFolderText() {
468 return text_folder
.getText();
476 * @return the folderText
478 public Text
getFolderComposite() {
482 private void createClassificationSelectionCombo(Composite parent
){
483 // classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
485 Composite classificationSelection
= new Composite(parent
, SWT
.NULL
);
486 classificationSelection
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false, 2, 1));
488 GridLayout layout
= new GridLayout();
489 classificationSelection
.setLayout(layout
);
491 classificationLabel
= new Label(classificationSelection
, SWT
.NULL
);
492 // TODO not working is not really true but leave it here to remind everyone that this is under construction
493 classificationLabel
.setText("Export complete classification");
495 classificationSelectionCombo
= new Combo(classificationSelection
, SWT
.BORDER
| SWT
.READ_ONLY
);
496 classificationSelectionCombo
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, true, 2,1));
498 for(Classification tree
: classifications
){
499 classificationSelectionCombo
.add(tree
.getName().getText(), classifications
.indexOf(tree
));
503 classificationSelectionCombo
.select(classifications
.indexOf(selectedClassification
));
505 // TODO remember last selection
506 classificationSelectionCombo
.addSelectionListener(this);
510 //return classificationSelection;
513 public Combo
getCombo(){
514 return classificationSelectionCombo
;
518 public void widgetSelected(SelectionEvent e
) {
519 selectedClassification
= classifications
.get(classificationSelectionCombo
.getSelectionIndex());
524 public void widgetDefaultSelected(SelectionEvent e
) {
529 public UUID
getSelectedClassificationUUID() {
531 return selectedClassification
.getUuid();
534 public boolean getCheckUseSelectedTaxonNode() {
535 if (checkUseSelectedtaxonNode
== null){
538 return checkUseSelectedtaxonNode
.getSelection();
543 public boolean canFlipToNextPage() {
544 return getFolderText() != null;