Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / VectorialUniqueValue.java @ 14169

History | View | Annotate | Download (17.8 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.io.IOException;
49
import java.util.Random;
50

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

    
58
import org.apache.log4j.Logger;
59
import org.gvsig.gui.beans.swing.JButton;
60
import org.gvsig.raster.datastruct.ColorItem;
61

    
62
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
63
import com.hardcode.gdbms.engine.data.DataSource;
64
import com.hardcode.gdbms.engine.values.NullValue;
65
import com.hardcode.gdbms.engine.values.Value;
66
import com.hardcode.gdbms.engine.values.ValueFactory;
67
import com.iver.andami.PluginServices;
68
import com.iver.andami.messages.NotificationManager;
69
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
70
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
71
import com.iver.cit.gvsig.fmap.core.symbols.PictureMarkerSymbol;
72
import com.iver.cit.gvsig.fmap.layers.FLayer;
73
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
74
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
75
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
76
import com.iver.cit.gvsig.fmap.rendering.ILegend;
77
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
78
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
79
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
80
import com.iver.cit.gvsig.fmap.rendering.ZSort;
81
import com.iver.cit.gvsig.gui.styling.JComboBoxColorScheme;
82
import com.iver.cit.gvsig.gui.styling.SymbolLevelsWindow;
83

    
84

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

    
95
    // private TOC m_TOC;
96
    private VectorialUniqueValueLegend theLegend;
97
    private ClassifiableVectorial layer;
98

    
99
    //private boolean m_bCacheDirty = true;
100
    private FSymbolTable symbolTable;
101
    private JComboBox cmbFields;
102
    private JButton btnRemoveAll;
103
    private JButton btnRemove;
104
    private JCheckBox chbUseDefault = null;
105
    private VectorialUniqueValueLegend auxLegend;
106
        private PictureMarkerSymbol previewSymbol;
107
        private JPanel pnlCenter;
108
        private ZSort zSort;
109
        private JButton btnOpenSymbolLevelsEditor;
110

    
111
        private JComboBoxColorScheme cmbColorScheme;
112

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

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

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

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

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

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

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

    
158
                pnlCenter = new JPanel();
159
        pnlCenter.setLayout(new BorderLayout());
160

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

    
166
        JPanel pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 0));
167
        JPanel pAux1 = new JPanel();
168
        
169
        JLabel lblFieldClassification = new JLabel(PluginServices.getText(
170
                    this, "Campo_de_clasificacion"));
171
        pAux1.add(lblFieldClassification);
172
        pAux1.add(cmbFields);
173
        pAux1.add(getChbUseDefault(), null);
174
        pnlNorth.add(pAux1);
175
        
176
        pAux1 = new JPanel();
177
        pAux1.add(new JLabel(PluginServices.getText(this, "color_scheme")+":"));
178
        cmbColorScheme = new JComboBoxColorScheme(false);
179
       
180
        pAux1.add(cmbColorScheme);
181
        
182
        pnlNorth.add(pAux1);
183
        
184
        this.setLayout(new BorderLayout());
185
        this.add(pnlNorth, BorderLayout.NORTH);
186
        this.add(pnlCenter, BorderLayout.CENTER);
187
        this.add(pnlButtons, BorderLayout.SOUTH);
188
        
189
    }
190

    
191
    /**
192
     * DOCUMENT ME!
193
     */
