svn-gvsig-desktop / tags / v1_1_Build_1004 / libraries / libCq_CMS_praster / src / org / cresques / ui / raster / listener / PalettePanelListener.java @ 12319
History | View | Annotate | Download (14 KB)
1 | 8026 | nacho | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2005 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 | package org.cresques.ui.raster.listener; |
||
20 | |||
21 | import java.awt.Color; |
||
22 | import java.awt.event.ActionEvent; |
||
23 | import java.awt.event.ActionListener; |
||
24 | import java.awt.event.KeyEvent; |
||
25 | import java.awt.event.KeyListener; |
||
26 | import java.io.BufferedReader; |
||
27 | import java.io.BufferedWriter; |
||
28 | import java.io.File; |
||
29 | import java.io.FileInputStream; |
||
30 | import java.io.FileNotFoundException; |
||
31 | import java.io.FileReader; |
||
32 | import java.io.FileWriter; |
||
33 | import java.io.IOException; |
||
34 | import java.io.Reader; |
||
35 | import java.io.StringWriter; |
||
36 | import java.io.Writer; |
||
37 | import java.util.ArrayList; |
||
38 | |||
39 | import javax.swing.JButton; |
||
40 | import javax.swing.JColorChooser; |
||
41 | |||
42 | import org.cresques.ui.raster.NameChooserDialog; |
||
43 | import org.cresques.ui.raster.PalettePanel; |
||
44 | import org.gvsig.gui.beans.table.TableControlerPanel; |
||
45 | import org.gvsig.gui.beans.table.exceptions.NotInitializeException; |
||
46 | import org.kxml2.io.KXmlParser; |
||
47 | import org.kxml2.io.KXmlSerializer; |
||
48 | import org.xmlpull.v1.XmlPullParserException; |
||
49 | |||
50 | /**
|
||
51 | * Listener para eventos del panel de paleta
|
||
52 | * @author Nacho Brodin <brodin_ign@gva.es>
|
||
53 | *
|
||
54 | */
|
||
55 | public class PalettePanelListener implements KeyListener, ActionListener{ |
||
56 | |||
57 | private PalettePanel panel = null; |
||
58 | public static boolean comboEventEnable = true; |
||
59 | private TableControlerPanel control = null; |
||
60 | private JColorChooser colorSelector = null;; |
||
61 | private NameChooserDialog nameChooser;
|
||
62 | private String savePaletteName; |
||
63 | |||
64 | /**
|
||
65 | * Constructor
|
||
66 | * @param p Panel
|
||
67 | */
|
||
68 | public PalettePanelListener(PalettePanel p){
|
||
69 | panel = p; |
||
70 | } |
||
71 | |||
72 | /**
|
||
73 | * Asigna a los controles que el listener por defecto sea esta clase.
|
||
74 | * El panel tendr? que llamar a este m?todo explicitamente si quiere que
|
||
75 | * los listener sean asignados.
|
||
76 | */
|
||
77 | public void setControlListeners(){ |
||
78 | control = panel.getPTable().getControl(); |
||
79 | |||
80 | panel.getBSave().addActionListener(this);
|
||
81 | 10645 | nacho | panel.getBSaveAs().addActionListener(this);
|
82 | 8026 | nacho | panel.getBAccept().addActionListener(this);
|
83 | panel.getBApply().addActionListener(this);
|
||
84 | panel.getBCancel().addActionListener(this);
|
||
85 | panel.getCActive().addActionListener(this);
|
||
86 | panel.getCbInterpolar().addActionListener(this);
|
||
87 | panel.getCbList().addActionListener(this);
|
||
88 | |||
89 | control.getBNew().addActionListener(this);
|
||
90 | control.getBDelPoint().addActionListener(this);
|
||
91 | control.getBClear().addActionListener(this);
|
||
92 | panel.getPTable().setEnableControlsListener(false);
|
||
93 | |||
94 | /*panel.getPTable().addKeyListener(this);
|
||
95 | panel.getBAccept().addKeyListener(this);
|
||
96 | panel.getBApply().addKeyListener(this);
|
||
97 | panel.getBCancel().addKeyListener(this);
|
||
98 | panel.getCActive().addKeyListener(this);
|
||
99 | panel.getCbInterpolar().addKeyListener(this);
|
||
100 | panel.getCbList().addKeyListener(this);*/
|
||
101 | } |
||
102 | |||
103 | /**
|
||
104 | * Controlamos las acciones de:
|
||
105 | * <UL>
|
||
106 | * <LI>Nueva fila: Asigna un nuevo bot?n con su listener para que pueda se controlado desde esta misma clase</LI>
|
||
107 | * <LI>Lista de botones: Para cada bot?n de la tabla asignamos el listener que hace que se muestre el
|
||
108 | * dialogo de color. El valor retornado por este se pondra en la columna RGB y el bot?n se colorear?</LI>
|
||
109 | * </UL>
|
||
110 | * @throws NotInitializeException
|
||
111 | */
|
||
112 | public void actionPerformed(ActionEvent e) { |
||
113 | |||
114 | 10645 | nacho | //Bot?n salvar como
|
115 | if(e.getSource() == panel.getBSaveAs()){
|
||
116 | 8026 | nacho | if (nameChooser == null) |
117 | nameChooser = new NameChooserDialog(panel, this); |
||
118 | else
|
||
119 | nameChooser.init(); |
||
120 | } |
||
121 | |||
122 | 10645 | nacho | //Bot?n de salvar tabla
|
123 | if(e.getSource() == panel.getBSave()){
|
||
124 | if(panel.getCbList().getSelectedIndex() != 0){ //Ya existe. Hay que sustituir la que hay |
||
125 | replacePalette(panel.getPalettesPath(), panel.getCbList().getSelectedItem().toString()); |
||
126 | }else{ //No hay nombre seleccionado -> se comporta como salvar como |
||
127 | if (nameChooser == null) |
||
128 | nameChooser = new NameChooserDialog(panel, this); |
||
129 | else
|
||
130 | nameChooser.init(); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | 8026 | nacho | //Bot?n de nueva fila
|
135 | if(e.getSource() == control.getBNew())
|
||
136 | try {
|
||
137 | panel.addRowToTable(Color.WHITE, "C" + panel.getPTable().getRowCount(), "0", "255"); |
||
138 | } catch (NotInitializeException e1) {
|
||
139 | panel.addRowToTable(Color.WHITE, "C0" , "0", "255"); |
||
140 | } |
||
141 | |||
142 | |||
143 | //Bot?n eliminar fila
|
||
144 | if(e.getSource() == control.getBDelPoint())
|
||
145 | panel.deleteSelectedRow(); |
||
146 | |||
147 | //Bot?n eliminar todas las filas
|
||
148 | if(e.getSource() == control.getBClear())
|
||
149 | panel.deleteAllRows(); |
||
150 | |||
151 | |||
152 | //Comobo elegir paleta predeterminada.
|
||
153 | if(e.getSource() == panel.getCbList()){
|
||
154 | if (panel.getCbList().getSelectedItem()!=null){ |
||
155 | loadPalette(panel.getPalettesPath(), panel.getCbList().getSelectedItem().toString()); |
||
156 | panel.orderRows(); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | //Check de activo
|
||
161 | if(e.getSource() == panel.getCActive()){
|
||
162 | if(panel.getCActive().isSelected())
|
||
163 | panel.setEnabled(true);
|
||
164 | else
|
||
165 | panel.setEnabled(false);
|
||
166 | } |
||
167 | |||
168 | } |
||
169 | |||
170 | |||
171 | |||
172 | public void keyPressed(KeyEvent e) { |
||
173 | /*if(e.getKeyCode() == 10){//Enter=Aceptar
|
||
174 | |||
175 | }
|
||
176 | |||
177 | if(e.getKeyCode() == 27){ //Escape=Cancelar
|
||
178 | |||
179 | }*/
|
||
180 | } |
||
181 | |||
182 | public void keyReleased(KeyEvent e) { |
||
183 | // TODO Auto-generated method stub
|
||
184 | |||
185 | } |
||
186 | |||
187 | public void keyTyped(KeyEvent e) { |
||
188 | // TODO Auto-generated method stub
|
||
189 | |||
190 | } |
||
191 | |||
192 | |||
193 | /**
|
||
194 | * Lee una paleta del fichero xml de paletas y la carga en la tabla del panel.
|
||
195 | *
|
||
196 | * @param palettesPath Camino al fichero de paletas predefinidas.
|
||
197 | * @param paletteName Nombre de paleta a cargar desde el fichero xml.
|
||
198 | *
|
||
199 | */
|
||
200 | public void loadPalette(String palettesPath, String paletteName){ |
||
201 | |||
202 | File palettesFile = new File(palettesPath); |
||
203 | if(!palettesFile.exists())
|
||
204 | return;
|
||
205 | |||
206 | try {
|
||
207 | KXmlParser parser = new KXmlParser();
|
||
208 | FileInputStream fileInputStream = new FileInputStream(palettesPath); |
||
209 | parser.setInput(fileInputStream, null);
|
||
210 | int tag = parser.nextTag();
|
||
211 | |||
212 | parser.require(KXmlParser.START_TAG, null, "palettes"); |
||
213 | tag = parser.nextTag(); |
||
214 | parser.require(KXmlParser.START_TAG, null, "palette_list"); |
||
215 | parser.skipSubTree(); |
||
216 | parser.require(KXmlParser.END_TAG, null, "palette_list"); |
||
217 | tag = parser.nextTag(); |
||
218 | |||
219 | while (tag == KXmlParser.START_TAG){
|
||
220 | parser.require(KXmlParser.START_TAG, null, "palette"); |
||
221 | if(parser.getAttributeCount()==2) |
||
222 | if (parser.getAttributeValue(0).equals(paletteName)){ |
||
223 | boolean interpolate = Boolean.valueOf(parser.getAttributeValue(1)).booleanValue(); |
||
224 | tag = parser.nextTag(); |
||
225 | parser.require(KXmlParser.START_TAG, null, "table"); |
||
226 | tag = parser.nextTag(); |
||
227 | try {
|
||
228 | panel.getPTable().removeAllRows(); |
||
229 | } catch (NotInitializeException e1) {
|
||
230 | e1.printStackTrace(); |
||
231 | } |
||
232 | |||
233 | ArrayList rows = new ArrayList(); |
||
234 | |||
235 | while(tag == KXmlParser.START_TAG){
|
||
236 | parser.require(KXmlParser.START_TAG, null, "entry"); |
||
237 | if (parser.getAttributeCount()==3){ |
||
238 | |||
239 | String rgb = parser.getAttributeValue(1).substring(parser.getAttributeValue(1).indexOf(",")+1, |
||
240 | parser.getAttributeValue(1).length());
|
||
241 | |||
242 | int a = Integer.valueOf(parser.getAttributeValue(1).substring(0,parser.getAttributeValue(1).indexOf(","))).intValue(); |
||
243 | int r= Integer.valueOf(rgb.substring(0,rgb.indexOf(","))).intValue(); |
||
244 | int g=Integer.valueOf(rgb.substring(rgb.indexOf(",")+1,rgb.lastIndexOf(","))).intValue(); |
||
245 | int b=Integer.valueOf(rgb.substring(rgb.lastIndexOf(",")+1,rgb.length())).intValue(); |
||
246 | |||
247 | //No a?ado filas a la tabla hasta que no se ha leido correctamente la paleta completa:
|
||
248 | Object row[] = {new Color(r,g,b), parser.getAttributeValue(0), parser.getAttributeValue(2), String.valueOf(a)}; |
||
249 | rows.add(row); |
||
250 | } |
||
251 | tag = parser.nextTag(); |
||
252 | parser.require(KXmlParser.END_TAG, null, "entry"); |
||
253 | tag = parser.nextTag(); |
||
254 | } |
||
255 | parser.require(KXmlParser.END_TAG, null, "table"); |
||
256 | |||
257 | // Rellenar la tabla con las entradas leidas:
|
||
258 | for (int i=0;i<rows.size();i++) |
||
259 | panel.addRowToTable((Color)((Object[])rows.get(i))[0], (String)((Object[])rows.get(i))[1], |
||
260 | (String)((Object[])rows.get(i))[2], (String)((Object[])rows.get(i))[3]); |
||
261 | panel.getCbInterpolar().setSelected(interpolate); |
||
262 | fileInputStream.close(); |
||
263 | return;
|
||
264 | } |
||
265 | parser.skipSubTree(); |
||
266 | parser.require(KXmlParser.END_TAG, null, "palette"); |
||
267 | tag = parser.nextTag(); |
||
268 | } |
||
269 | |||
270 | parser.require(KXmlParser.END_TAG, null, "palettes"); |
||
271 | panel.deleteAllRows(); |
||
272 | fileInputStream.close(); |
||
273 | return;
|
||
274 | |||
275 | }catch (FileNotFoundException fnfEx) { |
||
276 | fnfEx.printStackTrace(); |
||
277 | }catch (XmlPullParserException xmlEx) {
|
||
278 | System.out.println("El fichero de paletas predeterminadas no tiene la estructura correcta:\n " + xmlEx.getMessage()); |
||
279 | }catch (IOException e) { |
||
280 | } |
||
281 | } |
||
282 | |||
283 | /**
|
||
284 | * Salva la paleta que hay en la tabla al fichero xml de paletas conel nombre especificado.
|
||
285 | *
|
||
286 | * @param palettesPath Camino al fichero de paletas predefinidas.
|
||
287 | * @param paletteName Nombre de paleta a cargar desde el fichero xml.
|
||
288 | *
|
||
289 | */
|
||
290 | public void savePalette(String palettesPath, String paletteName){ |
||
291 | |||
292 | File tmp = new File(palettesPath.substring(0,palettesPath.lastIndexOf(File.separator)+1)+"palettes.tmp"); |
||
293 | File palettes = new File(palettesPath); |
||
294 | |||
295 | try {
|
||
296 | if (!tmp.exists())
|
||
297 | tmp.createNewFile(); |
||
298 | |||
299 | Reader reader = new FileReader(palettesPath); |
||
300 | BufferedReader bReader = new BufferedReader(reader); |
||
301 | |||
302 | Writer writer = new FileWriter(palettesPath.substring(0,palettesPath.lastIndexOf(File.separator)+1)+"palettes.tmp"); |
||
303 | BufferedWriter bWriter = new BufferedWriter(writer); |
||
304 | |||
305 | String line = bReader.readLine();
|
||
306 | while (line != null){ |
||
307 | |||
308 | if (line.indexOf("</palette_list>")>=0){ |
||
309 | bWriter.write("\t\t<name>"+paletteName+"</name>"); |
||
310 | bWriter.newLine(); |
||
311 | }else if (line.indexOf("</palettes>")>=0){ |
||
312 | bWriter.newLine(); |
||
313 | bWriter.write(createXmlStringFromTable(paletteName)); |
||
314 | bWriter.newLine(); |
||
315 | } |
||
316 | |||
317 | bWriter.write(line); |
||
318 | bWriter.newLine(); |
||
319 | |||
320 | line = bReader.readLine(); |
||
321 | } |
||
322 | |||
323 | bReader.close(); |
||
324 | bWriter.close(); |
||
325 | |||
326 | palettes.delete(); |
||
327 | tmp.renameTo(palettes); |
||
328 | } catch (FileNotFoundException e) { |
||
329 | e.printStackTrace(); |
||
330 | }catch (IOException e) { |
||
331 | e.printStackTrace(); |
||
332 | } |
||
333 | } |
||
334 | |||
335 | 10645 | nacho | |
336 | 8026 | nacho | /**
|
337 | 10645 | nacho | * Sustituye la paleta que hay en la tabla al fichero xml de paletas conel nombre especificado.
|
338 | *
|
||
339 | * @param palettesPath Camino al fichero de paletas predefinidas.
|
||
340 | * @param paletteName Nombre de paleta a sustituir
|
||
341 | *
|
||
342 | */
|
||
343 | public void replacePalette(String palettesPath, String paletteName){ |
||
344 | |||
345 | File tmp = new File(palettesPath.substring(0,palettesPath.lastIndexOf(File.separator)+1)+"palettes.tmp"); |
||
346 | File palettes = new File(palettesPath); |
||
347 | |||
348 | try {
|
||
349 | if (!tmp.exists())
|
||
350 | tmp.createNewFile(); |
||
351 | |||
352 | Reader reader = new FileReader(palettesPath); |
||
353 | BufferedReader bReader = new BufferedReader(reader); |
||
354 | |||
355 | Writer writer = new FileWriter(palettesPath.substring(0,palettesPath.lastIndexOf(File.separator)+1)+"palettes.tmp"); |
||
356 | BufferedWriter bWriter = new BufferedWriter(writer); |
||
357 | |||
358 | String line = bReader.readLine();
|
||
359 | boolean write = true; |
||
360 | while (line != null){ |
||
361 | |||
362 | if (line.indexOf("<palette name=\""+paletteName+"\"") >= 0) |
||
363 | write = false;
|
||
364 | |||
365 | if(write){
|
||
366 | bWriter.write(line); |
||
367 | bWriter.newLine(); |
||
368 | }else{
|
||
369 | bWriter.write(createXmlStringFromTable(paletteName)); |
||
370 | bWriter.newLine(); |
||
371 | while ((line.indexOf("</palette>") < 0) && (line != null)) |
||
372 | line = bReader.readLine(); |
||
373 | write = true;
|
||
374 | } |
||
375 | |||
376 | line = bReader.readLine(); |
||
377 | } |
||
378 | |||
379 | bReader.close(); |
||
380 | bWriter.close(); |
||
381 | |||
382 | palettes.delete(); |
||
383 | tmp.renameTo(palettes); |
||
384 | } catch (FileNotFoundException e) { |
||
385 | e.printStackTrace(); |
||
386 | }catch (IOException e) { |
||
387 | e.printStackTrace(); |
||
388 | } |
||
389 | } |
||
390 | |||
391 | /**
|
||
392 | 8026 | nacho | * Crea un String con el bloque XML correspondiente a la paleta que hay en la tabla.
|
393 | *
|
||
394 | * @param palettesPath Camino al fichero de paletas predefinidas.
|
||
395 | * @param paletteName Nombre de paleta a cargar desde el fichero xml.
|
||
396 | *
|
||
397 | */
|
||
398 | private String createXmlStringFromTable(String paletteName){ |
||
399 | Writer writer = new StringWriter(); |
||
400 | try {
|
||
401 | KXmlSerializer serializer = new KXmlSerializer();
|
||
402 | serializer.setOutput(writer); |
||
403 | |||
404 | serializer.text("\t");
|
||
405 | serializer.startTag(null,"palette").attribute(null,"name",paletteName).attribute(null,"interpolate", |
||
406 | Boolean.toString(panel.getCbInterpolar().isSelected()));
|
||
407 | serializer.text("\n");
|
||
408 | serializer.text("\t\t");
|
||
409 | serializer.startTag(null,"table"); |
||
410 | serializer.text("\n");
|
||
411 | |||
412 | |||
413 | for (int row = 0; row<panel.getPTable().getRowCount();row++){ |
||
414 | |||
415 | String name = (String)panel.getPTable().getModel().getValueAt(row,1); |
||
416 | String value = (String)panel.getPTable().getModel().getValueAt(row,3); |
||
417 | String rgb = (String)panel.getPTable().getModel().getValueAt(row,2); |
||
418 | String argb = (String)panel.getPTable().getModel().getValueAt(row,4) + "," + rgb; |
||
419 | |||
420 | serializer.text("\t\t\t");
|
||
421 | serializer.startTag(null,"entry").attribute(null,"class",name); |
||
422 | serializer.attribute(null,"argb",argb); |
||
423 | serializer.attribute(null,"value",value).endTag(null,"entry"); |
||
424 | serializer.text("\n");
|
||
425 | } |
||
426 | |||
427 | serializer.text("\t\t");
|
||
428 | serializer.endTag(null,"table"); |
||
429 | serializer.text("\n");
|
||
430 | serializer.text("\t");
|
||
431 | serializer.endTag(null,"palette"); |
||
432 | |||
433 | }catch (FileNotFoundException fnfEx) { |
||
434 | fnfEx.printStackTrace(); |
||
435 | }catch (IOException e) { |
||
436 | }catch (NotInitializeException e) {
|
||
437 | // TODO Auto-generated catch block
|
||
438 | e.printStackTrace(); |
||
439 | } |
||
440 | |||
441 | return writer.toString();
|
||
442 | } |
||
443 | |||
444 | public String getSavePaletteName() { |
||
445 | return savePaletteName;
|
||
446 | } |
||
447 | |||
448 | public void setSavePaletteName(String savePaletteName) { |
||
449 | this.savePaletteName = savePaletteName;
|
||
450 | } |
||
451 | } |