Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / VectorialUniqueValue.java @ 10679

History | View | Annotate | Download (17.6 KB)

1 10679 jaume
/*
2
 * Created on 30-abr-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package com.iver.cit.gvsig.project.documents.view.legend.gui;
48
49
import java.awt.BorderLayout;
50
import java.awt.Component;
51
import java.awt.Dimension;
52
import java.awt.event.ActionEvent;
53
import java.awt.event.ActionListener;
54
import java.io.IOException;
55
56
import javax.swing.DefaultComboBoxModel;
57
import javax.swing.JButton;
58
import javax.swing.JCheckBox;
59
import javax.swing.JComboBox;
60
import javax.swing.JLabel;
61
import javax.swing.JOptionPane;
62
import javax.swing.JPanel;
63
64
import org.apache.log4j.Logger;
65
66
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
67
import com.hardcode.gdbms.engine.data.DataSource;
68
import com.hardcode.gdbms.engine.data.driver.DriverException;
69
import com.hardcode.gdbms.engine.values.NullValue;
70
import com.hardcode.gdbms.engine.values.Value;
71
import com.hardcode.gdbms.engine.values.ValueFactory;
72
import com.iver.andami.PluginServices;
73
import com.iver.andami.messages.NotificationManager;
74
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
75
import com.iver.cit.gvsig.fmap.core.symbols.PictureMarkerSymbol;
76
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
77
import com.iver.cit.gvsig.fmap.layers.FLayer;
78
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
79
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
80
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
81
import com.iver.cit.gvsig.fmap.rendering.Legend;
82
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
83
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
84
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
85
86
87
/**
88
 * DOCUMENT ME!
89
 *
90
 * @author fjp To change the template for this generated type comment go to
91
 *         Window>Preferences>Java>Code Generation>Code and
92
 *         Comments
93
 */
