Statistics
| Revision:

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

History | View | Annotate | Download (19.2 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.project.documents.view.legend.gui;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.FlowLayout;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.io.File;
49
import java.io.IOException;
50
import java.util.Random;
51

    
52
import javax.swing.DefaultComboBoxModel;
53
import javax.swing.JCheckBox;
54
import javax.swing.JComboBox;
55
import javax.swing.JLabel;
56
import javax.swing.JOptionPane;
57
import javax.swing.JPanel;
58

    
59
import org.apache.log4j.Logger;
60
import org.gvsig.gui.beans.swing.JButton;
61
import org.gvsig.raster.datastruct.ColorItem;
62

    
63
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
64
import com.hardcode.gdbms.engine.data.DataSource;
65
import com.hardcode.gdbms.engine.values.NullValue;
66
import com.hardcode.gdbms.engine.values.Value;
67
import com.hardcode.gdbms.engine.values.ValueFactory;
68
import com.iver.andami.PluginServices;
69
import com.iver.andami.messages.NotificationManager;
70
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
71
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
72
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
73
import com.iver.cit.gvsig.fmap.core.symbols.PictureFillSymbol;
74
import com.iver.cit.gvsig.fmap.layers.FLayer;
75
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
76
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
77
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
78
import com.iver.cit.gvsig.fmap.rendering.AbstractClassifiedVectorLegend;
79
import com.iver.cit.gvsig.fmap.rendering.ILegend;
80
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
81
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
82
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
83
import com.iver.cit.gvsig.fmap.rendering.ZSort;
84
import com.iver.cit.gvsig.gui.styling.JComboBoxColorScheme;
85
import com.iver.cit.gvsig.gui.styling.SymbolLevelsWindow;
86

    
87

    
88
/**
89
 * DOCUMENT ME!
90
 *
91
 * @author fjp To change the template for this generated type comment go to
92
 *         Window>Preferences>Java>Code Generation>Code and
93
 *         Comments
94
 */
95
public class VectorialUniqueValue extends JPanel implements ILegendPanel, ActionListener{
96
    private static Logger logger = Logger.getLogger(VectorialUniqueValue.class.getName());
97

    
98
    // private TOC m_TOC;
99
    private VectorialUniqueValueLegend theLegend;
100
    private ClassifiableVectorial layer;
101

    
102
    //private boolean m_bCacheDirty = true;
103
    private SymbolTable symbolTable;
104
    private JComboBox cmbFields;
105
    private JButton btnRemoveAll;
106
    private JButton btnRemove;
107
    private JCheckBox chbUseDefault = null;
108
    private VectorialUniqueValueLegend auxLegend;
109
        private PictureFillSymbol previewSymbol;
110
        private JPanel pnlCenter;
111
        private ZSort zSort;
112
        private JButton btnOpenSymbolLevelsEditor;
113

    
114
        private JComboBoxColorScheme cmbColorScheme;
115

    
116
    /**
117
     *
118
     */
119
    public VectorialUniqueValue() {
120
        super();
121
        initComponents();
122
    }
123

    
124
    /**
125
     * DOCUMENT ME!
126
     */
127
    protected void initComponents() {
128
        /* JLabel label = new JLabel();
129
           label.setIcon(new javax.swing.ImageIcon(Abrir.class.getClassLoader()
130
                                                                                                              .getResource("images/ValoresUnicos.png")));
131
           limagen[1] = new JLabel();
132
           limagen[1] = label; */
133
        JPanel pnlButtons = new JPanel();
134

    
135
        JButton btnAddAll = new JButton(PluginServices.getText(this,
136
                    "Anadir_todos"));
137
        btnAddAll.setActionCommand("ADD_ALL_VALUES");
138
        btnAddAll.addActionListener(this);
139
        pnlButtons.add(btnAddAll);
140

    
141
        JButton btnAdd = new JButton(PluginServices.getText(this, "Anadir"));
142
        btnAdd.setActionCommand("ADD_VALUE");
143
        btnAdd.addActionListener(this);
144
        pnlButtons.add(btnAdd);
145

    
146
        btnRemoveAll = new JButton(PluginServices.getText(this, "Quitar_todos"));
147
        btnRemoveAll.setActionCommand("REMOVE_ALL");
148
        btnRemoveAll.addActionListener(this);
149
        pnlButtons.add(btnRemoveAll);
150

    
151
        btnRemove = new JButton(PluginServices.getText(this, "Quitar"));
152
        btnRemove.setActionCommand("REMOVE");
153
        btnRemove.addActionListener(this);
154
        pnlButtons.add(btnRemove);
155

    
156
        btnOpenSymbolLevelsEditor = new JButton(PluginServices.getText(this, "symbol_levels"));
157
        btnOpenSymbolLevelsEditor.addActionListener(this);
158
        btnOpenSymbolLevelsEditor.setActionCommand("OPEN_SYMBOL_LEVEL_EDITOR");
159
        pnlButtons.add(btnOpenSymbolLevelsEditor);
160

    
161
                pnlCenter = new JPanel();
162
        pnlCenter.setLayout(new BorderLayout());
163

    
164
        cmbFields = new JComboBox();
165
        cmbFields.setActionCommand("FIELD_SELECTED");
166
        cmbFields.addActionListener(this);
167
        cmbFields.setVisible(true);
168

    
169
        JPanel pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 0));
170
        JPanel pAux1 = new JPanel();
171

    
172
        JLabel lblFieldClassification = new JLabel(PluginServices.getText(
173
                    this, "Campo_de_clasificacion"));
174
        pAux1.add(lblFieldClassification);
175
        pAux1.add(cmbFields);
176
        pAux1.add(getChbUseDefault(), null);
177
        pnlNorth.add(pAux1);
178

    
179
        pAux1 = new JPanel();
180
        pAux1.add(new JLabel(PluginServices.getText(this, "color_scheme")+":"));
181
        cmbColorScheme = new JComboBoxColorScheme(false);
182

    
183
        pAux1.add(cmbColorScheme);
184

    
185
        pnlNorth.add(pAux1);
186

    
187
        this.setLayout(new BorderLayout());
188
        this.add(pnlNorth, BorderLayout.NORTH);
189
        this.add(pnlCenter, BorderLayout.CENTER);
190
        this.add(pnlButtons, BorderLayout.SOUTH);
191

    
192
    }
