Statistics
| Revision:

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

History | View | Annotate | Download (16.5 KB)

1
/*
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.Dimension;
51
import java.awt.event.ActionEvent;
52
import java.awt.event.ActionListener;
53

    
54
import javax.swing.DefaultComboBoxModel;
55
import javax.swing.JButton;
56
import javax.swing.JCheckBox;
57
import javax.swing.JComboBox;
58
import javax.swing.JLabel;
59
import javax.swing.JOptionPane;
60
import javax.swing.JPanel;
61

    
62
import org.apache.log4j.Logger;
63

    
64
import com.hardcode.gdbms.engine.data.DataSource;
65
import com.hardcode.gdbms.engine.data.driver.DriverException;
66
import com.hardcode.gdbms.engine.values.NullValue;
67
import com.hardcode.gdbms.engine.values.Value;
68
import com.hardcode.gdbms.engine.values.ValueFactory;
69
import com.iver.andami.PluginServices;
70
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
71
import com.iver.cit.gvsig.fmap.layers.FLayer;
72
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
73
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
74
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
75
import com.iver.cit.gvsig.fmap.rendering.Legend;
76
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
77
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
78
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
79

    
80

    
81
/**
82
 * DOCUMENT ME!
83
 *
84
 * @author fjp To change the template for this generated type comment go to
85
 *         Window>Preferences>Java>Code Generation>Code and
86
 *         Comments
87
 */