94
public class VectorialUniqueValue extends JPanel implements ILegendPanel {
95
    private static Logger logger = Logger.getLogger(VectorialUniqueValue.class.getName());
96
    private MyListener listener = new MyListener();
97
98
    // private TOC m_TOC;
99
    private VectorialUniqueValueLegend m_Renderer;
100
    private ClassifiableVectorial layer;
101
102
    //private boolean m_bCacheDirty = true;
103
    private FSymbolTable symbolTable;
104
    private JComboBox cmbFields;
105
    private JButton quitartodo;
106
    private JButton quitar;
107
    private JCheckBox chbUseDefault = null;
108
    private VectorialUniqueValueLegend auxLegend;
109
        private PictureMarkerSymbol previewSymbol;
110
        private JPanel pnlCenter;
111
112
    /**
113
     *
114
     */
115
    public VectorialUniqueValue() {
116
        super();
117
        initComponents();
118
    }
119
120
    /**
121
     * DOCUMENT ME!
122
     */
123
    protected void initComponents() {
124
        /* JLabel label = new JLabel();
125
           label.setIcon(new javax.swing.ImageIcon(Abrir.class.getClassLoader()
126
                                                                                                              .getResource("images/ValoresUnicos.png")));
127
           limagen[1] = new JLabel();
128
           limagen[1] = label; */
129
        JPanel botonestabla = new JPanel();
130
131
        JButton btnAddAll = new JButton(PluginServices.getText(this,
132
                    "Anadir_todos"));
133
        btnAddAll.setActionCommand("ADD_ALL_VALUES");
134
        btnAddAll.addActionListener(listener);
135
        botonestabla.add(btnAddAll);
136
137
        JButton btnAdd = new JButton(PluginServices.getText(this, "Anadir"));
138
        btnAdd.setActionCommand("ADD_VALUE");
139
        btnAdd.addActionListener(listener);
140
        botonestabla.add(btnAdd);
141
142
        quitartodo = new JButton(PluginServices.getText(this, "Quitar_todos"));
143
        quitartodo.setActionCommand("REMOVE_ALL");
144
        quitartodo.addActionListener(listener);
145
        botonestabla.add(quitartodo);
146
147
        quitar = new JButton(PluginServices.getText(this, "Quitar"));
148
        quitar.setActionCommand("REMOVE");
149
        quitar.addActionListener(listener);
150
        botonestabla.add(quitar);
151
152
                pnlCenter = new JPanel();
153
        pnlCenter.setLayout(new BorderLayout());
154
155
        cmbFields = new JComboBox();
156
        cmbFields.setActionCommand("FIELD_SELECTED");
157
        cmbFields.addActionListener(listener);
158
        cmbFields.setVisible(true);
159
160
        JPanel pAux1 = new JPanel();
161
162
        /* pAux1.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
163
           "Campo de clasificaci?n",
164
           javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
165
           javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); */
166
        pAux1.setPreferredSize(new Dimension(300, 50));
167
168
        JLabel lblFieldClassification = new JLabel(PluginServices.getText(
169
                    this, "Campo_de_clasificacion"));
170
        pAux1.add(lblFieldClassification);
171
        pAux1.add(cmbFields);
172
173
        this.setLayout(new BorderLayout());
174
        this.add(pAux1, BorderLayout.NORTH);
175
        this.add(pnlCenter, BorderLayout.CENTER);
176
        this.add(botonestabla, BorderLayout.SOUTH);
177
        pAux1.add(getChbUseDefault(), null);
178
    }
179
180
    /**
181
     * DOCUMENT ME!
182
     */
183
    private void fillTableValues() {
184
        DataSource elRs;
185
186
        try {
187
            elRs = ((FLyrVect) layer).getRecordset();
188
            logger.debug("elRs.start()");
189
            elRs.start();
190
191
            int idField = -1;
192
            String fieldName = (String) cmbFields.getSelectedItem();
193
            if (fieldName==null) {
194
                    JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_hay_campo_seleccionado"));
195
                    return;
196
            }
197
198
            System.out.println("Nombre del campo: " + fieldName);
199
            idField = elRs.getFieldIndexByName(fieldName);
200
            m_Renderer.setFieldName(fieldName);
201
202
            //long numReg = elRs.getRowCount();
203
            if (idField == -1) {
204
                System.err.println("Campo no reconocido " + fieldName);
205
206
                return;
207
            }
208
209
            symbolTable.removeAllItems();
210
211
            int numSymbols = 0;
212
            FSymbol theSymbol = null;
213
214
            //auxLegend=(VectorialUniqueValueLegend)m_lyr.getLegend();
215
            auxLegend = LegendFactory.createVectorialUniqueValueLegend(layer.getShapeType());
216
217
            Value clave;
218
219
            //Object resul;
220
            if (chbUseDefault.isSelected()) {
221
                auxLegend.getDefaultSymbol().setDescription("Default");
222
                auxLegend.addSymbol(new NullUniqueValue(),
223
                    auxLegend.getDefaultSymbol());
224
            }
225
226
            for (int j = 0; j < elRs.getRowCount(); j++) {
227
                clave = elRs.getFieldValue(j, idField);
228
229
                if (clave instanceof NullValue) {
230
                    continue;
231
                }
232
233
                ////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
234
                if (auxLegend.getSymbolByValue(clave) == null) {
235
                    //si no esta creado el simbolo se crea
236
                    theSymbol = new FSymbol(layer.getShapeType());
237
238
                    theSymbol.setDescription(clave.toString());
239
                    auxLegend.addSymbol(clave, theSymbol);
240
241
                    numSymbols++;
242
243
                    if (numSymbols == 100) {
244
                        int resp = JOptionPane.showConfirmDialog(this,
245
                                PluginServices.getText(this,
246
                                    "mas_de_100_simbolos"),
247
                                PluginServices.getText(this, "quiere_continuar"),
248
                                JOptionPane.YES_NO_OPTION,
249
                                JOptionPane.WARNING_MESSAGE);
250
251
                        if ((resp == JOptionPane.NO_OPTION) ||
252
                                (resp == JOptionPane.DEFAULT_OPTION)) {
253
                            return;
254
                        }
255
                    }
256
                }
257
            } // for
258
259
            symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
260
                auxLegend.getValues(), auxLegend.getDescriptions());
261
            elRs.stop();
262
        } catch (ReadDriverException e) {
263
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
264
        }
265
266
        quitartodo.setEnabled(true);
267
        quitar.setEnabled(true);
268
269
        //m_bCacheDirty = false;
270
    }
271
272
    /**
273
     * A partir de los registros de la tabla, regenera el FRenderer. (No solo
274
     * el symbolList, si no tambi?n el arrayKeys y el defaultRenderer
275
     */
