Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / legend / gui / VectorialUniqueValue.java @ 33205

History | View | Annotate | Download (24.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 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.tools.dispose.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.legend.IVectorialUniqueValueLegend;
80
import org.gvsig.fmap.mapcontext.rendering.legend.ZSort;
81
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
82
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
83
import org.gvsig.gui.beans.swing.JBlank;
84
import org.gvsig.gui.beans.swing.JButton;
85
import org.gvsig.raster.datastruct.ColorItem;
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.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
90

    
91

    
92

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
193

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

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

    
203

    
204
    }
205

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

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

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

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

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

    
235
            symbolTable.removeAllItems();
236

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

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

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

    
248
            Object clave=null;
249

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

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

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

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

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

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

    
272
                                int count = 0;
273

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

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

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

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

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

    
308

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

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

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

    
342
        btnRemoveAll.setEnabled(true);
343
        btnRemove.setEnabled(true);
344

    
345
        //m_bCacheDirty = false;
346
    }
347

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

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

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

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

    
406
                FLyrVect m = (FLyrVect) layer;
407

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

    
429
                if(changedLegend){
430
                        auxLegend.clear();
431
                }
432

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

    
495
    /**
496
     * DOCUMENT ME!
497
     */
498
    private void fillFieldNames() {
499
        FeatureStore rs;
500

    
501
        try {
502
            rs = ((FLyrVect) layer).getFeatureStore();
503
//            logger.debug("rs.start()");
504
//            rs.start();
505

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

    
520
            DefaultComboBoxModel cM = new DefaultComboBoxModel(names.toArray(new String[0]));
521
            cmbFields.setModel(cM);
522

    
523
            // fieldsListValor.setSelectedIndex(0);
524
        } catch (DataException e) {
525
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
526
        }
527
    }
528

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

    
538
              getDefaultSymbolPrev(shapeType);
539

    
540
              if (symbolTable != null) {
541
                        pnlCenter.remove(symbolTable);
542
                }
543
              symbolTable = new SymbolTable(this, SymbolTable.VALUES_TYPE, shapeType);
544
              pnlCenter.add(symbolTable, BorderLayout.CENTER);
545

    
546
        fillFieldNames();
547

    
548
        symbolTable.removeAllItems();
549

    
550
            if (VectorialUniqueValueLegend.class.equals(legend.getClass())) {
551
                        auxLegend = (VectorialUniqueValueLegend) legend.cloneLegend();
552
                        getChbUseDefault().setSelected(auxLegend.isUseDefaultSymbol());
553
                        cmbFields.getModel().setSelectedItem(auxLegend.getClassifyingFieldNames()[0]);
554
                        setColorScheme();
555
                        symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
556
                                        auxLegend.getValues(),auxLegend.getDescriptions());
557
                        chbUseDefault.setSelected(auxLegend.isUseDefaultSymbol());
558
                } else {
559
                        auxLegend = new VectorialUniqueValueLegend(shapeType);
560
                }
561
                defaultSymbolPrev.setSymbol(auxLegend.getDefaultSymbol());
562
                btnOpenSymbolLevelsEditor.setEnabled(symbolTable != null && symbolTable.getRowCount()>0);
563
    }
564

    
565

    
566
    private void setColorScheme(){
567

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

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

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

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

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

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

    
608
                    theLegend = (VectorialUniqueValueLegend) auxLegend.cloneLegend();
609
                    theLegend.setZSort(auxLegend.getZSort());
610
            }
611

    
612
            return theLegend;
613
    }
614

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

    
631
                return chbUseDefault;
632
    }
633

    
634
    /**
635
     * A?ade el resto de valores.
636
     */
637
    private void addDefault() {
638
        auxLegend.getDefaultSymbol().setDescription("Default");
639
        auxLegend.addSymbol(null, auxLegend.getDefaultSymbol());
640
        symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
641
            null, auxLegend.getDefaultSymbol().getDescription());
642
        symbolTable.repaint();
643
    }
644

    
645
    /**
646
     * Elimina el resto de valores que no estan representados por ning?n otro s?mbolo..
647
     */
648
    private void delDefault() {
649
        auxLegend.delSymbol(null);
650
        symbolTable.removeRow(null);
651
        symbolTable.repaint();
652
    }
653

    
654

    
655
    public void actionPerformed(ActionEvent e) {
656

    
657
            //modificar el combobox de valor
658
            if (e.getActionCommand() == "FIELD_SELECTED") {
659
                    JComboBox cb = (JComboBox) e.getSource();
660
                    String fieldName = (String) cb.getSelectedItem();
661
                    symbolTable.removeAllItems();
662
                        btnOpenSymbolLevelsEditor.setEnabled(false);
663

    
664

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

    
684
            // add all elements by value
685
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
686
                    fillTableValues();
687
                        btnOpenSymbolLevelsEditor.setEnabled(symbolTable != null && symbolTable.getRowCount()>0);
688
            }
689

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

    
705
            //Vacia la tabla
706
            if (e.getActionCommand() == "REMOVE_ALL") {
707
                    symbolTable.removeAllItems();
708
                    auxLegend.setZSort(null);
709
                        btnOpenSymbolLevelsEditor.setEnabled(false);
710

    
711
            }
712

    
713
            //Quitar solo el elemento seleccionado
714
            if (e.getActionCommand() == "REMOVE") {
715
                    symbolTable.removeSelectedRows();
716
                        btnOpenSymbolLevelsEditor.setEnabled(symbolTable.getRowCount()>0);
717
            }
718

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

    
736
        public String getDescription() {
737
                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") + ".";
738
        }
739

    
740
        public ImageIcon getIcon() {
741
                return new ImageIcon(this.getClass().getClassLoader().
742
                                getResource("images/ValoresUnicos.png"));
743
        }
744

    
745
        public Class getParentClass() {
746
                return Categories.class;
747
        }
748

    
749
        public String getTitle() {
750
                return PluginServices.getText(this,"Valores_unicos");
751
        }
752

    
753
        public JPanel getPanel() {
754
                return this;
755
        }
756

    
757
        public Class getLegendClass() {
758
                return VectorialUniqueValueLegend.class;
759
        }
760

    
761

    
762
        public boolean isSuitableFor(FLayer layer) {
763
                return (layer instanceof FLyrVect);
764
        }
765
}