refactory whole plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / URLSetupControl.java
1 package com.quantum.wizards;
2
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.Map;
9
10 import com.quantum.Messages;
11 import com.quantum.adapters.AdapterFactory;
12 import com.quantum.adapters.DatabaseAdapter;
13 import com.quantum.model.JDBCDriver;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Group;
20
21
22 /**
23  * @author BC
24  */
25 public abstract class URLSetupControl extends Composite {
26         
27         private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
28         private String urlPattern;
29         private Map properties = Collections.synchronizedMap(new HashMap());
30         private final JDBCDriver driver;
31         private String connectionURL;
32
33         /**
34          * @param parent
35          * @param style
36          */
37         public URLSetupControl(Composite parent, JDBCDriver driver) {
38                 super(parent, SWT.NONE);
39                 this.driver = driver;
40                 GridLayout layout = new GridLayout();
41                 setLayout(layout);
42                 this.urlPattern = AdapterFactory.getInstance().getURLPattern(driver.getClassName());
43                 
44                 setPropertyDefaults();
45                 setConnectionURL(URLBuilder.createURL(this.urlPattern, getProperties()));
46         }
47
48         /**
49          * 
50          */
51         protected void createPart() {
52                 Group group = new Group(this, SWT.SHADOW_ETCHED_IN);
53                 group.setText(Messages.getString(URLSetupControl.class, "text"));
54                 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
55                 
56                 createPart(group);
57         }
58
59         /**
60          * 
61          */
62         private void setPropertyDefaults() {
63                 DatabaseAdapter adapter = 
64                         AdapterFactory.getInstance().getAdapter(getDriver().getType());
65                 Map properties = (adapter == null) ? new HashMap() : adapter.getDefaultConnectionParameters();
66                 for (Iterator i = properties.keySet().iterator(); i.hasNext();) {
67                         String key = (String) i.next();
68                         putProperty(key, (String) properties.get(key));
69                         
70                 }
71         }
72
73         protected abstract void createPart(Composite parent);
74         
75         private Map getProperties() {
76                 return this.properties;
77         }
78
79         protected String getProperty(String name) {
80                 String value = (String) this.properties.get(name);
81                 return value == null ? "" : value;
82         }
83
84         protected void putProperty(String name, String value) {
85                 this.properties.put(name, value);
86                 setConnectionURL(URLBuilder.createURL(this.urlPattern, getProperties()));
87         }
88         
89         public void addPropertyChangeListener(PropertyChangeListener listener) {
90                 this.propertyChangeSupport.addPropertyChangeListener(listener);
91         }
92         public void removePropertyChangeListener(PropertyChangeListener listener) {
93                 this.propertyChangeSupport.removePropertyChangeListener(listener);
94         }
95         protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
96                 this.propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
97         }
98         
99         protected JDBCDriver getDriver() {
100                 return this.driver;
101         }
102         public String getConnectionURL() {
103                 return this.connectionURL == null ? "" : this.connectionURL;
104         }
105         public void setConnectionURL(String connectionURL) {
106                 if (connectionURL != null && !connectionURL.equals(this.connectionURL)) {
107                         String original = this.connectionURL;
108                         this.connectionURL = connectionURL;
109                         firePropertyChange("connectionURL", original, connectionURL);
110                 }
111         }
112 }