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 / CdmDataSourceMySQLWizardPage.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.swt.SWT;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Group;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.swt.widgets.Text;
19
20 import eu.etaxonomy.cdm.database.CdmDataSource;
21 import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
22 import eu.etaxonomy.cdm.database.ICdmDataSource;
23
24 /**
25 * <p>CdmDataSourceMySQLWizardPage class.</p>
26 *
27 * @author n.hoffmann
28 * @created 19.05.2009
29 * @version 1.0
30 */
31 public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizardPage {
32
33 private Text text_port;
34 private Text text_server;
35
36 private String server;
37
38 private int port;
39
40
41
42 /**
43 * <p>Constructor for CdmDataSourceMySQLWizardPage.</p>
44 *
45 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
46 */
47 protected CdmDataSourceMySQLWizardPage(ICdmDataSource dataSource) {
48 super("MySQL", dataSource, CdmDataSourceWizard.Mode.CREATE);
49 setTitle("MySQL Server");
50 setDescription("Enter credentials for MySQL database");
51
52 }
53
54 /**
55 * <p>Constructor for CdmDataSourceMySQLWizardPage.</p>
56 *
57 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
58 */
59 protected CdmDataSourceMySQLWizardPage(ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) {
60 super("MySQL", dataSource, mode);
61 setTitle("MySQL Server");
62 setDescription("Enter credentials for MySQL database");
63
64 }
65
66 /* (non-Javadoc)
67 * @see eu.etaxonomy.taxeditor.store.datasource.CdmDataSourceCredentialsWizardPage#createDatabaseForm()
68 */
69 /** {@inheritDoc} */
70 @Override
71 public void createDatabaseForm() {
72 // Create group composite for location data
73 locationGroup = new Group(composite, SWT.NONE);
74 locationGroup.setText("Location");
75 locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 2));
76 GridLayout locationLayout = new GridLayout();
77 locationLayout.numColumns = 2;
78 locationGroup.setLayout(locationLayout);
79
80 // Create host label
81 Label serverLabel = new Label(locationGroup, SWT.NONE);
82 serverLabel.setText("Host:");
83
84 // Create host input
85 text_server = new Text(locationGroup, SWT.BORDER);
86 text_server.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
87
88
89 // Create port label
90 Label portLabel = new Label(locationGroup, SWT.NONE);
91 portLabel.setText("Port:");
92
93 // Create port input
94 text_port = new Text(locationGroup, SWT.BORDER);
95 text_port.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
96
97
98 }
99
100 /* (non-Javadoc)
101 * @see eu.etaxonomy.taxeditor.store.datasource.CdmDataSourceCredentialsWizardPage#updateLocation()
102 */
103 /** {@inheritDoc} */
104 @Override
105 public void updateLocation() {
106 server = text_server.getText();
107 try{
108 if(! text_port.getText().equals("")){
109 port = new Integer(text_port.getText());
110 setErrorMessage(null);
111 }
112 }catch(NumberFormatException e){
113 setErrorMessage("Port number contains invalid characters");
114 }
115 }
116
117 /* (non-Javadoc)
118 * @see eu.etaxonomy.taxeditor.datasource.wizard.CdmDataSourceCredentialsWizardPage#updateDataSource()
119 */
120 /** {@inheritDoc} */
121 @Override
122 public void updateDataSource() {
123 ICdmDataSource dataSource = getDataSource();
124
125 if(dataSource == null) {
126 setDataSource(CdmDataSource.NewMySqlInstance(server,
127 database,
128 port,
129 username,
130 password,
131 nomenclaturalCode));
132 } else {
133 dataSource.setName(name);
134 dataSource.setServer(server);
135 dataSource.setDatabase(database);
136 dataSource.setPort(port);
137 dataSource.setUsername(username);
138 dataSource.setPassword(password);
139 dataSource.setNomenclaturalCode(nomenclaturalCode);
140 }
141 }
142
143 /* (non-Javadoc)
144 * @see eu.etaxonomy.taxeditor.datasource.wizard.CdmDataSourceCredentialsWizardPage#checkPageComplete()
145 */
146 /** {@inheritDoc} */
147 @Override
148 public void checkPageComplete() {
149 // check if widgets of this component are complete
150 boolean complete = server.trim().length() != 0;
151
152 // set default port
153 if(port == 0){
154 port = DatabaseTypeEnum.MySQL.getDefaultPort();
155 }
156
157 setPageComplete(complete);
158
159 // check if widgets of the extended class are complete
160 super.checkPageComplete();
161 }
162
163 /* (non-Javadoc)
164 * @see eu.etaxonomy.taxeditor.datasource.wizard.CdmDataSourceCredentialsWizardPage#init()
165 */
166 /** {@inheritDoc} */
167 @Override
168 public void init() {
169 super.init();
170 if(getDataSource() != null){
171 removeListeners();
172 text_server.setText(getDataSource().getServer());
173 text_port.setText(String.valueOf(getDataSource().getPort()));
174 // add listeners after setting text to avoid the modify event being called
175 // for the initial value
176 addListeners();
177 }
178
179 }
180
181 private void addListeners() {
182 text_server.addModifyListener(this);
183 text_port.addModifyListener(this);
184 }
185
186 private void removeListeners() {
187 text_server.removeModifyListener(this);
188 text_port.removeModifyListener(this);
189 }
190 }