194
    private void fillTableValues() {
195
        DataSource elRs;
196

    
197
        try {
198
            elRs = ((FLyrVect) layer).getRecordset();
199
            logger.debug("elRs.start()");
200
            elRs.start();
201

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

    
209
            idField = elRs.getFieldIndexByName(fieldName);
210
            theLegend.setClassifyingFieldNames(new String[] {fieldName});
211

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

    
217
                return;
218
            }
219

    
220
            symbolTable.removeAllItems();
221

    
222
            int numSymbols = 0;
223
            ISymbol theSymbol = null;
224

    
225
            //auxLegend=(VectorialUniqueValueLegend)m_lyr.getLegend();
226
            auxLegend = LegendFactory.createVectorialUniqueValueLegend(layer.getShapeType());
227

    
228
            Value clave;
229

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

    
237
            ColorItem[] colorScheme = cmbColorScheme.getSelectedColors();
238
            Random rand = new Random(System.currentTimeMillis());
239
            
240
            for (int j = 0; j < elRs.getRowCount(); j++) {
241
                clave = elRs.getFieldValue(j, idField);
242

    
243
                if (clave instanceof NullValue) {
244
                    continue;
245
                }
246

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

    
257
                    numSymbols++;
258

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

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

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

    
282
        btnRemoveAll.setEnabled(true);
283
        btnRemove.setEnabled(true);
284

    
285
        //m_bCacheDirty = false;
286
    }
287

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

    
296
        // Borramos las anteriores listas:
297
        theLegend.clear();
298

    
299
        boolean bRestoValores = false; // PONERLO EN UN CHECKBOX
300
        int hasta;
301
        String fieldName = (String) cmbFields.getSelectedItem();
302
        theLegend.setClassifyingFieldNames(new String[] {fieldName} );
303

    
304
        if (bRestoValores) {
305
            hasta = symbolTable.getRowCount() - 1;
306
        } else {
307
            hasta = symbolTable.getRowCount();
308
        }
309

    
310
        for (int row = 0; row < symbolTable.getRowCount(); row++) {
311
            clave = (Value) symbolTable.getFieldValue(row, 1);
312
            theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
313
            theSymbol.setDescription((String) symbolTable.getFieldValue(row, 2));
314
            theLegend.addSymbol(clave, theSymbol);
315
        }
316

    
317
        if (bRestoValores) {
318
            theSymbol = (ISymbol) symbolTable.getFieldValue(hasta, 0);
319
            theLegend.setDefaultSymbol(theSymbol);
320
        }
321
    }
322

    
323
    /**
324
     * DOCUMENT ME!
325
     */
326
    private void fillFieldNames() {
327
        DataSource rs;
328

    
329
        try {
330
            rs = ((AlphanumericData) layer).getRecordset();
331
            logger.debug("rs.start()");
332
            rs.start();
333

    
334
            String[] nomFields = new String[rs.getFieldCount()];
335

    
336
            for (int i = 0; i < rs.getFieldCount(); i++) {
337
                nomFields[i] = rs.getFieldName(i).trim();
338
            }
339

    
340
            rs.stop();
341

    
342
            DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields);
343
            cmbFields.setModel(cM);
344

    
345
            // fieldsListValor.setSelectedIndex(0);
346
        } catch (ReadDriverException e) {
347
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
348
        }
349
    }
350

    
351
    public void setData(FLayer layer, ILegend legend) {
352
            this.layer = (ClassifiableVectorial) layer;
353
              int shapeType = 0;
354
              try {
355
                      shapeType = this.layer.getShapeType();
356
              } catch (ReadDriverException e) {
357
                    NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
358
                }
359

    
360
              if (symbolTable != null)
361
                      pnlCenter.remove(symbolTable);
362
              symbolTable = new FSymbolTable("values", shapeType);
363
              pnlCenter.add(symbolTable, BorderLayout.CENTER);
364

    
365
        fillFieldNames();
366

    
367
        symbolTable.removeAllItems();
368

    
369
        if (legend instanceof VectorialUniqueValueLegend) {
370
            theLegend = (VectorialUniqueValueLegend) legend;
371
            getChbUseDefault().setSelected(theLegend.isUseDefaultSymbol());
372
            cmbFields.getModel().setSelectedItem(theLegend.getClassifyingFieldNames()[0]);
373
            symbolTable.fillTableFromSymbolList(theLegend.getSymbols(),
374
                                                                                    theLegend.getValues(),
375
                                                                                    theLegend.getDescriptions());
376
            zSort = theLegend.getZSort();
377
        } else {
378
            theLegend = new VectorialUniqueValueLegend(shapeType);
379
        }
380
    }
381

    
382
    /* (non-Javadoc)
383
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
384
     */
385
    public ILegend getLegend() {
386
        fillSymbolListFromTable();
387

    
388
        if (auxLegend != null) {
389
            theLegend.setDefaultSymbol(auxLegend.getDefaultSymbol());
390
            theLegend.useDefaultSymbol(chbUseDefault.isSelected());
391
        }
392
        theLegend.setZSort(zSort);
393
        
394
        return theLegend;
395
    }
396

    
397
    private JCheckBox getChbUseDefault() {
398
        if (chbUseDefault == null) {
399
            chbUseDefault = new JCheckBox();
400
            chbUseDefault.setSelected(true);
401
            chbUseDefault.addActionListener(new java.awt.event.ActionListener() {
402
                    public void actionPerformed(java.awt.event.ActionEvent e) {
403
                            if (auxLegend==null)auxLegend=theLegend;
404
                            if (chbUseDefault.isSelected()) {
405
                                    addDefault();
406
                            } else {
407
                                    delDefault();
408
                            }
409
                    }
410
            });
411
            chbUseDefault.setText(PluginServices.getText(this, "resto_valores"));
412
        }
413

    
414
        return chbUseDefault;
415
    }
