Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2060 / extensions / extGeoDB / src / org / gvsig / geodb / vectorialdb / wizard / UserTableSettingsVectorialPanel.java @ 39350

History | View | Annotate | Download (11.5 KB)

1
/* 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

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

    
28
package org.gvsig.geodb.vectorialdb.wizard;
29

    
30
import java.awt.Insets;
31
import java.awt.event.ActionEvent;
32

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

    
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.addlayer.AddLayerDialog;
47
import org.gvsig.app.gui.panels.CRSSelectPanel;
48
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
49
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
50
import org.gvsig.fmap.geom.GeometryLocator;
51
import org.gvsig.fmap.geom.GeometryManager;
52
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
53
import org.gvsig.fmap.geom.primitive.Envelope;
54
import org.gvsig.fmap.mapcontrol.MapControl;
55
import org.gvsig.i18n.Messages;
56

    
57

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

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

    
80
    public UserTableSettingsVectorialPanel(FieldComboItem[] idComboItems,
81
                        FieldComboItem[] geoComboItems, String initialLayerName,
82
                        MapControl mapc, boolean empty, WizardVectorialDB _p,
83
                        DBStoreParameters parameters,
84
                        IProjection curProjection) {
85
            super();
86
                setInitValues(idComboItems, initialLayerName, empty, _p, parameters);
87

    
88
        mControl = mapc;
89
                geos = geoComboItems;
90
                currentProj = curProjection;
91
                initialize(empty);
92
        }
93

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

    
127
                add(getGeomComboBox(), null);
128
                add(geomLabel, null);
129
            add(waLabel, null);
130
                add(topLabel, null);
131
                add(getTopTextField(), null);
132
                add(getBottomTextField(), null);
133
                add(getRightTextField(), null);
134
                add(getLeftTextField(), null);
135
                add(bottomLabel, null);
136
                add(rightLabel, null);
137
                add(leftLabel, null);
138
                add(getGetviewButton(), null);
139
                add(getActivateWACheckBox(), null);
140
                loadValues(_empty);
141
        }
142

    
143
    private CRSSelectPanel getJPanelProj() {
144
                if (panelProj == null) {
145
                        panelProj = CRSSelectPanel.getPanel(currentProj);
146

    
147
                        panelProj.setTransPanelActive(true);
148
                        panelProj.setBounds(70, 165, 350, 21);
149
                        panelProj.addActionListener(new java.awt.event.ActionListener() {
150
                                public void actionPerformed(java.awt.event.ActionEvent e) {
151
                                        if (panelProj.isOkPressed()) {
152
                                                AddLayerDialog
153
                                                                .setLastProjection(panelProj.getCurProj());
154
                                        }
155
                                }
156
                        });
157
                }
158
                return panelProj;
159
        }
160

    
161

    
162
    public boolean hasValidValues() {
163
                if (!super.hasValidValues()) {
164
                        return false;
165
                }
166

    
167
                if ((activateWACheckBox.isSelected()) && (getWorkingArea() == null)) {
168
                        return false;
169
                }
170

    
171
                return true;
172
        }
173

    
174
    private JComboBox getGeomComboBox() {
175
                if (geomComboBox == null) {
176
                        geomComboBox = new JComboBox();
177
                        // geomComboBox.setBounds(new java.awt.Rectangle(355, 55, 131, 21));
178
                        geomComboBox.setBounds(new java.awt.Rectangle(243, 140, 174, 21));
179
                }
180

    
181
                return geomComboBox;
182
        }
183

    
184
        private JTextField getTopTextField() {
185
                if (topTextField == null) {
186
                        topTextField = new JTextField();
187
                        topTextField.addKeyListener(this);
188
                        // topTextField.setBounds(new java.awt.Rectangle(15, 190, 111, 21));
189
                        topTextField.setBounds(new java.awt.Rectangle(65+310, 215, 70, 21));
190
                }
191

    
192
                return topTextField;
193
        }
194

    
195
        private JTextField getBottomTextField() {
196
                if (bottomTextField == null) {
197
                        bottomTextField = new JTextField();
198
                        bottomTextField.addKeyListener(this);
199
                        bottomTextField
200
                                        .setBounds(new java.awt.Rectangle(70+230, 215, 70, 21));
201
                }
202

    
203
                return bottomTextField;
204
        }
205

    
206
        private JTextField getRightTextField() {
207
                if (rightTextField == null) {
208
                        rightTextField = new JTextField();
209
                        rightTextField.addKeyListener(this);
210
                        rightTextField.setBounds(new java.awt.Rectangle(75+150, 215, 70, 21));
211
                }
212

    
213
                return rightTextField;
214
        }
215

    
216
        private JTextField getLeftTextField() {
217
                if (leftTextField == null) {
218
                        leftTextField = new JTextField();
219
                        leftTextField.addKeyListener(this);
220
                        leftTextField.setBounds(new java.awt.Rectangle(80+70, 215, 70, 21));
221
                }
222

    
223
                return leftTextField;
224
        }
225

    
226
        private JButton getGetviewButton() {
227
                if (getviewButton == null) {
228
                        getviewButton = new org.gvsig.gui.beans.swing.JButton();
229
                        getviewButton.addActionListener(this);
230
                        //getviewButton.setBounds(new java.awt.Rectangle(195, 145, 111, 26));
231
                        getviewButton.setBounds(new java.awt.Rectangle(455, 205, 31, 31));
232
            ImageIcon ii = IconThemeHelper.getImageIcon("geodb-get-view-extent"); 
233
            // ImageIcon ii = IconThemeHelper.getImageIcon("geodb-connection-add"); 
234
                        
235
            getviewButton.setMargin(new Insets(1,1,1,1));
236
                getviewButton.setIcon(ii);
237
                getviewButton.setToolTipText(
238
                        Messages.getText("_Get_view_extent"));
239
                getviewButton.setEnabled(false);
240
                }
241

    
242
                return getviewButton;
243
        }
244

    
245
        private JCheckBox getActivateWACheckBox() {
246
                if (activateWACheckBox == null) {
247
                        activateWACheckBox = new JCheckBox();
248
                        activateWACheckBox.addActionListener(this);
249
//                        activateWACheckBox
250
//                                        .setBounds(new java.awt.Rectangle(15, 145, 21, 21));
251
                        activateWACheckBox
252
                                        .setBounds(new java.awt.Rectangle(5, 215, 21, 21));
253

    
254
                }
255

    
256
                return activateWACheckBox;
257
        }
258

    
259
    protected void loadValues(boolean is_empty) {
260
                super.loadValues(is_empty);
261
                if (is_empty) {
262
                        enableAlphaControls(false);
263
                        enableSpatialControls(false);
264
                        getActivateWACheckBox().setSelected(false);
265
                } else {
266
                        enableAlphaControls(true);
267
                        enableSpatialControls(true);
268

    
269
                        getGeomComboBox().removeAllItems();
270

    
271
                        for (int i = 0; i < geos.length; i++) {
272
                                getGeomComboBox().addItem(geos[i]);
273
                        }
274

    
275
                        add(getJPanelProj(), null);
276

    
277
                }
278
        }
279

    
280
    public void enableSpatialControls(boolean enable) {
281
            super.enableSpatialControls(enable);
282
                getGeomComboBox().setEnabled(enable);
283

    
284
                getActivateWACheckBox().setEnabled(enable);
285

    
286
                boolean there_is_view = ((mControl != null) &&
287
                    (mControl.getViewPort().getAdjustedEnvelope() != null));
288

    
289
                getGetviewButton().setEnabled(enable && there_is_view);
290
                getTopTextField().setEnabled(enable);
291
                getBottomTextField().setEnabled(enable);
292
                getRightTextField().setEnabled(enable);
293
                getLeftTextField().setEnabled(enable);
294
        }
295

    
296
    public void actionPerformed(ActionEvent e) {
297
                super.actionPerformed(e);
298
                Object src = e.getSource();
299

    
300
        if (src == getviewButton) {
301
                        getViewIntoFourBounds();
302
                        parent.checkFinishable();
303
                }
304

    
305
                if (src == activateWACheckBox) {
306
                        enableWASettings(activateWACheckBox.isSelected());
307
                        parent.checkFinishable();
308
                }
309
        }
310

    
311
    private void enableWASettings(boolean b) {
312
                getviewButton.setEnabled(b
313
                                && (mControl.getViewPort().getAdjustedEnvelope() != null));
314
                rightTextField.setEnabled(b);
315
                leftTextField.setEnabled(b);
316
                topTextField.setEnabled(b);
317
                bottomTextField.setEnabled(b);
318
        }
319

    
320
    private void getViewIntoFourBounds() {
321
                Envelope rect = mControl.getViewPort().getAdjustedEnvelope();
322
                topTextField.setText(getFormattedDouble(rect.getMaximum(1)));
323
                bottomTextField.setText(getFormattedDouble(rect.getMinimum(1)));
324
                rightTextField.setText(getFormattedDouble(rect.getMaximum(0)));
325
                leftTextField.setText(getFormattedDouble(rect.getMinimum(0)));
326
        }
327

    
328
    public Envelope getWorkingArea() {
329
                if (!activateWACheckBox.isSelected()) {
330
                        return null;
331
                }
332

    
333
                double maxx;
334
                double maxy;
335
                double minx;
336
                double miny;
337

    
338
                try {
339
                        maxx = Double.parseDouble(rightTextField.getText());
340
                        miny = Double.parseDouble(bottomTextField.getText());
341
                        minx = Double.parseDouble(leftTextField.getText());
342
                        maxy = Double.parseDouble(topTextField.getText());
343
                } catch (NumberFormatException nfe) {
344
                        logger.info("Not valid value in working area: " + nfe.getMessage());
345
                        return null;
346
                }
347
                GeometryManager geoMan = GeometryLocator.getGeometryManager();
348

    
349
                try {
350
                        return geoMan.createEnvelope(minx, miny, maxx, maxy,
351
                                        SUBTYPES.GEOM2D);
352
                } catch (CreateEnvelopeException e) {
353
                        // FIXME Exception
354
                        throw new RuntimeException(e);
355
                }
356
        }
357

    
358
    public boolean combosHaveItems() {
359
                if (!super.combosHaveItems()) {
360
                        return false;
361
                }
362

    
363
                if (getGeomComboBox().getItemCount() == 0) {
364
                        return false;
365
                }
366

    
367
                return true;
368
        }
369

    
370
    public void repaint() {
371
                super.repaint();
372
                getGeomComboBox().updateUI();
373
        }
374

    
375
    public String getGeoFieldName() {
376
                if (getGeomComboBox().getSelectedItem() == null) {
377
                        return null;
378
                }
379
                return getGeomComboBox().getSelectedItem().toString();
380
        }
381
    public IProjection getProjection() {
382
                return panelProj.getCurProj();
383
        }
384

    
385
}