ref #10448 rename WFOExport -> WfoBackboneExport (cont.)
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / wizard / ExportToFileDestinationWizardPage.java
index 7f6e271a67cbc2942822b53a22dea239578806c8..9d5783130da73e32f9ebf3800b6e42bc99ed9c69 100644 (file)
@@ -9,6 +9,7 @@
 package eu.etaxonomy.taxeditor.io.wizard;
 
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.HashSet;
@@ -28,6 +29,7 @@ import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.DirectoryDialog;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Listener;
@@ -45,8 +47,9 @@ import eu.etaxonomy.cdm.io.dwca.out.DwcaTaxExportConfigurator;
 import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
 import eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator;
 import eu.etaxonomy.cdm.io.tcsxml.out.TcsXmlExportConfigurator;
-import eu.etaxonomy.cdm.io.wfo.out.WfoExportConfigurator;
+import eu.etaxonomy.cdm.io.wfo.out.WfoBackboneExportConfigurator;
 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
+import eu.etaxonomy.cdm.model.name.Rank;
 import eu.etaxonomy.cdm.model.taxon.Classification;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByNameComparator;
@@ -78,7 +81,7 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
     public static final String SDD_EXPORT = "SDD_EXPORT";
 
     public static final String DWCA_EXPORT = "DWCA_EXPORT";
-    public static final String WFO_EXPORT = "WFO_EXPORT";
+    public static final String WFO_BACKBONE_EXPORT = "WFO_BACKBONE_EXPORT";
     public static final String CDM_LIGHT_EXPORT = "CDM_LIGHT(CSV)";
     public static final String COL_DP_EXPORT = "COL DP";
     public static final String CSV_EXPORT = "CSV_EXPORT";
@@ -101,7 +104,7 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
 
     private boolean colDPExport = false;
     private boolean dwcaExport = false;
-    private boolean wfoExport = false;
+    private boolean wfoBackboneExport = false;
 
     private DirectoryDialog folderDialog;
     private Text text_exportFileName;
@@ -130,6 +133,8 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
 
     private Combo orderSelectionCombo;
 
+    private Text text_familyString;
+
     protected ExportToFileDestinationWizardPage(String pageName, String type, String title, String description,
             String extension, ExportConfiguratorBase<?, ?, ?> configurator) {
         super(pageName);
@@ -154,8 +159,8 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
         case DWCA_EXPORT:
             dwcaExport = true;
             break;
-        case WFO_EXPORT:
-            wfoExport = true;
+        case WFO_BACKBONE_EXPORT:
+            wfoBackboneExport = true;
             break;
         default:
             throw new RuntimeException("Export type not yet handeled: " + type);
@@ -184,10 +189,10 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
         return new ExportToFileDestinationWizardPage(DWCA_EXPORT, DWCA_EXPORT, "DwC-Archive Export",
                 "Export the content of the currently selected database into Darwin Core Archive format.", ZIP, config);
     }
