Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / legendmanager / panels / FPanelLegendValues.java @ 1850

History | View | Annotate | Download (11.1 KB)

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

    
49
import java.awt.BorderLayout;
50
import java.awt.Dimension;
51
import java.awt.event.ActionEvent;
52
import java.awt.event.ActionListener;
53

    
54
import javax.swing.DefaultComboBoxModel;
55
import javax.swing.JButton;
56
import javax.swing.JComboBox;
57
import javax.swing.JLabel;
58
import javax.swing.JOptionPane;
59
import javax.swing.JPanel;
60

    
61
import org.apache.log4j.Logger;
62

    
63
import com.hardcode.gdbms.engine.data.DataSource;
64
import com.hardcode.gdbms.engine.data.driver.DriverException;
65
import com.hardcode.gdbms.engine.values.Value;
66
import com.iver.andami.PluginServices;
67
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
68
import com.iver.cit.gvsig.fmap.layers.FLayer;
69
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
70
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
71
import com.iver.cit.gvsig.fmap.rendering.Legend;
72
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
73
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
74

    
75
/**
76
 * @author fjp
77
 *
78
 * To change the template for this generated type comment go to
79
 * Window>Preferences>Java>Code Generation>Code and Comments
80
 */
81
public class FPanelLegendValues extends JPanel implements ILegendPanel {
82
        private static Logger logger = Logger.getLogger(FPanelLegendValues.class.getName());
83
        private MyListener listener = new MyListener();
84
        // private TOC m_TOC;
85
        private VectorialUniqueValueLegend m_Renderer;
86
        private ClassifiableVectorial m_lyr;
87
        private boolean m_bCacheDirty = true;
88
        
89
        private FSymbolTable m_symbolTable = new FSymbolTable();
90

    
91
        private JComboBox m_cboFields;
92
        
93
        private JButton quitartodo;
94
        private JButton quitar;
95
        
96
        
97
        
98
        class MyListener implements ActionListener 
99
        {
100
                //private FLyrShp m_layer;
101
                // private FPanelLegendValues m_Parent;
102

    
103
                public MyListener() { // FPanelLegendValues p) {
104
                        // m_Parent = p;
105
                }
106

    
107
                public void actionPerformed(ActionEvent e) {
108

    
109
                        // rellenarValue();
110
                        System.out.println("ActionEvent con " + e.getActionCommand());
111

    
112
                        //modificar el combobox de valor
113
                        if (e.getActionCommand() == "FIELD_SELECTED") {
114
                                JComboBox cb = (JComboBox) e.getSource();
115
                                String fieldName = (String) cb.getSelectedItem();
116
                                System.out.println("Nombre del campo: " + fieldName);
117
                                m_symbolTable.removeAllItems();
118
                                if (fieldName != m_Renderer.getFieldName())
119
                                        m_bCacheDirty = true;
120
                                m_Renderer.setFieldName(fieldName);
121
                        }
122

    
123

    
124
                        //A?adir todos los elementos por valor
125
                        if (e.getActionCommand() == "ADD_ALL_VALUES") {
126
                                fillTableValues();
127
                        }
128

    
129
                        //A?adir un ?nico elemento
130
                        if (e.getActionCommand() == "ADD_VALUE") {
131
                        /*        a?adir("Nuevo_Valor");
132
                                aceptar.setEnabled(true);
133
                                quitartodo.setEnabled(true);
134
                                quitar.setEnabled(true); */
135
                        }
136

    
137
                        //Vacia la tabla
138
                        if (e.getActionCommand() == "REMOVE_ALL") {
139
                                m_symbolTable.removeAllItems();
140
                                // aceptar.setEnabled(false);
141
                        }
142

    
143
                        //Quitar solo el elemento seleccionado
144
                        if (e.getActionCommand() == "REMOVE") {
145
                                m_symbolTable.removeSelectedRows();
146
                        }
147

    
148
                }
149
        }
150

    
151
        /**
152
         * 
153
         */
154
        public FPanelLegendValues() {
155
                super();
156
                initComponents();
157
        }
158
        protected void initComponents()
159
        {
160
                /* JLabel label = new JLabel();
161
                label.setIcon(new javax.swing.ImageIcon(Abrir.class.getClassLoader()
162
                                                                                                                   .getResource("images/ValoresUnicos.png")));
163
                limagen[1] = new JLabel();
164
                limagen[1] = label; */
165

    
166
                JPanel botonestabla = new JPanel();
167
                
168

    
169
                JButton btnAddAll = new JButton(PluginServices.getText(this,"Anadir_todos"));
170
                btnAddAll.setActionCommand("ADD_ALL_VALUES");
171
                btnAddAll.addActionListener(listener);
172
                botonestabla.add(btnAddAll);
173

    
174
                JButton btnAdd = new JButton(PluginServices.getText(this,"Anadir"));
175
                btnAdd.setActionCommand("ADD_VALUE");
176
                btnAdd.addActionListener(listener);
177
                botonestabla.add(btnAdd);
178

    
179
                quitartodo = new JButton(PluginServices.getText(this,"Quitar_todos"));
180
                quitartodo.setActionCommand("REMOVE_ALL");
181
                quitartodo.addActionListener(listener);
182
                botonestabla.add(quitartodo);
183

    
184
                quitar = new JButton(PluginServices.getText(this,"Quitar"));
185
                quitar.setActionCommand("REMOVE");
186
                quitar.addActionListener(listener);
187
                botonestabla.add(quitar);
188

    
189
                
190
                JPanel ptabla = new JPanel();                
191
                ptabla.setLayout(new BorderLayout());
192
                
193
                m_cboFields = new JComboBox();
194
                m_cboFields.setActionCommand("FIELD_SELECTED");
195
                m_cboFields.addActionListener(listener);
196
                m_cboFields.setVisible(true);
197
                JPanel pAux1 = new JPanel();
198
                /* pAux1.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
199
                                "Campo de clasificaci?n",
200
                                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
201
                                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); */
202
                pAux1.setPreferredSize(new Dimension(300,50));
203
                JLabel lblFieldClassification = new JLabel(PluginServices.getText(this,"Campo_de_clasificacion"));
204
                pAux1.add(lblFieldClassification);                  
205
                pAux1.add(m_cboFields);
206
                
207
                
208

    
209
                
210
                ptabla.add(m_symbolTable, BorderLayout.CENTER);
211
                m_symbolTable.setPreferredSize(new Dimension(400, 200));
212
                this.setLayout(new BorderLayout());
213
                this.add(pAux1, BorderLayout.NORTH);
214
                this.add(ptabla, BorderLayout.CENTER);
215
                this.add(botonestabla, BorderLayout.SOUTH);
216
        }
217
        private void fillTableValues()
218
        {
219
                DataSource elRs;
220
                try {
221
                        elRs = ((AlphanumericData)m_lyr).getRecordset();
222
                        logger.debug("elRs.start()");
223
                        elRs.start();
224
                        int idField = -1;
225
                        String fieldName = (String) m_cboFields.getSelectedItem();
226
                        System.out.println("Nombre del campo: " + fieldName);
227
            idField = elRs.getFieldIndexByName(fieldName);
228
            if (idField == -1)
229
            {       
230
                System.err.println("Campo no reconocido " + fieldName);
231
                return;
232
            } 
233
            
234
                        m_Renderer.setFieldName(fieldName);
235
                        long numReg = elRs.getRowCount();
236
                        
237
                        
238
                        m_symbolTable.removeAllItems();
239
                        int numSymbols = 0;
240
                        FSymbol theSymbol = null;
241
                        
242
                        VectorialUniqueValueLegend auxLegend;
243
                        auxLegend = LegendFactory.createVectorialUniqueValueLegend(m_lyr.getShapeType());
244
                        Value clave;
245
                        Object resul;
246
                        
247
                        for (int j = 0; j < elRs.getRowCount(); j++)
248
                        {
249
                                clave = elRs.getFieldValue(j,idField);        
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
                                        theSymbol = new FSymbol(m_lyr.getShapeType());
254
                                                                        
255
                                        theSymbol.setDescription(clave.toString());
256
                                        auxLegend.addSymbol(clave,theSymbol);
257
                                                                        
258
                                        numSymbols++;
259
        
260
                                        if (numSymbols > 100) {
261
                                                JOptionPane.showMessageDialog(this,
262
                                                        "M?s 100 de s?mbolos no aportan informaci?n en un plano. Por favor, revise el campo seleccionado.");
263
                                                break;
264
                                        }
265
                                }
266
                        } // for
267
        
268
                        m_symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
269
                                auxLegend.getValues(), auxLegend.getDescriptions());
270
                        elRs.stop();
271
                } catch (DriverException e) {
272
                        // TODO Auto-generated catch block
273
                        e.printStackTrace();
274
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
275
                        e.printStackTrace();
276
                }
