8ea4287668ede45515fb8fe62a8ee2643abc3719
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / io / ExportAction.java
1 /**
2 * Copyright (C) 2007 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
10 package eu.etaxonomy.taxeditor.actions.io;
11
12 import java.io.File;
13
14 import org.apache.log4j.Logger;
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.widgets.FileDialog;
20
21 import eu.etaxonomy.cdm.database.ICdmDataSource;
22 import eu.etaxonomy.cdm.io.common.CdmDefaultExport;
23 import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
24 import eu.etaxonomy.taxeditor.UiUtil;
25 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
26
27 /**
28 * @author p.ciardelli
29 * @created 05.11.2008
30 * @version 1.0
31 */
32 public class ExportAction extends Action {
33 private static final Logger logger = Logger.getLogger(ExportAction.class);
34
35 private static String text = "Export as ...";
36 private ImageDescriptor image = null;
37 public static final String ID = "eu.etaxonomy.taxeditor.actions.io.exportaction"; //$NON-NLS-1$
38
39 public static final String JAXB = "JAXB";
40
41 private FileDialog dialog;
42 private File file;
43
44 private String exportType;
45
46 public ExportAction() {
47 super(text);
48 setImageDescriptor(image);
49 setId(ID);
50 }
51
52 public ExportAction(String exportType) {
53 this();
54
55 this.exportType = exportType;
56 setText(exportType);
57 }
58
59 public void run() {
60
61 // Use same title "Export FORMAT" for all message dialogs
62 String title = "Export " + getText();
63
64 // Get file from user
65 dialog = new FileDialog(UiUtil.getShell(), SWT.SAVE);
66 dialog.setFileName("export.xml");
67 String filePath = dialog.open();
68 file = new File(filePath);
69
70 // Get current data source
71 ICdmDataSource source = CdmDataSourceRepository.getDefault().getCurrentDataSource();
72
73 // Format file path
74 String destination = null;
75 // destination = file.toURI().toURL().toString();
76 destination = file.toString();
77
78 JaxbExportConfigurator configurator = JaxbExportConfigurator.NewInstance(source, destination);
79 CdmDefaultExport<JaxbExportConfigurator> jaxbExport =
80 new CdmDefaultExport<JaxbExportConfigurator>();
81
82 // Start export
83 boolean isSuccessfulExport = jaxbExport.invoke(configurator);
84
85 // Tell user whether import was a success or a dismal failure
86 if (isSuccessfulExport) {
87 MessageDialog.openInformation(UiUtil.getShell(), title, "Export successful");
88 } else {
89 MessageDialog.openError(UiUtil.getShell(), title, "Export was unsuccessful.");
90 }
91 }
92 }