Statistics
| Revision:

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

History | View | Annotate | Download (17.6 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.Component;
51
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
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
66
import com.hardcode.gdbms.engine.data.DataSource;
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 JCheckBox chbOrder = null;
104
    private VectorialUniqueValueLegend auxLegend;
105

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

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

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

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

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

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

    
146
        JPanel ptabla = new JPanel();
147
        ptabla.setLayout(new BorderLayout());
148

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

    
154
        JPanel pAux1 = new JPanel();
155

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

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

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

    
177
    /**
178
     * DOCUMENT ME!
179
     */
180
    private void fillTableValues() {
181
        DataSource elRs;
182

    
183
        try {
184
            elRs = ((FLyrVect) m_lyr).getRecordset();
185
            logger.debug("elRs.start()");
186
            elRs.start();
187

    
188
            int idField = -1;
189
            String fieldName = (String) m_cboFields.getSelectedItem();
190
            if (fieldName==null) {
191
                    JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_hay_campo_seleccionado"));
192
                    return;
193
            }
194

    
195
            System.out.println("Nombre del campo: " + fieldName);
196
            idField = elRs.getFieldIndexByName(fieldName);
197
            m_Renderer.setFieldName(fieldName);
198

    
199
            //long numReg = elRs.getRowCount();
200
            if (idField == -1) {
201
                System.err.println("Campo no reconocido " + fieldName);
202

    
203
                return;
204
            }
205

    
206
            m_symbolTable.removeAllItems();
207

    
208
            int numSymbols = 0;
209
            FSymbol theSymbol = null;
210

    
211
            //auxLegend=(VectorialUniqueValueLegend)m_lyr.getLegend();
212
            auxLegend = LegendFactory.createVectorialUniqueValueLegend(m_lyr.getShapeType());
213

    
214
            Value clave;
215

    
216
            //Object resul;
217
            if (chbUseDefault.isSelected()) {
218
                auxLegend.getDefaultSymbol().setDescription("Default");
219
                auxLegend.addSymbol(new NullUniqueValue(),
220
                    auxLegend.getDefaultSymbol());
221
            }
222

    
223
            for (int j = 0; j < elRs.getRowCount(); j++) {
224
                clave = elRs.getFieldValue(j, idField);
225

    
226
                if (clave instanceof NullValue) {
227
                    continue;
228
                }
229

    
230
                ////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
231
                if (auxLegend.getSymbolByValue(clave) == null) {
232
                    //si no esta creado el simbolo se crea
233
                    theSymbol = new FSymbol(m_lyr.getShapeType());
234

    
235
                    theSymbol.setDescription(clave.toString());
236
                    auxLegend.addSymbol(clave, theSymbol);
237

    
238
                    numSymbols++;
239

    
240
                    if (numSymbols == 100) {
241
                        int resp = JOptionPane.showConfirmDialog(this,
242
                                PluginServices.getText(this,
243
                                    "mas_de_100_simbolos"),
244
                                PluginServices.getText(this, "quiere_continuar"),
245
                                JOptionPane.YES_NO_OPTION,
246
                                JOptionPane.WARNING_MESSAGE);
247

    
248
                        if ((resp == JOptionPane.NO_OPTION) ||
249
                                (resp == JOptionPane.DEFAULT_OPTION)) {
250
                            return;
251
                        }
252
                    }
253
                }
254
            } // for
255

    
256
            m_symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
257
                auxLegend.getValues(), auxLegend.getDescriptions());
258
            elRs.stop();
259
        } catch (ReadDriverException e) {
260
                        e.printStackTrace();
261
                }
262

    
263
        quitartodo.setEnabled(true);
264
        quitar.setEnabled(true);
265

    
266
        //m_bCacheDirty = false;
267
    }
268

    
269
    /**
270
     * A partir de los registros de la tabla, regenera el FRenderer. (No solo
271
     * el symbolList, si no tambi?n el arrayKeys y el defaultRenderer
272
     */
273
    private void fillSymbolListFromTable() {
274
        Value clave;
275
        FSymbol theSymbol;
276

    
277
        // Borramos las anteriores listas:
278
        m_Renderer.clear();
279

    
280
        boolean bRestoValores = false; // PONERLO EN UN CHECKBOX
281
        int hasta;
282
        String fieldName = (String) m_cboFields.getSelectedItem();
283
        m_Renderer.setFieldName(fieldName);
284

    
285
        if (bRestoValores) {
286
            hasta = m_symbolTable.getRowCount() - 1;
287
        } else {
288
            hasta = m_symbolTable.getRowCount();
289
        }
290

    
291
        for (int row = 0; row < m_symbolTable.getRowCount(); row++) {
292
            clave = (Value) m_symbolTable.getFieldValue(row, 1);
293
            theSymbol = (FSymbol) m_symbolTable.getFieldValue(row, 0);
294
            theSymbol.setDescription((String) m_symbolTable.getFieldValue(row, 2));
295
            m_Renderer.addSymbol(clave, theSymbol);
296
            System.out.println(clave);
297
        }
298

    
299
        if (bRestoValores) {
300
            theSymbol = (FSymbol) m_symbolTable.getFieldValue(hasta, 0);
301
            m_Renderer.setDefaultSymbol(theSymbol);
302
        }
303
    }
304

    
305
    /**
306
     * DOCUMENT ME!
307
     */
