*** empty log message ***
[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     private boolean requiresRebuild = false;
37     
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();
43                         }
44                 }
45     }; 
46     
47     /**
48          * Constructor for BookmarkPage.
49          * @param pageName
50          */
51         public BookmarkConnectionWizardPage(String pageName) {
52                 super(pageName);
53                 setTitle(Messages.getString(getClass(), "title"));
54                 setDescription(Messages.getString(getClass(), "description"));
55         }
56         public void createControl(Composite parent) {
57                 setPageComplete(false);
58                 
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;
64
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);
68                 
69                 
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();
75                                 setUserid(userid);
76                                 updateButtonState();
77                         }
78                 });
79
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);
90                                 updateButtonState();
91                         }
92                 });
93
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);
99  
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());
106                 updateButtonState();
107             }
108         });
109
110         this.container = container;
111                 setControl(container);
112         }
113         
114         public void setVisible(boolean visible) {
115                 if (visible && this.requiresRebuild) {
116                         rebuildJDBCControls(this.driver);
117                 }
118                 super.setVisible(visible);
119         }
120         
121         /**
122          * @param container
123          */
124         private void createStandardJDBCWidgets(Composite container) {
125                 setConnectionURL("");
126                 
127                 this.jdbcLabel = new Label(container, SWT.NULL);
128                 this.jdbcLabel.setText(Messages.getString(getClass(), "url")); //$NON-NLS-1$
129                 
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());
135                                 updateButtonState();
136                         }
137                 });
138                 
139                 updateButtonState();
140         }
141         /**
142          * @return Returns the driver.
143          */
144         public JDBCDriver getDriver() {
145                 return this.driver;
146         }
147         /**
148          * @param driver The driver to set.
149          */
150         public void setDriver(JDBCDriver driver) {
151                 String oldDriverClassName = this.driver == null ? null : this.driver.getClassName();
152                 this.driver = driver;
153                 
154                 if (oldDriverClassName == null 
155                                 || !oldDriverClassName.equals(this.driver.getClassName())) {
156                         this.requiresRebuild = true;
157                 }
158         }
159         /**
160          * 
161          */
162         private void rebuildJDBCControls(JDBCDriver driver) {
163         Point windowSize = getShell().getSize();
164         Point oldSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
165                 
166                 if (URLSetupControlFactory.hasControl(driver)) {
167                         disposeOfCurrentJDBCControls();
168                         
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);
173                         
174                         this.urlSetupControl.addPropertyChangeListener(this.listener);
175                         
176                         setConnectionURL(this.urlSetupControl.getConnectionURL());
177                         updateButtonState();
178
179                 resizeWindow(windowSize, oldSize);
180                         this.container.layout();
181                         
182                 } else if (this.jdbcLabel == null || this.jdbcUrl == null) {
183                         
184                         disposeOfCurrentJDBCControls();
185                         createStandardJDBCWidgets(this.container);
186
187                 resizeWindow(windowSize, oldSize);
188                 
189                         this.container.layout();
190                 }
191                 this.container.setVisible(true);
192                 this.container.redraw();
193         }
194         
195         /**
196          * @param windowSize
197          * @param oldSize
198          */
199         private void resizeWindow(Point windowSize, Point oldSize) {
200                 Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
201                 if (newSize.y > windowSize.y) {
202                         getShell().setSize(
203                             new Point(windowSize.x, windowSize.y + (newSize.y - oldSize.y)));
204                 }
205         }
206         private void disposeOfCurrentJDBCControls() {
207                 if (this.jdbcUrl != null) {
208                         this.jdbcUrl.dispose();
209                         this.jdbcUrl = null;
210                 }
211                 if (this.jdbcLabel != null) {
212                         this.jdbcLabel.dispose();
213                         this.jdbcLabel = null;
214                 }
215                 if (this.urlSetupControl != null) {
216                         this.urlSetupControl.removePropertyChangeListener(this.listener);
217                         this.urlSetupControl.dispose();
218                         this.urlSetupControl = null;
219                 }
220         }
221         /**
222          * 
223          */
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);
232         }
233         /**
234          * @return Returns the userid.
235          */
236         public String getUserid() {
237                 return this.userid;
238         }
239         /**
240          * @param userid The userid to set.
241          */
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);
247                 }
248         }
249         /**
250          * @return Returns the prompt.
251          */
252         public boolean isPrompt() {
253                 return this.prompt;
254         }
255         /**
256          * @param prompt The prompt to set.
257          */
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);
263                 }
264         }
265         /**
266          * @return Returns the connectionURL.
267          */
268         public String getConnectionURL() {
269                 return this.connectionURL;
270         }
271         /**
272          * @param connectionURL The connectionURL to set.
273          */
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);
279                 }
280         }
281         /**
282          * @return Returns the password.
283          */
284         public String getPassword() {
285                 return this.password;
286         }
287         /**
288          * @param password The password to set.
289          */
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);
295                 }
296         }
297 }