276
    private void fillSymbolListFromTable() {
277
        Value clave;
278
        FSymbol theSymbol;
279
280
        // Borramos las anteriores listas:
281
        m_Renderer.clear();
282
283
        boolean bRestoValores = false; // PONERLO EN UN CHECKBOX
284
        int hasta;
285
        String fieldName = (String) cmbFields.getSelectedItem();
286
        m_Renderer.setFieldName(fieldName);
287
288
        if (bRestoValores) {
289
            hasta = symbolTable.getRowCount() - 1;
290
        } else {
291
            hasta = symbolTable.getRowCount();
292
        }
293
294
        for (int row = 0; row < symbolTable.getRowCount(); row++) {
295
            clave = (Value) symbolTable.getFieldValue(row, 1);
296
            theSymbol = (FSymbol) symbolTable.getFieldValue(row, 0);
297
            theSymbol.setDescription((String) symbolTable.getFieldValue(row, 2));
298
            m_Renderer.addSymbol(clave, theSymbol);
299
            System.out.println(clave);
300
        }
301
302
        if (bRestoValores) {
303
            theSymbol = (FSymbol) symbolTable.getFieldValue(hasta, 0);
304
            m_Renderer.setDefaultSymbol(theSymbol);
305
        }
306
    }
307
308
    /**
309
     * DOCUMENT ME!
310
     */
311
    private void fillFieldNames() {
312
        DataSource rs;
313
314
        try {
315
            rs = ((AlphanumericData) layer).getRecordset();
316
            logger.debug("rs.start()");
317
            rs.start();
318
319
            String[] nomFields = new String[rs.getFieldCount()];
320
321
            for (int i = 0; i < rs.getFieldCount(); i++) {
322
                nomFields[i] = rs.getFieldName(i).trim();
323
            }
324
325
            rs.stop();
326
327
            DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields);
328
            cmbFields.setModel(cM);
329
330
            // fieldsListValor.setSelectedIndex(0);
331
        } catch (ReadDriverException e) {
332
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
333
        }
334
335
        symbolTable.removeAllItems();
336
    }
337
338
    /**
339
     * DOCUMENT ME!
340
     *
341
     * @param lyr DOCUMENT ME!
342
     * @param r DOCUMENT ME!
343
     */
344
    public void setData(FLayer layer, Legend legend) {
345
            this.layer = (ClassifiableVectorial) layer;
346
              int shapeType = 0;
347
              try {
348
                      shapeType = this.layer.getShapeType();
349
              } catch (ReadDriverException e) {
350
                    NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
351
                }
352
353
              if (symbolTable != null)
354
                      pnlCenter.remove(symbolTable);
355
              symbolTable = new FSymbolTable("values", shapeType);
356
              pnlCenter.add(symbolTable, BorderLayout.CENTER);
357
358
        fillFieldNames();
359
360
        // Si es de valor ?nico, rellenamos la tabla
361
        // y seleccionamos el campo que estamos usando
362
        symbolTable.removeAllItems();
363
364
        if (legend instanceof VectorialUniqueValueLegend) {
365
            m_Renderer = (VectorialUniqueValueLegend) legend;
366
            getChbUseDefault().setSelected(m_Renderer.isUseDefaultSymbol());
367
            cmbFields.getModel().setSelectedItem(m_Renderer.getFieldName());
368
            symbolTable.fillTableFromSymbolList(m_Renderer.getSymbols(),
369
                m_Renderer.getValues(), m_Renderer.getDescriptions());
370
        } else {
371
                m_Renderer = new VectorialUniqueValueLegend(shapeType);
372
        }
373
    }
374
375
    /* (non-Javadoc)
376
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
377
     */
378
    public Legend getLegend() {
379
        fillSymbolListFromTable();
380
381
        if (auxLegend != null) {
382
            m_Renderer.setDefaultSymbol(auxLegend.getDefaultSymbol());
383
            m_Renderer.useDefaultSymbol(chbUseDefault.isSelected());
384
        }
385
386
        return m_Renderer;
387
    }
388
389
    /**
390
     * This method initializes chbUseDefault
391
     *
392
     * @return javax.swing.JCheckBox
393
     */
394
    private JCheckBox getChbUseDefault() {
395
        if (chbUseDefault == null) {
396
            chbUseDefault = new JCheckBox();
397
            chbUseDefault.setSelected(true);
398
            chbUseDefault.addActionListener(new java.awt.event.ActionListener() {
399
                    public void actionPerformed(java.awt.event.ActionEvent e) {
400
                            if (auxLegend==null)auxLegend=m_Renderer;
401
                            if (chbUseDefault.isSelected()) {
402
                            addDefault();
403
                        } else {
404
                            delDefault();
405
                        }
406
                    }
407
                });
408
            chbUseDefault.setText(PluginServices.getText(this, "resto_valores"));
409
        }
410
411
        return chbUseDefault;
412
    }
