svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / legendmanager / panels / FPanelLegendValues.java @ 1774
History | View | Annotate | Download (11.4 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.driverManager.DriverLoadException; |
64 |
import com.hardcode.gdbms.engine.data.DataSource; |
65 |
import com.hardcode.gdbms.engine.data.DriverException; |
66 |
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException; |
67 |
import com.hardcode.gdbms.engine.values.Value; |
68 |
import com.iver.andami.PluginServices; |
69 |
import com.iver.andami.Utilities; |
70 |
import com.iver.cit.gvsig.fmap.core.FShape; |
71 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
72 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
73 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
74 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
75 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
76 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial; |
77 |
import com.iver.cit.gvsig.fmap.rendering.Legend; |
78 |
import com.iver.cit.gvsig.fmap.rendering.LegendFactory; |
79 |
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend; |
80 |
|
81 |
/**
|
82 |
* @author fjp
|
83 |
*
|
84 |
* To change the template for this generated type comment go to
|
85 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
86 |
*/
|
87 |
public class FPanelLegendValues extends JPanel implements ILegendPanel { |
88 |
private static Logger logger = Logger.getLogger(FPanelLegendValues.class.getName()); |
89 |
private MyListener listener = new MyListener(); |
90 |
// private TOC m_TOC;
|
91 |
private VectorialUniqueValueLegend m_Renderer;
|
92 |
private ClassifiableVectorial m_lyr;
|
93 |
private boolean m_bCacheDirty = true; |
94 |
|
95 |
private FSymbolTable m_symbolTable = new FSymbolTable(); |
96 |
|
97 |
private JComboBox m_cboFields; |
98 |
|
99 |
private JButton quitartodo; |
100 |
private JButton quitar; |
101 |
|
102 |
|
103 |
|
104 |
class MyListener implements ActionListener |
105 |
{ |
106 |
//private FLyrShp m_layer;
|
107 |
// private FPanelLegendValues m_Parent;
|
108 |
|
109 |
public MyListener() { // FPanelLegendValues p) { |
110 |
// m_Parent = p;
|
111 |
} |
112 |
|
113 |
public void actionPerformed(ActionEvent e) { |
114 |
|
115 |
// rellenarValue();
|
116 |
System.out.println("ActionEvent con " + e.getActionCommand()); |
117 |
|
118 |
//modificar el combobox de valor
|
119 |
if (e.getActionCommand() == "FIELD_SELECTED") { |
120 |
JComboBox cb = (JComboBox) e.getSource(); |
121 |
String fieldName = (String) cb.getSelectedItem(); |
122 |
System.out.println("Nombre del campo: " + fieldName); |
123 |
m_symbolTable.removeAllItems(); |
124 |
if (fieldName != m_Renderer.getFieldName())
|
125 |
m_bCacheDirty = true;
|
126 |
m_Renderer.setFieldName(fieldName); |
127 |
} |
128 |
|
129 |
|
130 |
//A?adir todos los elementos por valor
|
131 |
if (e.getActionCommand() == "ADD_ALL_VALUES") { |
132 |
fillTableValues(); |
133 |
} |
134 |
|
135 |
//A?adir un ?nico elemento
|
136 |
if (e.getActionCommand() == "ADD_VALUE") { |
137 |
/* a?adir("Nuevo_Valor");
|
138 |
aceptar.setEnabled(true);
|
139 |
quitartodo.setEnabled(true);
|
140 |
quitar.setEnabled(true); */
|
141 |
} |
142 |
|
143 |
//Vacia la tabla
|
144 |
if (e.getActionCommand() == "REMOVE_ALL") { |
145 |
m_symbolTable.removeAllItems(); |
146 |
// aceptar.setEnabled(false);
|
147 |
} |
148 |
|
149 |
//Quitar solo el elemento seleccionado
|
150 |
if (e.getActionCommand() == "REMOVE") { |
151 |
m_symbolTable.removeSelectedRows(); |
152 |
} |
153 |
|
154 |
} |
155 |
} |
156 |
|
157 |
/**
|
158 |
*
|
159 |
*/
|
160 |
public FPanelLegendValues() {
|
161 |
super();
|
162 |
initComponents(); |
163 |
} |
164 |
protected void initComponents() |
165 |
{ |
166 |
/* JLabel label = new JLabel();
|
167 |
label.setIcon(new javax.swing.ImageIcon(Abrir.class.getClassLoader()
|
168 |
.getResource("images/ValoresUnicos.png")));
|
169 |
limagen[1] = new JLabel();
|
170 |
limagen[1] = label; */
|
171 |
|
172 |
JPanel botonestabla = new JPanel(); |
173 |
|
174 |
|
175 |
JButton btnAddAll = new JButton(PluginServices.getText(this,"Anadir_todos")); |
176 |
btnAddAll.setActionCommand("ADD_ALL_VALUES");
|
177 |
btnAddAll.addActionListener(listener); |
178 |
botonestabla.add(btnAddAll); |
179 |
|
180 |
JButton btnAdd = new JButton(PluginServices.getText(this,"Anadir")); |
181 |
btnAdd.setActionCommand("ADD_VALUE");
|
182 |
btnAdd.addActionListener(listener); |
183 |
botonestabla.add(btnAdd); |
184 |
|
185 |
quitartodo = new JButton(PluginServices.getText(this,"Quitar_todos")); |
186 |
quitartodo.setActionCommand("REMOVE_ALL");
|
187 |
quitartodo.addActionListener(listener); |
188 |
botonestabla.add(quitartodo); |
189 |
|
190 |
quitar = new JButton(PluginServices.getText(this,"Quitar")); |
191 |
quitar.setActionCommand("REMOVE");
|
192 |
quitar.addActionListener(listener); |
193 |
botonestabla.add(quitar); |
194 |
|
195 |
|
196 |
JPanel ptabla = new JPanel(); |
197 |
ptabla.setLayout(new BorderLayout()); |
198 |
|
199 |
m_cboFields = new JComboBox(); |
200 |
m_cboFields.setActionCommand("FIELD_SELECTED");
|
201 |
m_cboFields.addActionListener(listener); |
202 |
m_cboFields.setVisible(true);
|
203 |
JPanel pAux1 = new JPanel(); |
204 |
/* pAux1.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
|
205 |
"Campo de clasificaci?n",
|
206 |
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
|
207 |
javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); */
|
208 |
pAux1.setPreferredSize(new Dimension(300,50)); |
209 |
JLabel lblFieldClassification = new JLabel(PluginServices.getText(this,"Campo_de_clasificacion")); |
210 |
pAux1.add(lblFieldClassification); |
211 |
pAux1.add(m_cboFields); |
212 |
|
213 |
|
214 |
|
215 |
|
216 |
ptabla.add(m_symbolTable, BorderLayout.CENTER);
|
217 |
m_symbolTable.setPreferredSize(new Dimension(400, 200)); |
218 |
this.setLayout(new BorderLayout()); |
219 |
this.add(pAux1, BorderLayout.NORTH); |
220 |
this.add(ptabla, BorderLayout.CENTER); |
221 |
this.add(botonestabla, BorderLayout.SOUTH); |
222 |
} |
223 |
private void fillTableValues() |
224 |
{ |
225 |
DataSource elRs;
|
226 |
try {
|
227 |
elRs = ((AlphanumericData)m_lyr).getRecordset(); |
228 |
logger.debug("elRs.start()");
|
229 |
elRs.start(); |
230 |
int idField = -1; |
231 |
String fieldName = (String) m_cboFields.getSelectedItem(); |
232 |
System.out.println("Nombre del campo: " + fieldName); |
233 |
idField = elRs.getFieldIndexByName(fieldName); |
234 |
m_Renderer.setFieldName(fieldName); |
235 |
long numReg = elRs.getRowCount();
|
236 |
|
237 |
if (idField == -1) |
238 |
{ |
239 |
System.err.println("Campo no reconocido " + fieldName); |
240 |
return;
|
241 |
} |
242 |
|
243 |
m_symbolTable.removeAllItems(); |
244 |
int numSymbols = 0; |
245 |
FSymbol theSymbol = null;
|
246 |
|
247 |
VectorialUniqueValueLegend auxLegend; |
248 |
auxLegend = LegendFactory.createVectorialUniqueValueLegend(m_lyr.getShapeType()); |
249 |
Value clave; |
250 |
Object resul;
|
251 |
|
252 |
for (int j = 0; j < elRs.getRowCount(); j++) |
253 |
{ |
254 |
clave = elRs.getFieldValue(j,idField); |
255 |
////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
|
256 |
if (auxLegend.getSymbolByValue(clave) == null) { |
257 |
//si no esta creado el simbolo se crea
|
258 |
theSymbol = new FSymbol(m_lyr.getShapeType());
|
259 |
|
260 |
theSymbol.setDescription(clave.toString()); |
261 |
auxLegend.addSymbol(clave,theSymbol); |
262 |
|
263 |
numSymbols++; |
264 |
|
265 |
if (numSymbols > 100) { |
266 |
JOptionPane.showMessageDialog(this, |
267 |
"M?s 100 de s?mbolos no aportan informaci?n en un plano. Por favor, revise el campo seleccionado.");
|
268 |
break;
|
269 |
} |
270 |
} |
271 |
} // for
|
272 |
|
273 |
m_symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(), |
274 |
auxLegend.getValues(), auxLegend.getDescriptions()); |
275 |
elRs.stop(); |
276 |
} catch (DriverException e) {
|
277 |
// TODO Auto-generated catch block
|
278 |
e.printStackTrace(); |
279 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
280 |
e.printStackTrace(); |
281 |
} |
282 |
|
283 |
quitartodo.setEnabled(true);
|
284 |
quitar.setEnabled(true);
|
285 |
|
286 |
m_bCacheDirty = false;
|
287 |
|
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 |
{ |
296 |
Value clave; |
297 |
FSymbol theSymbol; |
298 |
|
299 |
// Borramos las anteriores listas:
|
300 |
m_Renderer.clear(); |
301 |
boolean bRestoValores = false; // PONERLO EN UN CHECKBOX |
302 |
int hasta;
|
303 |
String fieldName = (String) m_cboFields.getSelectedItem(); |
304 |
m_Renderer.setFieldName(fieldName); |
305 |
|
306 |
if (bRestoValores)
|
307 |
hasta = m_symbolTable.getRowCount() - 1;
|
308 |
else
|
309 |
hasta = m_symbolTable.getRowCount(); |
310 |
for (int row=0; row < m_symbolTable.getRowCount();row++) |
311 |
{ |
312 |
clave = (Value) m_symbolTable.getFieldValue(row,1);
|
313 |
theSymbol = (FSymbol) m_symbolTable.getFieldValue(row,0);
|
314 |
theSymbol.setDescription((String) m_symbolTable.getFieldValue(row,2)); |
315 |
m_Renderer.addSymbol(clave,theSymbol); |
316 |
System.out.println(clave);
|
317 |
} |
318 |
if (bRestoValores)
|
319 |
{ |
320 |
theSymbol = (FSymbol) m_symbolTable.getFieldValue(hasta,0);
|
321 |
m_Renderer.setDefaultSymbol(theSymbol); |
322 |
} |
323 |
|
324 |
} |
325 |
|
326 |
private void fillFieldNames() { |
327 |
|
328 |
DataSource rs;
|
329 |
try {
|
330 |
rs = ((AlphanumericData)m_lyr).getRecordset(); |
331 |
logger.debug("rs.start()");
|
332 |
rs.start(); |
333 |
String[] nomFields = new String[rs.getFieldCount()]; |
334 |
for (int i = 0; i < rs.getFieldCount(); i++) { |
335 |
nomFields[i] = rs.getFieldName(i).trim(); |
336 |
} |
337 |
rs.stop(); |
338 |
DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields); |
339 |
m_cboFields.setModel(cM); |
340 |
// fieldsListValor.setSelectedIndex(0);
|
341 |
} catch (DriverException e) {
|
342 |
// TODO Auto-generated catch block
|
343 |
e.printStackTrace(); |
344 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
345 |
e.printStackTrace(); |
346 |
} |
347 |
|
348 |
m_symbolTable.removeAllItems(); |
349 |
|
350 |
} |
351 |
|
352 |
public void setLayer(FLayer lyr, Legend r) |
353 |
{ |
354 |
// OJO, COMPROBAR ANTES SI ES DE TIPO VECTORIAL
|
355 |
m_lyr = (ClassifiableVectorial) lyr; |
356 |
fillFieldNames(); |
357 |
|
358 |
// Si es de valor ?nico, rellenamos la tabla
|
359 |
// y seleccionamos el campo que estamos usando
|
360 |
m_symbolTable.removeAllItems(); |
361 |
if (r instanceof VectorialUniqueValueLegend) |
362 |
{ |
363 |
m_Renderer = (VectorialUniqueValueLegend) r; |
364 |
m_cboFields.getModel().setSelectedItem(m_Renderer.getFieldName()); |
365 |
m_symbolTable.fillTableFromSymbolList(m_Renderer.getSymbols(), |
366 |
m_Renderer.getValues(), m_Renderer.getDescriptions()); |
367 |
} |
368 |
else
|
369 |
{ |
370 |
try {
|
371 |
// Si la capa viene con otro tipo de leyenda, creamos
|
372 |
// una nueva del tipo que maneja este panel
|
373 |
m_Renderer = new VectorialUniqueValueLegend(m_lyr.getShapeType());
|
374 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
375 |
// TODO Auto-generated catch block
|
376 |
e.printStackTrace(); |
377 |
} |
378 |
} |
379 |
|
380 |
|
381 |
} |
382 |
/* (non-Javadoc)
|
383 |
* @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
|
384 |
*/
|
385 |
public Legend getLegend() {
|
386 |
fillSymbolListFromTable(); |
387 |
return m_Renderer;
|
388 |
} |
389 |
|
390 |
} |