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 @ 400

History | View | Annotate | Download (17.2 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.Collections;
32
import java.util.List;
33
import java.util.Locale;
34
import java.util.prefs.Preferences;
35

    
36
import javax.swing.JComponent;
37
import javax.swing.JFileChooser;
38
import javax.swing.JOptionPane;
39
import javax.swing.ListSelectionModel;
40
import javax.swing.event.ListSelectionEvent;
41
import javax.swing.event.ListSelectionListener;
42
import javax.swing.table.TableModel;
43

    
44
import org.apache.commons.lang.StringUtils;
45
import org.apache.commons.lang3.ArrayUtils;
46
import org.gvsig.filedialogchooser.FileDialogChooser;
47
import org.gvsig.filedialogchooser.FileDialogChooserManager;
48

    
49
import org.gvsig.fmap.IconThemeHelper;
50
import org.gvsig.fmap.dal.DALLocator;
51
import org.gvsig.fmap.dal.DataManager;
52
import org.gvsig.fmap.dal.DataStoreParameters;
53
import org.gvsig.fmap.dal.DataStoreProviderFactory;
54
import org.gvsig.fmap.dal.exception.DataException;
55
import org.gvsig.fmap.dal.exception.InitializeException;
56
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
57
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
58
import static org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanelController.OPEN_LAYER_FILE_CHOOSER_ID;
59
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
60
import org.gvsig.gdal.prov.ogr.OGRDataExplorer;
61
import org.gvsig.gdal.prov.ogr.OGRDataExplorerParameters;
62
import org.gvsig.gdal.prov.ogr.OGRDataStoreParameters;
63
import org.gvsig.gdal.prov.ogr.OGRDataStoreProvider;
64
import org.gvsig.gdal.prov.ogr.OGRDataStoreProviderFactory;
65
import org.gvsig.tools.ToolsLocator;
66
import org.gvsig.tools.dynobject.DynObject;
67
import org.gvsig.tools.folders.FoldersManager;
68
import org.gvsig.tools.i18n.I18nManager;
69
import org.gvsig.tools.service.ServiceException;
70
import org.gvsig.tools.util.ToolsUtilLocator;
71
import org.gvsig.tools.util.ToolsUtilManager;
72

    
73
import org.slf4j.Logger;
74
import org.slf4j.LoggerFactory;
75

    
76
/**
77
 *
78
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
79
 *
80
 */
81
public class JOGRDataExplorerController extends JOGRDataExplorerView implements JOGRDataExplorer {
82

    
83
    private static final Logger LOG = LoggerFactory.getLogger(JOGRDataExplorerController.class);
84

    
85
    private static final long serialVersionUID = 2347040531301288139L;
86

    
87
    private String lastConnectionString;
88

    
89
    private OGRDataExplorerParameters dataExplorerParameters;
90

    
91
    /**
92
     *
93
     */
94
    public JOGRDataExplorerController() {
95
        init();
96
    }
97

    
98
    private void init() {
99

    
100
        initFileChooserButton();
101
        initDataExplorerPropertiesButton();
102
        initOpenButton();
103
        initPropertiesButton();
104
        initLayerTable();
105
        translate();
106
    }
107

    
108
    private void initDataExplorerPropertiesButton() {
109

    
110
        this.dataExplorerPropertiesButton.setIcon(IconThemeHelper
111
            .getImageIcon("ogr-dataexplorer-settings"));
112
        this.dataExplorerPropertiesButton.addActionListener(new ActionListener() {
113

    
114
            @Override
115
            public void actionPerformed(ActionEvent e) {
116

    
117
                String connectionString = connectionField.getText();
118

    
119
                if (StringUtils.isBlank(connectionString)) {
120
                    lastConnectionString = null;
121
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
122
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
123
                        i18nManager.getTranslation("connection_string_can_not_be_empty"),
124
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
125
                    return;
126
                }
127

    
128
                if (!getConnectionString().equals(lastConnectionString)) {
129
                    setExplorerParameters();
130
                }
131

    
132
                try {
133
                    DynObjectEditor editor = new DynObjectEditor(dataExplorerParameters);
134
                    editor.editObject(true);
135
                } catch (ServiceException e1) {
136
                    LOG.error(String.format("Can not create DynObjectEditor with %1s",
137
                        dataExplorerParameters), e);
138
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
139
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
140
                        i18nManager.getTranslation("error_creating_properties_panel"),
141
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
142
                    return;
143
                }
144
            }
145

    
146
        });
147
    }
148

    
149
    @SuppressWarnings("unchecked")
150
    private void setExplorerParameters() {
151
        DataManager dataManager = DALLocator.getDataManager();
152
        List<String> storeProviders = dataManager.getStoreProviders();
153
        for (String storeProviderName : storeProviders) {
154

    
155
            // First we have to search specific data explorer
156
            // parameters
157
            // to open connection string so we have to avoid create
158
            // default OGR parameters.
159
            if (storeProviderName.equals(OGRDataStoreProvider.NAME)) {
160
                continue;
161
            }
162

    
163
            DataStoreProviderFactory factory =
164
                dataManager.getStoreProviderFactory(storeProviderName);
165
            if (factory instanceof OGRDataStoreProviderFactory) {
166
                OGRDataStoreProviderFactory ogrFactory = (OGRDataStoreProviderFactory) factory;
167
                DynObject explorerParameters =
168
                    ogrFactory.createDataExplorerParameters(getConnectionString());
169
                dataExplorerParameters = (OGRDataExplorerParameters) explorerParameters;
170
            }
171
        }
172

    
173
        // If there are not specific data explorer parameters,
174
        // create
175
        // default OGR data explorer parameters.
176
        if (dataExplorerParameters == null) {
177
            OGRDataStoreProviderFactory factory =
178
                (OGRDataStoreProviderFactory) dataManager
179
                    .getStoreProviderFactory(OGRDataStoreProvider.NAME);
180
            dataExplorerParameters =
181
                (OGRDataExplorerParameters) factory
182
                    .createDataExplorerParameters(getConnectionString());
183
        }
184

    
185
        File file = new File(getConnectionString());
186
        if (file.exists() && file.isFile()) {
187
            dataExplorerParameters.setFile(file);
188
        } else {
189
            dataExplorerParameters.setConnectionString(getConnectionString());
190
        }
191

    
192
        lastConnectionString = getConnectionString();
193

    
194
    }
195

    
196
    private void initLayerTable() {
197
        layerTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
198

    
199
        layerTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
200

    
201
            @Override
202
            public void valueChanged(ListSelectionEvent e) {
203
                if (layerTable.getSelectedRowCount() == 1) {
204
                    propertiesButton.setEnabled(true);
205
                } else {
206
                    propertiesButton.setEnabled(false);
207
                }
208
            }
209
        });
210
    }
