Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / vectorialdb / wizard / UserTableSettingsVectorialPanel.java @ 43891

History | View | Annotate | Download (16.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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 3
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.geodb.vectorialdb.wizard;
25

    
26
import java.awt.Insets;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29

    
30
import javax.swing.ImageIcon;
31
import javax.swing.JButton;
32
import javax.swing.JCheckBox;
33
import javax.swing.JComboBox;
34
import javax.swing.JLabel;
35
import javax.swing.JOptionPane;
36
import javax.swing.JPanel;
37
import javax.swing.JTextField;
38

    
39
import org.cresques.cts.ICoordTrans;
40
import org.cresques.cts.IProjection;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
import org.gvsig.andami.IconThemeHelper;
45
import org.gvsig.andami.PluginServices;
46
import org.gvsig.app.ApplicationLocator;
47
import org.gvsig.app.addlayer.AddLayerDialog;
48
import org.gvsig.app.gui.panels.CRSSelectPanel;
49
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
50
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
51
import org.gvsig.fmap.geom.GeometryLocator;
52
import org.gvsig.fmap.geom.GeometryManager;
53
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
54
import org.gvsig.fmap.geom.primitive.Envelope;
55
import org.gvsig.fmap.mapcontrol.MapControl;
56
import org.gvsig.i18n.Messages;
57

    
58
public class UserTableSettingsVectorialPanel extends UserTableSettingsPanel {
59

    
60
    private static Logger logger = LoggerFactory
61
            .getLogger(UserTableSettingsPanel.class.getName());
62

    
63
    private FieldComboItem[] geos;
64
    private JComboBox geomComboBox = null;
65
    private JLabel geomLabel = null;
66
    private JLabel waLabel = null;
67
    private JLabel topLabel = null;
68
    private JTextField topTextField = null;
69
    private JTextField bottomTextField = null;
70
    private JTextField rightTextField = null;
71
    private JTextField leftTextField = null;
72
    private JLabel bottomLabel = null;
73
    private JLabel rightLabel = null;
74
    private JLabel leftLabel = null;
75
    private JButton getviewButton = null;
76
    private JCheckBox activateWACheckBox = null;
77
    private MapControl mControl = null;
78
    private CRSSelectPanel panelProj;
79

    
80
    private IProjection proposedProjection = null;
81
    // private IProjection currentProj;
82

    
83
    private JPanel this_panel = null;
84

    
85
    public UserTableSettingsVectorialPanel(FieldComboItem[] idComboItems,
86
            FieldComboItem[] geoComboItems, String initialLayerName,
87
            MapControl mapc, boolean empty, WizardVectorialDB _p,
88
            DBStoreParameters parameters,
89
            IProjection proposed_proj) {
90
        super();
91

    
92
        this_panel = this;
93
        setInitValues(idComboItems, initialLayerName, empty, _p, parameters);
94

    
95
        mControl = mapc;
96
        geos = geoComboItems;
97
        proposedProjection = proposed_proj;
98
        initialize(empty);
99
    }
100

    
101
    protected void initialize(boolean _empty) {
102
        super.initialize(_empty);
103
        setBorder(javax.swing.BorderFactory.createTitledBorder(null,
104
                PluginServices.getText(this, "_Table_settings"),
105
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
106
                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
107
        leftLabel = new JLabel();
108
        // leftLabel.setBounds(new java.awt.Rectangle(375, 175, 111, 16));
109
        leftLabel.setBounds(new java.awt.Rectangle(80 + 70, 200, 70, 16));
110
        leftLabel.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 10));
111
        leftLabel.setText(PluginServices.getText(this, "xmin"));
112
        rightLabel = new JLabel();
113
        rightLabel.setBounds(new java.awt.Rectangle(75 + 150, 200, 70, 16));
114
        rightLabel.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 10));
115
        rightLabel.setText(PluginServices.getText(this, "xmax"));
116
        bottomLabel = new JLabel();
117
        bottomLabel.setBounds(new java.awt.Rectangle(70 + 230, 200, 70, 16));
118
        bottomLabel
119
                .setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 10));
120
        bottomLabel.setText(PluginServices.getText(this, "ymin"));
121
        topLabel = new JLabel();
122
        topLabel.setBounds(new java.awt.Rectangle(65 + 310, 200, 70, 16));
123
        topLabel.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 10));
124
        topLabel.setText(PluginServices.getText(this, "ymax"));
125
        waLabel = new JLabel();
