Statistics
| Revision:

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

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

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

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

    
63
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
64
import com.hardcode.gdbms.engine.data.DataSource;
65
import com.hardcode.gdbms.engine.values.NullValue;
66
import com.hardcode.gdbms.engine.values.Value;
67
import com.hardcode.gdbms.engine.values.ValueFactory;
68
import com.iver.andami.PluginServices;
69
import com.iver.andami.messages.NotificationManager;
70
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
71
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
72
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
73
import com.iver.cit.gvsig.fmap.core.symbols.PictureFillSymbol;
74
import com.iver.cit.gvsig.fmap.layers.FLayer;
75
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
76
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
77
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
78
import com.iver.cit.gvsig.fmap.rendering.ILegend;
79
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
80
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
81
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
82
import com.iver.cit.gvsig.fmap.rendering.ZSort;
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
    // private TOC m_TOC;
98
    private VectorialUniqueValueLegend theLegend;
99
    private ClassifiableVectorial layer;
100

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

    
113
        private JComboBoxColorScheme cmbColorScheme;
114

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

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

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

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

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

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

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

    
160
                pnlCenter = new JPanel();
161
        pnlCenter.setLayout(new BorderLayout());
162

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

    
168
        JPanel pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 0));
169
        JPanel pAux1 = new JPanel();
170

    
171
        JLabel lblFieldClassification = new JLabel(PluginServices.getText(
172
                    this, "Campo_de_clasificacion"));
173
        pAux1.add(lblFieldClassification);
174
        pAux1.add(cmbFields);
175
        pAux1.add(getChbUseDefault(), null);
176
        pnlNorth.add(pAux1);
177

    
178
        pAux1 = new JPanel();
179
        pAux1.add(new JLabel(PluginServices.getText(this, "color_scheme")+":"));
180
        cmbColorScheme = new JComboBoxColorScheme(false);
181

    
182
        pAux1.add(cmbColorScheme);
183

    
184
        pnlNorth.add(pAux1);
185

    
186
        this.setLayout(new BorderLayout());
187
        this.add(pnlNorth, BorderLayout.NORTH);
188
        this.add(pnlCenter, BorderLayout.CENTER);
189
        this.add(pnlButtons, BorderLayout.SOUTH);
190

    
191
    }
192

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

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

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

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

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

    
219
                return;
220
            }
221

    
222
            symbolTable.removeAllItems();
223

    
224
            int numSymbols = 0;
225
            ISymbol theSymbol = null;
226

    
227
            //auxLegend=(VectorialUniqueValueLegend)m_lyr.getLegend();
228
            auxLegend = LegendFactory.createVectorialUniqueValueLegend(layer.getShapeType());
229

    
230
            Value clave;
231

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

    
239
            ColorItem[] colorScheme = cmbColorScheme.getSelectedColors();
240
            Random rand = new Random(System.currentTimeMillis());
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 (ReadDriverException 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
        Value clave;
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

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

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

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

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

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

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

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

    
341
            rs.stop();
342

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

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

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

    
361
              if (symbolTable != null)
362
                      pnlCenter.remove(symbolTable);
363
              symbolTable = new SymbolTable(this, SymbolTable.VALUES_TYPE, shapeType);
364
              pnlCenter.add(symbolTable, BorderLayout.CENTER);
365

    
366
        fillFieldNames();
367

    
368
        symbolTable.removeAllItems();
369

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

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

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

    
395
        return theLegend;
396
    }
397

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

    
415
        return chbUseDefault;
416
    }
417

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

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

    
438

    
439
    public void actionPerformed(ActionEvent e) {
440

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

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

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

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

    
470
            //Vacia la tabla
471
            if (e.getActionCommand() == "REMOVE_ALL") {
472
                    symbolTable.removeAllItems();
473
                    theLegend.setZSort(null);
474
                    zSort = null;
475
            }
476

    
477
            //Quitar solo el elemento seleccionado
478
            if (e.getActionCommand() == "REMOVE") {
479
                    symbolTable.removeSelectedRows();
480
            }
481

    
482
            if (e.getActionCommand() == "OPEN_SYMBOL_LEVEL_EDITOR") {
483

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

    
496
            }
497
    }
498

    
499
        public String getDescription() {
500
                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") + ".";
501
        }
502

    
503
        public ISymbol getIconSymbol() {
504
                if (previewSymbol == null) {
505
                        try {
506
                            previewSymbol = new PictureFillSymbol();
507
                            previewSymbol.setImage(
508
                                            new File(
509
                                                        PluginServices.getIconTheme()
510
                                                        .getURL("vectorial-unique-value")
511
                                                        .getFile()));
512
                                previewSymbol.getMarkerFillProperties().
513
                            setFillStyle(
514
                                            IMarkerFillPropertiesStyle.SINGLE_CENTERED_SYMBOL);
515
                                
516
                        } catch (IOException e) {
517
                                return null;
518
                        }
519
                }
520
                return previewSymbol;
521
        }
522

    
523
        public Class getParentClass() {
524
                return Categories.class;
525
        }
526

    
527
        public String getTitle() {
528
                return PluginServices.getText(this,"Valores_unicos");
529
        }
530

    
531
        public JPanel getPanel() {
532
                return this;
533
        }
534

    
535
        public Class getLegendClass() {
536
                return VectorialUniqueValueLegend.class;
537
        }
538

    
539

    
540
        public boolean isSuitableFor(FLayer layer) {
541
                return (layer instanceof FLyrVect);
542
        }
543
}