Statistics
| Revision:

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

History | View | Annotate | Download (16.7 KB)

1 7304 caballero
/*
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 7738 jaume
import java.awt.BorderLayout;
50 9116 caballero
import java.awt.Component;
51 7738 jaume
import java.awt.Dimension;
52
import java.awt.event.ActionEvent;
53
import java.awt.event.ActionListener;
54
55
import javax.swing.DefaultComboBoxModel;
56
import javax.swing.JButton;
57
import javax.swing.JCheckBox;
58
import javax.swing.JComboBox;
59
import javax.swing.JLabel;
60
import javax.swing.JOptionPane;
61
import javax.swing.JPanel;
62
63
import org.apache.log4j.Logger;
64
65 7304 caballero
import com.hardcode.gdbms.engine.data.DataSource;
66
import com.hardcode.gdbms.engine.data.driver.DriverException;
67
import com.hardcode.gdbms.engine.values.NullValue;
68
import com.hardcode.gdbms.engine.values.Value;
69
import com.hardcode.gdbms.engine.values.ValueFactory;
70
import com.iver.andami.PluginServices;
71
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
72
import com.iver.cit.gvsig.fmap.layers.FLayer;
73
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
74
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
75
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
76
import com.iver.cit.gvsig.fmap.rendering.Legend;
77
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
78
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
79
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
80
81
82
/**
83
 * DOCUMENT ME!
84
 *
85
 * @author fjp To change the template for this generated type comment go to
86
 *         Window>Preferences>Java>Code Generation>Code and
87
 *         Comments
88
 */