88
public class FPanelLegendValues extends JPanel implements ILegendPanel {
89
    private static Logger logger = Logger.getLogger(FPanelLegendValues.class.getName());
90
    private MyListener listener = new MyListener();
91

    
92
    // private TOC m_TOC;
93
    private VectorialUniqueValueLegend m_Renderer;
94
    private ClassifiableVectorial m_lyr;
95

    
96
    //private boolean m_bCacheDirty = true;
97
    private FSymbolTable m_symbolTable = new FSymbolTable("values");
98
    private JComboBox m_cboFields;
99
    private JButton quitartodo;
100
    private JButton quitar;
101
    private JCheckBox chbUseDefault = null;
102
    private VectorialUniqueValueLegend auxLegend;
103

    
104
    /**
105
     *
106
     */
107
    public FPanelLegendValues() {
108
        super();
109
        initComponents();
110
    }
111

    
112
    /**
113
     * DOCUMENT ME!
114
     */
115
    protected void initComponents() {
116
        /* JLabel label = new JLabel();
117
           label.setIcon(new javax.swing.ImageIcon(Abrir.class.getClassLoader()
118
                                                                                                              .getResource("images/ValoresUnicos.png")));
119
           limagen[1] = new JLabel();
120
           limagen[1] = label; */
121
        JPanel botonestabla = new JPanel();
122

    
123
        JButton btnAddAll = new JButton(PluginServices.getText(this,
124
                    "Anadir_todos"));
125
        btnAddAll.setActionCommand("ADD_ALL_VALUES");
126
        btnAddAll.addActionListener(listener);
127
        botonestabla.add(btnAddAll);
128

    
129
        JButton btnAdd = new JButton(PluginServices.getText(this, "Anadir"));
130
        btnAdd.setActionCommand("ADD_VALUE");
131
        btnAdd.addActionListener(listener);
132
        botonestabla.add(btnAdd);
133

    
134
        quitartodo = new JButton(PluginServices.getText(this, "Quitar_todos"));
135
        quitartodo.setActionCommand("REMOVE_ALL");
136
        quitartodo.addActionListener(listener);
137
        botonestabla.add(quitartodo);
138

    
139
        quitar = new JButton(PluginServices.getText(this, "Quitar"));
140
        quitar.setActionCommand("REMOVE");
141
        quitar.addActionListener(listener);
142
        botonestabla.add(quitar);
143

    
144
        JPanel ptabla = new JPanel();
145
        ptabla.setLayout(new BorderLayout());
146

    
147
        m_cboFields = new JComboBox();
148
        m_cboFields.setActionCommand("FIELD_SELECTED");
149
        m_cboFields.addActionListener(listener);
150
        m_cboFields.setVisible(true);
151

    
152
        JPanel pAux1 = new JPanel();
153

    
154
        /* pAux1.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
155
           "Campo de clasificaci?n",
156
           javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
157
           javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); */
158
        pAux1.setPreferredSize(new Dimension(300, 50));
159

    
160
        JLabel lblFieldClassification = new JLabel(PluginServices.getText(
161
                    this, "Campo_de_clasificacion"));
162
        pAux1.add(lblFieldClassification);
163
        pAux1.add(m_cboFields);
164

    
165
        ptabla.add(m_symbolTable, BorderLayout.CENTER);
166
        m_symbolTable.setPreferredSize(new Dimension(400, 200));
167
        this.setLayout(new BorderLayout());
168
        this.add(pAux1, BorderLayout.NORTH);
169
        this.add(ptabla, BorderLayout.CENTER);
170
        this.add(botonestabla, BorderLayout.SOUTH);
171
        pAux1.add(getChbUseDefault(), null);
172
    }
173

    
174
    /**
175
     * DOCUMENT ME!
176
     */
177
    private void fillTableValues() {
178
        DataSource elRs;
179

    
180
        try {
181
            elRs = ((FLyrVect) m_lyr).getRecordset();
182
            logger.debug("elRs.start()");
183
            elRs.start();
184

    
185
            int idField = -1;
186
            String fieldName = (String) m_cboFields.getSelectedItem();
187
            System.out.println("Nombre del campo: " + fieldName);
188
            idField = elRs.getFieldIndexByName(fieldName);
189
            m_Renderer.setFieldName(fieldName);
190

    
191
            //long numReg = elRs.getRowCount();
192
            if (idField == -1) {
193
                System.err.println("Campo no reconocido " + fieldName);
194

    
195
                return;
196
            }
197

    
198
            m_symbolTable.removeAllItems();
199

    
200
            int numSymbols = 0;
201
            FSymbol theSymbol = null;
202

    
203
            //auxLegend=(VectorialUniqueValueLegend)m_lyr.getLegend();
204
            auxLegend = LegendFactory.createVectorialUniqueValueLegend(m_lyr.getShapeType());
205

    
206
            Value clave;
207

    
208
            //Object resul;
209
            if (chbUseDefault.isSelected()) {
210
                auxLegend.getDefaultSymbol().setDescription("Default");
211
                auxLegend.addSymbol(new NullUniqueValue(),
212
                    auxLegend.getDefaultSymbol());
213
            }
214

    
215
            for (int j = 0; j < elRs.getRowCount(); j++) {
216
                clave = elRs.getFieldValue(j, idField);
217

    
218
                if (clave instanceof NullValue) {
219
                    continue;
220
                }
221

    
222
                ////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
223
                if (auxLegend.getSymbolByValue(clave) == null) {
224
                    //si no esta creado el simbolo se crea
225
                    theSymbol = new FSymbol(m_lyr.getShapeType());
226

    
227
                    theSymbol.setDescription(clave.toString());
228
                    auxLegend.addSymbol(clave, theSymbol);
229

    
230
                    numSymbols++;
231

    
232
                    if (numSymbols == 100) {
233
                        int resp = JOptionPane.showConfirmDialog(this,
234
                                PluginServices.getText(this,
235
                                    "mas_de_100_simbolos"),
236
                                PluginServices.getText(this, "quiere_continuar"),
237
                                JOptionPane.YES_NO_OPTION,
238
                                JOptionPane.WARNING_MESSAGE);
239

    
240
                        if ((resp == JOptionPane.NO_OPTION) ||
241
                                (resp == JOptionPane.DEFAULT_OPTION)) {
242
                            return;
243
                        }
244
                    }
245
                }
246
            } // for
247

    
248
            m_symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
249
                auxLegend.getValues(), auxLegend.getDescriptions());
250
            elRs.stop();
251
        } catch (DriverException e) {
252
            // TODO Auto-generated catch block
253
            e.printStackTrace();
254
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
255
            e.printStackTrace();
256
        }
257

    
258
        quitartodo.setEnabled(true);
259
        quitar.setEnabled(true);
260

    
261
        //m_bCacheDirty = false;        
262
    }
263

    
264
    /**
265
     * A partir de los registros de la tabla, regenera el FRenderer. (No solo
266
     * el symbolList, si no tambi?n el arrayKeys y el defaultRenderer
267
     */