211

    
212
    private void initOpenButton() {
213

    
214
        this.openButton.addActionListener(new ActionListener() {
215

    
216
            @SuppressWarnings("unchecked")
217
            @Override
218
            public void actionPerformed(ActionEvent e) {
219

    
220
                String connectionString = connectionField.getText();
221

    
222
                if (StringUtils.isBlank(connectionString)) {
223
                    lastConnectionString = null;
224
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
225
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
226
                        i18nManager.getTranslation("connection_string_can_not_be_empty"),
227
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
228
                    return;
229
                }
230

    
231
                DataManager dataManager = DALLocator.getDataManager();
232
                OGRDataExplorer serverExplorer = null;
233
                if (!getConnectionString().equals(lastConnectionString)) {
234
                    setExplorerParameters();
235
                }
236

    
237
                // If there are not specific data explorer parameters,
238
                // create
239
                // default OGR data explorer parameters.
240
                if (dataExplorerParameters == null) {
241
                    OGRDataStoreProviderFactory factory =
242
                        (OGRDataStoreProviderFactory) dataManager
243
                            .getStoreProviderFactory(OGRDataStoreProvider.NAME);
244
                    dataExplorerParameters =
245
                        (OGRDataExplorerParameters) factory
246
                            .createDataExplorerParameters(connectionString);
247
                }
248

    
249
                File file = new File(connectionString);
250
                if (file.exists() && file.isFile()) {
251
                    dataExplorerParameters.setFile(file);
252
                } else {
253
                    dataExplorerParameters.setConnectionString(connectionString);
254
                }
255

    
256
                lastConnectionString = connectionString;
257

    
258
                try {
259
                    serverExplorer =
260
                        (OGRDataExplorer) dataManager.openServerExplorer(
261
                            dataExplorerParameters.getExplorerName(), dataExplorerParameters);
262
                } catch (InitializeException ex) {
263
                    LOG.error("Can not create OGR server explorer parameters", ex);
264

    
265
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
266
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
267
                        i18nManager.getTranslation("error_opening_connection_string"),
268
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
269
                    return;
270

    
271
                } catch (ProviderNotRegisteredException ex) {
272
                    LOG.error("OGR Provider is not registered", ex);
273

    
274
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
275
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
276
                        i18nManager.getTranslation("error_getting_OGR_provider"),
277
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
278
                    return;
279
                } catch (ValidateDataParametersException ex) {
280
                    LOG.error(String.format("Can not open OGR server explorer. Parameters: %1s",
281
                        connectionString), ex);
282

    
283
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
284
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
285
                        i18nManager.getTranslation("error_opening_connection_string"),
286
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
287
                    return;
288
                }
289

    
290
                List storeParametersList;
291
                try {
292
                    storeParametersList = serverExplorer.list();
293
                } catch (DataException ex) {
294
                    LOG.error("Can not get layer list of OGR Data Explorer", ex);
295

    
296
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
297
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
298
                        i18nManager.getTranslation("error_getting_layers_from_connection_string"),
299
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
300
                    return;
301
                }
302

    
303
                layerTable.setModel(new OGRDataStoreParameterTableModel(storeParametersList));
304
            }
305
        });
306
    }