126
        // waLabel.setBounds(new java.awt.Rectangle(40, 145, 131, 21));
127
        waLabel.setBounds(new java.awt.Rectangle(30, 215, 125, 21));
128
        waLabel.setText(PluginServices.getText(this, "_Working_area"));
129
        geomLabel = new JLabel();
130
        // geomLabel.setBounds(new java.awt.Rectangle(240, 55, 111, 21));
131
        geomLabel.setBounds(new java.awt.Rectangle(8, 140, 160, 21));
132
        geomLabel.setText(PluginServices.getText(this, "_Geo_field"));
133

    
134
        add(getGeomComboBox(), null);
135
        add(geomLabel, null);
136
        add(waLabel, null);
137
        add(topLabel, null);
138
        add(getTopTextField(), null);
139
        add(getBottomTextField(), null);
140
        add(getRightTextField(), null);
141
        add(getLeftTextField(), null);
142
        add(bottomLabel, null);
143
        add(rightLabel, null);
144
        add(leftLabel, null);
145
        add(getGetviewButton(), null);
146
        add(getActivateWACheckBox(), null);
147
        loadValues(_empty);
148
    }
149

    
150
    private CRSSelectPanel getJPanelProj(IProjection pro) {
151
        if (panelProj == null) {
152
            panelProj = CRSSelectPanel.getPanel(pro);
153

    
154
            panelProj.setTransPanelActive(true);
155
            panelProj.setBounds(8, 165, 477, 21);
156
//                        panelProj.addActionListener(new java.awt.event.ActionListener() {
157
//                                public void actionPerformed(java.awt.event.ActionEvent e) {
158
//                                        if (panelProj.isOkPressed()) {
159
//                                                AddLayerDialog.setLastProjection(panelProj.getCurProj());
160
//                                        }
161
//                                }
162
//                        });
163
        }
164
        return panelProj;
165
    }
166

    
167
    public boolean hasValidValues() {
168
        if (!super.hasValidValues()) {
169
            return false;
170
        }
171

    
172
        if ((activateWACheckBox.isSelected()) && (getWorkingArea() == null)) {
173
            return false;
174
        }
175

    
176
        return true;
177
    }
178

    
179
    private JComboBox getGeomComboBox() {
180
        if (geomComboBox == null) {
181
            geomComboBox = new JComboBox();
182
            // geomComboBox.setBounds(new java.awt.Rectangle(355, 55, 131, 21));
183
            geomComboBox.setBounds(new java.awt.Rectangle(150, 140, 335, 21));
184
            geomComboBox.addActionListener(new ActionListener() {
185
                public void actionPerformed(ActionEvent e) {
186

    
187
                    if (panelProj == null) {
188
                        return;
189
                    }
190
                    /*
191
                     * Set the proj of the field in the 
192
                     * SRS chooser
193
                     */
194
                    FieldComboItem fci = (FieldComboItem) geomComboBox.getSelectedItem();
195

    
196
                    if (fci == null) {
197
                        return;
198
                    }
199

    
200
                    IProjection iproj = fci.getProjection();
201
                    if (iproj != null) {
202
                        /*
203
                         * Remove and recreate crs selection panel
204
                         */
205
                        this_panel.remove(panelProj);
206
                        panelProj = null;
207
                        add(getJPanelProj(iproj), null);
208
                    }
209
                }
210
            });
211
        }
212

    
213
        return geomComboBox;
214
    }
215

    
216
    private JTextField getTopTextField() {
217
        if (topTextField == null) {
218
            topTextField = new JTextField();
219
            topTextField.addKeyListener(this);
220
            // topTextField.setBounds(new java.awt.Rectangle(15, 190, 111, 21));
221
            topTextField.setBounds(new java.awt.Rectangle(65 + 310, 215, 70, 21));
222
        }
223

    
224
        return topTextField;
225
    }
226

    
227
    private JTextField getBottomTextField() {
228
        if (bottomTextField == null) {
229
            bottomTextField = new JTextField();
230
            bottomTextField.addKeyListener(this);
231
            bottomTextField
232
                    .setBounds(new java.awt.Rectangle(70 + 230, 215, 70, 21));
233
        }
234

    
235
        return bottomTextField;
236
    }
237

    
238
    private JTextField getRightTextField() {
239
        if (rightTextField == null) {
240
            rightTextField = new JTextField();
241
            rightTextField.addKeyListener(this);
242
            rightTextField.setBounds(new java.awt.Rectangle(75 + 150, 215, 70, 21));
243
        }
244

    
245
        return rightTextField;
246
    }
