root / branches / gvSIG_03_SLD / applications / appgvSIG / src / com / iver / cit / gvsig / gui / thememanager / legendmanager / panels / FPanelLegendValues.java @ 2076
History | View | Annotate | Download (11 KB)
1 | 1671 | jmorell | /*
|
---|---|---|---|
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.thememanager.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 | 1830 | fernando | import com.hardcode.gdbms.engine.data.driver.DriverException; |
65 | 1671 | jmorell | 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 | 2076 | vcaballero | private FSymbolTable m_symbolTable = new FSymbolTable("values"); |
90 | 1671 | jmorell | |
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 | m_Renderer.setFieldName(fieldName); |
||
229 | long numReg = elRs.getRowCount();
|
||
230 | |||
231 | if (idField == -1) |
||
232 | { |
||
233 | System.err.println("Campo no reconocido " + fieldName); |
||
234 | return;
|
||
235 | } |
||
236 | |||
237 | m_symbolTable.removeAllItems(); |
||
238 | int numSymbols = 0; |
||
239 | FSymbol theSymbol = null;
|
||
240 | |||
241 | VectorialUniqueValueLegend auxLegend; |
||
242 | auxLegend = LegendFactory.createVectorialUniqueValueLegend(m_lyr.getShapeType()); |
||
243 | Value clave; |
||
244 | Object resul;
|
||
245 | |||
246 | for (int j = 0; j < elRs.getRowCount(); j++) |
||
247 | { |
||
248 | clave = elRs.getFieldValue(j,idField); |
||
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 | theSymbol = new FSymbol(m_lyr.getShapeType());
|
||
253 | |||
254 | theSymbol.setDescription(clave.toString()); |
||
255 | auxLegend.addSymbol(clave,theSymbol); |
||
256 | |||
257 | numSymbols++; |
||
258 | |||
259 | if (numSymbols > 100) { |
||
260 | JOptionPane.showMessageDialog(this, |
||
261 | "M?s 100 de s?mbolos no aportan informaci?n en un plano. Por favor, revise el campo seleccionado.");
|
||
262 | break;
|
||
263 | } |
||
264 | } |
||
265 | } // for
|
||
266 | |||
267 | m_symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(), |
||
268 | auxLegend.getValues(), auxLegend.getDescriptions()); |
||
269 | elRs.stop(); |
||
270 | } catch (DriverException e) {
|
||
271 | // TODO Auto-generated catch block
|
||
272 | e.printStackTrace(); |
||
273 | } catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
||
274 | e.printStackTrace(); |
||
275 | } |
||
276 | |||
277 | quitartodo.setEnabled(true);
|
||
278 | quitar.setEnabled(true);
|
||
279 | |||
280 | m_bCacheDirty = false;
|
||
281 | |||
282 | |||
283 | } |
||
284 | /**
|
||
285 | * A partir de los registros de la tabla, regenera el FRenderer. (No solo
|
||
286 | * el symbolList, si no tambi?n el arrayKeys y el defaultRenderer
|
||
287 | */
|
||
288 | private void fillSymbolListFromTable() |
||
289 | { |
||
290 | Value clave; |
||
291 | FSymbol theSymbol; |
||
292 | |||
293 | // Borramos las anteriores listas:
|
||
294 | m_Renderer.clear(); |
||
295 | boolean bRestoValores = false; // PONERLO EN UN CHECKBOX |
||
296 | int hasta;
|
||
297 | String fieldName = (String) m_cboFields.getSelectedItem(); |
||
298 | m_Renderer.setFieldName(fieldName); |
||
299 | |||
300 | if (bRestoValores)
|
||
301 | hasta = m_symbolTable.getRowCount() - 1;
|
||
302 | else
|
||
303 | hasta = m_symbolTable.getRowCount(); |
||
304 | for (int row=0; row < m_symbolTable.getRowCount();row++) |
||
305 | { |
||
306 | clave = (Value) m_symbolTable.getFieldValue(row,1);
|
||
307 | theSymbol = (FSymbol) m_symbolTable.getFieldValue(row,0);
|
||
308 | theSymbol.setDescription((String) m_symbolTable.getFieldValue(row,2)); |
||
309 | m_Renderer.addSymbol(clave,theSymbol); |
||
310 | System.out.println(clave);
|
||
311 | } |
||
312 | if (bRestoValores)
|
||
313 | { |
||
314 | theSymbol = (FSymbol) m_symbolTable.getFieldValue(hasta,0);
|
||
315 | m_Renderer.setDefaultSymbol(theSymbol); |
||
316 | } |
||
317 | |||
318 | } |
||
319 | |||
320 | private void fillFieldNames() { |
||
321 | |||
322 | DataSource rs;
|
||
323 | try {
|
||
324 | rs = ((AlphanumericData)m_lyr).getRecordset(); |
||
325 | logger.debug("rs.start()");
|
||
326 | rs.start(); |
||
327 | String[] nomFields = new String[rs.getFieldCount()]; |
||
328 | for (int i = 0; i < rs.getFieldCount(); i++) { |
||
329 | nomFields[i] = rs.getFieldName(i).trim(); |
||
330 | } |
||
331 | rs.stop(); |
||
332 | DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields); |
||
333 | m_cboFields.setModel(cM); |
||
334 | // fieldsListValor.setSelectedIndex(0);
|
||
335 | } catch (DriverException e) {
|
||
336 | e.printStackTrace(); |
||
337 | } catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
||
338 | e.printStackTrace(); |
||
339 | } |
||
340 | |||
341 | m_symbolTable.removeAllItems(); |
||
342 | |||
343 | } |
||
344 | |||
345 | public void setLayer(FLayer lyr, Legend r) |
||
346 | { |
||
347 | // OJO, COMPROBAR ANTES SI ES DE TIPO VECTORIAL
|
||
348 | m_lyr = (ClassifiableVectorial) lyr; |
||
349 | fillFieldNames(); |
||
350 | |||
351 | // Si es de valor ?nico, rellenamos la tabla
|
||
352 | // y seleccionamos el campo que estamos usando
|
||
353 | m_symbolTable.removeAllItems(); |
||
354 | if (r instanceof VectorialUniqueValueLegend) |
||
355 | { |
||
356 | m_Renderer = (VectorialUniqueValueLegend) r; |
||
357 | m_cboFields.getModel().setSelectedItem(m_Renderer.getFieldName()); |
||
358 | m_symbolTable.fillTableFromSymbolList(m_Renderer.getSymbols(), |
||
359 | m_Renderer.getValues(), m_Renderer.getDescriptions()); |
||
360 | } |
||
361 | else
|
||
362 | { |
||
363 | try {
|
||
364 | // Si la capa viene con otro tipo de leyenda, creamos
|
||
365 | // una nueva del tipo que maneja este panel
|
||
366 | m_Renderer = new VectorialUniqueValueLegend(m_lyr.getShapeType());
|
||
367 | } catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
||
368 | // TODO Auto-generated catch block
|
||
369 | e.printStackTrace(); |
||
370 | } |
||
371 | } |
||
372 | |||
373 | |||
374 | } |
||
375 | /* (non-Javadoc)
|
||
376 | * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
|
||
377 | */
|
||
378 | public Legend getLegend() {
|
||
379 | fillSymbolListFromTable(); |
||
380 | return m_Renderer;
|
||
381 | } |
||
382 | |||
383 | } |