-    public static ExportToFileDestinationWizardPage WFO(WfoExportConfigurator config) {
-        return new ExportToFileDestinationWizardPage(WFO_EXPORT, WFO_EXPORT, "WFO DwC-Archive Classification Export",
-                "Export the content of the currently selected database into WFO "
-                + "Darwin Core Archive format.", ZIP, config);
+    public static ExportToFileDestinationWizardPage WFO(WfoBackboneExportConfigurator config) {
+        return new ExportToFileDestinationWizardPage(WFO_BACKBONE_EXPORT, WFO_BACKBONE_EXPORT, "WFO Backbone Export",
+                "Export the content of the currently selected database into WFO Backbone format."
+                , ZIP, config);
     }
 
     public static ExportToFileDestinationWizardPage CdmLight(CdmLightExportConfigurator config) {
@@ -268,7 +273,7 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
             gridDataSep.horizontalSpan = 2;
             separator.setLayoutData(gridDataSep);
         }
-        if (classifications == null) {
+        if (classifications == null && !wfoBackboneExport) {
             classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
             Collections.sort(classifications, (o1, o2) -> {
                 if (o1.equals(o2)) {
@@ -289,7 +294,7 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
             }
         }
 
-        if (csvExport || csvPrintExport || cdmLightExport || csvNameExport || dwcaExport || colDPExport || wfoExport) {
+        if (csvExport || csvPrintExport || cdmLightExport || csvNameExport || dwcaExport || colDPExport || wfoBackboneExport) {
 
             // Label comboBoxLabel = new Label(composite, SWT.NONE);
             // comboBoxLabel.setText("Classification");
@@ -297,12 +302,16 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
             GridLayout grid = new GridLayout();
             grid.numColumns = 1;
             selectNodeOrClassification.setLayout(grid);
-            if ((cdmLightExport || dwcaExport || colDPExport) && !configurator.getTaxonNodeFilter().getSubtreeFilter().isEmpty()) {
-                checkUseSelectedTaxonNode = new Button(selectNodeOrClassification, SWT.RADIO);
+            if ((cdmLightExport || dwcaExport || colDPExport || wfoBackboneExport) && !configurator.getTaxonNodeFilter().getSubtreeFilter().isEmpty()) {
+                if (!wfoBackboneExport) {
+                    checkUseSelectedTaxonNode = new Button(selectNodeOrClassification, SWT.RADIO);
+                }
                 String taxonStr = "";
                 int count = configurator.getTaxonNodeFilter().getSubtreeFilter().size();
+                List<TaxonNode> selectedNodes = new ArrayList<>();
                 for (LogicFilter<TaxonNode> filter : configurator.getTaxonNodeFilter().getSubtreeFilter()) {
                     node = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(filter.getUuid());
+                    selectedNodes.add(node);
                     count--;
                     if (node.hasTaxon()) {
                         taxonStr += node.getTaxon().getName().getTitleCache();
@@ -311,17 +320,49 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
                         }
                     }
                 }
+                if (!wfoBackboneExport) {
+                    checkUseSelectedTaxonNode.setText("Export all selected subtree(s) (" + taxonStr + ")");
+                    checkUseSelectedTaxonNode.addListener(SWT.Selection, e -> {
+                        Button b = (Button) e.widget;
+                        GridData data = (GridData) classificationSelectionCombo.getLayoutData();
+                        data.exclude = b.getSelection();
+                        if (classificationSelectionCombo != null) {
+                            classificationSelectionCombo.setEnabled(!data.exclude);
+                        }
+                    });
+                    checkUseSelectedTaxonNode.setSelection(true);
+                }else {
+                    Composite wfoComp = new Composite(selectNodeOrClassification, SWT.NONE);
+                    grid = new GridLayout();
+                    grid.numColumns = 2;
+                    wfoComp.setLayout(grid);
+
+                    Label label = new Label(wfoComp, SWT.NONE);
+                    label.setText("Export all selected subtree(s) (" + taxonStr + ")");
+                    GridData gridData = new GridData();
+                    gridData.horizontalSpan = 2;
+                    gridData.grabExcessHorizontalSpace = true;
+                    label.setLayoutData(gridData);
+
+                    Label familyString = new Label(wfoComp, SWT.NONE);
+                    familyString.setText("Family");
+                    text_familyString = new Text(wfoComp, SWT.BORDER);
+                    text_familyString.setText(generateFamilyName(selectedNodes));
+                    text_familyString.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
 
-                checkUseSelectedTaxonNode.setText("Export selected subtree(s) (" + taxonStr + ")");
-                checkUseSelectedTaxonNode.addListener(SWT.Selection, e -> {
-                    Button b = (Button) e.widget;
-                    GridData data = (GridData) classificationSelectionCombo.getLayoutData();
-                    data.exclude = b.getSelection();
-                    classificationSelectionCombo.setEnabled(!data.exclude);
-                });
-                checkUseSelectedTaxonNode.setSelection(true);
+                }
+            }else if (wfoBackboneExport) {
+
+                Label label = new Label(composite, SWT.NONE);
+                label.setText("WFO Backbone export is only available for subtrees, not for classifications");
+                GridData gridData = new GridData();
+                gridData.horizontalSpan = 2;
+                gridData.grabExcessHorizontalSpace = true;
+                label.setLayoutData(gridData);
+                label.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
             }
-            if (cdmLightExport || dwcaExport || colDPExport || wfoExport) {
+
+            if (cdmLightExport || dwcaExport || colDPExport ) {
                 if (dwcaExport) {
                     checkUseAllClassification = new Button(selectNodeOrClassification, SWT.RADIO);
                     checkUseAllClassification.setText("Export all classifications");
@@ -346,10 +387,12 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
                 });
             }
 
-            if (node != null) {
+            if (node != null && !wfoBackboneExport) {
                 selectedClassification = node.getClassification();
             }
-            createClassificationSelectionCombo(selectNodeOrClassification);
+            if (!wfoBackboneExport) {
+                createClassificationSelectionCombo(selectNodeOrClassification);
+            }
             if (cdmLightExport) {
                 Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
                 GridData gridDataSep = new GridData();
@@ -403,13 +446,15 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
                 });
             }
 
-            if (checkUseSelectedTaxonNode != null) {
-                if (checkUseSelectedTaxonNode.getSelection()) {
-                    classificationSelectionCombo.setEnabled(false);
+            if (classificationSelectionCombo != null) {
+                if (checkUseSelectedTaxonNode != null) {
+                    if (checkUseSelectedTaxonNode.getSelection()) {
+                        classificationSelectionCombo.setEnabled(false);
+                    }
+                } else {
+                    classificationSelectionCombo.setEnabled(true);
+                    classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
                 }
-            } else {
-                classificationSelectionCombo.setEnabled(true);
-                classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
             }
             Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
             GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
@@ -452,7 +497,7 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
         if (!cdmLightExport) {
 
             Label fileLabel = new Label(composite, SWT.NONE);
-            fileLabel.setText("File");
+            fileLabel.setText("Filename");
             text_exportFileName = new Text(composite, SWT.BORDER);
             text_exportFileName.setText(generateFilename());
             text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
@@ -462,6 +507,26 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
         setControl(composite);
     }
 
+    /**
+     * @return
+     */
+    private String generateFamilyName(List<TaxonNode> nodes) {
+        if (nodes.size() == 1){
+            TaxonNode node = nodes.get(0);
+            if (node.getTaxon() != null) {
+                if(node.getTaxon().getName().getRank().getOrderIndex() >= Rank.FAMILY().getOrderIndex()) {
+                    TaxonNode familyNode = node.getAncestorOfRank(Rank.FAMILY());
+                    if (familyNode != null && familyNode.getTaxon() != null) {
+                        return familyNode.getTaxon().getName().getNameCache();
+                    }
+
+                }
+            }
+
+        }
+        return "";
+    }
+
     protected void setComparatorToConfig() {
         if (orderSelectionCombo.getText().equals(NavigatorOrderEnum.NaturalOrder.getLabel())) {
             ((CdmLightExportConfigurator) configurator).setTaxonNodeComparator(new TaxonNodeDtoNaturalComparator());
@@ -497,6 +562,13 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
         return text_exportFileName.getText();
     }
 
+    /**
+     * @return the familyString
+     */
+    public String getFamilyString() {
+        return text_familyString.getText();
+    }
+
     public String getFolderText() {
         return text_folder.getText();
     }
@@ -519,16 +591,9 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
 
     private void createClassificationSelectionCombo(Composite parent) {
 
-        // Composite classificationSelection = new Composite(parent, SWT.NULL);
-        // classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP,
-        // true, false, 1, 1));
-
-        // GridLayout layout = new GridLayout();
-        // classificationSelection.setLayout(layout);
         GridData gridData = new GridData();
         gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
         gridData.horizontalIndent = 5;
-        // classificationSelection.setLayoutData(gridData);
 
         classificationSelectionCombo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
         classificationSelectionCombo.setLayoutData(gridData);
@@ -541,7 +606,6 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
         // TODO remember last selection
         classificationSelectionCombo.addSelectionListener(this);
 
-        // return classificationSelection;
     }
 
     public Combo getCombo() {
@@ -590,6 +654,6 @@ public class ExportToFileDestinationWizardPage extends WizardPage implements Sel
 
     @Override
     public boolean canFlipToNextPage() {
-        return getFolderText() != null;
+        return getFolderText() != null && !(wfoBackboneExport && configurator.getTaxonNodeFilter().getSubtreeFilter().isEmpty());
     }
 }