Statistics
| Revision:

gvsig-gdal / trunk / org.gvsig.gdal / org.gvsig.gdal.app / org.gvsig.gdal.app.ogr.mainplugin / src / main / java / org / gvsig / gdal / app / ogr / mainplugin / gui / JOGRDataExplorerController.java @ 231

History | View | Annotate | Download (16.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2016 gvSIG Association
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gdal.app.ogr.mainplugin.gui;
25

    
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.io.File;
29
import java.util.ArrayList;
30
import java.util.Arrays;
31
import java.util.List;
32
import java.util.Locale;
33
import java.util.prefs.Preferences;
34

    
35
import javax.swing.JComponent;
36
import javax.swing.JFileChooser;
37
import javax.swing.JOptionPane;
38
import javax.swing.ListSelectionModel;
39
import javax.swing.event.ListSelectionEvent;
40
import javax.swing.event.ListSelectionListener;
41

    
42
import org.apache.commons.lang.StringUtils;
43
import org.apache.commons.lang3.ArrayUtils;
44

    
45
import org.gvsig.fmap.IconThemeHelper;
46
import org.gvsig.fmap.dal.DALLocator;
47
import org.gvsig.fmap.dal.DataManager;
48
import org.gvsig.fmap.dal.DataStoreParameters;
49
import org.gvsig.fmap.dal.DataStoreProviderFactory;
50
import org.gvsig.fmap.dal.exception.DataException;
51
import org.gvsig.fmap.dal.exception.InitializeException;
52
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
53
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
54
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
55
import org.gvsig.gdal.prov.ogr.OGRDataExplorer;
56
import org.gvsig.gdal.prov.ogr.OGRDataExplorerParameters;
57
import org.gvsig.gdal.prov.ogr.OGRDataStoreParameters;
58
import org.gvsig.gdal.prov.ogr.OGRDataStoreProvider;
59
import org.gvsig.gdal.prov.ogr.OGRDataStoreProviderFactory;
60
import org.gvsig.tools.ToolsLocator;
61
import org.gvsig.tools.dynobject.DynObject;
62
import org.gvsig.tools.i18n.I18nManager;
63
import org.gvsig.tools.service.ServiceException;
64

    
65
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
67

    
68
/**
69
 *
70
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
71
 *
72
 */
73
public class JOGRDataExplorerController extends JOGRDataExplorerView implements JOGRDataExplorer {
74

    
75
    private static final Logger LOG = LoggerFactory.getLogger(JOGRDataExplorerController.class);
76

    
77
    private static final long serialVersionUID = 2347040531301288139L;
78

    
79
    private String lastConnectionString;
80

    
81
    private OGRDataExplorerParameters dataExplorerParameters;
82

    
83
    /**
84
     *
85
     */
86
    public JOGRDataExplorerController() {
87
        init();
88
    }
89

    
90
    private void init() {
91

    
92
        initFileChooserButton();
93
        initDataExplorerPropertiesButton();
94
        initOpenButton();
95
        initPropertiesButton();
96
        initLayerTable();
97
        translate();
98
    }
99

    
100
    private void initDataExplorerPropertiesButton() {
101

    
102
        this.dataExplorerPropertiesButton.setIcon(IconThemeHelper
103
            .getImageIcon("ogr-dataexplorer-settings"));
104
        this.dataExplorerPropertiesButton.addActionListener(new ActionListener() {
105

    
106
            @Override
107
            public void actionPerformed(ActionEvent e) {
108

    
109
                String connectionString = connectionField.getText();
110

    
111
                if (StringUtils.isBlank(connectionString)) {
112
                    lastConnectionString = null;
113
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
114
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
115
                        i18nManager.getTranslation("connection_string_can_not_be_empty"),
116
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
117
                    return;
118
                }
119

    
120
                if (!getConnectionString().equals(lastConnectionString)) {
121
                    setExplorerParameters();
122
                }
123

    
124
                try {
125
                    DynObjectEditor editor = new DynObjectEditor(dataExplorerParameters);
126
                    editor.editObject(true);
127
                } catch (ServiceException e1) {
128
                    LOG.error(String.format("Can not create DynObjectEditor with %1s",
129
                        dataExplorerParameters), e);
130
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
131
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
132
                        i18nManager.getTranslation("error_creating_properties_panel"),
133
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
134
                    return;
135
                }
136
            }
137

    
138
        });
139
    }
140

    
141
    @SuppressWarnings("unchecked")
142
    private void setExplorerParameters() {
143
        DataManager dataManager = DALLocator.getDataManager();
144
        List<String> storeProviders = dataManager.getStoreProviders();
145
        for (String storeProviderName : storeProviders) {
146

    
147
            // First we have to search specific data explorer
148
            // parameters
149
            // to open connection string so we have to avoid create
150
            // default OGR parameters.
151
            if (storeProviderName.equals(OGRDataStoreProvider.NAME)) {
152
                continue;
153
            }
154

    
155
            DataStoreProviderFactory factory =
156
                dataManager.getStoreProviderFactory(storeProviderName);
157
            if (factory instanceof OGRDataStoreProviderFactory) {
158
                OGRDataStoreProviderFactory ogrFactory = (OGRDataStoreProviderFactory) factory;
159
                DynObject explorerParameters =
160
                    ogrFactory.createDataExplorerParameters(getConnectionString());
161
                dataExplorerParameters = (OGRDataExplorerParameters) explorerParameters;
162
            }
163
        }
164

    
165
        // If there are not specific data explorer parameters,
166
        // create
167
        // default OGR data explorer parameters.
168
        if (dataExplorerParameters == null) {
169
            OGRDataStoreProviderFactory factory =
170
                (OGRDataStoreProviderFactory) dataManager
171
                    .getStoreProviderFactory(OGRDataStoreProvider.NAME);
172
            dataExplorerParameters =
173
                (OGRDataExplorerParameters) factory
174
                    .createDataExplorerParameters(getConnectionString());
175
        }
176

    
177
        File file = new File(getConnectionString());
178
        if (file.exists() && file.isFile()) {
179
            dataExplorerParameters.setFile(file);
180
        } else {
181
            dataExplorerParameters.setConnectionString(getConnectionString());
182
        }
183

    
184
        lastConnectionString = getConnectionString();
185

    
186
    }
187

    
188
    private void initLayerTable() {
189
        layerTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
190

    
191
        layerTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
192

    
193
            @Override
194
            public void valueChanged(ListSelectionEvent e) {
195
                if (layerTable.getSelectedRowCount() == 1) {
196
                    propertiesButton.setEnabled(true);
197
                } else {
198
                    propertiesButton.setEnabled(false);
199
                }
200
            }
201
        });
202
    }