89
public class FPanelLegendValues extends JPanel implements ILegendPanel {
90
    private static Logger logger = Logger.getLogger(FPanelLegendValues.class.getName());
91
    private MyListener listener = new MyListener();
92
93
    // private TOC m_TOC;
94
    private VectorialUniqueValueLegend m_Renderer;
95
    private ClassifiableVectorial m_lyr;
96
97
    //private boolean m_bCacheDirty = true;
98
    private FSymbolTable m_symbolTable = new FSymbolTable("values");
99
    private JComboBox m_cboFields;
100
    private JButton quitartodo;
101
    private JButton quitar;
102
    private JCheckBox chbUseDefault = null;
103
    private VectorialUniqueValueLegend auxLegend;
104
105
    /**
106
     *
107
     */
108
    public FPanelLegendValues() {
109
        super();
110
        initComponents();
111
    }
112
113
    /**
114
     * DOCUMENT ME!
115
     */
116
    protected void initComponents() {
117
        /* JLabel label = new JLabel();
118
           label.setIcon(new javax.swing.ImageIcon(Abrir.class.getClassLoader()
119
                                                                                                              .getResource("images/ValoresUnicos.png")));
120
           limagen[1] = new JLabel();
121
           limagen[1] = label; */
122
        JPanel botonestabla = new JPanel();
123
124
        JButton btnAddAll = new JButton(PluginServices.getText(this,
125
                    "Anadir_todos"));
126
        btnAddAll.setActionCommand("ADD_ALL_VALUES");
127
        btnAddAll.addActionListener(listener);
128
        botonestabla.add(btnAddAll);
129
130
        JButton btnAdd = new JButton(PluginServices.getText(this, "Anadir"));
131
        btnAdd.setActionCommand("ADD_VALUE");
132
        btnAdd.addActionListener(listener);
133
        botonestabla.add(btnAdd);
134
135
        quitartodo = new JButton(PluginServices.getText(this, "Quitar_todos"));
136
        quitartodo.setActionCommand("REMOVE_ALL");
137
        quitartodo.addActionListener(listener);
138
        botonestabla.add(quitartodo);
139
140
        quitar = new JButton(PluginServices.getText(this, "Quitar"));
141
        quitar.setActionCommand("REMOVE");
142
        quitar.addActionListener(listener);
143
        botonestabla.add(quitar);
144
145
        JPanel ptabla = new JPanel();
146
        ptabla.setLayout(new BorderLayout());
147
148
        m_cboFields = new JComboBox();
149
        m_cboFields.setActionCommand("FIELD_SELECTED");
150
        m_cboFields.addActionListener(listener);
151
        m_cboFields.setVisible(true);
152
153
        JPanel pAux1 = new JPanel();
154
155
        /* pAux1.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
156
           "Campo de clasificaci?n",
157
           javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
158
           javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); */
159
        pAux1.setPreferredSize(new Dimension(300, 50));
160
161
        JLabel lblFieldClassification = new JLabel(PluginServices.getText(
162
                    this, "Campo_de_clasificacion"));
163
        pAux1.add(lblFieldClassification);
164
        pAux1.add(m_cboFields);
165
166
        ptabla.add(m_symbolTable, BorderLayout.CENTER);
167
        m_symbolTable.setPreferredSize(new Dimension(400, 200));
168
        this.setLayout(new BorderLayout());
169
        this.add(pAux1, BorderLayout.NORTH);
170
        this.add(ptabla, BorderLayout.CENTER);
171
        this.add(botonestabla, BorderLayout.SOUTH);
172
        pAux1.add(getChbUseDefault(), null);
173
    }
174
175
    /**
176
     * DOCUMENT ME!
177
     */
178
    private void fillTableValues() {
179
        DataSource elRs;
180
181
        try {
182
            elRs = ((FLyrVect) m_lyr).getRecordset();
183
            logger.debug("elRs.start()");
184
            elRs.start();
185
186
            int idField = -1;
187
            String fieldName = (String) m_cboFields.getSelectedItem();
188 9116 caballero
            if (fieldName==null) {
189
                    JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_hay_campo_seleccionado"));
190
                    return;
191
            }
192
193 7304 caballero
            System.out.println("Nombre del campo: " + fieldName);
194
            idField = elRs.getFieldIndexByName(fieldName);
195
            m_Renderer.setFieldName(fieldName);
196
197
            //long numReg = elRs.getRowCount();
198
            if (idField == -1) {
199
                System.err.println("Campo no reconocido " + fieldName);
200
201
                return;
202
            }
203
204
            m_symbolTable.removeAllItems();
205
206
            int numSymbols = 0;
207
            FSymbol theSymbol = null;
208
209
            //auxLegend=(VectorialUniqueValueLegend)m_lyr.getLegend();
210
            auxLegend = LegendFactory.createVectorialUniqueValueLegend(m_lyr.getShapeType());
211
212
            Value clave;
213
214
            //Object resul;
215
            if (chbUseDefault.isSelected()) {
216
                auxLegend.getDefaultSymbol().setDescription("Default");
217
                auxLegend.addSymbol(new NullUniqueValue(),
218
                    auxLegend.getDefaultSymbol());
219
            }
220
221
            for (int j = 0; j < elRs.getRowCount(); j++) {
222
                clave = elRs.getFieldValue(j, idField);
223
224
                if (clave instanceof NullValue) {
225
                    continue;
226
                }
227
228
                ////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
229
                if (auxLegend.getSymbolByValue(clave) == null) {
230
                    //si no esta creado el simbolo se crea
231
                    theSymbol = new FSymbol(m_lyr.getShapeType());
232
233
                    theSymbol.setDescription(clave.toString());
234
                    auxLegend.addSymbol(clave, theSymbol);
235
236
                    numSymbols++;
237
238
                    if (numSymbols == 100) {
239
                        int resp = JOptionPane.showConfirmDialog(this,
240
                                PluginServices.getText(this,
241
                                    "mas_de_100_simbolos"),
242
                                PluginServices.getText(this, "quiere_continuar"),
243
                                JOptionPane.YES_NO_OPTION,
244
                                JOptionPane.WARNING_MESSAGE);
245
246
                        if ((resp == JOptionPane.NO_OPTION) ||
247
                                (resp == JOptionPane.DEFAULT_OPTION)) {
248
                            return;
249
                        }
250
                    }
251
                }
252
            } // for
253
254
            m_symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
255
                auxLegend.getValues(), auxLegend.getDescriptions());
256
            elRs.stop();
257
        } catch (DriverException e) {
258
            // TODO Auto-generated catch block
259
            e.printStackTrace();
260
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
261
            e.printStackTrace();
262
        }
263
264
        quitartodo.setEnabled(true);
265
        quitar.setEnabled(true);
266
267 9116 caballero
        //m_bCacheDirty = false;
268 7304 caballero
    }
