svn-gvsig-desktop / trunk / extensions / extOracleSpatial / src / es / prodevelop / cit / gvsig / jdbc_spatial / gui / jdbcwizard / NewOracleSpatialTableWizard.java @ 18370
History | View | Annotate | Download (8.21 KB)
1 |
package es.prodevelop.cit.gvsig.jdbc_spatial.gui.jdbcwizard; |
---|---|
2 |
|
3 |
|
4 |
import java.awt.Component; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.awt.event.ItemEvent; |
8 |
import java.awt.event.ItemListener; |
9 |
import java.beans.PropertyChangeEvent; |
10 |
import java.beans.PropertyChangeListener; |
11 |
|
12 |
import javax.swing.ImageIcon; |
13 |
import javax.swing.JButton; |
14 |
import javax.swing.JComboBox; |
15 |
import javax.swing.JLabel; |
16 |
import javax.swing.JOptionPane; |
17 |
|
18 |
|
19 |
import org.apache.log4j.Logger; |
20 |
|
21 |
|
22 |
import com.iver.andami.PluginServices; |
23 |
import com.iver.cit.gvsig.SingleVectorialDBConnectionExtension; |
24 |
import com.iver.cit.gvsig.fmap.drivers.DBException; |
25 |
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams; |
26 |
import com.iver.cit.gvsig.fmap.drivers.db.utils.SingleVectorialDBConnectionManager; |
27 |
import com.iver.utiles.swing.JPasswordDlg; |
28 |
import com.prodevelop.cit.gvsig.vectorialdb.wizard.VectorialDBConnectionParamsDialog; |
29 |
|
30 |
|
31 |
import jwizardcomponent.JWizardComponents; |
32 |
import jwizardcomponent.JWizardPanel; |
33 |
|
34 |
|
35 |
public class NewOracleSpatialTableWizard extends JWizardPanel implements ItemListener, PropertyChangeListener, ActionListener { |
36 |
|
37 |
private static Logger logger = Logger.getLogger(NewOracleSpatialTableWizard.class.getName()); |
38 |
|
39 |
|
40 |
public NewOracleSpatialTableWizard(JWizardComponents wizardComponents) {
|
41 |
super(wizardComponents);
|
42 |
wizardComponents.addPropertyChangeListener(this);
|
43 |
initialize(); |
44 |
} |
45 |
|
46 |
private JLabel chooseConnLabel = null; |
47 |
private JComboBox datasourceComboBox; |
48 |
private ConnectionWithParams theConnWithParams = null; |
49 |
private JButton dbButton; |
50 |
|
51 |
/**
|
52 |
* This method initializes this
|
53 |
*
|
54 |
*/
|
55 |
private void initialize() { |
56 |
|
57 |
datasourceComboBox = getDatasourceComboBox(); |
58 |
loadVectorialDBDatasourcesCombo(); |
59 |
|
60 |
chooseConnLabel = new JLabel(); |
61 |
chooseConnLabel.setText(PluginServices.getText(this, "choose_connection")); |
62 |
chooseConnLabel.setBounds(new java.awt.Rectangle(14,9,300,21)); |
63 |
|
64 |
dbButton = getJdbcButton(); |
65 |
|
66 |
this.setLayout(null); |
67 |
this.setSize(new java.awt.Dimension(358,263)); |
68 |
this.add(chooseConnLabel, null); |
69 |
this.add(datasourceComboBox, null); |
70 |
this.add(dbButton, null); |
71 |
getWizardComponents().getFinishButton().setEnabled(false);
|
72 |
} |
73 |
|
74 |
public ConnectionWithParams getConnectionWithParams() {
|
75 |
return theConnWithParams;
|
76 |
} |
77 |
|
78 |
private JComboBox getDatasourceComboBox() { |
79 |
if (datasourceComboBox == null) { |
80 |
datasourceComboBox = new JComboBox(); |
81 |
datasourceComboBox.setBounds(new java.awt.Rectangle(14,32,300,22)); |
82 |
datasourceComboBox.addItemListener(this);
|
83 |
} |
84 |
|
85 |
return datasourceComboBox;
|
86 |
} |
87 |
|
88 |
private void loadVectorialDBDatasourcesCombo() { |
89 |
|
90 |
getDatasourceComboBox().removeAllItems(); |
91 |
getDatasourceComboBox().addItem(new ConnectionWithParams());
|
92 |
ConnectionWithParams[] conn =
|
93 |
SingleVectorialDBConnectionManager.instance().getAllConnections(); |
94 |
|
95 |
if (conn == null) { |
96 |
return;
|
97 |
} |
98 |
|
99 |
for (int i = 0; i < conn.length; i++) { |
100 |
getDatasourceComboBox().addItem(conn[i]); |
101 |
} |
102 |
} |
103 |
|
104 |
public void itemStateChanged(ItemEvent e) { |
105 |
|
106 |
Object src = e.getSource();
|
107 |
if (src == datasourceComboBox) {
|
108 |
|
109 |
Object sel_obj = datasourceComboBox.getSelectedItem();
|
110 |
|
111 |
if (sel_obj == null) { |
112 |
getWizardComponents().getFinishButton().setEnabled(false);
|
113 |
theConnWithParams = null;
|
114 |
return;
|
115 |
} |
116 |
|
117 |
if (!(sel_obj instanceof ConnectionWithParams)) { |
118 |
getWizardComponents().getFinishButton().setEnabled(false);
|
119 |
theConnWithParams = null;
|
120 |
return;
|
121 |
} |
122 |
|
123 |
ConnectionWithParams cwp = (ConnectionWithParams) sel_obj; |
124 |
|
125 |
if (cwp.isNull()) {
|
126 |
getWizardComponents().getFinishButton().setEnabled(false);
|
127 |
theConnWithParams = null;
|
128 |
return;
|
129 |
} |
130 |
|
131 |
if (!cwp.isConnected()) {
|
132 |
if (!tryToConnect(cwp)) {
|
133 |
getWizardComponents().getFinishButton().setEnabled(false);
|
134 |
theConnWithParams = null;
|
135 |
datasourceComboBox.setSelectedIndex(0);
|
136 |
return;
|
137 |
} |
138 |
} |
139 |
|
140 |
theConnWithParams = cwp; |
141 |
getWizardComponents().getFinishButton().setEnabled(true);
|
142 |
datasourceComboBox.repaint(); |
143 |
} |
144 |
} |
145 |
|
146 |
private boolean tryToConnect(ConnectionWithParams _cwp) { |
147 |
JPasswordDlg dlg = new JPasswordDlg();
|
148 |
dlg.setLocationRelativeTo((Component)PluginServices.getMainFrame());
|
149 |
String strMessage = PluginServices.getText(this, "conectar_jdbc"); |
150 |
String strPassword = PluginServices.getText(this, "password"); |
151 |
dlg.setMessage(strMessage + " [" + _cwp.getDrvName() + ", " + |
152 |
_cwp.getHost() + ", " + _cwp.getPort() + ", " + _cwp.getDb() + |
153 |
", " + _cwp.getUser() + "]. " + strPassword + "?"); |
154 |
|
155 |
dlg.setVisible(true);
|
156 |
|
157 |
String clave = dlg.getPassword();
|
158 |
|
159 |
if (clave == null) { |
160 |
return false; |
161 |
} |
162 |
|
163 |
try {
|
164 |
_cwp.connect(clave); |
165 |
} |
166 |
catch (DBException e) {
|
167 |
showConnectionErrorMessage(e.getMessage()); |
168 |
|
169 |
return false; |
170 |
} |
171 |
|
172 |
return true; |
173 |
} |
174 |
|
175 |
private void showConnectionErrorMessage(String _msg) { |
176 |
String msg = (_msg.length() > 300) ? "" : (": " + _msg); |
177 |
String title = PluginServices.getText(this, "connection_error"); |
178 |
JOptionPane.showMessageDialog(this, title + msg, title, |
179 |
JOptionPane.ERROR_MESSAGE);
|
180 |
} |
181 |
|
182 |
public void propertyChange(PropertyChangeEvent evt) { |
183 |
|
184 |
if (evt.getPropertyName().compareToIgnoreCase(JWizardComponents.CURRENT_PANEL_PROPERTY) == 0) { |
185 |
|
186 |
if (evt.getNewValue() == this) { |
187 |
boolean valid_conn = !(theConnWithParams == null); |
188 |
getWizardComponents().getFinishButton().setEnabled(valid_conn); |
189 |
} |
190 |
|
191 |
} |
192 |
|
193 |
} |
194 |
|
195 |
|
196 |
private ConnectionWithParams addNewConnection() {
|
197 |
ConnectionWithParams resp = null;
|
198 |
|
199 |
VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
|
200 |
newco.showDialog(); |
201 |
|
202 |
if (newco.isOkPressed()) {
|
203 |
String _drvname = newco.getConnectionDriverName();
|
204 |
String _host = newco.getConnectionServerUrl();
|
205 |
String _port = newco.getConnectionPort();
|
206 |
String _dbname = newco.getConnectionDBName();
|
207 |
String _user = newco.getConnectionUser();
|
208 |
String _pw = newco.getConnectionPassword();
|
209 |
String _conn_usr_name = newco.getConnectionName();
|
210 |
|
211 |
boolean hasToBeCon = newco.hasToBeConnected();
|
212 |
|
213 |
try {
|
214 |
resp = SingleVectorialDBConnectionManager.instance() |
215 |
.getConnection(_drvname, |
216 |
_user, _pw, _conn_usr_name, _host, _port, _dbname, |
217 |
hasToBeCon); |
218 |
} |
219 |
catch (DBException e) {
|
220 |
showConnectionErrorMessage(e.getMessage()); |
221 |
|
222 |
return null; |
223 |
} |
224 |
SingleVectorialDBConnectionExtension.saveAllToPersistence(); |
225 |
return resp;
|
226 |
} |
227 |
else {
|
228 |
return null; |
229 |
} |
230 |
} |
231 |
|
232 |
/**
|
233 |
* This method initializes jdbcButton
|
234 |
*
|
235 |
* @return javax.swing.JButton
|
236 |
*/
|
237 |
private JButton getJdbcButton() { |
238 |
if (dbButton == null) { |
239 |
dbButton = new JButton(); |
240 |
dbButton.addActionListener(this);
|
241 |
dbButton.setToolTipText(PluginServices.getText(this,
|
242 |
"add_connection"));
|
243 |
dbButton.setBounds(new java.awt.Rectangle(320, 32, 26, 21)); |
244 |
|
245 |
String _file = createResourceUrl("images/jdbc.png").getFile(); |
246 |
dbButton.setIcon(new ImageIcon(_file)); |
247 |
} |
248 |
|
249 |
return dbButton;
|
250 |
} |
251 |
|
252 |
private java.net.URL createResourceUrl(String path) { |
253 |
return getClass().getClassLoader().getResource(path);
|
254 |
} |
255 |
|
256 |
public void actionPerformed(ActionEvent e) { |
257 |
|
258 |
Object src = e.getSource();
|
259 |
|
260 |
if (src == dbButton) {
|
261 |
ConnectionWithParams sel = addNewConnection(); |
262 |
|
263 |
if (sel != null) { |
264 |
loadVectorialDBDatasourcesCombo(); |
265 |
getDatasourceComboBox().setSelectedItem(sel); |
266 |
} |
267 |
} |
268 |
|
269 |
} |
270 |
|
271 |
} |