Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / legend / gui / VectorialUniqueValue.java @ 30121

History | View | Annotate | Download (24.3 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 org.gvsig.app.project.documents.view.legend.gui;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Color;
45
import java.awt.Component;
46
import java.awt.Dimension;
47
import java.awt.GridLayout;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.util.ArrayList;
51
import java.util.Iterator;
52
import java.util.Random;
53

    
54
import javax.swing.DefaultComboBoxModel;
55
import javax.swing.ImageIcon;
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.gvsig.andami.PluginServices;
63
import org.gvsig.andami.messages.NotificationManager;
64
import org.gvsig.app.gui.styling.JComboBoxColorScheme;
65
import org.gvsig.app.gui.styling.SymbolLevelsWindow;
66
import org.gvsig.fmap.dal.exception.DataException;
67
import org.gvsig.fmap.dal.exception.ReadException;
68
import org.gvsig.fmap.dal.feature.DisposableIterator;
69
import org.gvsig.fmap.dal.feature.Feature;
70
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
71
import org.gvsig.fmap.dal.feature.FeatureSet;
72
import org.gvsig.fmap.dal.feature.FeatureStore;
73
import org.gvsig.fmap.mapcontext.MapContextLocator;
74
import org.gvsig.fmap.mapcontext.MapContextManager;
75
import org.gvsig.fmap.mapcontext.layers.FLayer;
76
import org.gvsig.fmap.mapcontext.layers.operations.ClassifiableVectorial;
77
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
78
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
79
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
80
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
81
import org.gvsig.gui.beans.swing.JBlank;
82
import org.gvsig.gui.beans.swing.JButton;
83
import org.gvsig.raster.datastruct.ColorItem;
84
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
85
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.ZSort;
86
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.AbstractClassifiedVectorLegend;
87
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialUniqueValueLegend;
88
import org.gvsig.utils.XMLException;
89
import org.slf4j.Logger;
90
import org.slf4j.LoggerFactory;
91

    
92

    
93

    
94
/**
95
 * DOCUMENT ME!
96
 *
97
 * @author fjp To change the template for this generated type comment go to
98
 *         Window>Preferences>Java>Code Generation>Code and
99
 *         Comments
100
 */
101
public class VectorialUniqueValue extends JPanel implements ILegendPanel, ActionListener{
102
    private static final Logger logger = LoggerFactory
103
    .getLogger(VectorialUniqueValue.class);
104

    
105
    protected VectorialUniqueValueLegend theLegend;
106
    private ClassifiableVectorial layer;
107
    private SymbolTable symbolTable;
108
    protected JComboBox cmbFields;
109
    private JButton btnRemoveAll;
110
    private JButton btnRemove;
111
    private JCheckBox chbUseDefault = null;
112
    private JSymbolPreviewButton defaultSymbolPrev;
113
    private VectorialUniqueValueLegend auxLegend;
114
        private JPanel pnlCenter;
115
        private JButton btnOpenSymbolLevelsEditor;
116

    
117
        private JComboBoxColorScheme cmbColorScheme;
118
        private GridBagLayoutPanel defaultSymbolPanel = new GridBagLayoutPanel();
119
                
120
        private MapContextManager mapContextManager = MapContextLocator
121
                        .getMapContextManager();
122

    
123
    /**
124
     *
125
     */
126
    public VectorialUniqueValue() {
127
        super();
128
        initComponents();
129
    }
130

    
131
    /**
132
     * DOCUMENT ME!
133
     */
134
    protected void initComponents() {
135
        /* JLabel label = new JLabel();
136
           label.setIcon(new javax.swing.ImageIcon(Abrir.class.getClassLoader()
137
                                                                                                              .getResource("images/ValoresUnicos.png")));
138
           limagen[1] = new JLabel();
139
           limagen[1] = label; */
140
        JPanel pnlButtons = new JPanel();
141

    
142
        JButton btnAddAll = new JButton(PluginServices.getText(this,
143
                    "Anadir_todos"));
144
        btnAddAll.setActionCommand("ADD_ALL_VALUES");
145
        btnAddAll.addActionListener(this);
146
        pnlButtons.add(btnAddAll);
147

    
148
        JButton btnAdd = new JButton(PluginServices.getText(this, "Anadir"));
149
        btnAdd.setActionCommand("ADD_VALUE");
150
        btnAdd.addActionListener(this);
151
        pnlButtons.add(btnAdd);
152

    
153
        btnRemoveAll = new JButton(PluginServices.getText(this, "Quitar_todos"));
154
        btnRemoveAll.setActionCommand("REMOVE_ALL");
155
        btnRemoveAll.addActionListener(this);
156
        pnlButtons.add(btnRemoveAll);
157

    
158
        btnRemove = new JButton(PluginServices.getText(this, "Quitar"));
159
        btnRemove.setActionCommand("REMOVE");
160
        btnRemove.addActionListener(this);
161
        pnlButtons.add(btnRemove);
162

    
163
        btnOpenSymbolLevelsEditor = new JButton(PluginServices.getText(this, "symbol_levels"));
164
        btnOpenSymbolLevelsEditor.addActionListener(this);
165
        btnOpenSymbolLevelsEditor.setActionCommand("OPEN_SYMBOL_LEVEL_EDITOR");
166
        pnlButtons.add(btnOpenSymbolLevelsEditor);
167
                btnOpenSymbolLevelsEditor.setEnabled(symbolTable != null && symbolTable.getRowCount()>0);
168

    
169
                pnlCenter = new JPanel();
170
        pnlCenter.setLayout(new BorderLayout());
171

    
172
        cmbFields = new JComboBox();
173
        cmbFields.setActionCommand("FIELD_SELECTED");
174
        cmbFields.addActionListener(this);
175
        cmbFields.setVisible(true);
176

    
177
                JPanel pnlNorth = new JPanel();
178
                pnlNorth.setLayout(new GridLayout(0,2));
179

    
180
                GridBagLayoutPanel auxPanel = new GridBagLayoutPanel();
181
                JLabel lblFieldClassification = new JLabel(PluginServices.getText(
182
                                this, "Campo_de_clasificacion")+": ");
183
                auxPanel.add(lblFieldClassification);
184
                auxPanel.add(cmbFields);
185
                pnlNorth.add(auxPanel);
186

    
187
                auxPanel = new GridBagLayoutPanel();
188
                auxPanel.add(new JLabel(PluginServices.getText(this, "color_scheme")+": "));
189
                cmbColorScheme = new JComboBoxColorScheme(false);
190
                cmbColorScheme.addActionListener(this);
191
                auxPanel.add(cmbColorScheme);
192
                pnlNorth.add(auxPanel);
193

    
194

    
195
                defaultSymbolPanel.add(getChbUseDefault(), null);
196
                pnlNorth.add(defaultSymbolPanel);
197
                pnlNorth.add(new JBlank(0,30));
198

    
199
                this.setLayout(new BorderLayout());
200
                this.add(pnlNorth, BorderLayout.NORTH);
201
                this.add(pnlCenter, BorderLayout.CENTER);
202
                this.add(pnlButtons, BorderLayout.SOUTH);
203

    
204

    
205
    }
206

    
207
    /**
208
     * DOCUMENT ME!
209
     */
210
    private void fillTableValues() {
211
        FeatureStore elRs;
212

    
213
        try {
214
            elRs = ((FLyrVect) layer).getFeatureStore();
215
//            logger.debug("elRs.start()");
216
//            elRs.start();
217

    
218
//            int idField = -1;
219
            String fieldName = (String) cmbFields.getSelectedItem();
220
            if (fieldName==null) {
221
                    JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_hay_campo_seleccionado"));
222
                    return;
223
            }
224

    
225
//            idField = elRs.getFieldIndexByName(fieldName);
226
            auxLegend.setClassifyingFieldNames(new String[] {fieldName});
227

    
228
            //long numReg = elRs.getRowCount();
229
//            if (idField == -1) {
230
//                NotificationManager.addWarning(
231
//                                PluginServices.getText(this, "unrecognized_field_name")+" " + fieldName, null);
232
//
233
//                return;
234
//            }
235

    
236
            symbolTable.removeAllItems();
237

    
238
            int numSymbols = 0;
239
            ISymbol theSymbol = null;
240

    
241
            //auxLegend=(VectorialUniqueValueLegend)m_lyr.getLegend();
242

    
243
            //            auxLegend = LegendFactory.createVectorialUniqueValueLegend(layer.getShapeType());
244
            auxLegend = (VectorialUniqueValueLegend) MapContextLocator
245
                                        .getMapContextManager().createLegend(
246
                                                        IVectorialUniqueValueLegend.LEGEND_NAME);
247
            auxLegend.setShapeType(layer.getShapeType());
248

    
249
            Object clave=null;
250

    
251
            //Object resul;
252
            if (chbUseDefault.isSelected()) {
253
                auxLegend.getDefaultSymbol().setDescription("Default");
254
                auxLegend.addSymbol(null, auxLegend.getDefaultSymbol());
255
            }
256

    
257
            ColorItem[] colorScheme = cmbColorScheme.getSelectedColors();
258

    
259
            Color[] colors = new Color[colorScheme.length];
260
                        for (int i = 0; i < colorScheme.length; i++) {
261
                                colors[i] = colorScheme[i].getColor();
262
                        }
263
                        auxLegend.setColorScheme(colors);
264

    
265
            Random rand = new Random(System.currentTimeMillis());
266

    
267
            FeatureSet set = null;
268
                        DisposableIterator iterator = null;
269

    
270
                        try {
271
                                set = elRs.getFeatureSet();
272

    
273
                                int count = 0;
274

    
275
                                iterator = set.iterator();
276
                                while (iterator.hasNext()) {
277
                                        Feature feature = (Feature) iterator.next();
278
                                        clave = feature.get(fieldName);
279
                                        // }
280
                                        // for (int j = 0; j < elRs.getRowCount(); j++) {
281
                                        // clave = elRs.getFieldValue(j, idField);
282

    
283
//                                        if (clave instanceof NullValue) {
284
//                                                continue;
285
//                                        }
286
                                        if (clave == null) {
287
                                                continue;
288
                                        }
289

    
290
                                        // //Comprobar que no esta repetido y no hace falta
291
                                        // introducir en el hashtable el campo junto con el simbolo.
292
                                        if (auxLegend.getSymbolByValue(clave) == null) {
293

    
294
                                                if (count == 100) {
295
                                                        int resp = JOptionPane.showConfirmDialog(this,
296
                                                                        PluginServices.getText(this,
297
                                                                                        "mas_de_100_simbolos"),
298
                                                                        PluginServices.getText(this,
299
                                                                                        "quiere_continuar"),
300
                                                                        JOptionPane.YES_NO_OPTION,
301
                                                                        JOptionPane.WARNING_MESSAGE);
302

    
303
                                                        if ((resp == JOptionPane.NO_OPTION)
304
                                                                        || (resp == JOptionPane.DEFAULT_OPTION)) {
305
                                                                break;
306
                                                        }
307
                                                }
308

    
309

    
310
                                                // si no esta creado el simbolo se crea
311
                                                // jaume (moved to ISymbol); theSymbol = new
312
                                                // FSymbol(layer.getShapeType());
313
                                                theSymbol = mapContextManager.createSymbol(layer
314
                                                                                .getShapeType(), colorScheme[rand
315
                                                                                .nextInt(colorScheme.length)]
316
                                                                                .getColor());
317
                                                theSymbol.setDescription(clave.toString());
318
                                                auxLegend.addSymbol(clave, theSymbol);
319
                                                count++;
320
                                        }
321

    
322
                                } // for
323
                        }finally{
324
                                if (iterator != null){
325
                                        iterator.dispose();
326
                                }
327
                                if (set != null) {
328
                                        set.dispose();
329
                                }
330
                        }
331

    
332
            symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
333
                auxLegend.getValues(), auxLegend.getDescriptions());
334
//            elRs.stop();
335
//            set.dispose();
336
        } catch (DataException e) {
337
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
338
        }
339

    
340
        btnRemoveAll.setEnabled(true);
341
        btnRemove.setEnabled(true);
342

    
343
        //m_bCacheDirty = false;
344
    }
345

    
346
        private boolean compareClassifyingFieldNames(String[] a, String[] b){
347
                if (a==b) {
348
                        return true;
349
                }
350
                if (a == null || b == null) {
351
                        return false;
352
                }
353
                if (a.length != b.length) {
354
                        return false;
355
                }
356
                for (int i=0; i<a.length; i++){
357
                        if (!a[i].equals(b[i])) {
358
                                return false;
359
                        }
360
                }
361
                return true;
362
        }
363

    
364
        private boolean compareClassifyingFieldTypes(int[] a, int[] b){
365
                if (a==b) {
366
                        return true;
367
                }
368
                if (a == null || b == null) {
369
                        return false;
370
                }
371
                if (a.length != b.length) {
372
                        return false;
373
                }
374
                for (int i=0; i<a.length; i++){
375
                        if (a[i]!=b[i]) {
376
                                return false;
377
                        }
378
                }
379
                return true;
380
        }
381

    
382
    /**
383
     * A partir de los registros de la tabla, regenera el FRenderer. (No solo
384
     * el symbolList, si no tambi?n el arrayKeys y el defaultRenderer
385
     */
386
    private void fillSymbolListFromTable() {
387
        Object clave=null;
388
        ISymbol theSymbol;
389
                ArrayList visitedKeys = new ArrayList();
390
                boolean changedLegend = false;
391

    
392
            String fieldName = (String) cmbFields.getSelectedItem();
393
                String[] classifyingFieldNames = new String[] {fieldName};
394
                if(auxLegend!=null){
395
                        if(!compareClassifyingFieldNames(classifyingFieldNames,auxLegend.getClassifyingFieldNames())){
396
                                auxLegend.setClassifyingFieldNames(classifyingFieldNames);
397
                                changedLegend = true;
398
                        }
399
                } else {
400
                        auxLegend.setClassifyingFieldNames(classifyingFieldNames);
401
                        changedLegend = true;
402
                }
403

    
404
                FLyrVect m = (FLyrVect) layer;
405

    
406
        try {
407
                int fieldType = m.getFeatureStore().getDefaultFeatureType()
408
                                        .getAttributeDescriptor(cmbFields.getSelectedIndex())
409
                                        .getDataType();
410
//                int fieldType = m.getSource().getRecordset().getFieldType((int)cmbFields.getSelectedIndex());
411
                        int[] classifyingFieldTypes = new int[] {fieldType};
412
                        if(auxLegend!=null){
413
                                if(!compareClassifyingFieldTypes(classifyingFieldTypes,auxLegend.getClassifyingFieldTypes())){
414
                                        auxLegend.setClassifyingFieldTypes(classifyingFieldTypes);
415
                                        changedLegend = true;
416
                                }
417
                        } else {
418
                                auxLegend.setClassifyingFieldTypes(classifyingFieldTypes);
419
                                changedLegend = true;
420
                        }
421
        } catch (DataException e) {
422
                NotificationManager.addError(PluginServices.getText(this, "could_not_setup_legend"), e);
423
        }
424

    
425
                if(changedLegend){
426
                        auxLegend.clear();
427
                }
428

    
429
        for (int row = 0; row < symbolTable.getRowCount(); row++) {
430
            clave = symbolTable.getFieldValue(row, 1);
431
            theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
432
                        String description = (String) symbolTable.getFieldValue(row, 2);
433
                        theSymbol.setDescription(description);
434
                        ISymbol legendSymbol = null;
435
                        if (auxLegend != null){
436
                                legendSymbol = auxLegend.getSymbolByValue(clave);
437
                        }
438
                        if( legendSymbol == null || ( auxLegend.isUseDefaultSymbol() && legendSymbol == auxLegend.getDefaultSymbol())){
439
                                if (auxLegend != null){
440
                                        auxLegend.addSymbol(clave, theSymbol);
441
                                }
442
                        } else {
443
                                /* FIXME: Se optimizar?a descomentarizando el if, pero el metodo equals del AbstractSymbol
444
                                 * no tiene en cuenta determinadas propiedades del simbolo, como, por ejemplo, el tama?o.
445
                                 * Descomentarizar al arreglar el metodo equals del AbstractSymbol.
446
                                 */
447
//                                if(!legendSymbol.equals(theSymbol)){
448
                                        auxLegend.replace(legendSymbol, theSymbol);
449
//                                }
450
                        }
451
                        visitedKeys.add(clave);
452
                }
453
                if(auxLegend != null){
454
                        Object[] keys = auxLegend.getValues();
455
                        for(int i=0; i<keys.length; i++){
456
                                Object key = keys[i];
457
                                if(!visitedKeys.contains(key)){
458
                                        auxLegend.delSymbol(key);
459
                                }
460
                        }
461
                }
462
                clave = null;
463
                if(chbUseDefault.isSelected()){
464
                        theSymbol = defaultSymbolPrev.getSymbol();
465
                        if(theSymbol != null){
466
                                String description = PluginServices.getText(this,"default");
467
                                theSymbol.setDescription(description);
468
                                ISymbol legendSymbol = null;
469
                                if (auxLegend != null){
470
                                        legendSymbol = auxLegend.getSymbolByValue(clave);
471
                                }
472
                                if( legendSymbol == null){
473
                                        auxLegend.addSymbol(clave, theSymbol);
474
                                } else {
475
//                                        if(!legendSymbol.equals(theSymbol)){
476
                                        if(legendSymbol!=theSymbol){
477
                                                auxLegend.replace(legendSymbol, theSymbol);
478
                                        }
479
                                }
480
                        }
481
                } else {
482
                        if (auxLegend != null){
483
                                ISymbol legendSymbol = auxLegend.getSymbolByValue(clave);
484
                                if( legendSymbol != null){
485
                                        auxLegend.replace(legendSymbol, null);
486
                                }
487
                        }
488
                }
489
        }
490

    
491
    /**
492
     * DOCUMENT ME!
493
     */
494
    private void fillFieldNames() {
495
        FeatureStore rs;
496

    
497
        try {
498
            rs = ((FLyrVect) layer).getFeatureStore();
499
//            logger.debug("rs.start()");
500
//            rs.start();
501

    
502
            ArrayList names=new ArrayList();
503
            Iterator iterator=rs.getDefaultFeatureType().iterator();
504
            while (iterator.hasNext()) {
505
                                FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator.next();
506
                                names.add(descriptor.getName());
507
                        }
508
//            String[] nomFields = new String[rs.getFieldCount()];
509
//
510
//            for (int i = 0; i < rs.getFieldCount(); i++) {
511
//                nomFields[i] = rs.getFieldName(i).trim();
512
//            }
513
//
514
//            rs.stop();
515

    
516
            DefaultComboBoxModel cM = new DefaultComboBoxModel(names.toArray(new String[0]));
517
            cmbFields.setModel(cM);
518

    
519
            // fieldsListValor.setSelectedIndex(0);
520
        } catch (DataException e) {
521
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
522
        }
523
    }
524

    
525
    public void setData(FLayer layer, ILegend legend) {
526
            this.layer = (ClassifiableVectorial) layer;
527
              int shapeType = 0;
528
              try {
529
                      shapeType = this.layer.getShapeType();
530
              } catch (ReadException e) {
531
                    NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
532
                }
533

    
534
              getDefaultSymbolPrev(shapeType);
535

    
536
              if (symbolTable != null) {
537
                        pnlCenter.remove(symbolTable);
538
                }
539
              symbolTable = new SymbolTable(this, SymbolTable.VALUES_TYPE, shapeType);
540
              pnlCenter.add(symbolTable, BorderLayout.CENTER);
541

    
542
        fillFieldNames();
543

    
544
        symbolTable.removeAllItems();
545

    
546
            if (VectorialUniqueValueLegend.class.equals(legend.getClass())) {
547
                        try {
548
                                auxLegend = (VectorialUniqueValueLegend) legend.cloneLegend();
549
                        } catch (XMLException e) {
550
                                // TODO Auto-generated catch block
551
                                e.printStackTrace();
552
                        }
553
                        getChbUseDefault().setSelected(auxLegend.isUseDefaultSymbol());
554
                        cmbFields.getModel().setSelectedItem(auxLegend.getClassifyingFieldNames()[0]);
555
                        setColorScheme();
556
                        symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
557
                                        auxLegend.getValues(),auxLegend.getDescriptions());
558
                        chbUseDefault.setSelected(auxLegend.isUseDefaultSymbol());
559
                } else {
560
                        auxLegend = new VectorialUniqueValueLegend(shapeType);
561
                }
562
                defaultSymbolPrev.setSymbol(auxLegend.getDefaultSymbol());
563
                btnOpenSymbolLevelsEditor.setEnabled(symbolTable != null && symbolTable.getRowCount()>0);
564
    }