277

    
278
                quitartodo.setEnabled(true);
279
                quitar.setEnabled(true);        
280
                
281
                m_bCacheDirty = false;        
282
                        
283
                
284
        }
285
        /**
286
         * A partir de los registros de la tabla, regenera el FRenderer. (No solo
287
         * el symbolList, si no tambi?n el arrayKeys y el defaultRenderer
288
         */
289
        private void fillSymbolListFromTable()
290
        {        
291
                Value clave;
292
                FSymbol theSymbol; 
293
                
294
                // Borramos las anteriores listas:
295
                m_Renderer.clear();
296
                boolean bRestoValores = false; // PONERLO EN UN CHECKBOX
297
                int hasta;
298
                String fieldName = (String) m_cboFields.getSelectedItem();
299
                m_Renderer.setFieldName(fieldName);
300
                
301
                if (bRestoValores)
302
                        hasta = m_symbolTable.getRowCount() - 1;
303
                else
304
                        hasta = m_symbolTable.getRowCount();
305
                for (int row=0; row < m_symbolTable.getRowCount();row++)
306
                { 
307
                        clave = (Value) m_symbolTable.getFieldValue(row,1);
308
                        theSymbol = (FSymbol) m_symbolTable.getFieldValue(row,0);
309
                        theSymbol.setDescription((String) m_symbolTable.getFieldValue(row,2));
310
                        m_Renderer.addSymbol(clave,theSymbol);
311
                        System.out.println(clave);
312
                }
313
                if (bRestoValores)
314
                {
315
                        theSymbol = (FSymbol) m_symbolTable.getFieldValue(hasta,0);
316
                        m_Renderer.setDefaultSymbol(theSymbol);
317
                }
318
                                
319
        }