193

    
194
    /**
195
     * DOCUMENT ME!
196
     */
197
    private void fillTableValues() {
198
        DataSource elRs;
199

    
200
        try {
201
            elRs = ((FLyrVect) layer).getRecordset();
202
            logger.debug("elRs.start()");
203
            elRs.start();
204

    
205
            int idField = -1;
206
            String fieldName = (String) cmbFields.getSelectedItem();
207
            if (fieldName==null) {
208
                    JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_hay_campo_seleccionado"));
209
                    return;
210
            }
211

    
212
            idField = elRs.getFieldIndexByName(fieldName);
213
            theLegend.setClassifyingFieldNames(new String[] {fieldName});
214

    
215
            //long numReg = elRs.getRowCount();
216
            if (idField == -1) {
217
                NotificationManager.addWarning(
218
                                PluginServices.getText(this, "unrecognized_field_name")+" " + fieldName, null);
219

    
220
                return;
221
            }
222

    
223
            symbolTable.removeAllItems();
224

    
225
            int numSymbols = 0;
226
            ISymbol theSymbol = null;
227

    
228
            //auxLegend=(VectorialUniqueValueLegend)m_lyr.getLegend();
229
            auxLegend = LegendFactory.createVectorialUniqueValueLegend(layer.getShapeType());
230

    
231
            Value clave;
232

    
233
            //Object resul;
234
            if (chbUseDefault.isSelected()) {
235
                auxLegend.getDefaultSymbol().setDescription("Default");
236
                auxLegend.addSymbol(new NullUniqueValue(),
237
                    auxLegend.getDefaultSymbol());
238
            }
239

    
240
            ColorItem[] colorScheme = cmbColorScheme.getSelectedColors();
241
            Random rand = new Random(System.currentTimeMillis());
242

    
243
            for (int j = 0; j < elRs.getRowCount(); j++) {
244
                clave = elRs.getFieldValue(j, idField);
245

    
246
                if (clave instanceof NullValue) {
247
                    continue;
248
                }
249

    
250
                ////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
251
                if (auxLegend.getSymbolByValue(clave) == null) {
252
                    //si no esta creado el simbolo se crea
253
                    // jaume (moved to ISymbol); theSymbol = new FSymbol(layer.getShapeType());
254
                        theSymbol = SymbologyFactory.
255
                                createDefaultSymbolByShapeType(layer.getShapeType(),
256
                                                colorScheme[rand.nextInt(colorScheme.length)].getColor());
257
                    theSymbol.setDescription(clave.toString());
258
                    auxLegend.addSymbol(clave, theSymbol);
259

    
260
                    numSymbols++;
261

    
262
                    if (numSymbols == 100) {
263
                        int resp = JOptionPane.showConfirmDialog(this,
264
                                PluginServices.getText(this,
265
                                    "mas_de_100_simbolos"),
266
                                PluginServices.getText(this, "quiere_continuar"),
267
                                JOptionPane.YES_NO_OPTION,
268
                                JOptionPane.WARNING_MESSAGE);
269

    
270
                        if ((resp == JOptionPane.NO_OPTION) ||
271
                                (resp == JOptionPane.DEFAULT_OPTION)) {
272
                            return;
273
                        }
274
                    }
275
                }
276
            } // for
277

    
278
            symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
279
                auxLegend.getValues(), auxLegend.getDescriptions());
280
            elRs.stop();
281
        } catch (ReadDriverException e) {
282
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
283
        }