565

    
566

    
567
    private void setColorScheme(){
568

    
569
                if(auxLegend.getColorScheme() != null) {
570
                        ColorItem[] colors = new ColorItem[auxLegend.getColorScheme().length];
571
                        for (int i = 0; i < auxLegend.getColorScheme().length; i++) {
572
                                colors[i] = new ColorItem();
573
                                colors[i].setColor(auxLegend.getColorScheme()[i]);
574
                        }
575
                        cmbColorScheme.setSelectedColors(colors);
576
                }
577
        }
578

    
579
    private void getDefaultSymbolPrev(int shapeType) {
580
                if(defaultSymbolPrev == null){
581
                        defaultSymbolPrev = new JSymbolPreviewButton(shapeType);
582
                        defaultSymbolPrev.setPreferredSize(new Dimension(110,20));
583
                        defaultSymbolPrev.addActionListener(this);
584
                        defaultSymbolPanel.add(defaultSymbolPrev,null);
585
                }
586
        }
587

    
588
    /* (non-Javadoc)
589
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
590
     */
591
    public ILegend getLegend() {
592
            fillSymbolListFromTable();
593

    
594
            if (auxLegend != null) {
595
                    // your settings that are not the set of symbols must be located here
596
//                  auxLegend.setClassifyingFieldNames(
597
//                  new String[] {(String) cmbFields.getSelectedItem()});
598

    
599
                    ISymbol defaultSymbolLegend = auxLegend.getDefaultSymbol();
600
                    ISymbol symbol = defaultSymbolPrev.getSymbol();
601
                    if(symbol != null){
602
                            if(symbol!=defaultSymbolLegend){
603
                                    auxLegend.setDefaultSymbol(symbol);
604
                            }
605
                    }
606

    
607
                    auxLegend.useDefaultSymbol(chbUseDefault.isSelected());
608

    
609
                    try {
610
                            theLegend = (VectorialUniqueValueLegend) auxLegend.cloneLegend();
611
                    } catch (XMLException e) {
612
                            // TODO Auto-generated catch block
613
                            e.printStackTrace();
614
                    }
615
                    theLegend.setZSort(auxLegend.getZSort());
616
            }
617

    
618
            return theLegend;
619
    }
