Statistics
| Revision:

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

History | View | Annotate | Download (19.4 KB)

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

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

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

    
60
import org.apache.log4j.Logger;
61
import org.gvsig.data.ReadException;
62
import org.gvsig.data.vectorial.Feature;
63
import org.gvsig.data.vectorial.FeatureAttributeDescriptor;
64
import org.gvsig.data.vectorial.FeatureStore;
65
import org.gvsig.fmap.mapcontext.layers.FLayer;
66
import org.gvsig.fmap.mapcontext.layers.operations.AlphanumericData;
67
import org.gvsig.fmap.mapcontext.layers.operations.ClassifiableVectorial;
68
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
69
import org.gvsig.fmap.mapcontext.rendering.legend.AbstractClassifiedVectorLegend;
70
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
71
import org.gvsig.fmap.mapcontext.rendering.legend.LegendFactory;
72
import org.gvsig.fmap.mapcontext.rendering.legend.NullUniqueValue;
73
import org.gvsig.fmap.mapcontext.rendering.legend.NullValue;
74
import org.gvsig.fmap.mapcontext.rendering.legend.VectorialUniqueValueLegend;
75
import org.gvsig.fmap.mapcontext.rendering.legend.ZSort;
76
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
77
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
78
import org.gvsig.gui.beans.swing.JButton;
79
import org.gvsig.raster.datastruct.ColorItem;
80

    
81
import com.iver.andami.PluginServices;
82
import com.iver.andami.messages.NotificationManager;
83
import com.iver.cit.gvsig.gui.styling.JComboBoxColorScheme;
84
import com.iver.cit.gvsig.gui.styling.SymbolLevelsWindow;
85

    
86

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

    
97
    protected VectorialUniqueValueLegend theLegend;
98
    private ClassifiableVectorial layer;
99
    private SymbolTable symbolTable;
100
    protected JComboBox cmbFields;
101
    private JButton btnRemoveAll;
102
    private JButton btnRemove;
103
    private JCheckBox chbUseDefault = null;
104
    private VectorialUniqueValueLegend auxLegend;
105
        private JPanel pnlCenter;
106
        private ZSort zSort;
107
        private JButton btnOpenSymbolLevelsEditor;
108

    
109
        private JComboBoxColorScheme cmbColorScheme;
110

    
111
    /**
112
     *
113
     */
114
    public VectorialUniqueValue() {
115
        super();
116
        initComponents();
117
    }
118

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

    
130
        JButton btnAddAll = new JButton(PluginServices.getText(this,
131
                    "Anadir_todos"));
132
        btnAddAll.setActionCommand("ADD_ALL_VALUES");
133
        btnAddAll.addActionListener(this);
134
        pnlButtons.add(btnAddAll);
135

    
136
        JButton btnAdd = new JButton(PluginServices.getText(this, "Anadir"));
137
        btnAdd.setActionCommand("ADD_VALUE");
138
        btnAdd.addActionListener(this);
139
        pnlButtons.add(btnAdd);
140

    
141
        btnRemoveAll = new JButton(PluginServices.getText(this, "Quitar_todos"));
142
        btnRemoveAll.setActionCommand("REMOVE_ALL");
143
        btnRemoveAll.addActionListener(this);
144
        pnlButtons.add(btnRemoveAll);
145

    
146
        btnRemove = new JButton(PluginServices.getText(this, "Quitar"));
147
        btnRemove.setActionCommand("REMOVE");
148
        btnRemove.addActionListener(this);
149
        pnlButtons.add(btnRemove);
150

    
151
        btnOpenSymbolLevelsEditor = new JButton(PluginServices.getText(this, "symbol_levels"));
152
        btnOpenSymbolLevelsEditor.addActionListener(this);
153
        btnOpenSymbolLevelsEditor.setActionCommand("OPEN_SYMBOL_LEVEL_EDITOR");
154
        pnlButtons.add(btnOpenSymbolLevelsEditor);
155

    
156
                pnlCenter = new JPanel();
157
        pnlCenter.setLayout(new BorderLayout());
158

    
159
        cmbFields = new JComboBox();
160
        cmbFields.setActionCommand("FIELD_SELECTED");
161
        cmbFields.addActionListener(this);
162
        cmbFields.setVisible(true);
163

    
164
        JPanel pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 0));
165
        JPanel pAux1 = new JPanel();
166

    
167
        JLabel lblFieldClassification = new JLabel(PluginServices.getText(
168
                    this, "Campo_de_clasificacion"));
169
        pAux1.add(lblFieldClassification);
170
        pAux1.add(cmbFields);
171
        pAux1.add(getChbUseDefault(), null);
172
        pnlNorth.add(pAux1);
173

    
174
        pAux1 = new JPanel();
175
        pAux1.add(new JLabel(PluginServices.getText(this, "color_scheme")+":"));
176
        cmbColorScheme = new JComboBoxColorScheme(false);