308
    private void fillFieldNames() {
309
        DataSource rs;
310

    
311
        try {
312
            rs = ((AlphanumericData) m_lyr).getRecordset();
313
            logger.debug("rs.start()");
314
            rs.start();
315

    
316
            String[] nomFields = new String[rs.getFieldCount()];
317

    
318
            for (int i = 0; i < rs.getFieldCount(); i++) {
319
                nomFields[i] = rs.getFieldName(i).trim();
320
            }
321

    
322
            rs.stop();
323

    
324
            DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields);
325
            m_cboFields.setModel(cM);
326

    
327
            // fieldsListValor.setSelectedIndex(0);
328
        } catch (ReadDriverException e) {
329
                        e.printStackTrace();
330
                }
331

    
332
        m_symbolTable.removeAllItems();
333
    }
334

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

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

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

    
369
    /* (non-Javadoc)
370
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
371
     */
372
    public Legend getLegend() {
373
        fillSymbolListFromTable();
374

    
375
        if (auxLegend != null) {
376
            m_Renderer.setDefaultSymbol(auxLegend.getDefaultSymbol());
377
            m_Renderer.useDefaultSymbol(chbUseDefault.isSelected());
378
        }
379

    
380
        return m_Renderer;
381
    }
382

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

    
405
        return chbUseDefault;
406
    }
407
    /**
408
     * This method initializes chbUseDefault
409
     *
410
     * @return javax.swing.JCheckBox
411
     */
412
    private JCheckBox getChbSorter() {
413
        if (chbOrder == null) {
414
            chbOrder = new JCheckBox();
415
            chbOrder.setSelected(true);
416
            chbOrder.setToolTipText(PluginServices.getText(this,"keep_the_order_of_values"));
417
            chbOrder.addActionListener(new java.awt.event.ActionListener() {
418
                    public void actionPerformed(java.awt.event.ActionEvent e) {
419
                            if (auxLegend==null)auxLegend=m_Renderer;
420
                            if (chbOrder.isSelected()) {
421
                            setSorter(true);
422
                        } else {
423
                            setSorter(false);
424
                        }
425
                    }
426
                });
427
            chbOrder.setText(PluginServices.getText(this, "sorter"));
428
        }
429

    
430
        return chbOrder;
431
    }
432
    private void setSorter(boolean b) {
433
                auxLegend.setSorter(b);
434
                m_symbolTable.repaint();
435
        }
436
    /**
437
     * A?ade el resto de valores.
438
     */
439
    private void addDefault() {
440
        auxLegend.getDefaultSymbol().setDescription("Default");
441
        auxLegend.addSymbol(new NullUniqueValue(), auxLegend.getDefaultSymbol());
442
        m_symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
443
            new NullUniqueValue(), auxLegend.getDefaultSymbol().getDescription());
444
        m_symbolTable.repaint();
445
    }
446

    
447
    /**
448
     * Elimina el resto de valores que no estan representados por ning?n otro s?mbolo..
449
     */
450
    private void delDefault() {
451
        auxLegend.delSymbol(new NullUniqueValue());
452
        m_symbolTable.removeRow(new NullUniqueValue());
453
        m_symbolTable.repaint();
454
    }
455

    
456
    /**
457
     * Listener.
458
     *
459
     * @author Vicente Caballero Navarro
460
     */
461
    class MyListener implements ActionListener {
462
        //private FLyrShp m_layer;
463
        // private FPanelLegendValues m_Parent;
464
        public MyListener() { // FPanelLegendValues p) {
465

    
466
            // m_Parent = p;
467
        }
468

    
469
        /**
470
         * DOCUMENT ME!
471
         *
472
         * @param e DOCUMENT ME!
473
         */
474
        public void actionPerformed(ActionEvent e) {
475
            // rellenarValue();
476
            System.out.println("ActionEvent con " + e.getActionCommand());
477

    
478
            //modificar el combobox de valor
479
            if (e.getActionCommand() == "FIELD_SELECTED") {
480
                JComboBox cb = (JComboBox) e.getSource();
481
                String fieldName = (String) cb.getSelectedItem();
482
                System.out.println("Nombre del campo: " + fieldName);
483
                m_symbolTable.removeAllItems();
484

    
485
                if (fieldName != m_Renderer.getFieldName()) {
486
                    //m_bCacheDirty = true;
487
                    m_Renderer.setFieldName(fieldName);
488
                }
489
            }
490

    
491
            //A?adir todos los elementos por valor
492
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
493
                fillTableValues();
494
            }
495

    
496
            //A?adir un ?nico elemento
497
            if (e.getActionCommand() == "ADD_VALUE") {
498
                try {
499
                    m_symbolTable.addTableRecord(new FSymbol(
500
                            m_lyr.getShapeType()),
501
                        ValueFactory.createValue(0.0), "0 - 0");
502
                } catch (ReadDriverException e1) {
503
                    e1.printStackTrace();
504
                }
505

    
506
                /*        a?adir("Nuevo_Valor");
507
                   aceptar.setEnabled(true);
508
                   quitartodo.setEnabled(true);
509
                   quitar.setEnabled(true); */
510
            }
511

    
512
            //Vacia la tabla
513
            if (e.getActionCommand() == "REMOVE_ALL") {
514
                m_symbolTable.removeAllItems();
515

    
516
                // aceptar.setEnabled(false);
517
            }
518

    
519
            //Quitar solo el elemento seleccionado
520
            if (e.getActionCommand() == "REMOVE") {
521
                m_symbolTable.removeSelectedRows();
522
            }
523
        }
524
    }
525
}