203

    
204
    private void initOpenButton() {
205

    
206
        this.openButton.addActionListener(new ActionListener() {
207

    
208
            @SuppressWarnings("unchecked")
209
            @Override
210
            public void actionPerformed(ActionEvent e) {
211

    
212
                String connectionString = connectionField.getText();
213

    
214
                if (StringUtils.isBlank(connectionString)) {
215
                    lastConnectionString = null;
216
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
217
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
218
                        i18nManager.getTranslation("connection_string_can_not_be_empty"),
219
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
220
                    return;
221
                }
222

    
223
                DataManager dataManager = DALLocator.getDataManager();
224
                OGRDataExplorer serverExplorer = null;
225
                if (!getConnectionString().equals(lastConnectionString)) {
226
                    setExplorerParameters();
227
                }
228

    
229
                // If there are not specific data explorer parameters,
230
                // create
231
                // default OGR data explorer parameters.
232
                if (dataExplorerParameters == null) {
233
                    OGRDataStoreProviderFactory factory =
234
                        (OGRDataStoreProviderFactory) dataManager
235
                            .getStoreProviderFactory(OGRDataStoreProvider.NAME);
236
                    dataExplorerParameters =
237
                        (OGRDataExplorerParameters) factory
238
                            .createDataExplorerParameters(connectionString);
239
                }
240

    
241
                File file = new File(connectionString);
242
                if (file.exists() && file.isFile()) {
243
                    dataExplorerParameters.setFile(file);
244
                } else {
245
                    dataExplorerParameters.setConnectionString(connectionString);
246
                }
247

    
248
                lastConnectionString = connectionString;
249

    
250
                try {
251
                    serverExplorer =
252
                        (OGRDataExplorer) dataManager.openServerExplorer(
253
                            dataExplorerParameters.getExplorerName(), dataExplorerParameters);
254
                } catch (InitializeException ex) {
255
                    LOG.error("Can not create OGR server explorer parameters", ex);
256

    
257
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
258
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
259
                        i18nManager.getTranslation("error_opening_connection_string"),
260
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
261
                    return;
262

    
263
                } catch (ProviderNotRegisteredException ex) {
264
                    LOG.error("OGR Provider is not registered", ex);
265

    
266
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
267
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
268
                        i18nManager.getTranslation("error_getting_OGR_provider"),
269
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
270
                    return;
271
                } catch (ValidateDataParametersException ex) {
272
                    LOG.error(String.format("Can not open OGR server explorer. Parameters: %1s",
273
                        connectionString), ex);
274

    
275
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
276
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
277
                        i18nManager.getTranslation("error_opening_connection_string"),
278
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
279
                    return;
280
                }
281

    
282
                List storeParametersList;
283
                try {
284
                    storeParametersList = serverExplorer.list();
285
                } catch (DataException ex) {
286
                    LOG.error("Can not get layer list of OGR Data Explorer", ex);
287

    
288
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
289
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
290
                        i18nManager.getTranslation("error_getting_layers_from_connection_string"),
291
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
292
                    return;
293
                }
294

    
295
                layerTable.setModel(new OGRDataStoreParameterTableModel(storeParametersList));
296
            }
297
        });
298
    }
