f27c09b2033c06d3e4607a4ebad7752cd9e52355
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / io / ImportAction.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.widgets.FileDialog;
19
20 import eu.etaxonomy.cdm.database.ICdmDataSource;
21 import eu.etaxonomy.cdm.io.common.ImportWrapper;
22 import eu.etaxonomy.taxeditor.UiUtil;
23 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
24
25 /**
26 * @author p.ciardelli
27 * @created 05.11.2008
28 * @version 1.0
29 */
30 public class ImportAction extends Action {
31 private static final Logger logger = Logger.getLogger(ImportAction.class);
32
33 private static String text = "Import ...";
34 private ImageDescriptor image = null;
35 public static final String ID = "eu.etaxonomy.taxeditor.actions.io.importaction"; //$NON-NLS-1$
36
37 private FileDialog dialog;
38 private File file;
39 private ImportWrapper importWrapper;
40
41 private ImportAction() {
42 super(text);
43 setImageDescriptor(image);
44 setId(ID);
45 }
46
47 public ImportAction(ImportWrapper importWrapper) {
48 this();
49
50 this.importWrapper = importWrapper;
51 setText(importWrapper.getLabel());
52 }
53
54 public void run() {
55
56 // Use same title "Import FORMAT" for all message dialogs
57 String title = "Import " + getText();
58
59 // Make sure user is aware of the implications of an import
60 boolean doProceed = MessageDialog.openConfirm(UiUtil.getShell(), title,
61 "Import may require a great deal of time and system resources.\n\n" +
62 "Are you sure you would like to proceed?");
63 if (!doProceed) {
64 return;
65 }
66
67 // Get file from user
68 dialog = new FileDialog(UiUtil.getShell());
69 String filePath = dialog.open();
70 file = new File(filePath);
71
72 // Get current data source
73 ICdmDataSource destination = CdmDataSourceRepository.getDefault().getCurrentDataSource();
74
75 // Get plugin application controller - from Anahit
76 // CdmApplicationController app = TaxEditorPlugin.getDefault().getCdmApp();
77 // TaxEditorPlugin.getDefault().commitTransaction();
78 // TaxEditorPlugin.getDefault().getCdmApp().close();
79
80 // Format file path
81 String source = null;
82 try {
83 source = file.toURI().toURL().toString();
84 //source = file.toString();
85 } catch (/*MalformedURL*/Exception e) {
86 MessageDialog.openError(UiUtil.getShell(), title, "File could not be read.");
87 return;
88 }
89
90 // Start import
91 boolean isSuccessfulImport = importWrapper.invoke(source, destination, null);
92
93 // Tell user whether import was a success or a dismal failure
94 if (isSuccessfulImport) {
95 MessageDialog.openInformation(UiUtil.getShell(), title, "Import successful");
96 UiUtil.getTreeViewer().refresh();
97 } else {
98 MessageDialog.openError(UiUtil.getShell(), title, "Import was unsuccessful.");
99 }
100
101 }
102 }