177

    
178
        pAux1.add(cmbColorScheme);
179

    
180
        pnlNorth.add(pAux1);
181

    
182
        this.setLayout(new BorderLayout());
183
        this.add(pnlNorth, BorderLayout.NORTH);
184
        this.add(pnlCenter, BorderLayout.CENTER);
185
        this.add(pnlButtons, BorderLayout.SOUTH);
186

    
187
    }
188

    
189
    /**
190
     * DOCUMENT ME!
191
     */
192
    private void fillTableValues() {
193
        FeatureStore elRs;
194

    
195
        try {
196
            elRs = ((FLyrVect) layer).getFeatureStore();
197
//            logger.debug("elRs.start()");
198
//            elRs.start();
199

    
200
            int idField = -1;
201
            String fieldName = (String) cmbFields.getSelectedItem();
202
            if (fieldName==null) {
203
                    JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_hay_campo_seleccionado"));
204
                    return;
205
            }
206

    
207
//            idField = elRs.getFieldIndexByName(fieldName);
208
            theLegend.setClassifyingFieldNames(new String[] {fieldName});
209

    
210
            //long numReg = elRs.getRowCount();
211
//            if (idField == -1) {
212
//                NotificationManager.addWarning(
213
//                                PluginServices.getText(this, "unrecognized_field_name")+" " + fieldName, null);
214
//
215
//                return;
216
//            }
217

    
218
            symbolTable.removeAllItems();
219

    
220
            int numSymbols = 0;
221
            ISymbol theSymbol = null;
222

    
223
            //auxLegend=(VectorialUniqueValueLegend)m_lyr.getLegend();
224
            auxLegend = LegendFactory.createVectorialUniqueValueLegend(layer.getShapeType());
225

    
226
            Object clave=null;
227

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

    
235
            ColorItem[] colorScheme = cmbColorScheme.getSelectedColors();
236
            Random rand = new Random(System.currentTimeMillis());
237

    
238
            Iterator iterator=elRs.getDataCollection().iterator();
239
            while (iterator.hasNext()) {
240
                                Feature feature = (Feature) iterator.next();
241
                                feature.get(fieldName);
242
//                        }
243
//            for (int j = 0; j < elRs.getRowCount(); j++) {
244
//                clave = elRs.getFieldValue(j, idField);
245

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

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

    
260
                    numSymbols++;
261

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

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

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

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

    
288
        //m_bCacheDirty = false;
289
    }
290

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

    
299
        // Borramos las anteriores listas:
300

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

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

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

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

    
340
        try {
341
            rs = ((AlphanumericData) layer).getFeatureStore();
342
//            logger.debug("rs.start()");
343
//            rs.start();
344

    
345
            ArrayList names=new ArrayList();
346
            Iterator iterator=rs.getDefaultFeatureType().iterator();
347
            while (iterator.hasNext()) {
348
                                FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator.next();
349
                                names.add(descriptor.getDataType());
350
                        }
351
//            String[] nomFields = new String[rs.getFieldCount()];
352
//
353
//            for (int i = 0; i < rs.getFieldCount(); i++) {
354
//                nomFields[i] = rs.getFieldName(i).trim();
355
//            }
356
//
357
//            rs.stop();
358

    
359
            DefaultComboBoxModel cM = new DefaultComboBoxModel((String[])names.toArray(new String[0]));
360
            cmbFields.setModel(cM);
361

    
362
            // fieldsListValor.setSelectedIndex(0);
363
        } catch (ReadException e) {
364
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
365
        }
366
    }
367

    
368
    public void setData(FLayer layer, ILegend legend) {
369
            this.layer = (ClassifiableVectorial) layer;
370
              int shapeType = 0;
371
              try {
372
                      shapeType = this.layer.getShapeType();
373
              } catch (ReadException e) {
374
                    NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
375
                }
376

    
377
              if (symbolTable != null)
378
                      pnlCenter.remove(symbolTable);
379
              symbolTable = new SymbolTable(this, SymbolTable.VALUES_TYPE, shapeType);
380
              pnlCenter.add(symbolTable, BorderLayout.CENTER);
381

    
382
        fillFieldNames();
383

    
384
        symbolTable.removeAllItems();
385

    
386
        if (VectorialUniqueValueLegend.class.equals(legend.getClass())) {
387
            theLegend = (VectorialUniqueValueLegend) legend;
388
            getChbUseDefault().setSelected(theLegend.isUseDefaultSymbol());
389
            cmbFields.getModel().setSelectedItem(theLegend.getClassifyingFieldNames()[0]);
390
            symbolTable.fillTableFromSymbolList(theLegend.getSymbols(),
391
                                                                                    theLegend.getValues(),
392
                                                                                    theLegend.getDescriptions());
393
            zSort = theLegend.getZSort();
394
        } else {
395
            theLegend = new VectorialUniqueValueLegend(shapeType);
396
        }
397
    }
398

    
399
    /* (non-Javadoc)
400
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
401
     */
402
    public ILegend getLegend() {
403
        fillSymbolListFromTable();
404

    
405
        if (auxLegend != null) {
406
                // your settings that are not the set of symbols must be located here
407
                auxLegend.setClassifyingFieldNames(
408
                                new String[] {(String) cmbFields.getSelectedItem()});
409

    
410
            theLegend = auxLegend;
411
        }
412
        theLegend.setZSort(zSort);
413

    
414
        return theLegend;
415
    }
416

    
417
    private JCheckBox getChbUseDefault() {
418
        if (chbUseDefault == null) {
419
            chbUseDefault = new JCheckBox();
420
            chbUseDefault.setSelected(true);
421
            chbUseDefault.addActionListener(new java.awt.event.ActionListener() {
422
                    public void actionPerformed(java.awt.event.ActionEvent e) {
423
                            if (auxLegend==null)auxLegend=theLegend;
424
                            if (chbUseDefault.isSelected()) {
425
                                    addDefault();
426
                            } else {
427
                                    delDefault();
428
                            }
429
                    }
430
            });
431
            chbUseDefault.setText(PluginServices.getText(this, "resto_valores"));
432
        }
433

    
434
        return chbUseDefault;
435
    }
436

    
437
    /**
438
     * A�ade el resto de valores.
439
     */
440
    private void addDefault() {
441
        auxLegend.getDefaultSymbol().setDescription("Default");
442
        auxLegend.addSymbol(new NullUniqueValue(), auxLegend.getDefaultSymbol());
443
        symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
444
            new NullUniqueValue(), auxLegend.getDefaultSymbol().getDescription());
445
        symbolTable.repaint();
446
    }
