Statistics
| Revision:

root / branches / gvSIG_1.11.0_Mejoras_gvSIG-EIEL / applications / appgvSIG / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / NewVectorDBConnectionPanel.java @ 34767

History | View | Annotate | Download (11.3 KB)

1 34767 jldominguez
package com.prodevelop.cit.gvsig.vectorialdb.wizard;
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.awt.event.KeyEvent;
10
import java.awt.event.KeyListener;
11
import java.beans.PropertyChangeEvent;
12
import java.beans.PropertyChangeListener;
13
14
import javax.swing.ImageIcon;
15
import javax.swing.JButton;
16
import javax.swing.JComboBox;
17
import javax.swing.JLabel;
18
import javax.swing.JOptionPane;
19
import javax.swing.JTextField;
20
21
22
import org.apache.log4j.Logger;
23
24
25
import com.iver.andami.PluginServices;
26
import com.iver.cit.gvsig.SingleVectorialDBConnectionExtension;
27
import com.iver.cit.gvsig.fmap.drivers.DBException;
28
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams;
29
import com.iver.cit.gvsig.fmap.drivers.db.utils.SingleDBConnectionManager;
30
import com.iver.utiles.swing.JPasswordDlg;
31
import com.prodevelop.cit.gvsig.vectorialdb.wizard.DBConnectionParamsDialog;
32
33
// import es.prodevelop.cit.gvsig.fmap.drivers.jdbc.oracle.OracleSpatialDriver;
34
35
36
import jwizardcomponent.JWizardComponents;
37
import jwizardcomponent.JWizardPanel;
38
39
40
public class NewVectorDBConnectionPanel extends JWizardPanel
41
implements ItemListener, PropertyChangeListener, ActionListener, KeyListener {
42
43
44
        private static int NEW_TABLE_NAME_INDEX = 0;
45
        private static Logger logger = Logger.getLogger(NewVectorDBConnectionPanel.class.getName());
46
47
        private String drvName = "";
48
        private int idMaxLen = 10;
49
50
51
        public NewVectorDBConnectionPanel(
52
                        JWizardComponents wizardComponents,
53
                        String drv_name,
54
                        int id_max_len) {
55
56
                super(wizardComponents);
57
                drvName = drv_name;
58
                idMaxLen = id_max_len;
59
                wizardComponents.addPropertyChangeListener(this);
60
                initialize();
61
        }
62
63
        private JLabel tableNameLabel = null;
64
        private JTextField tableNameField = null;
65
66
        private JLabel chooseConnLabel = null;
67
        private JComboBox datasourceComboBox;
68
        private ConnectionWithParams theConnWithParams = null;
69
        private JButton dbButton;
70
71
        /**
72
         * This method initializes this
73
         *
74
         */
75
        private void initialize() {
76
77
                datasourceComboBox = getDatasourceComboBox();
78
                loadVectorialDBDatasourcesCombo();
79
80
                chooseConnLabel = new JLabel();
81
                chooseConnLabel.setText(PluginServices.getText(this, "choose_connection"));
82
                chooseConnLabel.setBounds(new java.awt.Rectangle(14,9+50,300,22));
83
84
                dbButton = getJdbcButton();
85
86
        this.setLayout(null);
87
        this.setSize(new java.awt.Dimension(358,263));
88
89
        this.add(getTableNameLabel(), null);
90
        this.add(getTableNameField(), null);
91
92
        this.add(chooseConnLabel, null);
93
        this.add(datasourceComboBox, null);
94
        this.add(dbButton, null);
95
96
            this.updateFinishButton();
97
            getTableNameField().setText(getNewTableName());
98
        }
99
100
        private String getNewTableName() {
101
                String resp = "GVSIG_" + NEW_TABLE_NAME_INDEX;
102
                NEW_TABLE_NAME_INDEX++;
103
                return resp;
104
        }
105
106
        private JTextField getTableNameField() {
107
108
                if (tableNameField == null) {
109
                        tableNameField = new JTextField();
110
                        tableNameField.setBounds(new java.awt.Rectangle(14,32,300,22));
111
                        tableNameField.addKeyListener(this);
112
                }
113
                return tableNameField;
114
115
        }
116
117
        private JLabel getTableNameLabel() {
118
119
                if (tableNameLabel == null) {
120
                        tableNameLabel = new JLabel();
121
                        tableNameLabel.setText(PluginServices.getText(this, "Tabla") + ":");
122
                        tableNameLabel.setBounds(new java.awt.Rectangle(14,9,300,22));
123
                }
124
                return tableNameLabel;
125
        }
126
127
        public ConnectionWithParams getConnectionWithParams() {
128
                return theConnWithParams;
129
        }
130
131
        public String getTableName() {
132
                return getTableNameField().getText().toUpperCase();
133
        }
134
135
    private JComboBox getDatasourceComboBox() {
136
        if (datasourceComboBox == null) {
137
            datasourceComboBox = new JComboBox();
138
            datasourceComboBox.setBounds(new java.awt.Rectangle(14,32+50,300,22));
139
            datasourceComboBox.addItemListener(this);
140
        }
141
142
        return datasourceComboBox;
143
    }
144
145
    private void loadVectorialDBDatasourcesCombo() {
146
147
        getDatasourceComboBox().removeAllItems();
148
        getDatasourceComboBox().addItem(new ConnectionWithParams());
149
        ConnectionWithParams[] conn =
150
                SingleDBConnectionManager.instance().getAllConnections();
151
152
        if (conn == null) {
153
            return;
154
        }
155
156
        for (int i = 0; i < conn.length; i++) {
157
                if (conn[i].getDrvName().compareToIgnoreCase(drvName) == 0) {
158
                        getDatasourceComboBox().addItem(conn[i]);
159
                }
160
        }
161
162
    }
163
164
        public void itemStateChanged(ItemEvent e) {
165
166
                Object src = e.getSource();
167
                if (src == datasourceComboBox) {
168
169
            Object sel_obj = datasourceComboBox.getSelectedItem();
170
171
            if (sel_obj == null) {
172
                    getWizardComponents().getFinishButton().setEnabled(false);
173
                    theConnWithParams = null;
174
                    this.updateFinishButton();
175
                return;
176
            }
177
178
            if (!(sel_obj instanceof ConnectionWithParams)) {
179
                    getWizardComponents().getFinishButton().setEnabled(false);
180
                    theConnWithParams = null;
181
                    this.updateFinishButton();
182
                return;
183
            }
184
185
            ConnectionWithParams cwp = (ConnectionWithParams) sel_obj;
186
187
            if (cwp.isNull()) {
188
                    getWizardComponents().getFinishButton().setEnabled(false);
189
                    theConnWithParams = null;
190
                    this.updateFinishButton();
191
                return;
192
            }
193
194
            if (!cwp.isConnected()) {
195
                if (!tryToConnect(cwp)) {
196
                        getWizardComponents().getFinishButton().setEnabled(false);
197
                        theConnWithParams = null;
198
                        datasourceComboBox.setSelectedIndex(0);
199
                        this.updateFinishButton();
200
                    return;
201
                }
202
            }
203
204
            theConnWithParams = cwp;
205
                this.updateFinishButton();
206
            datasourceComboBox.repaint();
207
                }
208
        }
209
210
    private boolean tryToConnect(ConnectionWithParams _cwp) {
211
        JPasswordDlg dlg = new JPasswordDlg();
212
        dlg.setLocationRelativeTo((Component)PluginServices.getMainFrame());
213
        String strMessage = PluginServices.getText(this, "conectar_jdbc");
214
        String strPassword = PluginServices.getText(this, "password");
215
        dlg.setMessage(strMessage + " [" + _cwp.getDrvName() + ", " +
216
            _cwp.getHost() + ", " + _cwp.getPort() + ", " + _cwp.getDb() +
217
            ", " + _cwp.getUser() + "]. " + strPassword + "?");
218
219
        dlg.setVisible(true);
220
221
        String clave = dlg.getPassword();
222
223
        if (clave == null) {
224
            return false;
225
        }
226
227
        try {
228
            _cwp.connect(clave);
229
        }
230
        catch (DBException e) {
231
            showConnectionErrorMessage(e.getMessage());
232
233
            return false;
234
        }
235
236
        return true;
237
    }
238
239
    private void showConnectionErrorMessage(String _msg) {
240
        String msg = (_msg.length() > 300) ? "" : (": " + _msg);
241
        String title = PluginServices.getText(this, "connection_error");
242
        JOptionPane.showMessageDialog(this, title + msg, title,
243
            JOptionPane.ERROR_MESSAGE);
244
    }
245
246
        public void propertyChange(PropertyChangeEvent evt) {
247
248
                if (evt.getPropertyName().compareToIgnoreCase(JWizardComponents.CURRENT_PANEL_PROPERTY) == 0) {
249
250
                        if (evt.getNewValue() == this) {
251
                                this.updateFinishButton();
252
                        }
253
254
                }
255
256
        }
257
258
259
    private ConnectionWithParams addNewConnection() {
260
        ConnectionWithParams resp = null;
261
262
        DBConnectionParamsDialog newco = new DBConnectionParamsDialog();
263
        newco.showDialog();
264
265
        if (newco.isOkPressed()) {
266
            String _drvname = newco.getConnectionDriverName();
267
            String _host = newco.getConnectionServerUrl();
268
            String _port = newco.getConnectionPort();
269
            String _dbname = newco.getConnectionDBName();
270
            String _user = newco.getConnectionUser();
271
            String _pw = newco.getConnectionPassword();
272
            String _sche = newco.getConnectionSchema();
273
            String _conn_usr_name = newco.getConnectionName();
274
275
            boolean hasToBeCon = newco.hasToBeConnected();
276
277
            try {
278
                resp = SingleDBConnectionManager.instance()
279
                                                  .getConnection(_drvname,
280
                        _user, _pw, _conn_usr_name, _host, _port, _dbname, _sche,
281
                        hasToBeCon);
282
            }
283
            catch (DBException e) {
284
                showConnectionErrorMessage(e.getMessage());
285
286
                return null;
287
            }
288
            SingleVectorialDBConnectionExtension.saveAllToPersistence();
289
            return resp;
290
        }
291
        else {
292
            return null;
293
        }
294
    }
295
296
    /**
297
     * This method initializes jdbcButton
298
     *
299
     * @return javax.swing.JButton
300
     */
301
    private JButton getJdbcButton() {
302
        if (dbButton == null) {
303
            dbButton = new JButton();
304
            dbButton.addActionListener(this);
305
            dbButton.setToolTipText(PluginServices.getText(this,
306
                    "add_connection"));
307
            dbButton.setBounds(new java.awt.Rectangle(320, 32+50, 26, 21));
308
309
            String _file = createResourceUrl("images/jdbc.png").getFile();
310
            dbButton.setIcon(new ImageIcon(_file));
311
        }
312
313
        return dbButton;
314
    }
315
316
    private java.net.URL createResourceUrl(String path) {
317
        return getClass().getClassLoader().getResource(path);
318
    }
319
320
        public void actionPerformed(ActionEvent e) {
321
322
                Object src = e.getSource();
323
324
        if (src == dbButton) {
325
            ConnectionWithParams sel = addNewConnection();
326
327
            if (sel != null) {
328
                loadVectorialDBDatasourcesCombo();
329
                getDatasourceComboBox().setSelectedItem(sel);
330
            }
331
        }
332
333
        }
334
335
        public void keyPressed(KeyEvent e) {
336
                // TODO Auto-generated method stub
337
338
        }
339
340
        public void keyReleased(KeyEvent e) {
341
                updateFinishButton();
342
        }
343
344
        public void keyTyped(KeyEvent e) {
345
        }
346
347
        private void updateFinishButton() {
348
                String aux_table_name = getTableName();
349
                boolean active = (theConnWithParams != null) && validTableName(aux_table_name);
350
                getWizardComponents().getFinishButton().setEnabled(active);
351
        }
352
353
        private boolean validTableName(String str) {
354
355
                int len = str.length();
356
357
                if ((len == 0) || (len > (idMaxLen - 3))) {
358
                        return false;
359
                }
360
361
                if (!validInitialChar(str.substring(0, 1))) {
362
                        return false;
363
                }
364
365
                String onechar = "";
366
                for (int i=1; i<len; i++) {
367
                        onechar = str.substring(i, i+1);
368
                        if (!validChar(onechar)) return false;
369
                }
370
                return true;
371
        }
372
373
        private static String[] VALID_CHAR = {
374
                "_", "A", "B", "C", "D", "E", "F", "G", "H", "I",
375
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
376
                "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3",
377
                "4", "5", "6", "7", "8", "9", "0"
378
                };
379
380
        private static String[] VALID_INITIAL_CHAR = {
381
                "_", "A", "B", "C", "D", "E", "F", "G", "H", "I",
382
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
383
                "T", "U", "V", "W", "X", "Y", "Z"
384
                };
385
386
387
        private boolean validChar(String onechar) {
388
                int len = VALID_CHAR.length;
389
                for (int i=0; i<len; i++) {
390
                        if (onechar.compareTo(VALID_CHAR[i]) == 0) return true;
391
                }
392
                return false;
393
        }
394
395
        private boolean validInitialChar(String onechar) {
396
                int len = VALID_INITIAL_CHAR.length;
397
                for (int i=0; i<len; i++) {
398
                        if (onechar.compareTo(VALID_INITIAL_CHAR[i]) == 0) return true;
399
                }
400
                return false;
401
        }
402
403
404
}
405
406
// [eiel-gestion-conexiones]