247

    
248
    private JTextField getLeftTextField() {
249
        if (leftTextField == null) {
250
            leftTextField = new JTextField();
251
            leftTextField.addKeyListener(this);
252
            leftTextField.setBounds(new java.awt.Rectangle(80 + 70, 215, 70, 21));
253
        }
254

    
255
        return leftTextField;
256
    }
257

    
258
    private JButton getGetviewButton() {
259
        if (getviewButton == null) {
260
            getviewButton = new org.gvsig.gui.beans.swing.JButton();
261
            getviewButton.addActionListener(this);
262
            //getviewButton.setBounds(new java.awt.Rectangle(195, 145, 111, 26));
263
            getviewButton.setBounds(new java.awt.Rectangle(455, 205, 31, 31));
264
            ImageIcon ii = IconThemeHelper.getImageIcon("geodb-get-view-extent");
265
            // ImageIcon ii = IconThemeHelper.getImageIcon("geodb-connection-add"); 
266

    
267
            getviewButton.setMargin(new Insets(1, 1, 1, 1));
268
            getviewButton.setIcon(ii);
269
            getviewButton.setToolTipText(
270
                    Messages.getText("_Get_view_extent"));
271
            getviewButton.setEnabled(false);
272
        }
273

    
274
        return getviewButton;
275
    }
276

    
277
    private JCheckBox getActivateWACheckBox() {
278
        if (activateWACheckBox == null) {
279
            activateWACheckBox = new JCheckBox();
280
            activateWACheckBox.addActionListener(this);
281
//                        activateWACheckBox
282
//                                        .setBounds(new java.awt.Rectangle(15, 145, 21, 21));
283
            activateWACheckBox
284
                    .setBounds(new java.awt.Rectangle(5, 215, 21, 21));
285

    
286
        }
287

    
288
        return activateWACheckBox;
289
    }
290

    
291
    protected void loadValues(boolean is_empty) {
292
        super.loadValues(is_empty);
293
        if (is_empty) {
294
            enableAlphaControls(false);
295
            enableSpatialControls(false);
296
            getActivateWACheckBox().setSelected(false);
297
        } else {
298
            enableAlphaControls(true);
299
            enableSpatialControls(true);
300

    
301
            getGeomComboBox().removeAllItems();
302

    
303
            for (int i = 0; i < geos.length; i++) {
304
                getGeomComboBox().addItem(geos[i]);
305
            }
306
            add(getJPanelProj(proposedProjection), null);
307
        }
308
    }
309

    
310
    public void enableSpatialControls(boolean enable) {
311
        super.enableSpatialControls(enable);
312
        getGeomComboBox().setEnabled(enable);
313

    
314
        getActivateWACheckBox().setEnabled(enable);
315

    
316
        boolean there_is_view = ((mControl != null)
317
                && (mControl.getViewPort().getAdjustedEnvelope() != null));
318

    
319
        getGetviewButton().setEnabled(enable && there_is_view);
320
        getTopTextField().setEnabled(enable);
321
        getBottomTextField().setEnabled(enable);
322
        getRightTextField().setEnabled(enable);
323
        getLeftTextField().setEnabled(enable);
324
    }
325

    
326
    public void actionPerformed(ActionEvent e) {
327
        super.actionPerformed(e);
328
        Object src = e.getSource();
329

    
330
        if (src == getviewButton) {
331
            getViewIntoFourBounds();
332
            parent.checkFinishable();
333
        }
334

    
335
        if (src == activateWACheckBox) {
336
            enableWASettings(activateWACheckBox.isSelected());
337
            parent.checkFinishable();
338
        }
339
    }
340

    
341
    private void enableWASettings(boolean b) {
342
        getviewButton.setEnabled(b
343
                && (mControl.getViewPort().getAdjustedEnvelope() != null));
344
        rightTextField.setEnabled(b);
345
        leftTextField.setEnabled(b);
346
        topTextField.setEnabled(b);
347
        bottomTextField.setEnabled(b);
348
    }
