merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / wizard / AbstractImportWizard.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.io.wizard;
12
13 import org.eclipse.jface.dialogs.MessageDialog;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.jface.wizard.IWizardContainer;
16 import org.eclipse.jface.wizard.Wizard;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.IEditorReference;
20 import org.eclipse.ui.IImportWizard;
21 import org.eclipse.ui.IWorkbench;
22 import org.eclipse.ui.IWorkbenchPage;
23
24 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
25 import eu.etaxonomy.cdm.io.common.IIoConfigurator;
26
27 /**
28 * <p>Abstract AbstractImportWizard class.</p>
29 *
30 * @author n.hoffmann
31 * @created 24.06.2009
32 * @version 1.0
33 */
34 public abstract class AbstractImportWizard<CONFIG extends IIoConfigurator> extends Wizard implements IImportWizard {
35
36 protected final String CONFIGURATION_PAGE = "CONFIGURATION_PAGE";
37
38 private GenericConfiguratorWizardPage pageConfiguration;
39 private NomenclaturalCodeWizardPage pageNomenclaturalCode;
40
41 private IWorkbench workbench;
42
43 private IStructuredSelection selection;
44
45
46 /* (non-Javadoc)
47 * @see org.eclipse.jface.wizard.Wizard#addPages()
48 */
49 /** {@inheritDoc} */
50 @Override
51 public void addPages() {
52 super.addPages();
53
54 pageConfiguration = GenericConfiguratorWizardPage.Import(CONFIGURATION_PAGE, getConfigurator());
55 this.addPage(pageConfiguration);
56
57 pageNomenclaturalCode = new NomenclaturalCodeWizardPage((IImportConfigurator)getConfigurator());
58 this.addPage(pageNomenclaturalCode);
59
60
61 }
62
63 /* (non-Javadoc)
64 * @see org.eclipse.jface.wizard.Wizard#setContainer(org.eclipse.jface.wizard.IWizardContainer)
65 */
66 @Override
67 public void setContainer(IWizardContainer wizardContainer) {
68 if(existUnsavedEditors()){
69 MessageDialog.open(MessageDialog.WARNING, getShell(), "Unsaved editors", "Please save all open editors before executing " +
70 "an import operation. The import operation will be cancelled", SWT.NONE);
71 }
72 else{
73 super.setContainer(wizardContainer);
74 }
75 }
76
77 /* (non-Javadoc)
78 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
79 */
80 @Override
81 public void init(IWorkbench workbench, IStructuredSelection selection) {
82 this.setWorkbench(workbench);
83 this.setSelection(selection);
84 }
85
86 protected boolean existUnsavedEditors(){
87 IWorkbenchPage activePage = getWorkbench().getActiveWorkbenchWindow().getActivePage();
88 if(activePage != null){
89 for (IEditorReference reference : activePage.getEditorReferences()) {
90 IEditorPart editor = reference.getEditor(false);
91 if(editor.isDirty()){
92 return true;
93 }
94 }
95 }
96 return false;
97 }
98
99 /* (non-Javadoc)
100 * @see org.eclipse.jface.wizard.Wizard#canFinish()
101 */
102 @Override
103 public boolean canFinish() {
104 return !existUnsavedEditors();
105 }
106
107 /**
108 * <p>getConfigurator</p>
109 *
110 * @return a CONFIG object.
111 */
112 public abstract CONFIG getConfigurator();
113
114 /**
115 * @param selection the selection to set
116 */
117 public void setSelection(IStructuredSelection selection) {
118 this.selection = selection;
119 }
120
121 /**
122 * @return the selection
123 */
124 public IStructuredSelection getSelection() {
125 return selection;
126 }
127
128 /**
129 * @param workbench the workbench to set
130 */
131 public void setWorkbench(IWorkbench workbench) {
132 this.workbench = workbench;
133 }
134
135 /**
136 * @return the workbench
137 */
138 public IWorkbench getWorkbench() {
139 return workbench;
140 }
141 }