Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / legend / gui / VectorialUniqueValue.java @ 40558

History | View | Annotate | Download (24.2 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.project.documents.view.legend.gui;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Color;
28
import java.awt.Component;
29
import java.awt.Dimension;
30
import java.awt.GridLayout;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.util.ArrayList;
34
import java.util.Iterator;
35
import java.util.Random;
36

    
37
import javax.swing.DefaultComboBoxModel;
38
import javax.swing.ImageIcon;
39
import javax.swing.JCheckBox;
40
import javax.swing.JComboBox;
41
import javax.swing.JLabel;
42
import javax.swing.JOptionPane;
43
import javax.swing.JPanel;
44

    
45
import org.gvsig.andami.IconThemeHelper;
46
import org.gvsig.andami.PluginServices;
47
import org.gvsig.andami.messages.NotificationManager;
48
import org.gvsig.app.gui.styling.JComboBoxColorScheme;
49
import org.gvsig.app.gui.styling.SymbolLevelsWindow;
50
import org.gvsig.fmap.dal.exception.DataException;
51
import org.gvsig.fmap.dal.exception.ReadException;
52
import org.gvsig.fmap.dal.feature.Feature;
53
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
54
import org.gvsig.fmap.dal.feature.FeatureSet;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.mapcontext.MapContextLocator;
57
import org.gvsig.fmap.mapcontext.MapContextManager;
58
import org.gvsig.fmap.mapcontext.layers.FLayer;
59
import org.gvsig.fmap.mapcontext.layers.operations.ClassifiableVectorial;
60
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
61
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
62
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
63
import org.gvsig.fmap.mapcontext.rendering.legend.ZSort;
64
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
65
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
66
import org.gvsig.gui.beans.swing.JBlank;
67
import org.gvsig.gui.beans.swing.JButton;
68
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.AbstractClassifiedVectorLegend;
69
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialUniqueValueLegend;
70
import org.gvsig.tools.dispose.DisposableIterator;
71
import org.slf4j.Logger;
72
import org.slf4j.LoggerFactory;
73

    
74

    
75

    
76
/**
77
 * DOCUMENT ME!
78
 *
79
 * @author fjp To change the template for this generated type comment go to
80
 *         Window>Preferences>Java>Code Generation>Code and
81
 *         Comments
82
 */
83
public class VectorialUniqueValue extends JPanel implements ILegendPanel, ActionListener{
84
    private static final Logger logger = LoggerFactory
85
    .getLogger(VectorialUniqueValue.class);
86

    
87
    protected VectorialUniqueValueLegend theLegend;
88
    private ClassifiableVectorial layer;
89
    private SymbolTable symbolTable;
90
    protected JComboBox cmbFields;
91
    private JButton btnRemoveAll;
92
    private JButton btnRemove;
93
    private JCheckBox chbUseDefault = null;
94
    private JSymbolPreviewButton defaultSymbolPrev;
95
    private VectorialUniqueValueLegend auxLegend;
96
        private JPanel pnlCenter;
97
        private JButton btnOpenSymbolLevelsEditor;
98

    
99
        private JComboBoxColorScheme cmbColorScheme;
100
        private GridBagLayoutPanel defaultSymbolPanel = new GridBagLayoutPanel();
101
                
102
        private MapContextManager mapContextManager = MapContextLocator
103
                        .getMapContextManager();
104

    
105
    /**
106
     *
107
     */
108
    public VectorialUniqueValue() {
109
        super();
110
        initComponents();
111
    }
112

    
113
    /**
114
     * DOCUMENT ME!
115
     */
116
    protected void initComponents() {
117
        JPanel pnlButtons = new JPanel();
118

    
119
        JButton btnAddAll = new JButton(PluginServices.getText(this,
120
                    "Anadir_todos"));
121
        btnAddAll.setActionCommand("ADD_ALL_VALUES");
122
        btnAddAll.addActionListener(this);
123
        pnlButtons.add(btnAddAll);
124

    
125
        JButton btnAdd = new JButton(PluginServices.getText(this, "Anadir"));
126
        btnAdd.setActionCommand("ADD_VALUE");
127
        btnAdd.addActionListener(this);
128
        pnlButtons.add(btnAdd);
129

    
130
        btnRemoveAll = new JButton(PluginServices.getText(this, "Quitar_todos"));
131
        btnRemoveAll.setActionCommand("REMOVE_ALL");
132
        btnRemoveAll.addActionListener(this);
133
        pnlButtons.add(btnRemoveAll);
134

    
135
        btnRemove = new JButton(PluginServices.getText(this, "Quitar"));
136
        btnRemove.setActionCommand("REMOVE");
137
        btnRemove.addActionListener(this);
138
        pnlButtons.add(btnRemove);
139

    
140
        btnOpenSymbolLevelsEditor = new JButton(PluginServices.getText(this, "symbol_levels"));
141
        btnOpenSymbolLevelsEditor.addActionListener(this);
142
        btnOpenSymbolLevelsEditor.setActionCommand("OPEN_SYMBOL_LEVEL_EDITOR");
143
        pnlButtons.add(btnOpenSymbolLevelsEditor);
144
                btnOpenSymbolLevelsEditor.setEnabled(symbolTable != null && symbolTable.getRowCount()>0);
145

    
146
                pnlCenter = new JPanel();
147
        pnlCenter.setLayout(new BorderLayout());
148

    
149
        cmbFields = new JComboBox();
150
        cmbFields.setActionCommand("FIELD_SELECTED");
151
        cmbFields.addActionListener(this);
152
        cmbFields.setVisible(true);
153

    
154
                JPanel pnlNorth = new JPanel();
155
                pnlNorth.setLayout(new GridLayout(0,2));
156

    
157
                GridBagLayoutPanel auxPanel = new GridBagLayoutPanel();
158
                JLabel lblFieldClassification = new JLabel(PluginServices.getText(
159
                                this, "Campo_de_clasificacion")+": ");
160
                auxPanel.add(lblFieldClassification);
161
                auxPanel.add(cmbFields);
162
                pnlNorth.add(auxPanel);
163

    
164
                auxPanel = new GridBagLayoutPanel();
165
                auxPanel.add(new JLabel(PluginServices.getText(this, "color_scheme")+": "));
166
                cmbColorScheme = new JComboBoxColorScheme(false);
167
                cmbColorScheme.addActionListener(this);
168
                auxPanel.add(cmbColorScheme);
169
                pnlNorth.add(auxPanel);
170

    
171

    
172
                defaultSymbolPanel.add(getChbUseDefault(), null);
173
                pnlNorth.add(defaultSymbolPanel);
174
                pnlNorth.add(new JBlank(0,30));
175

    
176
                this.setLayout(new BorderLayout());
177
                this.add(pnlNorth, BorderLayout.NORTH);
178
                this.add(pnlCenter, BorderLayout.CENTER);
179
                this.add(pnlButtons, BorderLayout.SOUTH);
180

    
181

    
182
    }
183

    
184
    /**
185
     * DOCUMENT ME!
186
     */
187
    private void fillTableValues() {
188
        FeatureStore elRs;
189

    
190
        try {
191
            elRs = ((FLyrVect) layer).getFeatureStore();
192
//            logger.debug("elRs.start()");
193
//            elRs.start();
194

    
195
//            int idField = -1;
196
            String fieldName = (String) cmbFields.getSelectedItem();
197
            if (fieldName==null) {
198
                    JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_hay_campo_seleccionado"));
199
                    return;
200
            }
201

    
202
//            idField = elRs.getFieldIndexByName(fieldName);
203
            auxLegend.setClassifyingFieldNames(new String[] {fieldName});
204

    
205
            //long numReg = elRs.getRowCount();
206
//            if (idField == -1) {
207
//                NotificationManager.addWarning(
208
//                                PluginServices.getText(this, "unrecognized_field_name")+" " + fieldName, null);
209
//
210
//                return;
211
//            }
212

    
213
            symbolTable.removeAllItems();
214

    
215
            int numSymbols = 0;
216
            ISymbol theSymbol = null;
217

    
218
            //auxLegend=(VectorialUniqueValueLegend)m_lyr.getLegend();
219

    
220
            //            auxLegend = LegendFactory.createVectorialUniqueValueLegend(layer.getShapeType());
221
            auxLegend = (VectorialUniqueValueLegend) MapContextLocator
222
                                        .getMapContextManager().createLegend(
223
                                                        IVectorialUniqueValueLegend.LEGEND_NAME);
224
            auxLegend.setShapeType(layer.getShapeType());
225

    
226
            Object clave=null;
227

    
228
            //Object resul;
229
            if (chbUseDefault.isSelected()) {
230
                auxLegend.getDefaultSymbol().setDescription("Default");
231
                auxLegend.addSymbol(null, auxLegend.getDefaultSymbol());
232
            }
233

    
234
            Color[] colorScheme = cmbColorScheme.getSelectedColors();
235

    
236
            Color[] colors = new Color[colorScheme.length];
237
                        for (int i = 0; i < colorScheme.length; i++) {
238
                                colors[i] = colorScheme[i];
239
                        }
240
                        auxLegend.setColorScheme(colors);
241

    
242
            Random rand = new Random(System.currentTimeMillis());
243

    
244
            FeatureSet set = null;
245
                        DisposableIterator iterator = null;
246

    
247
                        try {
248
                                set = elRs.getFeatureSet();
249

    
250
                                int count = 0;
251

    
252
                                iterator = set.iterator();
253
                                while (iterator.hasNext()) {
254
                                        Feature feature = (Feature) iterator.next();
255
                                        clave = feature.get(fieldName);
256
                                        // }
257
                                        // for (int j = 0; j < elRs.getRowCount(); j++) {
258
                                        // clave = elRs.getFieldValue(j, idField);
259

    
260
//                                        if (clave instanceof NullValue) {
261
//                                                continue;
262
//                                        }
263
                                        if (clave == null) {
264
                                                continue;
265
                                        }
266

    
267
                                        // //Comprobar que no esta repetido y no hace falta
268
                                        // introducir en el hashtable el campo junto con el simbolo.
269
                                        if (auxLegend.getSymbolByValue(clave) == null) {
270

    
271
                                                if (count == 100) {
272
                                                        int resp = JOptionPane.showConfirmDialog(this,
273
                                                                        PluginServices.getText(this,
274
                                                                                        "mas_de_100_simbolos"),
275
                                                                        PluginServices.getText(this,
276
                                                                                        "quiere_continuar"),
277
                                                                        JOptionPane.YES_NO_OPTION,
278
                                                                        JOptionPane.WARNING_MESSAGE);
279

    
280
                                                        if ((resp == JOptionPane.NO_OPTION)
281
                                                                        || (resp == JOptionPane.DEFAULT_OPTION)) {
282
                                                                break;
283
                                                        }
284
                                                }
285

    
286

    
287
                                                // si no esta creado el simbolo se crea
288
                                                // jaume (moved to ISymbol); theSymbol = new
289
                                                // FSymbol(layer.getShapeType());
290
                                                theSymbol =
291
                                                                mapContextManager.getSymbolManager()
292
                                                                                .createSymbol(
293
                                                                                                layer
294
                                                                                .getShapeType(), colorScheme[rand
295
                                                                                .nextInt(colorScheme.length)]);
296
                                                theSymbol.setDescription(clave.toString());
297
                                                auxLegend.addSymbol(clave, theSymbol);
298
                                                count++;
299
                                        }
300

    
301
                                } // for
302
                        }finally{
303
                                if (iterator != null){
304
                                        iterator.dispose();
305
                                }
306
                                if (set != null) {
307
                                        set.dispose();
308
                                }
309
                        }
310

    
311
                        Object[] values = auxLegend.getValues();
312
                        String[] descriptions = new String[values.length];
313
                        ISymbol[] symbols = new ISymbol[values.length];
314
                        
315
                        for (int i = 0; i < values.length; i++) {
316
                                Object value = values[i];
317
                                symbols[i] = auxLegend.getSymbolByValue(value);
318
                                descriptions[i] = symbols[i].getDescription();
319
                        }
320
                        
321
            symbolTable.fillTableFromSymbolList(symbols,
322
                values, descriptions);
323
//            elRs.stop();
324
//            set.dispose();
325
        } catch (DataException e) {
326
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
327
        }
328

    
329
        btnRemoveAll.setEnabled(true);
330
        btnRemove.setEnabled(true);
331

    
332
        //m_bCacheDirty = false;
333
    }
334

    
335
        private boolean compareClassifyingFieldNames(String[] a, String[] b){
336
                if (a==b) {
337
                        return true;
338
                }
339
                if (a == null || b == null) {
340
                        return false;
341
                }
342
                if (a.length != b.length) {
343
                        return false;
344
                }
345
                for (int i=0; i<a.length; i++){
346
                        if (!a[i].equals(b[i])) {
347
                                return false;
348
                        }
349
                }
350
                return true;
351
        }
352

    
353
        private boolean compareClassifyingFieldTypes(int[] a, int[] b){
354
                if (a==b) {
355
                        return true;
356
                }
357
                if (a == null || b == null) {
358
                        return false;
359
                }
360
                if (a.length != b.length) {
361
                        return false;
362
                }
363
                for (int i=0; i<a.length; i++){
364
                        if (a[i]!=b[i]) {
365
                                return false;
366
                        }
367
                }
368
                return true;
369
        }
370

    
371
    /**
372
     * A partir de los registros de la tabla, regenera el FRenderer. (No solo
373
     * el symbolList, si no tambi�n el arrayKeys y el defaultRenderer
374
     */
375
    private void fillSymbolListFromTable() {
376
        Object clave=null;
377
        ISymbol theSymbol;
378
                ArrayList visitedKeys = new ArrayList();
379
                boolean changedLegend = false;
380

    
381
            String fieldName = (String) cmbFields.getSelectedItem();
382
                String[] classifyingFieldNames = new String[] {fieldName};
383
                if(auxLegend!=null){
384
                        if(!compareClassifyingFieldNames(classifyingFieldNames,auxLegend.getClassifyingFieldNames())){
385
                                auxLegend.setClassifyingFieldNames(classifyingFieldNames);
386
                                changedLegend = true;
387
                        }
388
                } else {
389
                        auxLegend.setClassifyingFieldNames(classifyingFieldNames);
390
                        changedLegend = true;
391
                }
392

    
393
                FLyrVect m = (FLyrVect) layer;
394

    
395
        try {
396
                int fieldType = m.getFeatureStore().getDefaultFeatureType()
397
                                        .getAttributeDescriptor(cmbFields.getSelectedIndex())
398
                                        .getType();
399
//                int fieldType = m.getSource().getRecordset().getFieldType((int)cmbFields.getSelectedIndex());
400
                        int[] classifyingFieldTypes = new int[] {fieldType};
401
                        if(auxLegend!=null){
402
                                if(!compareClassifyingFieldTypes(classifyingFieldTypes,auxLegend.getClassifyingFieldTypes())){
403
                                        auxLegend.setClassifyingFieldTypes(classifyingFieldTypes);
404
                                        changedLegend = true;
405
                                }
406
                        } else {
407
                                auxLegend.setClassifyingFieldTypes(classifyingFieldTypes);
408
                                changedLegend = true;
409
                        }
410
        } catch (DataException e) {
411
                NotificationManager.addError(PluginServices.getText(this, "could_not_setup_legend"), e);
412
        } catch (Exception e) {
413
                        NotificationManager.showMessageWarning(PluginServices.getText(this, "could_not_setup_legend"), e);
414
                }
415

    
416
                if(changedLegend){
417
                        auxLegend.clear();
418
                }
419

    
420
        for (int row = 0; row < symbolTable.getRowCount(); row++) {
421
            clave = symbolTable.getFieldValue(row, 1);
422
            theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
423
                        String description = (String) symbolTable.getFieldValue(row, 2);
424
                        theSymbol.setDescription(description);
425
                        ISymbol legendSymbol = null;
426
                        if (auxLegend != null){
427
                                legendSymbol = auxLegend.getSymbolByValue(clave);
428
                        }
429
                        if( legendSymbol == null || ( auxLegend.isUseDefaultSymbol() && legendSymbol == auxLegend.getDefaultSymbol())){
430
                                if (auxLegend != null){
431
                                        auxLegend.addSymbol(clave, theSymbol);
432
                                }
433
                        } else {
434
                            
435
                            // avoid same instance
436
                            if (legendSymbol != theSymbol) {
437

    
438
                                /*
439
                                 * Perhaps use this "if" when implementations of ISymbol
440
                                 * properly implement the equals method. 
441
                                 */
442
                                // if (!legendSymbol.equals(theSymbol)) {
443
                                    auxLegend.replace(legendSymbol, theSymbol);
444
                                // }
445
                            }
446
                        }
447
                        visitedKeys.add(clave);
448
                }
449
                if(auxLegend != null){
450
                        Object[] keys = auxLegend.getValues();
451
                        for(int i=0; i<keys.length; i++){
452
                                Object key = keys[i];
453
                                if(!visitedKeys.contains(key)){
454
                                        auxLegend.delSymbol(key);
455
                                }
456
                        }
457
                }
458
                clave = null;
459
                if(chbUseDefault.isSelected()){
460
                        theSymbol = defaultSymbolPrev.getSymbol();
461
                        if(theSymbol != null){
462
                                String description = PluginServices.getText(this,"default");
463
                                theSymbol.setDescription(description);
464
                                ISymbol legendSymbol = null;
465
                                if (auxLegend != null){
466
                                        legendSymbol = auxLegend.getSymbolByValue(clave);
467
                                }
468
                                if( legendSymbol == null){
469
                                        auxLegend.addSymbol(clave, theSymbol);
470
                                } else {
471
//                                        if(!legendSymbol.equals(theSymbol)){
472
                                        if(legendSymbol!=theSymbol){
473
                                                auxLegend.replace(legendSymbol, theSymbol);
474
                                        }
475
                                }
476
                        }
477
                } else {
478
                        if (auxLegend != null){
479
                                ISymbol legendSymbol = auxLegend.getSymbolByValue(clave);
480
                                if( legendSymbol != null){
481
                                        auxLegend.replace(legendSymbol, null);
482
                                }
483
                        }
484
                }
485
        }
486

    
487
    /**
488
     * DOCUMENT ME!
489
     */
490
    private void fillFieldNames() {
491
        FeatureStore rs;
492

    
493
        try {
494
            rs = ((FLyrVect) layer).getFeatureStore();
495
//            logger.debug("rs.start()");
496
//            rs.start();
497

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

    
512
            DefaultComboBoxModel cM = new DefaultComboBoxModel(names.toArray(new String[0]));
513
            cmbFields.setModel(cM);
514

    
515
            // fieldsListValor.setSelectedIndex(0);
516
        } catch (DataException e) {
517
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
518
        }
519
    }
520

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

    
530
              getDefaultSymbolPrev(shapeType);
531

    
532
              if (symbolTable != null) {
533
                        pnlCenter.remove(symbolTable);
534
                }
535
              symbolTable = new SymbolTable(this, SymbolTable.VALUES_TYPE, shapeType);
536
              pnlCenter.add(symbolTable, BorderLayout.CENTER);
537

    
538
        fillFieldNames();
539

    
540
        symbolTable.removeAllItems();
541

    
542
            if (VectorialUniqueValueLegend.class.equals(legend.getClass())) {
543
                        auxLegend = (VectorialUniqueValueLegend) legend.cloneLegend();
544
                        getChbUseDefault().setSelected(auxLegend.isUseDefaultSymbol());
545
                        cmbFields.getModel().setSelectedItem(auxLegend.getClassifyingFieldNames()[0]);
546
                        setColorScheme();
547
                        
548
                        Object[] values = auxLegend.getValues();
549
                        String[] descriptions = new String[values.length];
550
                        ISymbol[] symbols = new ISymbol[values.length];
551
                        
552
                        for (int i = 0; i < values.length; i++) {
553
                                Object value = values[i];
554
                                symbols[i] = auxLegend.getSymbolByValue(value);
555
                                descriptions[i] = symbols[i].getDescription();
556
                        }
557

    
558
                        symbolTable.fillTableFromSymbolList(symbols,
559
                                        values,descriptions);
560
                        chbUseDefault.setSelected(auxLegend.isUseDefaultSymbol());
561
                } else {
562
                        auxLegend = new VectorialUniqueValueLegend(shapeType);
563
                }
564
                defaultSymbolPrev.setSymbol(auxLegend.getDefaultSymbol());
565
                btnOpenSymbolLevelsEditor.setEnabled(symbolTable != null && symbolTable.getRowCount()>0);
566
    }
567

    
568

    
569
    private void setColorScheme(){
570

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

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

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

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

    
602
                    ISymbol defaultSymbolLegend = auxLegend.getDefaultSymbol();
603
                    ISymbol symbol = defaultSymbolPrev.getSymbol();
604
                    if(symbol != null){
605
                            if(symbol!=defaultSymbolLegend){
606
                                    auxLegend.setDefaultSymbol(symbol);
607
                            }
608
                    }
609

    
610
                    auxLegend.useDefaultSymbol(chbUseDefault.isSelected());
611

    
612
                    theLegend = (VectorialUniqueValueLegend) auxLegend.cloneLegend();
613
                    theLegend.setZSort(auxLegend.getZSort());
614
            }
615

    
616
            return theLegend;
617
    }
618

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

    
635
                return chbUseDefault;
636
    }
637

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

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

    
658

    
659
    public void actionPerformed(ActionEvent e) {
660

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

    
668

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

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

    
694
            // add only one value
695
            if (e.getActionCommand() == "ADD_VALUE") {
696
                    try {
697
                                ISymbol symbol =
698
                                                mapContextManager.getSymbolManager().createSymbol(
699
                                                                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 IconThemeHelper.getImageIcon(
746
                "legend-overview-vectorial-unique-value");
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
}