416

    
417
    /**
418
     * A?ade el resto de valores.
419
     */
420
    private void addDefault() {
421
        auxLegend.getDefaultSymbol().setDescription("Default");
422
        auxLegend.addSymbol(new NullUniqueValue(), auxLegend.getDefaultSymbol());
423
        symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
424
            new NullUniqueValue(), auxLegend.getDefaultSymbol().getDescription());
425
        symbolTable.repaint();
426
    }
427

    
428
    /**
429
     * Elimina el resto de valores que no estan representados por ning?n otro s?mbolo..
430
     */
431
    private void delDefault() {
432
        auxLegend.delSymbol(new NullUniqueValue());
433
        symbolTable.removeRow(new NullUniqueValue());
434
        symbolTable.repaint();
435
    }
436

    
437

    
438
    public void actionPerformed(ActionEvent e) {
439

    
440
            //modificar el combobox de valor
441
            if (e.getActionCommand() == "FIELD_SELECTED") {
442
                    JComboBox cb = (JComboBox) e.getSource();
443
                    String fieldName = (String) cb.getSelectedItem();
444
                    System.out.println("Nombre del campo: " + fieldName);
445
                    symbolTable.removeAllItems();
446

    
447
                    if (theLegend.getClassifyingFieldNames()!=null && fieldName != theLegend.getClassifyingFieldNames()[0]) {
448
                            //m_bCacheDirty = true;
449
                            theLegend.setClassifyingFieldNames(new String[] {fieldName});
450
                    }
451
            }
452

    
453
            //A?adir todos los elementos por valor
454
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
455
                    fillTableValues();
456
            }
457

    
458
            //A?adir un ?nico elemento
459
            if (e.getActionCommand() == "ADD_VALUE") {
460
                    try {
461
                            symbolTable.addTableRecord(SymbologyFactory.
462
                                            createDefaultSymbolByShapeType(layer.getShapeType()),
463
                                            ValueFactory.createValue(0.0), "0 - 0");
464
                    } catch (ReadDriverException ex) {
465
                            NotificationManager.addError(PluginServices.getText(this, "getting_shape_type"), ex);
466
                    }
467
            }
468

    
469
            //Vacia la tabla
470
            if (e.getActionCommand() == "REMOVE_ALL") {
471
                    symbolTable.removeAllItems();
472
            }
473

    
474
            //Quitar solo el elemento seleccionado
475
            if (e.getActionCommand() == "REMOVE") {
476
                    symbolTable.removeSelectedRows();
477
            }
478

    
479
            if (e.getActionCommand() == "OPEN_SYMBOL_LEVEL_EDITOR") {
480

    
481
                        if (theLegend != null) {
482
                                ZSort myZSort = ((VectorialUniqueValueLegend) getLegend()).getZSort();
483
                                if (myZSort == null) {
484
                                                myZSort = new ZSort(theLegend);
485
                                }
486
                                SymbolLevelsWindow sl = new SymbolLevelsWindow(myZSort);
487
                                PluginServices.getMDIManager().addWindow(sl);
488
                                zSort = sl.getZSort();
489
                                if (auxLegend != null)
490
                                        auxLegend.setZSort(zSort);
491
                        }
492

    
493
            }
494
    }
495

    
496
        public String getDescription() {
497
                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") + ".";
498
        }
499

    
500
        public ISymbol getPreviewSymbol() {
501
                if (previewSymbol == null) {
502
                        try {
503
                                previewSymbol = new PictureMarkerSymbol(
504
                                                this.getClass().getClassLoader()
505
                                                                .getResource("images/ValoresUnicos.png"), null);
506
                        } catch (IOException e) {
507
                                return null;
508
                        }
509
                }
510
                return previewSymbol;
511
        }
512

    
513
        public Class getParentClass() {
514
                return Categories.class;
515
        }
516

    
517
        public String getTitle() {
518
                return PluginServices.getText(this,"Valores_unicos");
519
        }
520

    
521
        public JPanel getPanel() {
522
                return this;
523
        }
524

    
525
        public Class getLegendClass() {
526
                return VectorialUniqueValueLegend.class;
527
        }
528

    
529

    
530
        public boolean isSuitableFor(FLayer layer) {
531
                return (layer instanceof FLyrVect);
532
        }
533
}