Statistics
| Revision:

svn-gvsig-desktop / branches / simbologia / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / VectorialUniqueValue.java @ 10437

History | View | Annotate | Download (17.7 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
import java.io.File;
55
import java.io.IOException;
56

    
57
import javax.swing.DefaultComboBoxModel;
58
import javax.swing.JButton;
59
import javax.swing.JCheckBox;
60
import javax.swing.JComboBox;
61
import javax.swing.JLabel;
62
import javax.swing.JOptionPane;
63
import javax.swing.JPanel;
64

    
65
import org.apache.log4j.Logger;
66

    
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 (DriverException e) {
263
            // TODO Auto-generated catch block
264
            e.printStackTrace();
265
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
266
            e.printStackTrace();
267
        }
268

    
269
        quitartodo.setEnabled(true);
270
        quitar.setEnabled(true);
271

    
272
        //m_bCacheDirty = false;
273
    }
274

    
275
    /**
276
     * A partir de los registros de la tabla, regenera el FRenderer. (No solo
277
     * el symbolList, si no tambi?n el arrayKeys y el defaultRenderer
278
     */
279
    private void fillSymbolListFromTable() {
280
        Value clave;
281
        FSymbol theSymbol;
282

    
283
        // Borramos las anteriores listas:
284
        m_Renderer.clear();
285

    
286
        boolean bRestoValores = false; // PONERLO EN UN CHECKBOX
287
        int hasta;
288
        String fieldName = (String) cmbFields.getSelectedItem();
289
        m_Renderer.setFieldName(fieldName);
290

    
291
        if (bRestoValores) {
292
            hasta = symbolTable.getRowCount() - 1;
293
        } else {
294
            hasta = symbolTable.getRowCount();
295
        }
296

    
297
        for (int row = 0; row < symbolTable.getRowCount(); row++) {
298
            clave = (Value) symbolTable.getFieldValue(row, 1);
299
            theSymbol = (FSymbol) symbolTable.getFieldValue(row, 0);
300
            theSymbol.setDescription((String) symbolTable.getFieldValue(row, 2));
301
            m_Renderer.addSymbol(clave, theSymbol);
302
            System.out.println(clave);
303
        }
304

    
305
        if (bRestoValores) {
306
            theSymbol = (FSymbol) symbolTable.getFieldValue(hasta, 0);
307
            m_Renderer.setDefaultSymbol(theSymbol);
308
        }
309
    }
310

    
311
    /**
312
     * DOCUMENT ME!
313
     */
314
    private void fillFieldNames() {
315
        DataSource rs;
316

    
317
        try {
318
            rs = ((AlphanumericData) layer).getRecordset();
319
            logger.debug("rs.start()");
320
            rs.start();
321

    
322
            String[] nomFields = new String[rs.getFieldCount()];
323

    
324
            for (int i = 0; i < rs.getFieldCount(); i++) {
325
                nomFields[i] = rs.getFieldName(i).trim();
326
            }
327

    
328
            rs.stop();
329

    
330
            DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields);
331
            cmbFields.setModel(cM);
332

    
333
            // fieldsListValor.setSelectedIndex(0);
334
        } catch (DriverException e) {
335
            e.printStackTrace();
336
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
337
            e.printStackTrace();
338
        }
339

    
340
        symbolTable.removeAllItems();
341
    }
342

    
343
    /**
344
     * DOCUMENT ME!
345
     *
346
     * @param lyr DOCUMENT ME!
347
     * @param r DOCUMENT ME!
348
     */
349
    public void setData(FLayer layer, Legend legend) {
350
            this.layer = (ClassifiableVectorial) layer;
351
              int shapeType = 0;
352
              try {
353
                      shapeType = this.layer.getShapeType();
354
              } catch (com.iver.cit.gvsig.fmap.DriverException e) {
355
                    NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
356
                }
357

    
358
              if (symbolTable != null)
359
                      pnlCenter.remove(symbolTable);
360
              symbolTable = new FSymbolTable("values", shapeType);
361
              pnlCenter.add(symbolTable, BorderLayout.CENTER);
362

    
363
        fillFieldNames();
364

    
365
        // Si es de valor ?nico, rellenamos la tabla
366
        // y seleccionamos el campo que estamos usando
367
        symbolTable.removeAllItems();
368

    
369
        if (legend instanceof VectorialUniqueValueLegend) {
370
            m_Renderer = (VectorialUniqueValueLegend) legend;
371
            getChbUseDefault().setSelected(m_Renderer.isUseDefaultSymbol());
372
            cmbFields.getModel().setSelectedItem(m_Renderer.getFieldName());
373
            symbolTable.fillTableFromSymbolList(m_Renderer.getSymbols(),
374
                m_Renderer.getValues(), m_Renderer.getDescriptions());
375
        } else {
376
                m_Renderer = new VectorialUniqueValueLegend(shapeType);
377
        }
378
    }
379

    
380
    /* (non-Javadoc)
381
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
382
     */