447

    
448
    /**
449
     * Elimina el resto de valores que no estan representados por ning�n otro s�mbolo..
450
     */
451
    private void delDefault() {
452
        auxLegend.delSymbol(new NullUniqueValue());
453
        symbolTable.removeRow(new NullUniqueValue());
454
        symbolTable.repaint();
455
    }
456

    
457

    
458
    public void actionPerformed(ActionEvent e) {
459

    
460
            //modificar el combobox de valor
461
            if (e.getActionCommand() == "FIELD_SELECTED") {
462
                    JComboBox cb = (JComboBox) e.getSource();
463
                    String fieldName = (String) cb.getSelectedItem();
464
                    symbolTable.removeAllItems();
465

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

    
485
            // add all elements by value
486
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
487
                    fillTableValues();
488
            }
489

    
490
            // add only one value
491
            if (e.getActionCommand() == "ADD_VALUE") {
492
                    try {
493
                            symbolTable.addTableRecord(SymbologyFactory.
494
                                            createDefaultSymbolByShapeType(layer.getShapeType()),
495
                                            new Double(0.0), "0 - 0");
496
                    } catch (ReadException ex) {
497
                            NotificationManager.addError(PluginServices.getText(this, "getting_shape_type"), ex);
498
                    }
499
            }
500

    
501
            //Vacia la tabla
502
            if (e.getActionCommand() == "REMOVE_ALL") {
503
                    symbolTable.removeAllItems();
504
                    theLegend.setZSort(null);
505
                    zSort = null;
506
            }
507

    
508
            //Quitar solo el elemento seleccionado
509
            if (e.getActionCommand() == "REMOVE") {
510
                    symbolTable.removeSelectedRows();
511
            }
512

    
513
            if (e.getActionCommand() == "OPEN_SYMBOL_LEVEL_EDITOR") {
514

    
515
                        if (theLegend != null) {
516
                                ZSort myZSort = ((AbstractClassifiedVectorLegend) getLegend()).getZSort();
517
                                if (myZSort == null) {
518
                                                myZSort = new ZSort(theLegend);
519
                                }
520
                                SymbolLevelsWindow sl = new SymbolLevelsWindow(myZSort);
521
                                PluginServices.getMDIManager().addWindow(sl);
522
                                zSort = sl.getZSort();
523
                                if (auxLegend != null)
524
                                        auxLegend.setZSort(zSort);
525
                        }
526

    
527
            }
528
    }
529

    
530
        public String getDescription() {
531
                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") + ".";
532
        }
533

    
534
        public ImageIcon getIcon() {
535
                return new ImageIcon(this.getClass().getClassLoader().
536
                                getResource("images/ValoresUnicos.png"));
537
        }
538

    
539
        public Class getParentClass() {
540
                return Categories.class;
541
        }
542

    
543
        public String getTitle() {
544
                return PluginServices.getText(this,"Valores_unicos");
545
        }
546

    
547
        public JPanel getPanel() {
548
                return this;
549
        }
550

    
551
        public Class getLegendClass() {
552
                return VectorialUniqueValueLegend.class;
553
        }
554

    
555

    
556
        public boolean isSuitableFor(FLayer layer) {
557
                return (layer instanceof FLyrVect);
558
        }
559
}