Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / General.java @ 37845

History | View | Annotate | Download (16.9 KB)

1
/*
2
 * Created on 31-ene-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.cit.gvsig.project.documents.view.legend.gui;
45

    
46
import java.awt.BorderLayout;
47
import java.awt.ComponentOrientation;
48
import java.awt.Dimension;
49
import java.awt.FlowLayout;
50
import java.awt.GridBagConstraints;
51
import java.awt.GridBagLayout;
52
import java.awt.Insets;
53
import java.awt.SystemColor;
54
import java.awt.event.ActionEvent;
55
import java.awt.event.ActionListener;
56
import java.awt.geom.Rectangle2D;
57
import java.text.NumberFormat;
58
import java.text.ParseException;
59

    
60
import javax.swing.BorderFactory;
61
import javax.swing.ButtonGroup;
62
import javax.swing.JCheckBox;
63
import javax.swing.JLabel;
64
import javax.swing.JPanel;
65
import javax.swing.JRadioButton;
66
import javax.swing.JScrollPane;
67
import javax.swing.JTextArea;
68
import javax.swing.JTextField;
69
import javax.swing.border.BevelBorder;
70
import javax.swing.border.EtchedBorder;
71
import javax.swing.border.TitledBorder;
72

    
73
import org.cresques.cts.ICoordTrans;
74
import org.cresques.cts.IProjection;
75
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
76
import org.gvsig.gui.beans.swing.JBlank;
77

    
78
import com.hardcode.driverManager.Driver;
79
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
80
import com.hardcode.gdbms.engine.data.driver.DriverException;
81
import com.iver.andami.PluginServices;
82
import com.iver.andami.messages.NotificationManager;
83
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
84
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
85
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
86
import com.iver.cit.gvsig.fmap.layers.FLayer;
87
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
88
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
89
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
90
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
91
import com.iver.cit.gvsig.fmap.layers.VectorialDBAdapter;
92
import com.iver.cit.gvsig.fmap.layers.VectorialFileAdapter;
93
import com.iver.cit.gvsig.project.documents.view.legend.CreateSpatialIndexMonitorableTask;
94
import com.iver.utiles.swing.threads.IMonitorableTask;
95

    
96

    
97

    
98
/**
99
 * This class implements an useful and intuitive graphic interface to change some
100
 * properties of the layer. This class extends AbstractThemeManager. The properties
101
 * that allow modified are the name of the layer, the scale, if the user want to use
102
 * Spatial index or not and the HyperLink. Also shows a scroll with a little resume
103
 * with the properties of the layer.
104
 * @author jmorell
105
 *
106
 */