620

    
621
    private JCheckBox getChbUseDefault() {
622
            if (chbUseDefault == null) {
623
                        chbUseDefault = new JCheckBox();
624
                        chbUseDefault.setSelected(false);
625
                        chbUseDefault.addActionListener(new java.awt.event.ActionListener() {
626
                                public void actionPerformed(java.awt.event.ActionEvent e) {
627
                                        if (chbUseDefault.isSelected()) {
628
                                                auxLegend.useDefaultSymbol(true);
629
                                        } else {
630
                                                auxLegend.useDefaultSymbol(false);
631
                                        }
632
                                }
633
                        });
634
                        chbUseDefault.setText(PluginServices.getText(this, "resto_valores")+ ": ");
635
                }
636

    
637
                return chbUseDefault;
638
    }
639

    
640
    /**
641
     * A?ade el resto de valores.
642
     */
643
    private void addDefault() {
644
        auxLegend.getDefaultSymbol().setDescription("Default");
645
        auxLegend.addSymbol(null, auxLegend.getDefaultSymbol());
646
        symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
647
            null, auxLegend.getDefaultSymbol().getDescription());
648
        symbolTable.repaint();
649
    }
650

    
651
    /**
652
     * Elimina el resto de valores que no estan representados por ning?n otro s?mbolo..
653
     */