284

    
285
        btnRemoveAll.setEnabled(true);
286
        btnRemove.setEnabled(true);
287

    
288
        //m_bCacheDirty = false;
289
    }
290

    
291
    /**
292
     * A partir de los registros de la tabla, regenera el FRenderer. (No solo
293
     * el symbolList, si no tambi�n el arrayKeys y el defaultRenderer
294
     */
295
    private void fillSymbolListFromTable() {
296
        Value clave;
297
        ISymbol theSymbol;
298

    
299
        // Borramos las anteriores listas:
300

    
301
        boolean bRestoValores = false; // PONERLO EN UN CHECKBOX
302
        int hasta;
303
        String fieldName = (String) cmbFields.getSelectedItem();
304
        theLegend.setClassifyingFieldNames(new String[] {fieldName} );
305
        /////////////////////////////////////////PEPE
306
        FLyrVect m = (FLyrVect) layer;
307
        try {
308
                int fieldType = m.getSource().getRecordset().getFieldType((int)cmbFields.getSelectedIndex());
309
                theLegend.setClassifyingFieldTypes(new int[] {fieldType});
310
        } catch (ReadDriverException e) {
311
                NotificationManager.addError(PluginServices.getText(this, "could_not_setup_legend"), e);
312
        }
313
        /////////////////////////////////////////PEPE
314
        if (bRestoValores) {
315
            hasta = symbolTable.getRowCount() - 1;
316
        } else {
317
            hasta = symbolTable.getRowCount();
318
        }
319

    
320
        for (int row = 0; row < symbolTable.getRowCount(); row++) {
321
            clave = (Value) symbolTable.getFieldValue(row, 1);
322
            theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
323
            theSymbol.setDescription((String) symbolTable.getFieldValue(row, 2));
324
            theLegend.addSymbol(clave, theSymbol);
325
        }
326

    
327
        if (bRestoValores) {
328
            theSymbol = (ISymbol) symbolTable.getFieldValue(hasta, 0);
329
            theLegend.setDefaultSymbol(theSymbol);
330
        }
331
    }
332

    
333
    /**
334
     * DOCUMENT ME!
335
     */
