Statistics
| Revision:

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

History | View | Annotate | Download (19.3 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package 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.fmap.data.ReadException;
62
import org.gvsig.fmap.data.feature.Feature;
63
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
64
import org.gvsig.fmap.data.feature.FeatureStore;
65
import org.gvsig.fmap.mapcontext.layers.FLayer;
66
import org.gvsig.fmap.mapcontext.layers.operations.ClassifiableVectorial;
67
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
68
import org.gvsig.fmap.mapcontext.rendering.legend.AbstractClassifiedVectorLegend;
69
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
70
import org.gvsig.fmap.mapcontext.rendering.legend.LegendFactory;
71
import org.gvsig.fmap.mapcontext.rendering.legend.NullUniqueValue;
72
import org.gvsig.fmap.mapcontext.rendering.legend.NullValue;
73
import org.gvsig.fmap.mapcontext.rendering.legend.VectorialUniqueValueLegend;
74
import org.gvsig.fmap.mapcontext.rendering.legend.ZSort;
75
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
76
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
77
import org.gvsig.gui.beans.swing.JButton;
78
import org.gvsig.raster.datastruct.ColorItem;
79

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

    
85

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

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

    
108
        private JComboBoxColorScheme cmbColorScheme;
109

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

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

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

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

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

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

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

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

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

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

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

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

    
177
        pAux1.add(cmbColorScheme);
178

    
179
        pnlNorth.add(pAux1);
180

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

    
186
    }
187

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

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

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

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

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

    
217
            symbolTable.removeAllItems();
218

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

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

    
225
            Object clave=null;
226

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

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

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

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

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

    
259
                    numSymbols++;
260

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

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

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

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

    
287
        //m_bCacheDirty = false;
288
    }
289

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

    
298
        // Borramos las anteriores listas:
299

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

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

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

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

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

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

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

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

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

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

    
381
        fillFieldNames();
382

    
383
        symbolTable.removeAllItems();
384

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

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

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

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

    
413
        return theLegend;
414
    }
415

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

    
433
        return chbUseDefault;
434
    }
435

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

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

    
456

    
457
    public void actionPerformed(ActionEvent e) {
458

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

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

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

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

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

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

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

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

    
526
            }
527
    }
528

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

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

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

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

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

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

    
554

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