36a4077ffad4b4a03e240602ee7e37c234f575a6
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / BookmarkConnectionWizardPage.java
1 package com.quantum.wizards;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5
6 import com.quantum.Messages;
7 import com.quantum.model.JDBCDriver;
8
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;
21
22
23 class BookmarkConnectionWizardPage extends PropertyChangeWizardPage {
24         
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;
31     
32     private Label jdbcLabel;
33     private Text jdbcUrl;
34     private URLSetupControl urlSetupControl;
35     private Composite container;
36     
37     private PropertyChangeListener listener = new PropertyChangeListener() {
38                 public void propertyChange(PropertyChangeEvent event) {
39                         if ("connectionURL".equals(event.getPropertyName())) {
40                                 BookmarkConnectionWizardPage.this.setConnectionURL((String) event.getNewValue());
41                                 BookmarkConnectionWizardPage.this.updateButtonState();
42                         }
43                 }
44     }; 
45     
46     /**
47          * Constructor for BookmarkPage.
48          * @param pageName
49          */
50         public BookmarkConnectionWizardPage(String pageName) {
51                 super(pageName);
52                 setTitle(Messages.getString(getClass(), "title"));
53                 setDescription(Messages.getString(getClass(), "description"));
54         }
55         public void createControl(Composite parent) {
56                 setPageComplete(false);
57                 
58                 Composite container = new Composite(parent, SWT.NULL);
59                 GridLayout layout = new GridLayout();
60                 container.setLayout(layout);
61                 layout.numColumns = 2;
62                 layout.verticalSpacing = 9;
63
64                 Label label = new Label(container, SWT.NULL);
65                 label.setText(Messages.getString(getClass(), "userid")); //$NON-NLS-1$
66                 Text username = new Text(container, SWT.BORDER | SWT.SINGLE);
67                 
68                 
69                 GridData fullHorizontal = new GridData(GridData.FILL_HORIZONTAL);
70                 username.setLayoutData(fullHorizontal);
71                 username.addModifyListener(new ModifyListener() {
72                         public void modifyText(ModifyEvent event) {
73                                 String userid = ((Text) event.getSource()).getText();
74                                 setUserid(userid);
75                                 updateButtonState();
76                         }
77                 });
78
79                 label = new Label(container, SWT.NULL);
80                 label.setText(Messages.getString(getClass(), "password")); //$NON-NLS-1$
81                 final Text password = new Text(container, SWT.BORDER | SWT.SINGLE);
82                 password.setEchoChar('*');
83                 fullHorizontal = new GridData(GridData.FILL_HORIZONTAL);
84                 password.setLayoutData(fullHorizontal);
85                 password.addModifyListener(new ModifyListener() {
86                         public void modifyText(ModifyEvent event) {
87                                 String password = ((Text) event.getSource()).getText();
88                                 setPassword(password);
89                                 updateButtonState();
90                         }
91                 });
92
93         Button prompt = new Button(container, SWT.CHECK);
94                 prompt.setText(Messages.getString(getClass(), "prompt")); //$NON-NLS-1$
95                 fullHorizontal = new GridData(GridData.FILL_HORIZONTAL);
96                 fullHorizontal.horizontalSpan = 2;
97         prompt.setLayoutData(fullHorizontal);
98  
99                 createStandardJDBCWidgets(container);
100         prompt.addSelectionListener(new SelectionAdapter() {
101             public void widgetSelected(SelectionEvent event) {
102                 Button prompt = ((Button) event.getSource());
103                 password.setEditable(!prompt.getSelection());
104                 setPrompt(prompt.getSelection());
105                 updateButtonState();
106             }
107         });
108
109         this.container = container;
110                 setControl(container);
111         }
112         
113         /**
114          * @param container
115          */
116         private void createStandardJDBCWidgets(Composite container) {
117                 setConnectionURL("");
118                 
119                 this.jdbcLabel = new Label(container, SWT.NULL);
120                 this.jdbcLabel.setText(Messages.getString(getClass(), "url")); //$NON-NLS-1$
121                 
122                 this.jdbcUrl = new Text(container, SWT.BORDER | SWT.SINGLE);
123                 this.jdbcUrl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
124                 this.jdbcUrl.addModifyListener(new ModifyListener() {
125                         public void modifyText(ModifyEvent event) {
126                                 setConnectionURL(((Text) event.getSource()).getText());
127                                 updateButtonState();
128                         }
129                 });
130                 
131                 updateButtonState();
132         }
133         /**
134          * @return Returns the driver.
135          */
136         public JDBCDriver getDriver() {
137                 return this.driver;
138         }
139         /**
140          * @param driver The driver to set.
141          */
142         public void setDriver(JDBCDriver driver) {
143                 String oldDriverClassName = this.driver == null ? null : this.driver.getClassName();
144                 this.driver = driver;
145                 
146                 if (oldDriverClassName == null 
147                                 || !oldDriverClassName.equals(this.driver.getClassName())) {
148                         rebuildJDBCControls(this.driver);
149                 }
150         }
151         /**
152          * 
153          */
154         private void rebuildJDBCControls(JDBCDriver driver) {
155         Point windowSize = getShell().getSize();
156         Point oldSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
157                 
158                 if (URLSetupControlFactory.hasControl(driver)) {
159                         disposeOfCurrentJDBCControls();
160                         
161                         this.urlSetupControl = URLSetupControlFactory.create(driver, this.container);
162                         GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
163                         data.horizontalSpan = 2;
164                         this.urlSetupControl.setLayoutData(data);
165                         
166                         this.urlSetupControl.addPropertyChangeListener(this.listener);
167                         
168                         setConnectionURL(this.urlSetupControl.getConnectionURL());
169                         updateButtonState();
170
171                 resizeWindow(windowSize, oldSize);
172                         this.container.layout();
173                         
174                 } else if (this.jdbcLabel == null || this.jdbcUrl == null) {
175                         
176                         disposeOfCurrentJDBCControls();
177                         createStandardJDBCWidgets(this.container);
178
179                 resizeWindow(windowSize, oldSize);
180                 
181                         this.container.layout();
182                 }
183                 this.container.setVisible(true);
184                 this.container.redraw();
185         }
186         
187         /**
188          * @param windowSize
189          * @param oldSize
190          */
191         private void resizeWindow(Point windowSize, Point oldSize) {
192                 Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
193                 if (newSize.y > windowSize.y) {
194                         getShell().setSize(
195                             new Point(windowSize.x, windowSize.y + (newSize.y - oldSize.y)));
196                 }
197         }
198         private void disposeOfCurrentJDBCControls() {
199                 if (this.jdbcUrl != null) {
200                         this.jdbcUrl.dispose();
201                         this.jdbcUrl = null;
202                 }
203                 if (this.jdbcLabel != null) {
204                         this.jdbcLabel.dispose();
205                         this.jdbcLabel = null;
206                 }
207                 if (this.urlSetupControl != null) {
208                         this.urlSetupControl.removePropertyChangeListener(this.listener);
209                         this.urlSetupControl.dispose();
210                         this.urlSetupControl = null;
211                 }
212         }
213         /**
214          * 
215          */
216         private void updateButtonState() {
217                 boolean complete = true;
218                 complete &= (this.connectionURL != null 
219                                 && this.connectionURL.trim().length() > 0);
220                 complete &= (this.userid != null 
221                                 && this.userid.trim().length() > 0);
222                 setPageComplete(complete);
223         }
224         /**
225          * @return Returns the userid.
226          */
227         public String getUserid() {
228                 return this.userid;
229         }
230         /**
231          * @param userid The userid to set.
232          */
233         public void setUserid(String userid) {
234                 if (userid != null && !userid.equals(this.userid)) {
235                         String original = this.userid;
236                         this.userid = userid;
237                         firePropertyChange("userid", original, userid);
238                 }
239         }
240         /**
241          * @return Returns the prompt.
242          */
243         public boolean isPrompt() {
244                 return this.prompt;
245         }
246         /**
247          * @param prompt The prompt to set.
248          */
249         public void setPrompt(boolean prompt) {
250                 if (this.prompt != prompt) {
251                         boolean original = this.prompt;
252                         this.prompt = prompt;
253                         firePropertyChange("prompt", original, prompt);
254                 }
255         }
256         /**
257          * @return Returns the connectionURL.
258          */
259         public String getConnectionURL() {
260                 return this.connectionURL;
261         }
262         /**
263          * @param connectionURL The connectionURL to set.
264          */
265         public void setConnectionURL(String connectionURL) {
266                 if (connectionURL != null && !connectionURL.equals(this.connectionURL)) {
267                         String original = this.connectionURL;
268                         this.connectionURL = connectionURL;
269                         firePropertyChange("connectionURL", original, connectionURL);
270                 }
271         }
272         /**
273          * @return Returns the password.
274          */
275         public String getPassword() {
276                 return this.password;
277         }
278         /**
279          * @param password The password to set.
280          */
281         public void setPassword(String password) {
282                 if (password != null && !password.equals(this.password)) {
283                         String original = this.password;
284                         this.password = password;
285                         firePropertyChange("password", original, password);
286                 }
287         }
288 }