269
270
    /**
271
     * A partir de los registros de la tabla, regenera el FRenderer. (No solo
272
     * el symbolList, si no tambi?n el arrayKeys y el defaultRenderer
273
     */
274
    private void fillSymbolListFromTable() {
275
        Value clave;
276
        FSymbol theSymbol;
277
278
        // Borramos las anteriores listas:
279
        m_Renderer.clear();
280
281
        boolean bRestoValores = false; // PONERLO EN UN CHECKBOX
282
        int hasta;
283
        String fieldName = (String) m_cboFields.getSelectedItem();
284
        m_Renderer.setFieldName(fieldName);
285
286
        if (bRestoValores) {
287
            hasta = m_symbolTable.getRowCount() - 1;
288
        } else {
289
            hasta = m_symbolTable.getRowCount();
290
        }
291
292
        for (int row = 0; row < m_symbolTable.getRowCount(); row++) {
293
            clave = (Value) m_symbolTable.getFieldValue(row, 1);
294
            theSymbol = (FSymbol) m_symbolTable.getFieldValue(row, 0);
295
            theSymbol.setDescription((String) m_symbolTable.getFieldValue(row, 2));
296
            m_Renderer.addSymbol(clave, theSymbol);
297
            System.out.println(clave);
298
        }
299
300
        if (bRestoValores) {
301
            theSymbol = (FSymbol) m_symbolTable.getFieldValue(hasta, 0);
302
            m_Renderer.setDefaultSymbol(theSymbol);
303
        }
304
    }
305
306
    /**
307
     * DOCUMENT ME!
308
     */
309
    private void fillFieldNames() {
310
        DataSource rs;
311
312
        try {
313
            rs = ((AlphanumericData) m_lyr).getRecordset();
314
            logger.debug("rs.start()");
315
            rs.start();
316
317
            String[] nomFields = new String[rs.getFieldCount()];
318
319
            for (int i = 0; i < rs.getFieldCount(); i++) {
320
                nomFields[i] = rs.getFieldName(i).trim();
321
            }
322
323
            rs.stop();
324
325
            DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields);
326
            m_cboFields.setModel(cM);
327
328
            // fieldsListValor.setSelectedIndex(0);
329
        } catch (DriverException e) {
330
            e.printStackTrace();
331
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
332
            e.printStackTrace();
333
        }
334
335
        m_symbolTable.removeAllItems();
336
    }
337
338
    /**
339
     * DOCUMENT ME!
340
     *
341
     * @param lyr DOCUMENT ME!
342
     * @param r DOCUMENT ME!
343
     */
344
    public void setLayer(FLayer lyr, Legend r) {
345
        // OJO, COMPROBAR ANTES SI ES DE TIPO VECTORIAL
346
        m_lyr = (ClassifiableVectorial) lyr;
347
        fillFieldNames();
348
349
        // Si es de valor ?nico, rellenamos la tabla
350
        // y seleccionamos el campo que estamos usando
351
        m_symbolTable.removeAllItems();
352
353
        if (r instanceof VectorialUniqueValueLegend) {
354
            m_Renderer = (VectorialUniqueValueLegend) r;
355
            getChbUseDefault().setSelected(m_Renderer.isUseDefaultSymbol());
356
            m_cboFields.getModel().setSelectedItem(m_Renderer.getFieldName());
357
            m_symbolTable.fillTableFromSymbolList(m_Renderer.getSymbols(),
358
                m_Renderer.getValues(), m_Renderer.getDescriptions());
359
        } else {
360
            try {
361
                // Si la capa viene con otro tipo de leyenda, creamos
362
                // una nueva del tipo que maneja este panel
363
                m_Renderer = new VectorialUniqueValueLegend(m_lyr.getShapeType());
364
            } catch (com.iver.cit.gvsig.fmap.DriverException e) {
365
                // TODO Auto-generated catch block
366
                e.printStackTrace();
367
            }
368
        }
369
    }
370
371
    /* (non-Javadoc)
372
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
373
     */
