refactored wizard dialog and pages to allow for easy in place editing and cloning...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / datasource / wizard / CdmDataSourceWizard.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.datasource.wizard;
12
13 import org.eclipse.jface.wizard.Wizard;
14
15 import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
16 import eu.etaxonomy.cdm.database.ICdmDataSource;
17 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
18
19 /**
20 * <p>CdmDataSourceWizard class.</p>
21 *
22 * @author n.hoffmann
23 * @created 18.05.2009
24 * @version 1.0
25 */
26 public class CdmDataSourceWizard extends Wizard {
27
28 private CdmDataSourceCredentialsWizardPage dataSourcePage;
29
30 private CdmDataSourceTypeSelectionWizardPage dataSourceSelectionPage;
31
32 private ICdmDataSource dataSource;
33
34 private String dataSourceName;
35
36 //private boolean editMode;
37
38 public enum Mode {
39 CREATE,
40 EDIT,
41 CLONE
42 }
43
44 // default mode is to create a new datasource
45 Mode mode = Mode.CREATE;
46
47 /**
48 * <p>Constructor for CdmDataSourceWizard.</p>
49 */
50 public CdmDataSourceWizard() {
51 super();
52 this.mode = Mode.CREATE;
53 setForcePreviousAndNextButtons(true);
54 setWindowTitle("Datasource Dialog");
55
56 }
57
58
59 /**
60 * <p>Constructor for CdmDataSourceWizard.</p>
61 *
62 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
63 */
64 public CdmDataSourceWizard(ICdmDataSource dataSource, Mode mode) {
65 super();
66 if(dataSource != null){
67 this.mode = mode;
68 this.dataSource = dataSource;
69 dataSourceName = dataSource.getName();
70 }
71 //setForcePreviousAndNextButtons(true);
72 setWindowTitle("Datasource Dialog");
73 }
74
75
76 /* (non-Javadoc)
77 * @see org.eclipse.jface.wizard.Wizard#addPages()
78 */
79 /** {@inheritDoc} */
80 @Override
81 public void addPages() {
82 switch(mode) {
83 case EDIT:
84 case CLONE:
85 if(dataSource.getDatabaseType() == DatabaseTypeEnum.H2){
86 dataSourcePage = new CdmDataSourceH2WizardPage(dataSource);
87 }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.MySQL){
88 dataSourcePage = new CdmDataSourceMySQLWizardPage(dataSource, mode);
89 }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.SqlServer2005){
90 dataSourcePage = new CdmDataSourceSQLServerWizardPage(dataSource);
91 }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.PostgreSQL){
92 dataSourcePage = new CdmDataSourcePostgreSQLServerWizardPage(dataSource);
93 }else{
94 throw new RuntimeException("Editing a datasource of type '" + dataSource.getDatabaseType() + "' is not supported yet.");
95 }
96
97 this.addPage(dataSourcePage);
98 case CREATE:
99 dataSourceSelectionPage = new CdmDataSourceTypeSelectionWizardPage(dataSource);
100 this.addPage(dataSourceSelectionPage);
101 default:
102
103 }
104
105 }
106
107 /* (non-Javadoc)
108 * @see org.eclipse.jface.wizard.Wizard#performFinish()
109 */
110 /** {@inheritDoc} */
111 @Override
112 public boolean performFinish() {
113 ICdmDataSource dataSource;
114 switch(mode) {
115 case EDIT:
116 dataSource = dataSourcePage.getUpdatedDataSource();
117 if(dataSourceName.equals(dataSource.getName())) {
118 CdmDataSourceRepository.update(dataSourceName, dataSource);
119 } else {
120 CdmDataSourceRepository.replace(dataSourceName, dataSource);
121 }
122 return true;
123 case CLONE:
124 dataSource = dataSourcePage.getUpdatedDataSource();
125 CdmDataSourceRepository.save(dataSourcePage.getDataSourceName(), dataSource);
126 return true;
127 case CREATE:
128 if(dataSourceSelectionPage.getCredentialsWizardPage() != null){
129 CdmDataSourceCredentialsWizardPage credentialsWizardPage = dataSourceSelectionPage.getCredentialsWizardPage();
130 CdmDataSourceRepository.save(dataSourceSelectionPage.getDataSourceName(), credentialsWizardPage.getUpdatedDataSource());
131 return true;
132 } else {
133 throw new IllegalStateException("Expected a datasource credentials page to exist");
134 }
135 default:
136 return false;
137 }
138
139 }
140
141 /* (non-Javadoc)
142 * @see org.eclipse.jface.wizard.Wizard#canFinish()
143 */
144 /** {@inheritDoc} */
145 @Override
146 public boolean canFinish() {
147 switch(mode) {
148 case EDIT:
149 case CLONE:
150 return dataSourcePage.isPageComplete();
151 case CREATE:
152 boolean result = true;
153 result &= dataSourceSelectionPage.isPageComplete();
154 if(dataSourceSelectionPage.getCredentialsWizardPage() != null){
155 result &= dataSourceSelectionPage.getCredentialsWizardPage().isPageComplete();
156 }
157 return result;
158 default:
159 return false;
160 }
161
162 }
163 }