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
.text
.SimpleDateFormat
;
13 import java
.util
.Calendar
;
14 import java
.util
.Collections
;
15 import java
.util
.Comparator
;
16 import java
.util
.HashSet
;
17 import java
.util
.List
;
19 import java
.util
.UUID
;
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
.DirectoryDialog
;
32 import org
.eclipse
.swt
.widgets
.Event
;
33 import org
.eclipse
.swt
.widgets
.Label
;
34 import org
.eclipse
.swt
.widgets
.Listener
;
35 import org
.eclipse
.swt
.widgets
.Text
;
37 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
38 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
39 import eu
.etaxonomy
.cdm
.filter
.LogicFilter
;
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
.dwca
.out
.DwcaTaxExportConfigurator
;
45 import eu
.etaxonomy
.cdm
.io
.jaxb
.JaxbExportConfigurator
;
46 import eu
.etaxonomy
.cdm
.io
.sdd
.out
.SDDExportConfigurator
;
47 import eu
.etaxonomy
.cdm
.io
.tcsxml
.out
.TcsXmlExportConfigurator
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
50 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
51 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
52 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSourceException
;
53 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
57 * ExportToFileDestinationWizardPage class.
64 public class ExportToFileDestinationWizardPage
extends WizardPage
implements SelectionListener
{
66 /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
67 public static final String DATE_FORMAT_NOW
= "yyyyMMddHHmm";
69 /** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
70 public static final String JAXB_EXPORT
= "JAXB_EXPORT";
72 /** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
73 public static final String TCS_EXPORT
= "TCS_EXPORT";
75 /** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
76 public static final String SDD_EXPORT
= "SDD_EXPORT";
78 public static final String DWCA_EXPORT
= "DWCA_EXPORT";
79 public static final String OUTPUT_MODEL_EXPORT
= "CDM_LIGHT(CSV)";
80 public static final String CSV_EXPORT
= "CSV_EXPORT";
81 public static final String CSV_NAME_EXPORT
= "CSV_NAME_EXPORT";
82 private static final String CSV_PRINT_EXPORT
="CSV_PRINT_EXPORT";
83 private static final String EXPORT_FOLDER
="EXPORT_FOLDER";
84 public static final String XML
= "xml";
86 public static final String CSV
= "csv";
88 public static final String ZIP
= "zip";
90 private boolean csvExport
= false;
92 private boolean csvNameExport
= false;
94 private boolean csvPrintExport
= false;
96 private boolean outputModelExport
= false;
97 private boolean dwcaExport
= false;
99 private DirectoryDialog folderDialog
;
100 private Text text_exportFileName
;
102 private Text text_folder
;
104 private final String type
;
106 private final String extension
;
108 private Combo classificationSelectionCombo
;
110 private List
<Classification
> classifications
;
112 private Classification selectedClassification
;
114 private Label classificationLabel
;
116 private ExportConfiguratorBase configurator
;
117 private Button checkUseSelectedtaxonNode
;
119 private Button checkUseSelectedClassification
;
120 private Button checkUseAllClassification
;
122 protected ExportToFileDestinationWizardPage(String pageName
, String type
,
123 String title
, String description
, String extension
, ExportConfiguratorBase configurator
) {
125 this.configurator
= configurator
;
131 case CSV_NAME_EXPORT
:
132 csvNameExport
= true;
134 case OUTPUT_MODEL_EXPORT
:
135 outputModelExport
= true;
137 case CSV_PRINT_EXPORT
:
138 csvPrintExport
= true;
145 this.extension
= extension
;
146 this.setTitle(title
);
147 this.setDescription(description
);
150 public static ExportToFileDestinationWizardPage
Jaxb(JaxbExportConfigurator configurator
) {
151 return new ExportToFileDestinationWizardPage(
155 "Exports the content of the currently selected database into the cdm jaxb format.",
159 public static ExportToFileDestinationWizardPage
Tcs(TcsXmlExportConfigurator config
) {
160 return new ExportToFileDestinationWizardPage(
164 "Export the content of the currently selected database into TCS format.",
168 public static ExportToFileDestinationWizardPage
Sdd(SDDExportConfigurator config
) {
169 return new ExportToFileDestinationWizardPage(
173 "Export the content of the currently selected database into SDD format.",
177 public static ExportToFileDestinationWizardPage
Dwca(DwcaTaxExportConfigurator config
) {
178 return new ExportToFileDestinationWizardPage(
181 "DwC-Archive Export",
182 "Export the content of the currently selected database into Darwin Core Archive format.",
186 public static ExportToFileDestinationWizardPage
OutputModel(CdmLightExportConfigurator config
) {
188 return new ExportToFileDestinationWizardPage(
191 "CDM Light Export (csv)",
192 "Export the content of the currently selected database into the CDM light (csv) format.",
196 public static ExportToFileDestinationWizardPage
Csv(CsvDemoExportConfigurator config
) {
198 return new ExportToFileDestinationWizardPage(
202 "Export the content of the currently selected database into Comma Separated Value format.",
206 public static ExportToFileDestinationWizardPage
CsvNames(CsvNameExportConfigurator config
) {
208 return new ExportToFileDestinationWizardPage(
212 "Export the names of the currently selected database into Semicolon Separated Value format.",
216 public static ExportToFileDestinationWizardPage
CsvPrint(CsvNameExportConfigurator config
) {
218 return new ExportToFileDestinationWizardPage(
222 "Export the content of the currently selected database into Semicolon Separated Value format.",
228 public void createControl(Composite parent
) {
230 setPageComplete(false);
232 Composite composite
= new Composite(parent
, SWT
.NONE
);
233 GridLayout gridLayout
= new GridLayout();
235 gridLayout
.numColumns
= 2;
236 TaxonNode node
= null;
238 composite
.setLayout(gridLayout
);
239 if(classifications
== null){
240 classifications
= CdmStore
.getService(IClassificationService
.class).list(null, null, null, null, null);
241 Collections
.sort(classifications
, new Comparator
<Classification
>() {
244 public int compare(Classification o1
, Classification o2
) {
248 int result
= o1
.getTitleCache().compareTo(o2
.getTitleCache());
250 return o1
.getUuid().compareTo(o2
.getUuid());
255 if (!configurator
.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
256 selectedClassification
= CdmStore
.getService(IClassificationService
.class).load(configurator
.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
259 selectedClassification
= classifications
.iterator().next();
264 if(csvExport
|| csvPrintExport
|| outputModelExport
|| csvNameExport
|| dwcaExport
){
266 // Label comboBoxLabel = new Label(composite, SWT.NONE);
267 // comboBoxLabel.setText("Classification");
268 Composite selectNodeOrClassification
= new Composite(composite
, SWT
.NONE
);
269 GridLayout grid
= new GridLayout();
271 selectNodeOrClassification
.setLayout(grid
);
272 if ((outputModelExport
|| dwcaExport
)&& !configurator
.getTaxonNodeFilter().getSubtreeFilter().isEmpty()){
273 checkUseSelectedtaxonNode
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
274 String taxonStr
= "";
275 int count
= configurator
.getTaxonNodeFilter().getSubtreeFilter().size();
276 for (LogicFilter
<TaxonNode
> filter
: configurator
.getTaxonNodeFilter().getSubtreeFilter()){
277 node
= CdmApplicationState
.getCurrentAppConfig().getTaxonNodeService().load(filter
.getUuid());
279 if (node
.hasTaxon()){
280 taxonStr
+= node
.getTaxon().getName().getTitleCache();
287 checkUseSelectedtaxonNode
.setText("Export selected subtree(s) ("+ taxonStr
+")");
288 checkUseSelectedtaxonNode
.addListener(SWT
.Selection
, new Listener() {
290 public void handleEvent(Event e
) {
291 Button b
= (Button
) e
.widget
;
292 GridData data
= (GridData
) classificationSelectionCombo
.getLayoutData();
293 data
.exclude
= b
.getSelection();
294 classificationSelectionCombo
.setVisible(!data
.exclude
);
298 checkUseSelectedtaxonNode
.setSelection(true);
300 if(outputModelExport
|| dwcaExport
){
302 checkUseAllClassification
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
303 checkUseAllClassification
.setText("Export all classifications");
304 checkUseAllClassification
.addListener(SWT
.Selection
, new Listener() {
306 public void handleEvent(Event e
) {
307 Button b
= (Button
) e
.widget
;
308 GridData data
= (GridData
) classificationSelectionCombo
.getLayoutData();
309 data
.exclude
= b
.getSelection();
310 classificationSelectionCombo
.setVisible(!data
.exclude
);
315 checkUseSelectedClassification
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
316 checkUseSelectedClassification
.setText("Export selected classification");
317 checkUseSelectedClassification
.addListener(SWT
.Selection
, new Listener() {
319 public void handleEvent(Event e
) {
320 Button b
= (Button
) e
.widget
;
321 GridData data
= (GridData
) classificationSelectionCombo
.getLayoutData();
322 data
.exclude
= b
.getSelection();
323 classificationSelectionCombo
.setVisible(data
.exclude
);
330 selectedClassification
= node
.getClassification();
332 createClassificationSelectionCombo(selectNodeOrClassification
);
336 if (checkUseSelectedtaxonNode
!= null){
338 if (checkUseSelectedtaxonNode
.getSelection()){
339 classificationSelectionCombo
.setVisible(false);
343 classificationSelectionCombo
.setVisible(true);
344 classificationSelectionCombo
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
350 Label folderLabel
= new Label(composite
, SWT
.NONE
);
351 folderLabel
.setText("Select Folder for exported files");
352 folderLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
354 folderDialog
= new DirectoryDialog(parent
.getShell());
355 folderDialog
.setFilterPath(PreferencesUtil
.getPreferenceStore().getString(EXPORT_FOLDER
));
357 text_folder
= new Text(composite
, SWT
.BORDER
);
358 text_folder
.setEditable(false);
359 text_folder
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true,
361 if (PreferencesUtil
.getPreferenceStore().getString(EXPORT_FOLDER
) != null){
362 text_folder
.setText(PreferencesUtil
.getPreferenceStore().getString(EXPORT_FOLDER
));
363 setPageComplete(true);
366 Button button
= new Button(composite
, SWT
.PUSH
);
367 button
.setText("Browse...");
369 button
.addSelectionListener(new SelectionAdapter() {
371 public void widgetSelected(SelectionEvent e
) {
372 super.widgetSelected(e
);
374 String path
= folderDialog
.open();
375 if (path
!= null) { // a folder was selected
376 text_folder
.setText(path
);
377 PreferencesUtil
.getPreferenceStore().setValue(EXPORT_FOLDER
, path
);
378 setPageComplete(true);
382 if (!outputModelExport
){
384 Label fileLabel
= new Label(composite
, SWT
.NONE
);
385 fileLabel
.setText("File");
386 text_exportFileName
= new Text(composite
, SWT
.BORDER
);
387 text_exportFileName
.setText(generateFilename());
388 text_exportFileName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
391 // make the composite the wizard pages control
392 setControl(composite
);
395 protected String
generateFilename() {
396 StringBuffer buffer
= new StringBuffer();
398 Calendar cal
= Calendar
.getInstance();
399 SimpleDateFormat sdf
= new SimpleDateFormat(DATE_FORMAT_NOW
);
400 buffer
.append(sdf
.format(cal
.getTime()));
404 buffer
.append(type
+ "_export-");
406 buffer
.append(CdmDataSourceRepository
.getCurrentCdmSource());
407 } catch (CdmRemoteSourceException e
) {
408 buffer
.append("Unknown");
412 buffer
.append(extension
);
414 return buffer
.toString();
417 public String
getExportFileName() {
418 return text_exportFileName
.getText();
421 public String
getFolderText() {
422 return text_folder
.getText();
425 public Text
getFolderComposite() {
429 private void createClassificationSelectionCombo(Composite parent
){
431 // Composite classificationSelection = new Composite(parent, SWT.NULL);
432 // classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
434 // GridLayout layout = new GridLayout();
435 // classificationSelection.setLayout(layout);
436 GridData gridData
= new GridData();
437 gridData
= new GridData(GridData
.BEGINNING
, GridData
.CENTER
, true, false);
438 gridData
.horizontalIndent
= 5;
439 // classificationSelection.setLayoutData(gridData);
441 classificationSelectionCombo
= new Combo(parent
, SWT
.BORDER
| SWT
.READ_ONLY
);
442 classificationSelectionCombo
.setLayoutData(gridData
);
443 for(Classification tree
: classifications
){
444 classificationSelectionCombo
.add(tree
.getName().getText(), classifications
.indexOf(tree
));
448 classificationSelectionCombo
.select(classifications
.indexOf(selectedClassification
));
450 // TODO remember last selection
451 classificationSelectionCombo
.addSelectionListener(this);
455 //return classificationSelection;
458 public Combo
getCombo(){
459 return classificationSelectionCombo
;
463 public void widgetSelected(SelectionEvent e
) {
464 selectedClassification
= classifications
.get(classificationSelectionCombo
.getSelectionIndex());
469 public void widgetDefaultSelected(SelectionEvent e
) {
474 public UUID
getSelectedClassificationUUID() {
476 return selectedClassification
.getUuid();
479 public boolean getCheckUseSelectedTaxonNode() {
480 if (checkUseSelectedtaxonNode
== null){
483 return checkUseSelectedtaxonNode
.getSelection();
485 public boolean getCheckUseAllClassifications() {
486 if (checkUseAllClassification
== null){
489 return checkUseAllClassification
.getSelection();
492 public Set
<UUID
> getAllClassificationUuids(){
493 Set
<UUID
> allClassificationUuids
= new HashSet();
494 for (Classification classification
: this.classifications
){
495 allClassificationUuids
.add(classification
.getUuid());
497 return allClassificationUuids
;
501 public boolean canFlipToNextPage() {
502 return getFolderText() != null;