268
    private void fillSymbolListFromTable() {
269
        Value clave;
270
        FSymbol theSymbol;
271

    
272
        // Borramos las anteriores listas:
273
        m_Renderer.clear();
274

    
275
        boolean bRestoValores = false; // PONERLO EN UN CHECKBOX
276
        int hasta;
277
        String fieldName = (String) m_cboFields.getSelectedItem();
278
        m_Renderer.setFieldName(fieldName);
279

    
280
        if (bRestoValores) {
281
            hasta = m_symbolTable.getRowCount() - 1;
282
        } else {
283
            hasta = m_symbolTable.getRowCount();
284
        }
285

    
286
        for (int row = 0; row < m_symbolTable.getRowCount(); row++) {
287
            clave = (Value) m_symbolTable.getFieldValue(row, 1);
288
            theSymbol = (FSymbol) m_symbolTable.getFieldValue(row, 0);
289
            theSymbol.setDescription((String) m_symbolTable.getFieldValue(row, 2));
290
            m_Renderer.addSymbol(clave, theSymbol);
291
            System.out.println(clave);
292
        }
293

    
294
        if (bRestoValores) {
295
            theSymbol = (FSymbol) m_symbolTable.getFieldValue(hasta, 0);
296
            m_Renderer.setDefaultSymbol(theSymbol);
297
        }
298
    }
299

    
300
    /**
301
     * DOCUMENT ME!
302
     */
303
    private void fillFieldNames() {
304
        DataSource rs;
305

    
306
        try {
307
            rs = ((AlphanumericData) m_lyr).getRecordset();
308
            logger.debug("rs.start()");
309
            rs.start();
310

    
311
            String[] nomFields = new String[rs.getFieldCount()];
312

    
313
            for (int i = 0; i < rs.getFieldCount(); i++) {
314
                nomFields[i] = rs.getFieldName(i).trim();
315
            }
316

    
317
            rs.stop();
318

    
319
            DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields);
320
            m_cboFields.setModel(cM);
321

    
322
            // fieldsListValor.setSelectedIndex(0);
323
        } catch (DriverException e) {
324
            e.printStackTrace();
325
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
326
            e.printStackTrace();
327
        }
328

    
329
        m_symbolTable.removeAllItems();
330
    }
331

    
332
    /**
333
     * DOCUMENT ME!
334
     *
335
     * @param lyr DOCUMENT ME!
336
     * @param r DOCUMENT ME!
337
     */
338
    public void setLayer(FLayer lyr, Legend r) {
339
        // OJO, COMPROBAR ANTES SI ES DE TIPO VECTORIAL
340
        m_lyr = (ClassifiableVectorial) lyr;
341
        fillFieldNames();
342

    
343
        // Si es de valor ?nico, rellenamos la tabla
344
        // y seleccionamos el campo que estamos usando
345
        m_symbolTable.removeAllItems();
346

    
347
        if (r instanceof VectorialUniqueValueLegend) {
348
            m_Renderer = (VectorialUniqueValueLegend) r;
349
            getChbUseDefault().setSelected(m_Renderer.isUseDefaultSymbol());
350
            m_cboFields.getModel().setSelectedItem(m_Renderer.getFieldName());
351
            m_symbolTable.fillTableFromSymbolList(m_Renderer.getSymbols(),
352
                m_Renderer.getValues(), m_Renderer.getDescriptions());
353
        } else {
354
            try {
355
                // Si la capa viene con otro tipo de leyenda, creamos
356
                // una nueva del tipo que maneja este panel
357
                m_Renderer = new VectorialUniqueValueLegend(m_lyr.getShapeType());
358
            } catch (com.iver.cit.gvsig.fmap.DriverException e) {
359
                // TODO Auto-generated catch block
360
                e.printStackTrace();
361
            }
362
        }
363
    }
364

    
365
    /* (non-Javadoc)
366
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
367
     */
368
    public Legend getLegend() {
369
        fillSymbolListFromTable();
370

    
371
        if (auxLegend != null) {
372
            m_Renderer.setDefaultSymbol(auxLegend.getDefaultSymbol());
373
            m_Renderer.useDefaultSymbol(chbUseDefault.isSelected());
374
        }
375

    
376
        return m_Renderer;
377
    }
