Merge branch 'release/4.7.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / wizard / AbstractImportWizard.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.io.wizard;
11
12 import org.eclipse.jface.dialogs.MessageDialog;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.jface.wizard.IWizardContainer;
15 import org.eclipse.jface.wizard.Wizard;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.ui.IEditorPart;
18 import org.eclipse.ui.IEditorReference;
19 import org.eclipse.ui.IImportWizard;
20 import org.eclipse.ui.IWorkbench;
21 import org.eclipse.ui.IWorkbenchPage;
22
23 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
24 import eu.etaxonomy.cdm.io.common.IIoConfigurator;
25 import eu.etaxonomy.taxeditor.l10n.Messages;
26
27 /**
28 * @author n.hoffmann
29 * @created 24.06.2009
30 * @version 1.0
31 */
32 public abstract class AbstractImportWizard<CONFIG extends IIoConfigurator> extends Wizard implements IImportWizard {
33
34 protected final String CONFIGURATION_PAGE = "CONFIGURATION_PAGE";
35
36 protected GenericConfiguratorWizardPage pageConfiguration;
37 private NomenclaturalCodeWizardPage pageNomenclaturalCode;
38
39 private IWorkbench workbench;
40
41 private IStructuredSelection selection;
42
43 /** {@inheritDoc} */
44 @Override
45 public void addPages() {
46 super.addPages();
47
48 addConfiguratorPage();
49
50 pageNomenclaturalCode = new NomenclaturalCodeWizardPage((IImportConfigurator)getConfigurator());
51 this.addPage(pageNomenclaturalCode);
52 }
53
54 protected void addConfiguratorPage(){
55 pageConfiguration = GenericConfiguratorWizardPage.Import(CONFIGURATION_PAGE, getConfigurator(), null, Messages.AbstractImportWizard_ConfigurationLabel);
56 this.addPage(pageConfiguration);
57 }
58
59 @Override
60 public void setContainer(IWizardContainer wizardContainer) {
61 if(existUnsavedEditors()){
62 MessageDialog.open(MessageDialog.WARNING, getShell(), "Unsaved editors", "Please save all open editors before executing " +
63 "an import operation. The import operation will be cancelled", SWT.NONE);
64 }
65 else{
66 super.setContainer(wizardContainer);
67 }
68 }
69
70 @Override
71 public void init(IWorkbench workbench, IStructuredSelection selection) {
72 this.setWorkbench(workbench);
73 this.setSelection(selection);
74 }
75
76 protected boolean existUnsavedEditors(){
77 IWorkbenchPage activePage = getWorkbench().getActiveWorkbenchWindow().getActivePage();
78 if(activePage != null){
79 for (IEditorReference reference : activePage.getEditorReferences()) {
80 IEditorPart editor = reference.getEditor(false);
81 if(editor.isDirty()){
82 return true;
83 }
84 }
85 }
86 return false;
87 }
88
89 @Override
90 public boolean canFinish() {
91 return !existUnsavedEditors() && super.canFinish();
92 }
93
94 public abstract CONFIG getConfigurator();
95
96 public void setSelection(IStructuredSelection selection) {
97 this.selection = selection;
98 }
99
100 public IStructuredSelection getSelection() {
101 return selection;
102 }
103
104 public void setWorkbench(IWorkbench workbench) {
105 this.workbench = workbench;
106 }
107
108 public IWorkbench getWorkbench() {
109 return workbench;
110 }
111 }