374
    public Legend getLegend() {
375
        fillSymbolListFromTable();
376
377
        if (auxLegend != null) {
378
            m_Renderer.setDefaultSymbol(auxLegend.getDefaultSymbol());
379
            m_Renderer.useDefaultSymbol(chbUseDefault.isSelected());
380
        }
381
382
        return m_Renderer;
383
    }
384
385
    /**
386
     * This method initializes chbUseDefault
387
     *
388
     * @return javax.swing.JCheckBox
389
     */
390
    private JCheckBox getChbUseDefault() {
391
        if (chbUseDefault == null) {
392
            chbUseDefault = new JCheckBox();
393
            chbUseDefault.setSelected(true);
394
            chbUseDefault.addActionListener(new java.awt.event.ActionListener() {
395
                    public void actionPerformed(java.awt.event.ActionEvent e) {
396
                            if (auxLegend==null)auxLegend=m_Renderer;
397
                            if (chbUseDefault.isSelected()) {
398
                            addDefault();
399
                        } else {
400
                            delDefault();
401
                        }
402
                    }
403
                });
404
            chbUseDefault.setText(PluginServices.getText(this, "resto_valores"));
405
        }
406
407
        return chbUseDefault;
408
    }
409
410
    /**
411
     * A?ade el resto de valores.
412
     */
413
    private void addDefault() {
414
        auxLegend.getDefaultSymbol().setDescription("Default");
415
        auxLegend.addSymbol(new NullUniqueValue(), auxLegend.getDefaultSymbol());
416
        m_symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
417
            new NullUniqueValue(), auxLegend.getDefaultSymbol().getDescription());
418
        m_symbolTable.repaint();
419
    }
420
421
    /**
422
     * Elimina el resto de valores que no estan representados por ning?n otro s?mbolo..
423
     */
424
    private void delDefault() {
425
        auxLegend.delSymbol(new NullUniqueValue());
426
        m_symbolTable.removeRow(new NullUniqueValue());
427
        m_symbolTable.repaint();
428
    }
429
430
    /**
431
     * Listener.
432
     *
433
     * @author Vicente Caballero Navarro
434
     */
435
    class MyListener implements ActionListener {
436
        //private FLyrShp m_layer;
437
        // private FPanelLegendValues m_Parent;
438
        public MyListener() { // FPanelLegendValues p) {
439
440
            // m_Parent = p;
441
        }
442
443
        /**
444
         * DOCUMENT ME!
445
         *
446
         * @param e DOCUMENT ME!
447
         */
448
        public void actionPerformed(ActionEvent e) {
449
            // rellenarValue();
450
            System.out.println("ActionEvent con " + e.getActionCommand());
451
452
            //modificar el combobox de valor
453
            if (e.getActionCommand() == "FIELD_SELECTED") {
454
                JComboBox cb = (JComboBox) e.getSource();
455
                String fieldName = (String) cb.getSelectedItem();
456
                System.out.println("Nombre del campo: " + fieldName);
457
                m_symbolTable.removeAllItems();
458
459
                if (fieldName != m_Renderer.getFieldName()) {
460
                    //m_bCacheDirty = true;
461
                    m_Renderer.setFieldName(fieldName);
462
                }
463
            }
464
465
            //A?adir todos los elementos por valor
466
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
467
                fillTableValues();
468
            }
469
470
            //A?adir un ?nico elemento
471
            if (e.getActionCommand() == "ADD_VALUE") {
472
                try {
473
                    m_symbolTable.addTableRecord(new FSymbol(
474
                            m_lyr.getShapeType()),
475
                        ValueFactory.createValue(0.0), "0 - 0");
476
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
477
                    e1.printStackTrace();
478
                }
479
480
                /*        a?adir("Nuevo_Valor");
481
                   aceptar.setEnabled(true);
482
                   quitartodo.setEnabled(true);
483
                   quitar.setEnabled(true); */
484
            }
485
486
            //Vacia la tabla
487
            if (e.getActionCommand() == "REMOVE_ALL") {
488
                m_symbolTable.removeAllItems();
489
490
                // aceptar.setEnabled(false);
491
            }
492
493
            //Quitar solo el elemento seleccionado
494
            if (e.getActionCommand() == "REMOVE") {
495
                m_symbolTable.removeSelectedRows();
496
            }
497
        }
498
    }
499
}