Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / vectorialdb / wizard / UserTableSettingsVectorialPanel.java @ 42080

History | View | Annotate | Download (14.9 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

    
25
package org.gvsig.geodb.vectorialdb.wizard;
26

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

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

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

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

    
59

    
60
public class UserTableSettingsVectorialPanel extends UserTableSettingsPanel {
61
    private static Logger logger = LoggerFactory
62
                        .getLogger(UserTableSettingsPanel.class.getName());
63

    
64
    private FieldComboItem[] geos;
65
        private JComboBox geomComboBox = null;
66
    private JLabel geomLabel = null;
67
        private JLabel waLabel = null;
68
        private JLabel topLabel = null;
69
        private JTextField topTextField = null;
70
        private JTextField bottomTextField = null;
71
        private JTextField rightTextField = null;
72
        private JTextField leftTextField = null;
73
        private JLabel bottomLabel = null;
74
        private JLabel rightLabel = null;
75
        private JLabel leftLabel = null;
76
        private JButton getviewButton = null;
77
        private JCheckBox activateWACheckBox = null;
78
    private MapControl mControl = null;
79
        private CRSSelectPanel panelProj;
80
        
81
    private IProjection proposedProjection = null;
82
    // private IProjection currentProj;
83
        
84
        private JPanel this_panel = null;
85

    
86
    public UserTableSettingsVectorialPanel(FieldComboItem[] idComboItems,
87
                        FieldComboItem[] geoComboItems, String initialLayerName,
88
                        MapControl mapc, boolean empty, WizardVectorialDB _p,
89
                        DBStoreParameters parameters,
90
                        IProjection proposed_proj) {
91
            super();
92
            
93
            this_panel = this;
94
                setInitValues(idComboItems, initialLayerName, empty, _p, parameters);
95

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

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

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

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

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

    
169

    
170
    public boolean hasValidValues() {
171
                if (!super.hasValidValues()) {
172
                        return false;
173
                }
174

    
175
                if ((activateWACheckBox.isSelected()) && (getWorkingArea() == null)) {
176
                        return false;
177
                }
178

    
179
                return true;
180
        }
181

    
182
    private JComboBox getGeomComboBox() {
183
                if (geomComboBox == null) {
184
                        geomComboBox = new JComboBox();
185
                        // geomComboBox.setBounds(new java.awt.Rectangle(355, 55, 131, 21));
186
                        geomComboBox.setBounds(new java.awt.Rectangle(150, 140, 335, 21));
187
                        geomComboBox.addActionListener(new ActionListener() {
188
                public void actionPerformed(ActionEvent e) {
189
                    
190
                    if (panelProj == null) {
191
                        return;
192
                    }
193
                    /*
194
                     * Set the proj of the field in the 
195
                     * SRS chooser
196
                     */
197
                    FieldComboItem fci = (FieldComboItem) geomComboBox.getSelectedItem();
198
                    
199
                    if (fci == null) {
200
                        return;
201
                    }
202
                    
203
                    IProjection iproj = fci.getProjection();
204
                    if (iproj != null) {
205
                        /*
206
                         * Remove and recreate crs selection panel
207
                         */
208
                        this_panel.remove(panelProj);
209
                        panelProj = null;
210
                        add(getJPanelProj(iproj), null);
211
                    }
212
                }
213
                        });
214
                }
215

    
216
                return geomComboBox;
217
        }
218

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

    
227
                return topTextField;
228
        }
229

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

    
238
                return bottomTextField;
239
        }
240

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

    
248
                return rightTextField;
249
        }
250

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

    
258
                return leftTextField;
259
        }
260

    
261
        private JButton getGetviewButton() {
262
                if (getviewButton == null) {
263
                        getviewButton = new org.gvsig.gui.beans.swing.JButton();
264
                        getviewButton.addActionListener(this);
265
                        //getviewButton.setBounds(new java.awt.Rectangle(195, 145, 111, 26));
266
                        getviewButton.setBounds(new java.awt.Rectangle(455, 205, 31, 31));
267
            ImageIcon ii = IconThemeHelper.getImageIcon("geodb-get-view-extent"); 
268
            // ImageIcon ii = IconThemeHelper.getImageIcon("geodb-connection-add"); 
269
                        
270
            getviewButton.setMargin(new Insets(1,1,1,1));
271
                getviewButton.setIcon(ii);
272
                getviewButton.setToolTipText(
273
                        Messages.getText("_Get_view_extent"));
274
                getviewButton.setEnabled(false);
275
                }
276

    
277
                return getviewButton;
278
        }
279

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

    
289
                }
290

    
291
                return activateWACheckBox;
292
        }
293

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

    
304
                        getGeomComboBox().removeAllItems();
305

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

    
313
    public void enableSpatialControls(boolean enable) {
314
            super.enableSpatialControls(enable);
315
                getGeomComboBox().setEnabled(enable);
316

    
317
                getActivateWACheckBox().setEnabled(enable);
318

    
319
                boolean there_is_view = ((mControl != null) &&
320
                    (mControl.getViewPort().getAdjustedEnvelope() != null));
321

    
322
                getGetviewButton().setEnabled(enable && there_is_view);
323
                getTopTextField().setEnabled(enable);
324
                getBottomTextField().setEnabled(enable);
325
                getRightTextField().setEnabled(enable);
326
                getLeftTextField().setEnabled(enable);
327
        }
328

    
329
    public void actionPerformed(ActionEvent e) {
330
                super.actionPerformed(e);
331
                Object src = e.getSource();
332

    
333
        if (src == getviewButton) {
334
                        getViewIntoFourBounds();
335
                        parent.checkFinishable();
336
                }
337

    
338
                if (src == activateWACheckBox) {
339
                        enableWASettings(activateWACheckBox.isSelected());
340
                        parent.checkFinishable();
341
                }
342
        }
343

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

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

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

    
408
    private void messageDialog(String msg, int msgType) {
409
        
410
        JOptionPane.showMessageDialog(
411
            ApplicationLocator.getManager().getRootComponent(),
412
            msg,
413
            Messages.getText("_Working_area"),
414
            msgType);
415
    }
416

    
417
    public Envelope getWorkingArea() {
418
                if (!activateWACheckBox.isSelected()) {
419
                        return null;
420
                }
421

    
422
                double maxx;
423
                double maxy;
424
                double minx;
425
                double miny;
426

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

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

    
447
    public boolean combosHaveItems() {
448
                if (!super.combosHaveItems()) {
449
                        return false;
450
                }
451

    
452
                if (getGeomComboBox().getItemCount() == 0) {
453
                        return false;
454
                }
455

    
456
                return true;
457
        }
458

    
459
    public void repaint() {
460
                super.repaint();
461
                getGeomComboBox().updateUI();
462
        }
463

    
464
    public String getGeoFieldName() {
465
                if (getGeomComboBox().getSelectedItem() == null) {
466
                        return null;
467
                }
468
                return getGeomComboBox().getSelectedItem().toString();
469
        }
470
    public IProjection getProjection() {
471
                return panelProj.getCurProj();
472
        }
473

    
474
}