Moving editor sources back into trunk
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / store / datasource / wizard / CdmDataSourceCredentialsWizardPage.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.store.datasource.wizard;
12
13 import org.eclipse.jface.wizard.WizardPage;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.KeyEvent;
16 import org.eclipse.swt.events.KeyListener;
17 import org.eclipse.swt.events.MouseAdapter;
18 import org.eclipse.swt.events.MouseEvent;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Group;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Text;
26
27 import eu.etaxonomy.cdm.database.ICdmDataSource;
28
29
30 /**
31 * @author n.hoffmann
32 * @created 19.05.2009
33 * @version 1.0
34 */
35 public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage implements KeyListener{
36
37 protected ICdmDataSource dataSource;
38
39
40 protected Text passwordText;
41 protected Text databaseNameText;
42 protected Text usernameText;
43
44 protected Group authenticationGroup;
45 protected Group locationGroup;
46
47 protected Composite composite;
48
49 protected Composite parent;
50
51 /**
52 * @param pageName
53 */
54 protected CdmDataSourceCredentialsWizardPage(String pageName) {
55 super(pageName);
56 }
57
58 /* (non-Javadoc)
59 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
60 */
61 public void createControl(Composite parent) {
62 this.parent = parent;
63
64 // Create top-level composite
65
66 composite = new Composite(parent, SWT.NONE);
67 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
68 GridLayout formLayout = new GridLayout();
69 formLayout.numColumns = 2;
70 composite.setLayout(formLayout);
71
72 // create a database specific form
73 createDatabaseForm();
74
75 // create the authentication input fields
76 createAuthenticationForm();
77
78 // Create composite for buttons
79 Composite buttonComposite = new Composite(composite, SWT.NONE);
80 buttonComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
81 GridLayout buttonLayout = new GridLayout();
82 buttonLayout.numColumns = 2;
83 buttonComposite.setLayout(buttonLayout);
84
85 // Create test connection button
86 Button testButton = new Button(buttonComposite, SWT.NONE);
87 testButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
88 testButton.setText("Test connection");
89
90 // Test connection when button is pressed
91 testButton.addMouseListener(new MouseAdapter() {
92 public void mouseUp(MouseEvent e) {
93 testDbConfiguration();
94 }
95 });
96
97 setControl(composite);
98 }
99
100
101 protected void createAuthenticationForm(){
102 // Create group composite for authentication data
103 authenticationGroup = new Group(composite, SWT.NONE);
104 authenticationGroup.setText("Authentication");
105 authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
106 GridLayout authenticationLayout = new GridLayout();
107 authenticationLayout.numColumns = 2;
108 authenticationGroup.setLayout(authenticationLayout);
109
110 // Create database name label
111 Label databaseNameLabel = new Label(authenticationGroup, SWT.NONE);
112 databaseNameLabel.setText("Database Name:");
113
114 // Create database name input
115 databaseNameText = new Text(authenticationGroup, SWT.BORDER);
116 databaseNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
117 databaseNameText.addKeyListener(this);
118
119 // Create username label
120 Label usernameLabel = new Label(authenticationGroup, SWT.NONE);
121 usernameLabel.setText("User Name:");
122
123 // Create username input
124 usernameText = new Text(authenticationGroup, SWT.BORDER);
125 usernameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
126 usernameText.addKeyListener(this);
127
128 // Create password label
129 Label passwordLabel = new Label(authenticationGroup, SWT.NONE);
130 passwordLabel.setText("Password:");
131
132 // Create password input
133 passwordText = new Text(authenticationGroup, SWT.BORDER);
134 passwordText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
135 passwordText.addKeyListener(this);
136
137
138 if(dataSource != null){
139 // initialize text fields
140 databaseNameText.setText(getWizard().getDatabase());
141 usernameText.setText(getWizard().getUsername());
142 passwordText.setText(getWizard().getPassword());
143 }
144 }
145
146 /**
147 * concrete implementation of the test routine for the specific database
148 */
149 public abstract void testDbConfiguration();
150
151 /**
152 * Form implementation for the specific database
153 */
154 public abstract void createDatabaseForm();
155
156 public abstract void updateLocation();
157
158 /**
159 * updates the current datasource with form values
160 */
161 public void updateAuthentication(){
162 getWizard().setDatabase(databaseNameText.getText());
163 getWizard().setUsername(usernameText.getText());
164 getWizard().setPassword(passwordText.getText());
165 }
166
167 public CdmDataSourceWizard getWizard(){
168 return (CdmDataSourceWizard) super.getWizard();
169 }
170
171 /* (non-Javadoc)
172 * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
173 */
174 public void keyReleased(KeyEvent e) {
175 updateLocation();
176 updateAuthentication();
177 }
178
179 public void keyPressed(KeyEvent e){}
180
181 }