654
    private void delDefault() {
655
        auxLegend.delSymbol(null);
656
        symbolTable.removeRow(null);
657
        symbolTable.repaint();
658
    }
659

    
660

    
661
    public void actionPerformed(ActionEvent e) {
662

    
663
            //modificar el combobox de valor
664
            if (e.getActionCommand() == "FIELD_SELECTED") {
665
                    JComboBox cb = (JComboBox) e.getSource();
666
                    String fieldName = (String) cb.getSelectedItem();
667
                    symbolTable.removeAllItems();
668
                        btnOpenSymbolLevelsEditor.setEnabled(false);
669

    
670

    
671
//                    if (theLegend.getClassifyingFieldNames()!=null && fieldName != theLegend.getClassifyingFieldNames()[0]) {
672
//                            //m_bCacheDirty = true;
673
//                            theLegend.setClassifyingFieldNames(new String[] {fieldName});
674
//                    }
675
//
676
//                    //////////////////////////////////////////PEPE
677
//                    FLyrVect m = (FLyrVect) layer;
678
//                    try {
679
//                            int fieldType = m.getSource().getRecordset().getFieldType((int)cb.getSelectedIndex());
680
//                            if (theLegend.getClassifyingFieldTypes()!=null && fieldType != theLegend.getClassifyingFieldTypes()[0]) {
681
//                                    //m_bCacheDirty = true;
682
//                                    theLegend.setClassifyingFieldTypes(new int[] {fieldType});
683
//                            }
684
//                    } catch (ReadDriverException e1) {
685
//                            NotificationManager.addError(PluginServices.getText(this, "could_not_setup_legend"), e1);
686
//                    }
687
//                    /////////////////////////////////////////PEPE
688
            }
689

    
690
            // add all elements by value
691
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
692
                    fillTableValues();
693
                        btnOpenSymbolLevelsEditor.setEnabled(symbolTable != null && symbolTable.getRowCount()>0);
694
            }
