1 package com.quantum.wizards;
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
6 import com.quantum.Messages;
7 import com.quantum.model.JDBCDriver;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.ModifyEvent;
11 import org.eclipse.swt.events.ModifyListener;
12 import org.eclipse.swt.events.SelectionAdapter;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.graphics.Point;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Label;
20 import org.eclipse.swt.widgets.Text;
23 class BookmarkConnectionWizardPage extends PropertyChangeWizardPage {
25 /* use this to paint a more helpful UI for the JDBC URL */
26 private JDBCDriver driver;
27 private String userid;
28 private String password;
29 private String connectionURL;
30 private boolean prompt;
32 private Label jdbcLabel;
34 private URLSetupControl urlSetupControl;
35 private Composite container;
36 private boolean requiresRebuild = false;
38 private PropertyChangeListener listener = new PropertyChangeListener() {
39 public void propertyChange(PropertyChangeEvent event) {
40 if ("connectionURL".equals(event.getPropertyName())) {
41 BookmarkConnectionWizardPage.this.setConnectionURL((String) event.getNewValue());
42 BookmarkConnectionWizardPage.this.updateButtonState();
48 * Constructor for BookmarkPage.
51 public BookmarkConnectionWizardPage(String pageName) {
53 setTitle(Messages.getString(getClass(), "title"));
54 setDescription(Messages.getString(getClass(), "description"));
56 public void createControl(Composite parent) {
57 setPageComplete(false);
59 Composite container = new Composite(parent, SWT.NULL);
60 GridLayout layout = new GridLayout();
61 container.setLayout(layout);
62 layout.numColumns = 2;
63 layout.verticalSpacing = 9;
65 Label label = new Label(container, SWT.NULL);
66 label.setText(Messages.getString(getClass(), "userid")); //$NON-NLS-1$
67 Text username = new Text(container, SWT.BORDER | SWT.SINGLE);
70 GridData fullHorizontal = new GridData(GridData.FILL_HORIZONTAL);
71 username.setLayoutData(fullHorizontal);
72 username.addModifyListener(new ModifyListener() {
73 public void modifyText(ModifyEvent event) {
74 String userid = ((Text) event.getSource()).getText();
80 label = new Label(container, SWT.NULL);
81 label.setText(Messages.getString(getClass(), "password")); //$NON-NLS-1$
82 final Text password = new Text(container, SWT.BORDER | SWT.SINGLE);
83 password.setEchoChar('*');
84 fullHorizontal = new GridData(GridData.FILL_HORIZONTAL);
85 password.setLayoutData(fullHorizontal);
86 password.addModifyListener(new ModifyListener() {
87 public void modifyText(ModifyEvent event) {
88 String password = ((Text) event.getSource()).getText();
89 setPassword(password);
94 Button prompt = new Button(container, SWT.CHECK);
95 prompt.setText(Messages.getString(getClass(), "prompt")); //$NON-NLS-1$
96 fullHorizontal = new GridData(GridData.FILL_HORIZONTAL);
97 fullHorizontal.horizontalSpan = 2;
98 prompt.setLayoutData(fullHorizontal);
100 createStandardJDBCWidgets(container);
101 prompt.addSelectionListener(new SelectionAdapter() {
102 public void widgetSelected(SelectionEvent event) {
103 Button prompt = ((Button) event.getSource());
104 password.setEditable(!prompt.getSelection());
105 setPrompt(prompt.getSelection());
110 this.container = container;
111 setControl(container);
114 public void setVisible(boolean visible) {
115 if (visible && this.requiresRebuild) {
116 rebuildJDBCControls(this.driver);
118 super.setVisible(visible);
124 private void createStandardJDBCWidgets(Composite container) {
125 setConnectionURL("");
127 this.jdbcLabel = new Label(container, SWT.NULL);
128 this.jdbcLabel.setText(Messages.getString(getClass(), "url")); //$NON-NLS-1$
130 this.jdbcUrl = new Text(container, SWT.BORDER | SWT.SINGLE);
131 this.jdbcUrl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
132 this.jdbcUrl.addModifyListener(new ModifyListener() {
133 public void modifyText(ModifyEvent event) {
134 setConnectionURL(((Text) event.getSource()).getText());
142 * @return Returns the driver.
144 public JDBCDriver getDriver() {
148 * @param driver The driver to set.
150 public void setDriver(JDBCDriver driver) {
151 String oldDriverClassName = this.driver == null ? null : this.driver.getClassName();
152 this.driver = driver;
154 if (oldDriverClassName == null
155 || !oldDriverClassName.equals(this.driver.getClassName())) {
156 this.requiresRebuild = true;
162 private void rebuildJDBCControls(JDBCDriver driver) {
163 Point windowSize = getShell().getSize();
164 Point oldSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
166 if (URLSetupControlFactory.hasControl(driver)) {
167 disposeOfCurrentJDBCControls();
169 this.urlSetupControl = URLSetupControlFactory.create(driver, this.container);
170 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
171 data.horizontalSpan = 2;
172 this.urlSetupControl.setLayoutData(data);
174 this.urlSetupControl.addPropertyChangeListener(this.listener);
176 setConnectionURL(this.urlSetupControl.getConnectionURL());
179 resizeWindow(windowSize, oldSize);
180 this.container.layout();
182 } else if (this.jdbcLabel == null || this.jdbcUrl == null) {
184 disposeOfCurrentJDBCControls();
185 createStandardJDBCWidgets(this.container);
187 resizeWindow(windowSize, oldSize);
189 this.container.layout();
191 this.container.setVisible(true);
192 this.container.redraw();
199 private void resizeWindow(Point windowSize, Point oldSize) {
200 Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
201 if (newSize.y > windowSize.y) {
203 new Point(windowSize.x, windowSize.y + (newSize.y - oldSize.y)));
206 private void disposeOfCurrentJDBCControls() {
207 if (this.jdbcUrl != null) {
208 this.jdbcUrl.dispose();
211 if (this.jdbcLabel != null) {
212 this.jdbcLabel.dispose();
213 this.jdbcLabel = null;
215 if (this.urlSetupControl != null) {
216 this.urlSetupControl.removePropertyChangeListener(this.listener);
217 this.urlSetupControl.dispose();
218 this.urlSetupControl = null;
224 private void updateButtonState() {
225 boolean complete = true;
226 complete &= (this.connectionURL != null
227 && this.connectionURL.trim().length() > 0);
228 // Some databases don't use user id
229 //complete &= (this.userid != null
230 // && this.userid.trim().length() > 0);
231 setPageComplete(complete);
234 * @return Returns the userid.
236 public String getUserid() {
240 * @param userid The userid to set.
242 public void setUserid(String userid) {
243 if (userid != null && !userid.equals(this.userid)) {
244 String original = this.userid;
245 this.userid = userid;
246 firePropertyChange("userid", original, userid);
250 * @return Returns the prompt.
252 public boolean isPrompt() {
256 * @param prompt The prompt to set.
258 public void setPrompt(boolean prompt) {
259 if (this.prompt != prompt) {
260 boolean original = this.prompt;
261 this.prompt = prompt;
262 firePropertyChange("prompt", original, prompt);
266 * @return Returns the connectionURL.
268 public String getConnectionURL() {
269 return this.connectionURL;
272 * @param connectionURL The connectionURL to set.
274 public void setConnectionURL(String connectionURL) {
275 if (connectionURL != null && !connectionURL.equals(this.connectionURL)) {
276 String original = this.connectionURL;
277 this.connectionURL = connectionURL;
278 firePropertyChange("connectionURL", original, connectionURL);
282 * @return Returns the password.
284 public String getPassword() {
285 return this.password;
288 * @param password The password to set.
290 public void setPassword(String password) {
291 if (password != null && !password.equals(this.password)) {
292 String original = this.password;
293 this.password = password;
294 firePropertyChange("password", original, password);