299

    
300
    private void initPropertiesButton() {
301

    
302
        this.propertiesButton.setEnabled(false);
303
        this.propertiesButton.addActionListener(new ActionListener() {
304

    
305
            @Override
306
            public void actionPerformed(ActionEvent arg0) {
307

    
308
                OGRDataStoreParameterTableModel model =
309
                    (OGRDataStoreParameterTableModel) layerTable.getModel();
310

    
311
                OGRDataStoreParameters parameters =
312
                    model.getDataStoreParameter(layerTable.getSelectedRow());
313
                try {
314
                    DynObjectEditor editor = new DynObjectEditor(parameters);
315
                    editor.editObject(true);
316
                } catch (ServiceException e) {
317
                    LOG.error(String.format("Can not create DynObjectEditor with %1s", parameters),
318
                        e);
319
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
320
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
321
                        i18nManager.getTranslation("error_creating_properties_panel"),
322
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
323
                    return;
324
                }
325
            }
326
        });
327

    
328
    }
329

    
330
    private void initFileChooserButton() {
331

    
332
        fileChooserButton.addActionListener(new ActionListener() {
333

    
334
            @Override
335
            public void actionPerformed(ActionEvent e) {
336

    
337
                JFileChooser chooser = null;
338

    
339
                if (StringUtils.isNotBlank(getConnectionString())) {
340
                    File file = new File(getConnectionString());
341
                    if (file.exists()) {
342
                        chooser = new JFileChooser(file.getParentFile());
343
                    }
344
                }
345

    
346
                if (chooser == null) {
347
                    Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
348
                    String dataFolder = prefs.get("DataFolder", null);
349

    
350
                    if(dataFolder!=null && !dataFolder.isEmpty()){
351
                        chooser = new JFileChooser(dataFolder);
352
                    } else {
353
                        chooser = new JFileChooser();
354
                    }
355
                }
356

    
357
                int reply = chooser.showOpenDialog(asJComponent());
358

    
359
                if (reply == JFileChooser.APPROVE_OPTION) {
360

    
361
                    File file = chooser.getSelectedFile();
362
                    if (file.isFile() && file.exists() && file.canRead()) {
363
                        connectionField.setText(file.getAbsolutePath());
364
                    }
365
                }
366
            }
367
        });
368
    }
369

    
370
    @Override
371
    public JComponent asJComponent() {
372
        return this;
373
    }
374

    
375
    @Override
376
    public void setLocale(Locale l) {
377

    
378
        if (!this.getLocale().equals(l)) {
379
            this.translate();
380
        }
381
        super.setLocale(l);
382
    }
383

    
384
    private void translate() {
385
        I18nManager i18nManager = ToolsLocator.getI18nManager();
386
        this.connectionLabel.setText(i18nManager.getTranslation(this.connectionLabel.getText()));
387
        this.openButton.setText(i18nManager.getTranslation(this.openButton.getText()));
388
        this.propertiesButton.setText(i18nManager.getTranslation(this.propertiesButton.getText()));
389
    }
390

    
391
    @Override
392
    public List<String> getSelectedLayerNames() {
393

    
394
        List<String> selectedLayerNames = new ArrayList<String>();
395
        OGRDataStoreParameterTableModel model = getOGRTableModel();
396
        int[] selectedRows = layerTable.getSelectedRows();
397
        for (int i = 0; i < selectedRows.length; i++) {
398
            OGRDataStoreParameters dataStoreParameter =
399
                model.getDataStoreParameter(selectedRows[i]);
400
            selectedLayerNames.add(dataStoreParameter.getLayerName());
401
        }
402
        return selectedLayerNames;
403
    }
404

    
405
    public List<DataStoreParameters> getSelectedLayerProperties() throws InitializeException,
406
        ProviderNotRegisteredException {
407

    
408
        OGRDataStoreParameterTableModel model = getOGRTableModel();
409

    
410
        int[] selectedRows = layerTable.getSelectedRows();
411
        List<DataStoreParameters> parameters = new ArrayList<DataStoreParameters>();
412
        for (int i = 0; i < selectedRows.length; i++) {
413
            parameters.add(model.getDataStoreParameter(selectedRows[i]));
414
        }
415
        return parameters;
416
    }
417

    
418
    @Override
419
    public List<Integer> getSelectedLayerIndexes() {
420
        int[] selectedRows = layerTable.getSelectedRows();
421
        return Arrays.asList(ArrayUtils.toObject(selectedRows));
422
    }
423

    
424
    @Override
425
    public String getConnectionString() {
426
        return connectionField.getText();
427
    }
428

    
429
    private OGRDataStoreParameterTableModel getOGRTableModel() {
430
        return (OGRDataStoreParameterTableModel) layerTable.getModel();
431
    }
432
}