349

    
350
    private void getViewIntoFourBounds() {
351
        Envelope rect = mControl.getViewPort().getAdjustedEnvelope();
352

    
353
        IProjection viewProj = mControl.getViewPort().getProjection();
354
        IProjection srcProj = this.getProjection();
355
        if (viewProj == null || srcProj == null) {
356
            messageDialog(Messages.getText(
357
                    "_Unable_to_reproject_viewport_Default_bounds_used"),
358
                    JOptionPane.WARNING_MESSAGE);
359
            setLargeArea(srcProj);
360
        } else {
361
            try {
362
                ICoordTrans ct = null;
363
                if (viewProj.getAbrev().compareToIgnoreCase(
364
                        srcProj.getAbrev()) != 0) {
365
                    /*
366
                             * Reprojection needed
367
                     */
368
                    ct = viewProj.getCT(srcProj);
369
                    rect = rect.convert(ct);
370
                }
371
                topTextField.setText(getFormattedDouble(rect.getMaximum(1)));
372
                bottomTextField.setText(getFormattedDouble(rect.getMinimum(1)));
373
                rightTextField.setText(getFormattedDouble(rect.getMaximum(0)));
374
                leftTextField.setText(getFormattedDouble(rect.getMinimum(0)));
375
                if (ct != null) {
376
                    messageDialog(Messages.getText(
377
                            "_Viewport_bounds_reprojected_to_datasource_CRS"),
378
                            JOptionPane.WARNING_MESSAGE);
379
                }
380
            } catch (Exception exc) {
381
                messageDialog(Messages.getText(
382
                        "_Unable_to_reproject_viewport_Default_bounds_used"),
383
                        JOptionPane.WARNING_MESSAGE);
384
                setLargeArea(srcProj);
385
            }
386
        }
387

    
388
    }
389

    
390
    private void setLargeArea(IProjection proj) {
391

    
392
        if (proj == null || proj.isProjected()) {
393
            topTextField.setText(getFormattedDouble(20000000d));
394
            bottomTextField.setText(getFormattedDouble(-20000000d));
395
            rightTextField.setText(getFormattedDouble(20000000d));
396
            leftTextField.setText(getFormattedDouble(-20000000d));
397
        } else {
398
            topTextField.setText(getFormattedDouble(90d));
399
            bottomTextField.setText(getFormattedDouble(-90d));
400
            rightTextField.setText(getFormattedDouble(180d));
401
            leftTextField.setText(getFormattedDouble(-180d));
402
        }
403
    }
404

    
405
    private void messageDialog(String msg, int msgType) {
406

    
407
        JOptionPane.showMessageDialog(
408
                ApplicationLocator.getManager().getRootComponent(),
409
                msg,
410
                Messages.getText("_Working_area"),
411
                msgType);
412
    }
413

    
414
    public Envelope getWorkingArea() {
415
        if (!activateWACheckBox.isSelected()) {
416
            return null;
417
        }
418

    
419
        double maxx;
420
        double maxy;
421
        double minx;
422
        double miny;
423

    
424
        try {
425
            maxx = Double.parseDouble(rightTextField.getText());
426
            miny = Double.parseDouble(bottomTextField.getText());
427
            minx = Double.parseDouble(leftTextField.getText());
428
            maxy = Double.parseDouble(topTextField.getText());
429
        } catch (NumberFormatException nfe) {
430
            logger.info("Not valid value in working area: " + nfe.getMessage());
431
            return null;
432
        }
433
        GeometryManager geoMan = GeometryLocator.getGeometryManager();
434

    
435
        try {
436
            return geoMan.createEnvelope(minx, miny, maxx, maxy,
437
                    SUBTYPES.GEOM2D);
438
        } catch (CreateEnvelopeException e) {
439
            // FIXME Exception
440
            throw new RuntimeException(e);
441
        }
442
    }
443

    
444
    public boolean combosHaveItems() {
445
        if (!super.combosHaveItems()) {
446
            return false;
447
        }
448

    
449
        if (getGeomComboBox().getItemCount() == 0) {
450
            return false;
451
        }
452

    
453
        return true;
454
    }
455

    
456
    public void repaint() {
457
        super.repaint();
458
        getGeomComboBox().updateUI();
459
    }
460

    
461
    public String getGeoFieldName() {
462
        if (getGeomComboBox().getSelectedItem() == null) {
463
            return null;
464
        }
465
        return getGeomComboBox().getSelectedItem().toString();
466
    }
467

    
468
    public IProjection getProjection() {
469
        return panelProj.getCurProj();
470
    }
471

    
472
    public ICoordTrans getCoordTrans() {
473
        // TODO: Implementar con el nuevo API
474
        return null;
475
    }
476

    
477
}