307

    
308
    private void initPropertiesButton() {
309

    
310
        this.propertiesButton.setEnabled(false);
311
        this.propertiesButton.addActionListener(new ActionListener() {
312

    
313
            @Override
314
            public void actionPerformed(ActionEvent arg0) {
315

    
316
                OGRDataStoreParameterTableModel model =
317
                    (OGRDataStoreParameterTableModel) layerTable.getModel();
318

    
319
                OGRDataStoreParameters parameters =
320
                    model.getDataStoreParameter(layerTable.getSelectedRow());
321
                try {
322
                    DynObjectEditor editor = new DynObjectEditor(parameters);
323
                    editor.editObject(true);
324
                } catch (ServiceException e) {
325
                    LOG.error(String.format("Can not create DynObjectEditor with %1s", parameters),
326
                        e);
327
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
328
                    JOptionPane.showMessageDialog(JOGRDataExplorerController.this,
329
                        i18nManager.getTranslation("error_creating_properties_panel"),
330
                        i18nManager.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
331
                    return;
332
                }
333
            }
334
        });
335

    
336
    }
337

    
338
    private void initFileChooserButton() {
339

    
340
        fileChooserButton.addActionListener(new ActionListener() {
341

    
342
            @Override
343
            public void actionPerformed(ActionEvent e) {
344
                FoldersManager foldersManager = ToolsLocator.getFoldersManager();
345
                FileDialogChooserManager fileDialogChooserManager = ToolsUtilLocator.getFileDialogChooserManager();
346
                
347
                FileDialogChooser chooser = fileDialogChooserManager.create(OPEN_LAYER_FILE_CHOOSER_ID);
348
                chooser.setCurrentDirectory(foldersManager.get("DataFolder"));
349
                if (StringUtils.isNotBlank(getConnectionString())) {
350
                    File file = new File(getConnectionString());
351
                    if (file.exists()) {
352
                        chooser.setCurrentDirectory(file.getParentFile());
353
                    }
354
                }
355

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

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

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

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

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

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

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

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

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

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

    
407
        OGRDataStoreParameterTableModel model = getOGRTableModel();
408
        if( model == null ) {
409
            return Collections.EMPTY_LIST;
410
        }
411
        int[] selectedRows = layerTable.getSelectedRows();
412
        List<DataStoreParameters> parameters = new ArrayList<DataStoreParameters>();
413
        for (int i = 0; i < selectedRows.length; i++) {
414
            parameters.add(model.getDataStoreParameter(selectedRows[i]));
415
        }
416
        return parameters;
417
    }
418

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

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

    
430
    private OGRDataStoreParameterTableModel getOGRTableModel() {
431
        TableModel model = layerTable.getModel();
432
        if( model instanceof OGRDataStoreParameterTableModel) {
433
            return (OGRDataStoreParameterTableModel) model;
434
        }
435
        return null;
436
    }
437
}