107
public class General extends AbstractThemeManagerPage {
108

    
109
        private static final long serialVersionUID = 1L;
110
    private FLayer layer;
111
    private NumberFormat nf = NumberFormat.getInstance();
112
        private JPanel pnlLayerName = null;
113
        private GridBagLayoutPanel pnlScale = null;
114
        private JPanel pnlProperties = null;
115
        private JLabel lblLayerName = null;
116
        private JTextField txtLayerName = null;
117
        private JTextField txtMaxScale = null;
118
        private JTextArea propertiesTextArea = null;
119
        private JRadioButton rdBtnShowAlways = null;
120
        private JRadioButton rdBtnDoNotShow = null;
121
        private JTextField txtMinScale = null;
122
    private JCheckBox jCheckBoxSpatialIndex = null;
123
    private JScrollPane scrlProperties;
124

    
125

    
126
        public General() {
127
                super();
128
                initialize();
129
        }
130

    
131
        private  void initialize() {
132
                this.setLayout(new BorderLayout());
133
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
134
                aux.addComponent(getPnlLayerName());
135
                aux.addComponent(new JBlank(10, 10));
136
                aux.addComponent(getJCheckBoxSpatialIndex());
137
                aux.addComponent("", getPnlScale());
138
                aux.addComponent("", getPnlProperties());
139
                aux.setPreferredSize(getPreferredSize());
140
                this.add(aux, BorderLayout.CENTER);
141
                this.add(new JBlank(5, 10), BorderLayout.WEST);
142
                this.add(new JBlank(5, 10), BorderLayout.EAST);
143
        }
144

    
145

    
146
        /**
147
         * Sets the necessary properties in the panel. This properties are
148
         * extracted from the layer. With this properties fills the TextFields,
149
         * ComboBoxes and the rest of GUI components.
150
         * @param FLayer layer,
151
         */
152
        public void setModel(FLayer layer) {
153
                this.layer = layer;
154

    
155
        if (layer instanceof FLyrVect) {
156
            FLyrVect lyrVect = (FLyrVect) layer;
157

    
158

    
159
            if(lyrVect.getISpatialIndex() == null) {
160
                getJCheckBoxSpatialIndex().setSelected(false);
161
            } else {
162
                getJCheckBoxSpatialIndex().setSelected(true);
163
            }
164
        }
165
                if (layer.getMinScale() != -1)
166
                    getTxtMaxScale().setText(nf.format(layer.getMinScale()));
167
                if (layer.getMaxScale() != -1)
168
                    getTxtMinScale().setText(nf.format(layer.getMaxScale()));
169
                if (layer.getMinScale() == -1 && layer.getMaxScale() == -1) {
170
                        getRdBtnShowAlways().setSelected(true);
171
                        txtMaxScale.setEnabled(false);
172
                        txtMinScale.setEnabled(false);
173

    
174
                } else {
175
                        getRdBtnDoNotShowWhen().setSelected(true);
176
                        txtMaxScale.setEnabled(true);
177
                        txtMinScale.setEnabled(true);
178
                }
179
                txtLayerName.setText(layer.getName());
180
                showLayerInfo();
181
    }
182

    
183

    
184
        private JPanel getPnlLayerName() {
185
                if (pnlLayerName == null) {
186
                        lblLayerName = new JLabel();
187
                        pnlLayerName = new JPanel();
188
                        lblLayerName.setText(PluginServices.getText(this,"Nombre") + ":");
189
                        lblLayerName.setComponentOrientation(ComponentOrientation.UNKNOWN);
190
                        pnlLayerName.setComponentOrientation(ComponentOrientation.UNKNOWN);
191
                        pnlLayerName.add(lblLayerName, null);
192
                        pnlLayerName.add(getTxtLayerName(), null);
193
                }
194
                return pnlLayerName;
195
        }
196

    
197
        private GridBagLayoutPanel getPnlScale() {
198
                if (pnlScale == null) {
199
                        pnlScale = new GridBagLayoutPanel();
200
                        pnlScale.setBorder(BorderFactory.createTitledBorder(
201
                                    BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
202
                                        PluginServices.getText(this, "rango_de_escalas"),
203
                                        TitledBorder.DEFAULT_JUSTIFICATION,
204
                                        TitledBorder.DEFAULT_POSITION, null, null)
205
                                );
206
                        ButtonGroup buttonGroup = new ButtonGroup();
207
                        buttonGroup.add(getRdBtnShowAlways());
208
                        buttonGroup.add(getRdBtnDoNotShowWhen());
209
                        pnlScale.addComponent(getRdBtnShowAlways());
210
                        pnlScale.addComponent(getRdBtnDoNotShowWhen());
211
                        JPanel aux;
212

    
213

    
214
                        aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
215
                        aux.add(getTxtMaxScale());
216
                        aux.add(new JLabel("(" + PluginServices.getText(this,"escala_maxima") + ")"));
217

    
218
                        GridBagLayoutPanel aux2;
219
                        aux2 = new GridBagLayoutPanel();
220
                        aux2.addComponent(PluginServices.getText(
221
                                        this,"este_por_encima_de")+" 1:",
222
                                        aux);
223
                        aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
224
                        aux.add(getTxtMinScale());
225
                        aux.add(new JLabel("(" + PluginServices.getText(this,"escala_minima") + ")"));
226

    
227
                        aux2.addComponent(PluginServices.getText(
228
                                        this,"este_por_debajo_de_")+" 1:",
229
                                        aux);
230

    
231
                        pnlScale.addComponent(new JBlank(20, 1), aux2);
232

    
233
                        pnlScale.addComponent(new JBlank(20, 1), aux2);
234

    
235
                }
236
                return pnlScale;
237
        }
238
        
239
    /**
240
     * This method initializes pnlProperties, the jpanel which contains the
241
     * ScrollPane with the properties.
242
     */
243
        private JPanel getPnlProperties() {
244
                if (pnlProperties == null) {
245
                        pnlProperties = new JPanel(new GridBagLayout());
246
                        pnlProperties.setBorder(BorderFactory.createTitledBorder(
247
                                    BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
248
                                        PluginServices.getText(this, "propiedades"),
249
                                        TitledBorder.DEFAULT_JUSTIFICATION,
250
                                        TitledBorder.DEFAULT_POSITION, null, null)
251
                                );
252
                        GridBagConstraints constraints = new GridBagConstraints();
253
                        constraints.insets = new Insets(5, 5, 5, 5);
254
            constraints.fill = GridBagConstraints.BOTH;
255
                        constraints.weightx = 1.0;
256
                        constraints.weighty = 1.0;
257
                        pnlProperties.add(getScrlProperties(), constraints);
258
                }
259
                return pnlProperties;
260
        }
261

    
262
        private JTextField getTxtLayerName() {
263
                if (txtLayerName == null) {
264
                        txtLayerName = new JTextField(25);
265
                        txtLayerName.setEditable(true);
266
                }
267
                return txtLayerName;
268
        }
269

    
270
        private JTextField getTxtMaxScale() {
271
                if (txtMaxScale == null) {
272
                        txtMaxScale = new JTextField(15);
273
                        txtMaxScale.setEnabled(false);
274
                }
275
                return txtMaxScale;
276
        }
277
        
278
    /**
279
     * This method initializes propertiesTextArea, where are display the
280
     * properties of the layer
281
     */
282
        private JTextArea getPropertiesTextArea() {
283
                if (propertiesTextArea == null) {
284
                        propertiesTextArea = new JTextArea();
285
                        propertiesTextArea.setEditable(false);
286
                        propertiesTextArea.setBackground(SystemColor.control);
287

    
288
                }
289
                return propertiesTextArea;
290
        }
291

    
292
        private JScrollPane getScrlProperties() {
293
                if (scrlProperties == null) {
294
                        scrlProperties = new JScrollPane();
295
                        scrlProperties.setViewportView(getPropertiesTextArea());
296
                        scrlProperties.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
297
                        scrlProperties.setPreferredSize(new Dimension(350, 180));
298
                }
299
                return scrlProperties;
300
        }
301

    
302
        private JRadioButton getRdBtnShowAlways() {
303
                if (rdBtnShowAlways == null) {
304
                        rdBtnShowAlways = new JRadioButton();
305
                        rdBtnShowAlways.setText(PluginServices.getText(this,"Mostrar_siempre"));
306
                        rdBtnShowAlways.setSelected(true);
307
                        rdBtnShowAlways.addActionListener(new ActionListener() {
308
                                public void actionPerformed(ActionEvent e) {
309
                                        txtMaxScale.setEnabled(false);
310
                                        txtMinScale.setEnabled(false);
311
                                }
312
                        });
313
                }
314
                return rdBtnShowAlways;
315
        }
316

    
317
        private JRadioButton getRdBtnDoNotShowWhen() {
318
                if (rdBtnDoNotShow == null) {
319
                        rdBtnDoNotShow = new JRadioButton();
320
                        rdBtnDoNotShow.setText(PluginServices.getText(this,"No_mostrar_la_capa_cuando_la_escala"));
321
                        rdBtnDoNotShow.addActionListener(new ActionListener() {
322
                                public void actionPerformed(ActionEvent e) {
323
                                        txtMaxScale.setEnabled(true);
324
                                        txtMinScale.setEnabled(true);
325
                                }
326
                        });
327
                }
328
                return rdBtnDoNotShow;
329
        }
330

    
331
        private JTextField getTxtMinScale() {
332
                if (txtMinScale == null) {
333
                        txtMinScale = new JTextField(15);
334
                        txtMinScale.setEnabled(false);
335
                }
336
                return txtMinScale;
337
        }
338

    
339
        private String getLayerName(){
340
                return txtLayerName.getText().toString();
341
        }
342

    
343

    
344
    private JCheckBox getJCheckBoxSpatialIndex() {
345
            if (jCheckBoxSpatialIndex == null) {
346
                    jCheckBoxSpatialIndex = new JCheckBox();
347
                    jCheckBoxSpatialIndex.setBounds(2, 33, 242, 23);
348
                    jCheckBoxSpatialIndex.setText(PluginServices.getText(this,"Usar_indice_espacial"));
349
            }
350
            return jCheckBoxSpatialIndex;
351
    }
352

    
353

    
354
    private boolean isSpatialIndexSelected() {
355
        return getJCheckBoxSpatialIndex().isSelected();
356
    }
357

    
358
    /**
359
     * Add the information of the layer to the textArea
360
     */
361
    private void showLayerInfo() {
362
                try {
363
                        String info = ((FLyrDefault)layer).getInfoString();
364
                        if (info == null) {
365
                                Rectangle2D fullExtentViewPort = layer.getFullExtent();
366
                                IProjection viewPortProj = layer.getMapContext().getProjection();
367
                                info = PluginServices.getText(this,"Extent") + " " +
368
                                viewPortProj.getAbrev() +
369
                                " (" + PluginServices.getText(this, "view_projection") + "):\n\t" +
370
                                PluginServices.getText(this,"Superior") + ":\t" + fullExtentViewPort.getMaxY() + "\n\t" +
371
                                PluginServices.getText(this,"Inferior") + ":\t" + fullExtentViewPort.getMinY() + "\n\t" +
372
                                PluginServices.getText(this,"Izquierda") + ":\t" + fullExtentViewPort.getMinX() + "\n\t" +
373
                                PluginServices.getText(this,"Derecha") + ":\t" + fullExtentViewPort.getMaxX() + "\n";
374

    
375
                                // show layer native projection
376
                                if (!layer.getProjection().getAbrev().equals(viewPortProj.getAbrev())) {
377
                                        IProjection nativeLayerProj = layer.getProjection();
378
                                        ICoordTrans ct = viewPortProj.getCT(nativeLayerProj);
379
                                        Rectangle2D nativeLayerExtent = ct.convert(fullExtentViewPort);
380
                                        info += PluginServices.getText(this,"Extent") + " " +
381
                                        nativeLayerProj.getAbrev() +
382
                                        " (" + PluginServices.getText(this, "layer_native_projection") + "):\n\t" +
383
                                        PluginServices.getText(this,"Superior") + ":\t" + nativeLayerExtent.getMaxY() + "\n\t" +
384
                                        PluginServices.getText(this,"Inferior") + ":\t" + nativeLayerExtent.getMinY() + "\n\t" +
385
                                        PluginServices.getText(this,"Izquierda") + ":\t" + nativeLayerExtent.getMinX() + "\n\t" +
386
                                        PluginServices.getText(this,"Derecha") + ":\t" + nativeLayerExtent.getMaxX() + "\n";
387

    
388
                                }
389
                                if (layer instanceof FLyrVect) {
390
                                        ReadableVectorial rv=((FLyrVect)layer).getSource();
391
                                        if (rv instanceof VectorialEditableAdapter) {
392
                                                rv=((VectorialEditableAdapter) ((FLyrVect)layer).getSource()).getOriginalAdapter();
393
                                        }
394

    
395
                                        info = info + PluginServices.getText(this,"Origen_de_datos") + ": ";
396
                                        if (rv instanceof VectorialFileAdapter) {
397
                                                Driver driver=rv.getDriver();
398
                                                info = info + "\n" + driver.getName() + "\n" +
399
                                                PluginServices.getText(this,"fichero") +": " + ((VectorialFileAdapter)rv).getFile();
400
                                        } else if (rv instanceof VectorialDBAdapter) {
401
                                                DBLayerDefinition dbdef = ((VectorialDBAdapter) rv).getLyrDef();
402
                                                info = info + "\n" + rv.getDriver().getName() + "\n";
403

    
404
                                                try {
405
                                                        info = info +
406
                                                        PluginServices.getText(this,"url") +": " + dbdef.getConnection().getURL() + "\n";
407
                                                } catch (Exception e) {
408
                                                        e.printStackTrace();
409
                                                }
410

    
411
                                                info = info +
412
                                                PluginServices.getText(this,"Tabla") +": " + dbdef.getTableName() + "\n";
413
                                        } else if (rv instanceof VectorialAdapter){
414
                                                info = info + "\n" + rv.getDriver().getName() + "\n";
415
                                        }
416

    
417
                                        info+="\n" + PluginServices.getText(this,"type")+ ": "+((FLyrVect)layer).getTypeStringVectorLayer() + "\n";
418

    
419

    
420

    
421

    
422
                                } else {
423
                                        info = info + PluginServices.getText(this,"Origen_de_datos") + ": " + layer.getName();
424
                                }
425

    
426
                        }
427
                        getPropertiesTextArea().setText(info);
428

    
429
                } catch (ReadDriverException e) {
430
                        NotificationManager.addError(e.getMessage(), e);
431
                }
432

    
433
    }
434

    
435
    /**
436
     * Returns true or false if the scale is selected
437
     */
438
        private boolean isScaleActive() {
439
                return getRdBtnDoNotShowWhen().isSelected();
440
        }
441

    
442
    /**
443
     * Instantiates a ITask (@see com.iver.utiles.swing.threads.ITask) to create
444
     * a spatial index for the selected layer in background. This task also
445
     * allow to monitor process evolution, and to cancel this process.
446
     * 
447
     * @throws DriverException
448
     * @throws DriverIOException
449
     */
450
    private IMonitorableTask getCreateSpatialIndexTask() throws ReadDriverException {
451
            // FIXME REVISAR ESTO (Quizas lanzar TaskException)
452
            return new CreateSpatialIndexMonitorableTask((FLyrVect)layer);
453
    }
454

    
455
        public void acceptAction() {
456
        // does nothing
457
        }
458

    
459
        public void cancelAction() {
460
                // does nothing
461
        }
462

    
463
            /**
464
     * When we press the apply button, sets the new properties of the layer that
465
     * the user modified using the UI components
466
     */
467
        public void applyAction() {
468
                if (isScaleActive()) {
469
                        try        {
470
                                layer.setMinScale((nf.parse(getTxtMaxScale().getText())).doubleValue());
471
                        } catch (ParseException ex)        {
472
                            if (getTxtMaxScale().getText().compareTo("") == 0)
473
                                layer.setMinScale(-1);
474
                            else
475
                                System.err.print(ex.getLocalizedMessage());
476
                        }
477

    
478
                        try        {
479
                            layer.setMaxScale((nf.parse(getTxtMinScale().getText())).doubleValue());
480
                        } catch (ParseException ex)        {
481
                            if (getTxtMinScale().getText().compareTo("") == 0)
482
                                layer.setMaxScale(-1);
483
                            else
484
                                System.err.print(ex.getLocalizedMessage());
485
                        }
486

    
487
                } else {
488
                layer.setMinScale(-1);
489
                layer.setMaxScale(-1);
490
                }
491

    
492
                if (!getLayerName().equals(layer.getName())){
493
                        layer.setName(getLayerName());
494
                }
495

    
496
        if (layer instanceof FLyrVect){
497
            FLyrVect lyrVect = (FLyrVect) layer;
498
            if (isSpatialIndexSelected()) {
499
                    if(lyrVect.getISpatialIndex() == null) {
500
                        //AZABALA
501
                        try {
502
                                                PluginServices.
503
                                                        cancelableBackgroundExecution(getCreateSpatialIndexTask());
504
                                        } catch (ReadDriverException e) {
505
                                                NotificationManager.addError(e.getMessage(), e);
506
                                        }
507
                }
508
            }
509
            //AZABALA
510
            /*
511
             * If we unselect the spatial index checkbox...Must we delete
512
             * spatial index file, or we only have to put Spatial Index
513
             * reference to null?. I have followed the second choice
514
             */
515
            else{
516
                lyrVect.deleteSpatialIndex();
517
            }
518

    
519

    
520
        }
521

    
522
        }
523

    
524

    
525
        public String getName() {
526
                return PluginServices.getText(this,"General");
527
        }
528
}