320
        
321
        private void fillFieldNames() {
322

    
323
                DataSource rs;
324
                try {
325
                        rs = ((AlphanumericData)m_lyr).getRecordset();
326
                        logger.debug("rs.start()");
327
                        rs.start();
328
                        String[] nomFields = new String[rs.getFieldCount()];
329
                        for (int i = 0; i < rs.getFieldCount(); i++) {
330
                                nomFields[i] = rs.getFieldName(i).trim();
331
                        }
332
                        rs.stop();
333
                        DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields);
334
                        m_cboFields.setModel(cM);
335
                // fieldsListValor.setSelectedIndex(0);
336
                } catch (DriverException e) {
337
                        // TODO Auto-generated catch block
338
                        e.printStackTrace();
339
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
340
                        e.printStackTrace();
341
                }
342
                
343
                m_symbolTable.removeAllItems();
344

    
345
        }
346
        
347
        public void setLayer(FLayer lyr, Legend r)
348
        {
349
                // OJO, COMPROBAR ANTES SI ES DE TIPO VECTORIAL
350
                m_lyr = (ClassifiableVectorial) lyr;
351
                fillFieldNames();                                        
352
                
353
                // Si es de valor ?nico, rellenamos la tabla
354
                // y seleccionamos el campo que estamos usando
355
                m_symbolTable.removeAllItems();
356
                if (r instanceof VectorialUniqueValueLegend)
357
                {                        
358
                        m_Renderer = (VectorialUniqueValueLegend) r;
359
                        m_cboFields.getModel().setSelectedItem(m_Renderer.getFieldName());
360
                        m_symbolTable.fillTableFromSymbolList(m_Renderer.getSymbols(),
361
                                        m_Renderer.getValues(), m_Renderer.getDescriptions());
362
                }
363
                else
364
                {
365
                        try {
366
                                // Si la capa viene con otro tipo de leyenda, creamos
367
                                // una nueva del tipo que maneja este panel
368
                                m_Renderer = new VectorialUniqueValueLegend(m_lyr.getShapeType());
369
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
370
                                // TODO Auto-generated catch block
371
                                e.printStackTrace();
372
                        }
373
                }
374

    
375
                
376
        }
377
        /* (non-Javadoc)
378
         * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
379
         */
380
        public Legend getLegend() {
381
                fillSymbolListFromTable();
382
                return m_Renderer;
383
        }
384

    
385
}