Statistics
| Revision:

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

History | View | Annotate | Download (16.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 com.hardcode.gdbms.engine.data.DataSource;
50
import com.hardcode.gdbms.engine.data.driver.DriverException;
51
import com.hardcode.gdbms.engine.values.NullValue;
52
import com.hardcode.gdbms.engine.values.Value;
53
import com.hardcode.gdbms.engine.values.ValueFactory;
54

    
55
import com.iver.andami.PluginServices;
56

    
57
import com.iver.cit.gvsig.ProjectExtension;
58
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
59
import com.iver.cit.gvsig.fmap.layers.FLayer;
60
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
61
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
62
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
63
import com.iver.cit.gvsig.fmap.rendering.Legend;
64
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
65
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
66
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
67

    
68
import org.apache.log4j.Logger;
69

    
70
import java.awt.BorderLayout;
71
import java.awt.Dimension;
72
import java.awt.event.ActionEvent;
73
import java.awt.event.ActionListener;
74

    
75
import javax.swing.DefaultComboBoxModel;
76
import javax.swing.JButton;
77
import javax.swing.JCheckBox;
78
import javax.swing.JComboBox;
79
import javax.swing.JLabel;
80
import javax.swing.JOptionPane;
81
import javax.swing.JPanel;
82

    
83

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

    
95
    // private TOC m_TOC;
96
    private VectorialUniqueValueLegend m_Renderer;
97
    private ClassifiableVectorial m_lyr;
98

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

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

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

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

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

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

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

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

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

    
155
        JPanel pAux1 = new JPanel();
156

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

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

    
168
        ptabla.add(m_symbolTable, BorderLayout.CENTER);
169
        m_symbolTable.setPreferredSize(new Dimension(400, 200));
170
        this.setLayout(new BorderLayout());
171
        this.add(pAux1, BorderLayout.NORTH);
172
        this.add(ptabla, BorderLayout.CENTER);
173
        this.add(botonestabla, BorderLayout.SOUTH);
174
        pAux1.add(getChbUseDefault(), 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
            System.out.println("Nombre del campo: " + fieldName);
191
            idField = elRs.getFieldIndexByName(fieldName);
192
            m_Renderer.setFieldName(fieldName);
193

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

    
198
                return;
199
            }
200

    
201
            m_symbolTable.removeAllItems();
202

    
203
            int numSymbols = 0;
204
            FSymbol theSymbol = null;
205

    
206
            //auxLegend=(VectorialUniqueValueLegend)m_lyr.getLegend();
207
            auxLegend = LegendFactory.createVectorialUniqueValueLegend(m_lyr.getShapeType());
208

    
209
            Value clave;
210

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

    
218
            for (int j = 0; j < elRs.getRowCount(); j++) {
219
                clave = elRs.getFieldValue(j, idField);
220

    
221
                if (clave instanceof NullValue) {
222
                    continue;
223
                }
224

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

    
230
                    theSymbol.setDescription(clave.toString());
231
                    auxLegend.addSymbol(clave, theSymbol);
232

    
233
                    numSymbols++;
234

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

    
243
                        if ((resp == JOptionPane.NO_OPTION) ||
244
                                (resp == JOptionPane.DEFAULT_OPTION)) {
245
                            return;
246
                        }
247
                    }
248
                }
249
            } // for
250

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

    
261
        quitartodo.setEnabled(true);
262
        quitar.setEnabled(true);
263

    
264
        //m_bCacheDirty = false;        
265
    }
266

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

    
275
        // Borramos las anteriores listas:
276
        m_Renderer.clear();
277

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

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

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

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

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

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

    
314
            String[] nomFields = new String[rs.getFieldCount()];
315

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

    
320
            rs.stop();
321

    
322
            DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields);
323
            m_cboFields.setModel(cM);
324

    
325
            // fieldsListValor.setSelectedIndex(0);
326
        } catch (DriverException e) {
327
            e.printStackTrace();
328
        } catch (com.iver.cit.gvsig.fmap.DriverException 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
            m_cboFields.getModel().setSelectedItem(m_Renderer.getFieldName());
354
            m_symbolTable.fillTableFromSymbolList(m_Renderer.getSymbols(),
355
                m_Renderer.getValues(), m_Renderer.getDescriptions());
356
        } else {
357
            try {
358
                // Si la capa viene con otro tipo de leyenda, creamos
359
                // una nueva del tipo que maneja este panel
360
                m_Renderer = new VectorialUniqueValueLegend(m_lyr.getShapeType());
361
            } catch (com.iver.cit.gvsig.fmap.DriverException e) {
362
                // TODO Auto-generated catch block
363
                e.printStackTrace();
364
            }
365
        }
366
    }
367

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

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

    
379
        return m_Renderer;
380
    }
381

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

    
404
        return chbUseDefault;
405
    }
406

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

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

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

    
437
            // m_Parent = p;
438
        }
439

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

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

    
456
                if (fieldName != m_Renderer.getFieldName()) {
457
                    //m_bCacheDirty = true;
458
                    m_Renderer.setFieldName(fieldName);
459
                }
460
            }
461

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

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

    
477
                /*        a?adir("Nuevo_Valor");
478
                   aceptar.setEnabled(true);
479
                   quitartodo.setEnabled(true);
480
                   quitar.setEnabled(true); */
481
            }
482

    
483
            //Vacia la tabla
484
            if (e.getActionCommand() == "REMOVE_ALL") {
485
                m_symbolTable.removeAllItems();
486

    
487
                // aceptar.setEnabled(false);
488
            }
489

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