Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.oracle / src / org / gvsig / oracle / gui / ConnectionChooserPanel.java @ 30231

History | View | Annotate | Download (6.78 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 Prodevelop S.L. main development
26
 */
27
package org.gvsig.oracle.gui;
28

    
29
import java.awt.Dimension;
30
import java.awt.GridBagConstraints;
31
import java.awt.GridBagLayout;
32
import java.awt.event.ActionEvent;
33
import java.awt.event.ActionListener;
34
import java.util.List;
35

    
36
import javax.swing.ImageIcon;
37
import javax.swing.JButton;
38
import javax.swing.JComboBox;
39
import javax.swing.JLabel;
40
import javax.swing.JOptionPane;
41
import javax.swing.JPanel;
42

    
43
import org.gvsig.andami.PluginServices;
44
import org.gvsig.fmap.dal.DALLocator;
45
import org.gvsig.fmap.dal.DataManager;
46
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorerParameters;
47
import org.gvsig.geodb.SingleVectorialDBConnectionExtension;
48
import org.gvsig.geodb.vectorialdb.wizard.MyExplorer;
49
import org.gvsig.geodb.vectorialdb.wizard.VectorialDBConnectionParamsDialog;
50

    
51
/**
52
 * This dialog lets the user choose an available connection (explorer).
53
 * 
54
 * @author jldominguez, vsanjaime
55
 * 
56
 */
57
public class ConnectionChooserPanel extends JPanel implements ActionListener {
58

    
59
        private static final long serialVersionUID = 1L;
60

    
61
        private JLabel chooseConnLabel = null;
62
        public JComboBox availableConnectionsComboBox = null;
63
        private JButton jdbcButton = null;
64
        private boolean okPressed = false;
65

    
66
        /**
67
         * This method initializes
68
         * 
69
         */
70
        public ConnectionChooserPanel() {
71
                super();
72
                initialize();
73
        }
74

    
75
        /**
76
         * is ok button pressed
77
         * 
78
         * @return
79
         */
80
        public boolean isOkPressed() {
81
                return okPressed;
82
        }
83

    
84
        /**
85
         * Action Performed event
86
         */
87
        public void actionPerformed(ActionEvent arg0) {
88

    
89
                // button new jdbc connection (explorer)
90
                if (arg0.getSource() == jdbcButton) {
91
                        MyExplorer selExp = addNewConnection();
92
                        if (selExp != null) {
93
                                reloadCombo(selExp);
94
                                availableConnectionsComboBox.setSelectedItem(selExp);
95
                        }
96
                }
97
        }
98

    
99
        /**
100
         * Create a explorer parameters with name of selected explorer
101
         * 
102
         * @return
103
         */
104
        public MyExplorer getSelectedServerExplorer() {
105

    
106
                Object obj = availableConnectionsComboBox.getSelectedItem();
107
                if (obj == null || obj instanceof String) {
108
                        return null;
109
                }
110
                return (MyExplorer) obj;
111
        }
112

    
113
        /**
114
         * reload combo with oracle spatial explorers availables
115
         */
116
        private void reloadCombo() {
117
                availableConnectionsComboBox.removeAllItems();
118
                // TODO reload all availables exploreres
119
                DataManager dataManager = DALLocator.getDataManager();
120
                List<String> names = dataManager.getStoreProviders();
121

    
122
                if (availableConnectionsComboBox.getModel().getSize() > 0) {
123
                        availableConnectionsComboBox.setSelectedIndex(0);
124
                }
125
        }
126

    
127
        /**
128
         * reload combo with oracle spatial explorers availables
129
         */
130
        private void reloadCombo(MyExplorer selExp) {
131
                if (selExp != null) {
132
                        availableConnectionsComboBox.addItem(selExp);
133
                        availableConnectionsComboBox.setSelectedItem(selExp);
134
                }
135
        }
136

    
137
        /**
138
         * This method initializes this
139
         * 
140
         */
141
        private void initialize() {
142
                setLayout(new GridBagLayout());
143
                this.setMinimumSize(new Dimension(395, 89));
144

    
145
                // label choose connection
146
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
147
                gridBagConstraints.gridx = 0;
148
                gridBagConstraints.gridwidth = 2;
149
                gridBagConstraints.gridy = 0;
150
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
151
                gridBagConstraints.weightx = 1.0;
152
                gridBagConstraints.insets = new java.awt.Insets(5, 30, 5, 10);
153
                chooseConnLabel = new JLabel();
154
                chooseConnLabel
155
                                .setText(PluginServices.getText(this, "list_connection"));
156
                this.add(chooseConnLabel, gridBagConstraints);
157

    
158
                // combo connections
159
                availableConnectionsComboBox = new JComboBox();
160
                gridBagConstraints = new GridBagConstraints();
161
                gridBagConstraints.gridx = 0;
162
                gridBagConstraints.gridy = 1;
163
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
164
                gridBagConstraints.weightx = 1.0;
165
                gridBagConstraints.insets = new java.awt.Insets(0, 30, 5, 10);
166
                this.add(availableConnectionsComboBox, gridBagConstraints);
167

    
168
                // button
169
                jdbcButton = new JButton();
170
                jdbcButton.addActionListener(this);
171
                jdbcButton.setMinimumSize(new Dimension(20, 20));
172
                jdbcButton.setToolTipText(PluginServices
173
                                .getText(this, "add_connection"));
174
                jdbcButton.setBounds(new java.awt.Rectangle(381 - 26, 15, 26, 21));
175
                String _file = createResourceUrl("images/jdbc.png").getFile();
176
                jdbcButton.setIcon(new ImageIcon(_file));
177

    
178
                gridBagConstraints = new GridBagConstraints();
179
                gridBagConstraints.gridx = 1;
180
                gridBagConstraints.gridy = 1;
181
                gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 10);
182
                this.add(jdbcButton, gridBagConstraints);
183

    
184
                reloadCombo();
185
        }
186

    
187
        /**
188
         * show connection error message
189
         * 
190
         * @param _msg
191
         */
192
        private void showConnectionErrorMessage(String _msg) {
193
                String msg = (_msg.length() > 300) ? "" : (": " + _msg);
194
                String title = PluginServices.getText(this, "connection_error");
195
                JOptionPane.showMessageDialog(this, title + msg, title,
196
                                JOptionPane.ERROR_MESSAGE);
197
        }
198

    
199
        /**
200
         * show panel to define new connection and build the explorer
201
         * 
202
         * @return
203
         */
204
        private MyExplorer addNewConnection() {
205

    
206
                MyExplorer myExplorer = new MyExplorer();
207
                DBServerExplorerParameters resp = null;
208

    
209
                VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
210
                newco.showDialog();
211

    
212
                if (newco.isOkPressed()) {
213
                        try {
214
                                resp = newco.getParameters();
215
                        } catch (Exception e) {
216
                                showConnectionErrorMessage(e.getMessage());
217
                                return null;
218
                        }
219
                        SingleVectorialDBConnectionExtension.saveAllToPersistence();
220
                        myExplorer.setDbExplorerParameters(resp);
221
                        myExplorer.setName(newco.getConnectionName());
222
                        return myExplorer;
223
                } else {
224
                        return null;
225
                }
226
        }
227

    
228
        /**
229
         * Create resource url
230
         * 
231
         * @param path
232
         * @return
233
         */
234
        private java.net.URL createResourceUrl(String path) {
235
                return getClass().getClassLoader().getResource(path);
236
        }
237

    
238
}