383
    public Legend getLegend() {
384
        fillSymbolListFromTable();
385

    
386
        if (auxLegend != null) {
387
            m_Renderer.setDefaultSymbol(auxLegend.getDefaultSymbol());
388
            m_Renderer.useDefaultSymbol(chbUseDefault.isSelected());
389
        }
390

    
391
        return m_Renderer;
392
    }
393

    
394
    /**
395
     * This method initializes chbUseDefault
396
     *
397
     * @return javax.swing.JCheckBox
398
     */
399
    private JCheckBox getChbUseDefault() {
400
        if (chbUseDefault == null) {
401
            chbUseDefault = new JCheckBox();
402
            chbUseDefault.setSelected(true);
403
            chbUseDefault.addActionListener(new java.awt.event.ActionListener() {
404
                    public void actionPerformed(java.awt.event.ActionEvent e) {
405
                            if (auxLegend==null)auxLegend=m_Renderer;
406
                            if (chbUseDefault.isSelected()) {
407
                            addDefault();
408
                        } else {
409
                            delDefault();
410
                        }
411
                    }
412
                });
413
            chbUseDefault.setText(PluginServices.getText(this, "resto_valores"));
414
        }
415

    
416
        return chbUseDefault;
417
    }
418

    
419
    /**
420
     * A?ade el resto de valores.
421
     */
422
    private void addDefault() {
423
        auxLegend.getDefaultSymbol().setDescription("Default");
424
        auxLegend.addSymbol(new NullUniqueValue(), auxLegend.getDefaultSymbol());
425
        symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
426
            new NullUniqueValue(), auxLegend.getDefaultSymbol().getDescription());
427
        symbolTable.repaint();
428
    }
429

    
430
    /**
431
     * Elimina el resto de valores que no estan representados por ning?n otro s?mbolo..
432
     */
433
    private void delDefault() {
434
        auxLegend.delSymbol(new NullUniqueValue());
435
        symbolTable.removeRow(new NullUniqueValue());
436
        symbolTable.repaint();
437
    }
438

    
439
    /**
440
     * Listener.
441
     *
442
     * @author Vicente Caballero Navarro
443
     */
444
    class MyListener implements ActionListener {
445
        //private FLyrShp m_layer;
446
        // private FPanelLegendValues m_Parent;
447
        public MyListener() { // FPanelLegendValues p) {
448

    
449
            // m_Parent = p;
450
        }
451

    
452
        /**
453
         * DOCUMENT ME!
454
         *
455
         * @param e DOCUMENT ME!
456
         */
457
        public void actionPerformed(ActionEvent e) {
458
            // rellenarValue();
459
            System.out.println("ActionEvent con " + e.getActionCommand());
460

    
461
            //modificar el combobox de valor
462
            if (e.getActionCommand() == "FIELD_SELECTED") {
463
                JComboBox cb = (JComboBox) e.getSource();
464
                String fieldName = (String) cb.getSelectedItem();
465
                System.out.println("Nombre del campo: " + fieldName);
466
                symbolTable.removeAllItems();
467

    
468
                if (fieldName != m_Renderer.getFieldName()) {
469
                    //m_bCacheDirty = true;
470
                    m_Renderer.setFieldName(fieldName);
471
                }
472
            }
473

    
474
            //A?adir todos los elementos por valor
475
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
476
                fillTableValues();
477
            }
478

    
479
            //A?adir un ?nico elemento
480
            if (e.getActionCommand() == "ADD_VALUE") {
481
                try {
482
                    symbolTable.addTableRecord(new FSymbol(
483
                            layer.getShapeType()),
484
                        ValueFactory.createValue(0.0), "0 - 0");
485
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
486
                    e1.printStackTrace();
487
                }
488

    
489
                /*        a?adir("Nuevo_Valor");
490
                   aceptar.setEnabled(true);
491
                   quitartodo.setEnabled(true);
492
                   quitar.setEnabled(true); */
493
            }
494

    
495
            //Vacia la tabla
496
            if (e.getActionCommand() == "REMOVE_ALL") {
497
                symbolTable.removeAllItems();
498

    
499
                // aceptar.setEnabled(false);
500
            }
501

    
502
            //Quitar solo el elemento seleccionado
503
            if (e.getActionCommand() == "REMOVE") {
504
                symbolTable.removeSelectedRows();
505
            }
506
        }
507
    }
508

    
509
        public String getDescription() {
510
                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") + ".";
511
        }
512

    
513
        public ISymbol getPreviewSymbol() {
514
                if (previewSymbol == null) {
515
                        try {
516
                                previewSymbol = new PictureMarkerSymbol(
517
                                                this.getClass().getClassLoader()
518
                                                                .getResource("images/ValoresUnicos.png"));
519
                        } catch (IOException e) {
520
                                return null;
521
                        }
522
                }
523
                return previewSymbol;
524
        }
525

    
526
        public Class getParentClass() {
527
                return Categories.class;
528
        }
529

    
530
        public String getTitle() {
531
                return PluginServices.getText(this,"Valores_unicos");
532
        }
533

    
534
        public JPanel getPanel() {
535
                return this;
536
        }
537

    
538
        public Class getLegendClass() {
539
                return VectorialUniqueValueLegend.class;
540
        }
541
}