336
    private void fillFieldNames() {
337
        DataSource rs;
338

    
339
        try {
340
            rs = ((AlphanumericData) layer).getRecordset();
341
            logger.debug("rs.start()");
342
            rs.start();
343

    
344
            String[] nomFields = new String[rs.getFieldCount()];
345

    
346
            for (int i = 0; i < rs.getFieldCount(); i++) {
347
                nomFields[i] = rs.getFieldName(i).trim();
348
            }
349

    
350
            rs.stop();
351

    
352
            DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields);
353
            cmbFields.setModel(cM);
354

    
355
            // fieldsListValor.setSelectedIndex(0);
356
        } catch (ReadDriverException e) {
357
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
358
        }
359
    }
360

    
361
    public void setData(FLayer layer, ILegend legend) {
362
            this.layer = (ClassifiableVectorial) layer;
363
              int shapeType = 0;
364
              try {
365
                      shapeType = this.layer.getShapeType();
366
              } catch (ReadDriverException e) {
367
                    NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
368
                }
369

    
370
              if (symbolTable != null)
371
                      pnlCenter.remove(symbolTable);
372
              symbolTable = new SymbolTable(this, SymbolTable.VALUES_TYPE, shapeType);
373
              pnlCenter.add(symbolTable, BorderLayout.CENTER);
374

    
375
        fillFieldNames();
376

    
377
        symbolTable.removeAllItems();
378

    
379
        if (legend instanceof VectorialUniqueValueLegend) {
380
            theLegend = (VectorialUniqueValueLegend) legend;
381
            getChbUseDefault().setSelected(theLegend.isUseDefaultSymbol());
382
            cmbFields.getModel().setSelectedItem(theLegend.getClassifyingFieldNames()[0]);
383
            symbolTable.fillTableFromSymbolList(theLegend.getSymbols(),
384
                                                                                    theLegend.getValues(),
385
                                                                                    theLegend.getDescriptions());
386
            zSort = theLegend.getZSort();
387
        } else {
388
            theLegend = new VectorialUniqueValueLegend(shapeType);
389
        }
390
    }
391

    
392
    /* (non-Javadoc)
393
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
394
     */
395
    public ILegend getLegend() {
396
        fillSymbolListFromTable();
397

    
398
        if (auxLegend != null) {
399
                // your settings that are not the set of symbols must be located here
400
                auxLegend.setClassifyingFieldNames(
401
                                new String[] {(String) cmbFields.getSelectedItem()});
402

    
403
            theLegend = auxLegend;
404
        }
405
        theLegend.setZSort(zSort);
406

    
407
        return theLegend;
408
    }
409

    
410
    private JCheckBox getChbUseDefault() {
411
        if (chbUseDefault == null) {
412
            chbUseDefault = new JCheckBox();
413
            chbUseDefault.setSelected(true);
414
            chbUseDefault.addActionListener(new java.awt.event.ActionListener() {
415
                    public void actionPerformed(java.awt.event.ActionEvent e) {
416
                            if (auxLegend==null)auxLegend=theLegend;
417
                            if (chbUseDefault.isSelected()) {
418
                                    addDefault();
419
                            } else {
420
                                    delDefault();
421
                            }
422
                    }
423
            });
424
            chbUseDefault.setText(PluginServices.getText(this, "resto_valores"));
425
        }
426

    
427
        return chbUseDefault;
428
    }
429

    
430
    /**
431
     * A�ade el resto de valores.
432
     */
433
    private void addDefault() {
434
        auxLegend.getDefaultSymbol().setDescription("Default");
435
        auxLegend.addSymbol(new NullUniqueValue(), auxLegend.getDefaultSymbol());
436
        symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
437
            new NullUniqueValue(), auxLegend.getDefaultSymbol().getDescription());
438
        symbolTable.repaint();
439
    }
440

    
441
    /**
442
     * Elimina el resto de valores que no estan representados por ning�n otro s�mbolo..
443
     */
444
    private void delDefault() {
445
        auxLegend.delSymbol(new NullUniqueValue());
446
        symbolTable.removeRow(new NullUniqueValue());
447
        symbolTable.repaint();
448
    }