413
414
    /**
415
     * A?ade el resto de valores.
416
     */
417
    private void addDefault() {
418
        auxLegend.getDefaultSymbol().setDescription("Default");
419
        auxLegend.addSymbol(new NullUniqueValue(), auxLegend.getDefaultSymbol());
420
        symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
421
            new NullUniqueValue(), auxLegend.getDefaultSymbol().getDescription());
422
        symbolTable.repaint();
423
    }
424
425
    /**
426
     * Elimina el resto de valores que no estan representados por ning?n otro s?mbolo..
427
     */
428
    private void delDefault() {
429
        auxLegend.delSymbol(new NullUniqueValue());
430
        symbolTable.removeRow(new NullUniqueValue());
431
        symbolTable.repaint();
432
    }
433
434
    /**
435
     * Listener.
436
     *
437
     * @author Vicente Caballero Navarro
438
     */
439
    class MyListener implements ActionListener {
440
        //private FLyrShp m_layer;
441
        // private FPanelLegendValues m_Parent;
442
        public MyListener() { // FPanelLegendValues p) {
443
444
            // m_Parent = p;
445
        }
446
447
        /**
448
         * DOCUMENT ME!
449
         *
450
         * @param e DOCUMENT ME!
451
         */
452
        public void actionPerformed(ActionEvent e) {
453
            // rellenarValue();
454
            System.out.println("ActionEvent con " + e.getActionCommand());
455
456
            //modificar el combobox de valor
457
            if (e.getActionCommand() == "FIELD_SELECTED") {
458
                JComboBox cb = (JComboBox) e.getSource();
459
                String fieldName = (String) cb.getSelectedItem();
460
                System.out.println("Nombre del campo: " + fieldName);
461
                symbolTable.removeAllItems();
462
463
                if (fieldName != m_Renderer.getFieldName()) {
464
                    //m_bCacheDirty = true;
465
                    m_Renderer.setFieldName(fieldName);
466
                }
467
            }
468
469
            //A?adir todos los elementos por valor
470
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
471
                fillTableValues();
472
            }
473
474
            //A?adir un ?nico elemento
475
            if (e.getActionCommand() == "ADD_VALUE") {
476
                try {
477
                    symbolTable.addTableRecord(new FSymbol(
478
                            layer.getShapeType()),
479
                        ValueFactory.createValue(0.0), "0 - 0");
480
                } catch (ReadDriverException ex) {
481
                        NotificationManager.addError(PluginServices.getText(this, "getting_shape_type"), ex);
482
                }
483
484
                /*        a?adir("Nuevo_Valor");
485
                   aceptar.setEnabled(true);
486
                   quitartodo.setEnabled(true);
487
                   quitar.setEnabled(true); */
488
            }
489
490
            //Vacia la tabla
491
            if (e.getActionCommand() == "REMOVE_ALL") {
492
                symbolTable.removeAllItems();
493
494
                // aceptar.setEnabled(false);
495
            }
496
497
            //Quitar solo el elemento seleccionado
498
            if (e.getActionCommand() == "REMOVE") {
499
                symbolTable.removeSelectedRows();
500
            }
501
        }
502
    }
503
504
        public String getDescription() {
505
                return PluginServices.getText(this,"Dado_un_campo_de_atributos") + "," + PluginServices.getText(this,"muestra_los_elementos_de_la_capa_usando_un_simbolo_por_cada_valor_unico") + ".";
506
        }
507
508
        public ISymbol getPreviewSymbol() {
509
                if (previewSymbol == null) {
510
                        try {
511
                                previewSymbol = new PictureMarkerSymbol(
512
                                                this.getClass().getClassLoader()
513
                                                                .getResource("images/ValoresUnicos.png"));
514
                        } catch (IOException e) {
515
                                return null;
516
                        }
517
                }
518
                return previewSymbol;
519
        }
520
521
        public Class getParentClass() {
522
                return Categories.class;
523
        }
524
525
        public String getTitle() {
526
                return PluginServices.getText(this,"Valores_unicos");
527
        }
528
529
        public JPanel getPanel() {
530
                return this;
531
        }
532
533
        public Class getLegendClass() {
534
                return VectorialUniqueValueLegend.class;
535
        }
536
}