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