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