378

    
379
    /**
380
     * This method initializes chbUseDefault
381
     *
382
     * @return javax.swing.JCheckBox
383
     */
384
    private JCheckBox getChbUseDefault() {
385
        if (chbUseDefault == null) {
386
            chbUseDefault = new JCheckBox();
387
            chbUseDefault.setSelected(true);
388
            chbUseDefault.addActionListener(new java.awt.event.ActionListener() {
389
                    public void actionPerformed(java.awt.event.ActionEvent e) {
390
                            if (auxLegend==null)auxLegend=m_Renderer;
391
                            if (chbUseDefault.isSelected()) {
392
                            addDefault();
393
                        } else {
394
                            delDefault();
395
                        }
396
                    }
397
                });
398
            chbUseDefault.setText(PluginServices.getText(this, "resto_valores"));
399
        }
400

    
401
        return chbUseDefault;
402
    }
403

    
404
    /**
405
     * A?ade el resto de valores.
406
     */
407
    private void addDefault() {
408
        auxLegend.getDefaultSymbol().setDescription("Default");
409
        auxLegend.addSymbol(new NullUniqueValue(), auxLegend.getDefaultSymbol());
410
        m_symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
411
            new NullUniqueValue(), auxLegend.getDefaultSymbol().getDescription());
412
        m_symbolTable.repaint();
413
    }
414

    
415
    /**
416
     * Elimina el resto de valores que no estan representados por ning?n otro s?mbolo..
417
     */
418
    private void delDefault() {
419
        auxLegend.delSymbol(new NullUniqueValue());
420
        m_symbolTable.removeRow(new NullUniqueValue());
421
        m_symbolTable.repaint();
422
    }
423

    
424
    /**
425
     * Listener.
426
     *
427
     * @author Vicente Caballero Navarro
428
     */
429
    class MyListener implements ActionListener {
430
        //private FLyrShp m_layer;
431
        // private FPanelLegendValues m_Parent;
432
        public MyListener() { // FPanelLegendValues p) {
433

    
434
            // m_Parent = p;
435
        }
436

    
437
        /**
438
         * DOCUMENT ME!
439
         *
440
         * @param e DOCUMENT ME!
441
         */
442
        public void actionPerformed(ActionEvent e) {
443
            // rellenarValue();
444
            System.out.println("ActionEvent con " + e.getActionCommand());
445

    
446
            //modificar el combobox de valor
447
            if (e.getActionCommand() == "FIELD_SELECTED") {
448
                JComboBox cb = (JComboBox) e.getSource();
449
                String fieldName = (String) cb.getSelectedItem();
450
                System.out.println("Nombre del campo: " + fieldName);
451
                m_symbolTable.removeAllItems();
452

    
453
                if (fieldName != m_Renderer.getFieldName()) {
454
                    //m_bCacheDirty = true;
455
                    m_Renderer.setFieldName(fieldName);
456
                }
457
            }
458

    
459
            //A?adir todos los elementos por valor
460
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
461
                fillTableValues();
462
            }
463

    
464
            //A?adir un ?nico elemento
465
            if (e.getActionCommand() == "ADD_VALUE") {
466
                try {
467
                    m_symbolTable.addTableRecord(new FSymbol(
468
                            m_lyr.getShapeType()),
469
                        ValueFactory.createValue(0.0), "0 - 0");
470
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
471
                    e1.printStackTrace();
472
                }
473

    
474
                /*        a?adir("Nuevo_Valor");
475
                   aceptar.setEnabled(true);
476
                   quitartodo.setEnabled(true);
477
                   quitar.setEnabled(true); */
478
            }
479

    
480
            //Vacia la tabla
481
            if (e.getActionCommand() == "REMOVE_ALL") {
482
                m_symbolTable.removeAllItems();
483

    
484
                // aceptar.setEnabled(false);
485
            }
486

    
487
            //Quitar solo el elemento seleccionado
488
            if (e.getActionCommand() == "REMOVE") {
489
                m_symbolTable.removeSelectedRows();
490
            }
491
        }
492
    }
493
}