695

    
696
            // add only one value
697
            if (e.getActionCommand() == "ADD_VALUE") {
698
                    try {
699
                                ISymbol symbol = mapContextManager.createSymbol(layer.getShapeType());
700
                                Double clave = new Double(0.0);
701
                                symbolTable.addTableRecord(symbol,
702
                                                clave,"0 - 0");
703
                                btnOpenSymbolLevelsEditor.setEnabled(true);
704
                    } catch (ReadException ex) {
705
                            NotificationManager.addError(PluginServices.getText(this, "getting_shape_type"), ex);
706
                    }
707
            }
708

    
709
            //Vacia la tabla
710
            if (e.getActionCommand() == "REMOVE_ALL") {
711
                    symbolTable.removeAllItems();
712
                    auxLegend.setZSort(null);
713
                        btnOpenSymbolLevelsEditor.setEnabled(false);
714

    
715
            }
716

    
717
            //Quitar solo el elemento seleccionado
718
            if (e.getActionCommand() == "REMOVE") {
719
                    symbolTable.removeSelectedRows();
720
                        btnOpenSymbolLevelsEditor.setEnabled(symbolTable.getRowCount()>0);
721
            }
722

    
723
            if (e.getActionCommand() == "OPEN_SYMBOL_LEVEL_EDITOR") {
724
                        ZSort myZSort = null;
725
                        if (auxLegend != null) {
726
                                myZSort = ((AbstractClassifiedVectorLegend) getLegend()).getZSort();
727
                                if(myZSort == null){
728
                                        myZSort = new ZSort(auxLegend);
729
                                }
730
                        }
731
                        if (myZSort == null && theLegend != null) {
732
                                myZSort = new ZSort(theLegend);
733
                        }
734
                        SymbolLevelsWindow sl = new SymbolLevelsWindow(myZSort);
735
                        PluginServices.getMDIManager().addWindow(sl);
736
                        auxLegend.setZSort(sl.getZSort());
737
                }
738
    }
739

    
740
        public String getDescription() {
741
                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") + ".";
742
        }
743

    
744
        public ImageIcon getIcon() {
745
                return new ImageIcon(this.getClass().getClassLoader().
746
                                getResource("images/ValoresUnicos.png"));
747
        }
748

    
749
        public Class getParentClass() {
750
                return Categories.class;
751
        }
752

    
753
        public String getTitle() {
754
                return PluginServices.getText(this,"Valores_unicos");
755
        }
756

    
757
        public JPanel getPanel() {
758
                return this;
759
        }
760

    
761
        public Class getLegendClass() {
762
                return VectorialUniqueValueLegend.class;
763
        }
764

    
765

    
766
        public boolean isSuitableFor(FLayer layer) {
767
                return (layer instanceof FLyrVect);
768
        }
769
}