Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / dal / swing / datastore / DefaultDataStoreParametersCreationPanel.java @ 38564

History | View | Annotate | Download (11.5 KB)

1 35342 jpiera
/* 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
package org.gvsig.fmap.mapcontrol.dal.swing.datastore;
23
24
import java.awt.Dimension;
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.Insets;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.util.Iterator;
31
import java.util.List;
32
33
import javax.swing.JButton;
34
import javax.swing.JComboBox;
35
36
import org.gvsig.fmap.dal.DALLocator;
37
import org.gvsig.fmap.dal.DataManager;
38
import org.gvsig.fmap.dal.DataServerExplorer;
39
import org.gvsig.fmap.dal.DataServerExplorerParameters;
40
import org.gvsig.fmap.dal.NewDataStoreParameters;
41
import org.gvsig.fmap.dal.exception.InitializeException;
42
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
43
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
44
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
45
import org.gvsig.tools.service.ServiceException;
46 38564 jjdelcerro
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48 35342 jpiera
49
50
/**
51
 * @author gvSIG Team
52
 * @version $Id$
53
 *
54
 */
55
public class DefaultDataStoreParametersCreationPanel extends DataStoreParametersCreationPanel implements ActionListener{
56
    private static final long serialVersionUID = -1453149638996485029L;
57
58
    private static final Logger LOG =
59
        LoggerFactory.getLogger(DefaultDataStoreParametersCreationPanel.class);
60
61
    private static final DataManager DATA_MANAGER = DALLocator.getDataManager();
62 35344 jpiera
63 35342 jpiera
    private static final int FORM_HEIGTH = 18;
64
65
    private DataServerExplorer dataServerExplorer = null;
66
    private DataServerExplorerParameters dataServerExplorerParameters = null;
67
68
    private NewDataStoreParameters dataStoreParameters = null;
69
70
    private JButton explorersButton;
71
    private JComboBox explorersComboBox;
72
    private JButton providersButton;
73
    private JComboBox providersComboBox;
74
75
    public DefaultDataStoreParametersCreationPanel() {
76
        super();
77
        initComponents();
78
        populateExplorerCombo();
79
        initListeners();
80
        enableExplorerControls(false);
81
    }
82
83
    private void initListeners() {
84
        this.explorersComboBox.addActionListener(this);
85
        this.explorersButton.addActionListener(this);
86
        this.providersComboBox.addActionListener(this);
87
        this.providersButton.addActionListener(this);
88
    }
89
90
    @SuppressWarnings("rawtypes")
91
    private void populateExplorerCombo(){
92
        explorersComboBox.addItem(null);
93
94
        Iterator it = DATA_MANAGER.getExplorerProviders().iterator();
95
96
        while(it.hasNext()) {
97
            String explorer = (String)it.next();
98
            explorersComboBox.addItem(explorer);
99
        }
100
    }
101
102
    private void initComponents() {
103
        GridBagConstraints gridBagConstraints;
104
105
        explorersComboBox = new JComboBox();
106
        providersComboBox = new JComboBox();
107
        explorersButton = new JButton();
108
        providersButton = new JButton();
109
110
        setLayout(new GridBagLayout());
111
112
        explorersComboBox.setPreferredSize(new Dimension(0, FORM_HEIGTH));
113
        gridBagConstraints = new GridBagConstraints();
114
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
115
        gridBagConstraints.weightx = 1.0;
116
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
117
        add(explorersComboBox, gridBagConstraints);
118
119
        explorersButton.setText("...");
120
        explorersButton.setPreferredSize(new Dimension(25, FORM_HEIGTH));
121
        gridBagConstraints = new GridBagConstraints();
122
        gridBagConstraints.gridx = 1;
123
        gridBagConstraints.gridy = 0;
124
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
125
        add(explorersButton, gridBagConstraints);
126
127
        providersComboBox.setPreferredSize(new Dimension(0, FORM_HEIGTH));
128
        gridBagConstraints = new GridBagConstraints();
129
        gridBagConstraints.gridx = 2;
130
        gridBagConstraints.gridy = 0;
131
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
132
        gridBagConstraints.weightx = 1.0;
133
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
134
        add(providersComboBox, gridBagConstraints);
135
136
        providersButton.setText("...");
137
        providersButton.setPreferredSize(new Dimension(25, FORM_HEIGTH));
138
        gridBagConstraints = new GridBagConstraints();
139
        gridBagConstraints.gridx = 3;
140
        gridBagConstraints.gridy = 0;
141
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
142
        add(providersButton, gridBagConstraints);
143
    }
144
145
    @Override
146
    public NewDataStoreParameters getDataStoreParameters(){
147
        return dataStoreParameters;
148
    }
149
150
    @Override
151
    public DataServerExplorer getDataServerExplorer() {
152
        return dataServerExplorer;
153
    }
154
155
    public void actionPerformed(ActionEvent e) {
156
        if (e.getSource().equals(explorersButton)) {
157
            explorersButtonClicked();
158
        }else if (e.getSource().equals(providersButton)) {
159
            providersButtonClicked();
160
        }else if (e.getSource().equals(explorersComboBox)) {
161
            explorersComboSelectionChanged();
162
        }else if (e.getSource().equals(providersComboBox)) {
163
            providersComboSelectionChanged();
164
        }
165
    }
166
167
    private void explorersComboSelectionChanged(){
168
        String explorerName = (String)explorersComboBox.getSelectedItem();
169
        if (explorerName == null){
170
            dataServerExplorerParameters = null;
171
            dataServerExplorer = null;
172
            removeProvidersCombo();
173
            enableExplorerControls(false);
174
        }else{
175
            //Only update the explorer if the selection is different
176
            if ((dataServerExplorer == null) || (!explorerName.equals(dataServerExplorer.getProviderName()))){
177
                dataServerExplorerParameters = null;
178 35344 jpiera
179 35342 jpiera
                //Remove all the previous providers
180
                removeProvidersCombo();
181 35344 jpiera
182 35342 jpiera
                //Disable all the components and enable the explorer button
183
                this.enableExplorerControls(false);
184
                this.explorersButton.setEnabled(true);
185
                //Sometimes is possible to create an explorer without parameters (e.g: filesystem explorer"
186
                try {
187
                    dataServerExplorerParameters =
188
                        DATA_MANAGER.createServerExplorerParameters(explorerName);
189
                    dataServerExplorer =
190
                        DATA_MANAGER.openServerExplorer(explorerName, dataServerExplorerParameters);
191
                    populateProvidersCombo();
192
                    this.providersComboBox.setEnabled(true);
193
                } catch (InitializeException e) {
194
                    LOG.error("Error creating the explorer parameters", e);
195
                } catch (ProviderNotRegisteredException e) {
196
                    LOG.error("The explorer has not been registeger", e);
197
                } catch (ValidateDataParametersException e) {
198
                    LOG.error("Error creating the explorer", e);
199
                }
200
            }
201
        }
202
    }
203
204
    private void enableExplorerControls(boolean isEnabled){
205
        this.explorersButton.setEnabled(isEnabled);
206
        this.providersComboBox.setEnabled(isEnabled);
207
        this.providersButton.setEnabled(isEnabled);
208
    }
209
210
    private void enableProviderControls(boolean isEnabled){
211
        this.providersButton.setEnabled(isEnabled);
212
    }
213
214
    private void explorersButtonClicked(){
215
        dataServerExplorer = null;
216
        String explorerName = (String)explorersComboBox.getSelectedItem();
217
        if (explorerName != null){
218
            try {
219
                if ((dataServerExplorerParameters == null) || (!dataServerExplorerParameters.getExplorerName().equals(explorerName))){
220
                    dataServerExplorerParameters =
221
                        DATA_MANAGER.createServerExplorerParameters(explorerName);
222
                }
223
                DynObjectEditor dynObjectEditor = new DynObjectEditor(dataServerExplorerParameters);
224
                dynObjectEditor.editObject(true);
225
                dataServerExplorer =
226
                    DATA_MANAGER.openServerExplorer(explorerName, dataServerExplorerParameters);
227
                //Remove all the previous providers
228
                removeProvidersCombo();
229
                populateProvidersCombo();
230
                this.providersComboBox.setEnabled(true);
231
            } catch (InitializeException e) {
232
                LOG.error("Error creating the explorer parameters", e);
233
            } catch (ProviderNotRegisteredException e) {
234
                LOG.error("The explorer has not been registered", e);
235
            } catch (ServiceException e) {
236
                LOG.error("Error creating the explorer panel", e);
237
            } catch (ValidateDataParametersException e) {
238
                LOG.error("Error creating the explorer", e);
239
            }
240
        }
241
    }
242
243
    @SuppressWarnings("rawtypes")
244
    private void populateProvidersCombo(){
245
        providersComboBox.addItem(null);
246
        if (dataServerExplorer != null){
247
            List providerNames = dataServerExplorer.getDataStoreProviderNames();
248
            for (int i=0 ; i<providerNames.size() ; i++){
249
                providersComboBox.addItem(providerNames.get(i));
250
            }
251
        }
252
    }
253
254
    private void removeProvidersCombo(){
255
        providersComboBox.removeAllItems();
256
    }
257
258
    private void providersComboSelectionChanged(){
259
        dataStoreParameters = null;
260
        String providerName = (String)providersComboBox.getSelectedItem();
261
        if ((dataServerExplorer != null) && (providerName != null)){
262
            enableProviderControls(true);
263
        }else{
264
            enableProviderControls(false);
265
        }
266
    }
267
268
    private void providersButtonClicked(){
269
        String providerName = (String)providersComboBox.getSelectedItem();
270
        if ((dataServerExplorer != null) && (providerName != null)){
271
            try {
272 35344 jpiera
                if ((dataStoreParameters == null) || (!dataStoreParameters.getDataStoreName().equals(providerName))){
273
                    dataStoreParameters =
274
                        DATA_MANAGER.createNewStoreParameters(dataServerExplorer.getProviderName(), providerName);
275
                }
276 35342 jpiera
                DynObjectEditor dynObjectEditor = new DynObjectEditor(dataStoreParameters);
277
                dynObjectEditor.editObject(true);
278
            } catch (InitializeException e) {
279
                LOG.error("Error creating the store parameters", e);
280
            } catch (ProviderNotRegisteredException e) {
281
                LOG.error("The provider has not been registered", e);
282
            } catch (ServiceException e) {
283
                LOG.error("Error creating the store panel", e);
284
            }
285
        }
286
    }
287
}