449

    
450

    
451
    public void actionPerformed(ActionEvent e) {
452

    
453
            //modificar el combobox de valor
454
            if (e.getActionCommand() == "FIELD_SELECTED") {
455
                    JComboBox cb = (JComboBox) e.getSource();
456
                    String fieldName = (String) cb.getSelectedItem();
457
                    symbolTable.removeAllItems();
458

    
459
//                    if (theLegend.getClassifyingFieldNames()!=null && fieldName != theLegend.getClassifyingFieldNames()[0]) {
460
//                            //m_bCacheDirty = true;
461
//                            theLegend.setClassifyingFieldNames(new String[] {fieldName});
462
//                    }
463
//
464
//                    //////////////////////////////////////////PEPE
465
//                    FLyrVect m = (FLyrVect) layer;
466
//                    try {
467
//                            int fieldType = m.getSource().getRecordset().getFieldType((int)cb.getSelectedIndex());
468
//                            if (theLegend.getClassifyingFieldTypes()!=null && fieldType != theLegend.getClassifyingFieldTypes()[0]) {
469
//                                    //m_bCacheDirty = true;
470
//                                    theLegend.setClassifyingFieldTypes(new int[] {fieldType});
471
//                            }
472
//                    } catch (ReadDriverException e1) {
473
//                            NotificationManager.addError(PluginServices.getText(this, "could_not_setup_legend"), e1);
474
//                    }
475
//                    /////////////////////////////////////////PEPE
476
            }
477

    
478
            // add all elements by value
479
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
480
                    fillTableValues();
481
            }
482

    
483
            // add only one value
484
            if (e.getActionCommand() == "ADD_VALUE") {
485
                    try {
486
                            symbolTable.addTableRecord(SymbologyFactory.
487
                                            createDefaultSymbolByShapeType(layer.getShapeType()),
488
                                            ValueFactory.createValue(0.0), "0 - 0");
489
                    } catch (ReadDriverException ex) {
490
                            NotificationManager.addError(PluginServices.getText(this, "getting_shape_type"), ex);
491
                    }
492
            }
493

    
494
            //Vacia la tabla
495
            if (e.getActionCommand() == "REMOVE_ALL") {
496
                    symbolTable.removeAllItems();
497
                    theLegend.setZSort(null);
498
                    zSort = null;
499
            }
500

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

    
506
            if (e.getActionCommand() == "OPEN_SYMBOL_LEVEL_EDITOR") {
507

    
508
                        if (theLegend != null) {
509
                                ZSort myZSort = ((AbstractClassifiedVectorLegend) getLegend()).getZSort();
510
                                if (myZSort == null) {
511
                                                myZSort = new ZSort(theLegend);
512
                                }
513
                                SymbolLevelsWindow sl = new SymbolLevelsWindow(myZSort);
514
                                PluginServices.getMDIManager().addWindow(sl);
515
                                zSort = sl.getZSort();
516
                                if (auxLegend != null)
517
                                        auxLegend.setZSort(zSort);
518
                        }
519

    
520
            }
521
    }
522

    
523
        public String getDescription() {
524
                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") + ".";
525
        }
526

    
527
        public ISymbol getIconSymbol() {
528
                if (previewSymbol == null) {
529
                        try {
530
                            previewSymbol = new PictureFillSymbol();
531
                            previewSymbol.setImage(
532
                                            new File(
533
                                                            this.getClass().getClassLoader().
534
                                                        getResource("images/ValoresUnicos.png").
535
                                                        getFile()));
536
                                previewSymbol.getMarkerFillProperties().
537
                            setFillStyle(
538
                                            IMarkerFillPropertiesStyle.SINGLE_CENTERED_SYMBOL);
539

    
540
                        } catch (IOException e) {
541
                                return null;
542
                        }
543
                }
544
                return previewSymbol;
545
        }
546

    
547
        public Class getParentClass() {
548
                return Categories.class;
549
        }
550

    
551
        public String getTitle() {
552
                return PluginServices.getText(this,"Valores_unicos");
553
        }
554

    
555
        public JPanel getPanel() {
556
                return this;
557
        }
558

    
559
        public Class getLegendClass() {
560
                return VectorialUniqueValueLegend.class;
561
        }
562

    
563

    
564
        public boolean isSuitableFor(FLayer layer) {
565
                return (layer instanceof FLyrVect);
566
        }
567
}