svn-gvsig-desktop / tags / v1_1_Build_1007 / libraries / libCq_CMS_praster / src / org / cresques / ui / raster / PalettePanel.java @ 12478
History | View | Annotate | Download (19.9 KB)
1 | 8026 | nacho | /*
|
---|---|---|---|
2 | * Created on 23-ago-2006
|
||
3 | *
|
||
4 | */
|
||
5 | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
6 | *
|
||
7 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
8 | *
|
||
9 | * This program is free software; you can redistribute it and/or
|
||
10 | * modify it under the terms of the GNU General Public License
|
||
11 | * as published by the Free Software Foundation; either version 2
|
||
12 | * of the License, or (at your option) any later version.
|
||
13 | *
|
||
14 | * This program is distributed in the hope that it will be useful,
|
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
17 | * GNU General Public License for more details.
|
||
18 | *
|
||
19 | * You should have received a copy of the GNU General Public License
|
||
20 | * along with this program; if not, write to the Free Software
|
||
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
22 | */
|
||
23 | package org.cresques.ui.raster; |
||
24 | |||
25 | import java.awt.Color; |
||
26 | import java.awt.FlowLayout; |
||
27 | import java.awt.GridBagConstraints; |
||
28 | import java.awt.GridBagLayout; |
||
29 | import java.io.File; |
||
30 | import java.io.FileInputStream; |
||
31 | import java.io.FileNotFoundException; |
||
32 | import java.io.IOException; |
||
33 | import java.util.ArrayList; |
||
34 | import java.util.Arrays; |
||
35 | import java.util.Hashtable; |
||
36 | |||
37 | import javax.swing.JButton; |
||
38 | import javax.swing.JCheckBox; |
||
39 | import javax.swing.JComboBox; |
||
40 | import javax.swing.JFrame; |
||
41 | import javax.swing.JLabel; |
||
42 | import javax.swing.JPanel; |
||
43 | |||
44 | import org.cresques.io.data.RasterBuf; |
||
45 | import org.cresques.io.datastruct.Palette; |
||
46 | import org.cresques.ui.raster.listener.PalettePanelListener; |
||
47 | import org.cresques.util.Utilities; |
||
48 | import org.gvsig.gui.beans.table.TableContainer; |
||
49 | import org.gvsig.gui.beans.table.exceptions.NotInitializeException; |
||
50 | import org.gvsig.gui.beans.table.models.TableButtonModel; |
||
51 | import org.gvsig.i18n.Messages; |
||
52 | import org.kxml2.io.KXmlParser; |
||
53 | import org.xmlpull.v1.XmlPullParserException; |
||
54 | |||
55 | /**
|
||
56 | * Interfaz de usuario para la selecci?n de paletas de color
|
||
57 | *
|
||
58 | * @author Nacho Brodin (brodin_ign@gva.es)
|
||
59 | *
|
||
60 | */
|
||
61 | |||
62 | public class PalettePanel extends BaseComponent { |
||
63 | |||
64 | private int HSELECT = 52; |
||
65 | private int HOPTIONS = 25; |
||
66 | private int HBUTTONS = 30; |
||
67 | private int MARGIN = 12; |
||
68 | |||
69 | private JPanel pMain = null; |
||
70 | private JPanel pNorth = null; |
||
71 | private JPanel pButtons = null; |
||
72 | private TableContainer pTable = null; |
||
73 | private JPanel pSelect = null; |
||
74 | private JPanel pOptions = null; |
||
75 | private JButton bAccept = null; |
||
76 | private JButton bApply = null; |
||
77 | private JButton bCancel = null; |
||
78 | private JButton bSave = null; |
||
79 | 10645 | nacho | private JButton bSaveAs = null; |
80 | 8026 | nacho | |
81 | private int width = 500; |
||
82 | private int height = 600; |
||
83 | private String[] columnNames = {Messages.getText("selec"), Messages.getText("clase"), "RGB", Messages.getText("valor"), Messages.getText("alpha")}; |
||
84 | private int[] columnWidths = {50, 90, 94, 95, 65}; |
||
85 | private JCheckBox cActive = null; |
||
86 | private JComboBox cbList = null; |
||
87 | private JCheckBox cbInterpolar = null; |
||
88 | private PalettePanelListener listener = null; |
||
89 | private String palettesPath = ""; |
||
90 | private JLabel lPredefinidas = null; |
||
91 | |||
92 | |||
93 | /**
|
||
94 | * This method initializes
|
||
95 | *
|
||
96 | */
|
||
97 | public PalettePanel(int w, int h, JFrame mainFrame) { |
||
98 | super();
|
||
99 | this.width = w;
|
||
100 | this.height = h;
|
||
101 | listener = new PalettePanelListener(this); |
||
102 | initialize(); |
||
103 | } |
||
104 | |||
105 | /**
|
||
106 | * This method initializes
|
||
107 | *
|
||
108 | */
|
||
109 | public PalettePanel(int w, int h, String palettesPath) { |
||
110 | //super();
|
||
111 | this.width = w;
|
||
112 | this.height = h;
|
||
113 | this.palettesPath = palettesPath;
|
||
114 | listener = new PalettePanelListener(this); |
||
115 | initialize(); |
||
116 | } |
||
117 | |||
118 | /**
|
||
119 | * This method initializes this
|
||
120 | *
|
||
121 | */
|
||
122 | private void initialize() { |
||
123 | this.add(getPMain(), null); |
||
124 | listener.setControlListeners(); |
||
125 | loadPaletteList(palettesPath); |
||
126 | } |
||
127 | |||
128 | /**
|
||
129 | * This method initializes jPanel
|
||
130 | *
|
||
131 | * @return javax.swing.JPanel
|
||
132 | */
|
||
133 | public JPanel getPMain() { |
||
134 | if (pMain == null) { |
||
135 | GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); |
||
136 | gridBagConstraints4.insets = new java.awt.Insets(0,0,0,0); |
||
137 | gridBagConstraints4.gridy = 1;
|
||
138 | gridBagConstraints4.gridx = 0;
|
||
139 | GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); |
||
140 | gridBagConstraints3.insets = new java.awt.Insets(0,0,0,0); |
||
141 | gridBagConstraints3.gridy = 0;
|
||
142 | gridBagConstraints3.gridx = 0;
|
||
143 | pMain = new JPanel(); |
||
144 | pMain.setLayout(new GridBagLayout()); |
||
145 | pMain.setPreferredSize(new java.awt.Dimension(width, height));
|
||
146 | pMain.add(getPNorth(), gridBagConstraints3); |
||
147 | pMain.add(getPButtons(), gridBagConstraints4); |
||
148 | } |
||
149 | return pMain;
|
||
150 | } |
||
151 | |||
152 | /**
|
||
153 | * This method initializes jPanel1
|
||
154 | *
|
||
155 | * @return javax.swing.JPanel
|
||
156 | */
|
||
157 | public JPanel getPNorth() { |
||
158 | if (pNorth == null) { |
||
159 | GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
||
160 | gridBagConstraints2.insets = new java.awt.Insets(0,0,0,0); |
||
161 | gridBagConstraints2.gridy = 2;
|
||
162 | gridBagConstraints2.gridx = 0;
|
||
163 | GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
||
164 | gridBagConstraints1.insets = new java.awt.Insets(0,0,0,0); |
||
165 | gridBagConstraints1.gridy = 1;
|
||
166 | gridBagConstraints1.gridx = 0;
|
||
167 | GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
||
168 | gridBagConstraints.insets = new java.awt.Insets(0,0,0,0); |
||
169 | gridBagConstraints.gridy = 0;
|
||
170 | gridBagConstraints.gridx = 0;
|
||
171 | pNorth = new JPanel(); |
||
172 | pNorth.setLayout(new GridBagLayout()); |
||
173 | pNorth.setPreferredSize(new java.awt.Dimension(width, height - HBUTTONS));
|
||
174 | pNorth.add(getPSelect(), gridBagConstraints); |
||
175 | pNorth.add(getPTable(), gridBagConstraints1); |
||
176 | pNorth.add(getPOptions(), gridBagConstraints2); |
||
177 | } |
||
178 | return pNorth;
|
||
179 | } |
||
180 | |||
181 | /**
|
||
182 | * This method initializes jPanel2
|
||
183 | *
|
||
184 | * @return javax.swing.JPanel
|
||
185 | */
|
||
186 | private JPanel getPButtons() { |
||
187 | if (pButtons == null) { |
||
188 | FlowLayout flowLayout = new FlowLayout(); |
||
189 | flowLayout.setAlignment(java.awt.FlowLayout.RIGHT); |
||
190 | flowLayout.setVgap(0);
|
||
191 | flowLayout.setHgap(3);
|
||
192 | pButtons = new JPanel(); |
||
193 | pButtons.setPreferredSize(new java.awt.Dimension(width, HBUTTONS));
|
||
194 | pButtons.setLayout(flowLayout); |
||
195 | 10645 | nacho | pButtons.add(getBSaveAs(), null);
|
196 | 8026 | nacho | pButtons.add(getBSave(), null);
|
197 | pButtons.add(getBAccept(), null);
|
||
198 | pButtons.add(getBApply(), null);
|
||
199 | pButtons.add(getBCancel(), null);
|
||
200 | } |
||
201 | return pButtons;
|
||
202 | } |
||
203 | |||
204 | /**
|
||
205 | * This method initializes jPanel3
|
||
206 | *
|
||
207 | * @return javax.swing.JPanel
|
||
208 | */
|
||
209 | public TableContainer getPTable() {
|
||
210 | if (pTable == null) { |
||
211 | pTable = new TableContainer(width -MARGIN, height - HSELECT - HOPTIONS - HBUTTONS - MARGIN, columnNames, columnWidths);
|
||
212 | pTable.setModel("TableButtonModel");
|
||
213 | pTable.initialize(); |
||
214 | } |
||
215 | return pTable;
|
||
216 | } |
||
217 | |||
218 | /**
|
||
219 | * This method initializes jPanel4
|
||
220 | *
|
||
221 | * @return javax.swing.JPanel
|
||
222 | */
|
||
223 | private JPanel getPSelect() { |
||
224 | if (pSelect == null) { |
||
225 | lPredefinidas = new JLabel(); |
||
226 | lPredefinidas.setText(Messages.getText("predefinidas")+":"); |
||
227 | |||
228 | GridBagConstraints gridBagConstraints6 = new GridBagConstraints(); |
||
229 | gridBagConstraints6.fill = java.awt.GridBagConstraints.HORIZONTAL; |
||
230 | gridBagConstraints6.gridx = 1;
|
||
231 | gridBagConstraints6.gridy = 1;
|
||
232 | gridBagConstraints6.weightx = 1.0;
|
||
233 | gridBagConstraints6.insets = new java.awt.Insets(0,0,0,8); |
||
234 | GridBagConstraints gridBagConstraints5 = new GridBagConstraints(); |
||
235 | gridBagConstraints5.insets = new java.awt.Insets(0,5,0,0); |
||
236 | gridBagConstraints5.gridy = 0;
|
||
237 | gridBagConstraints5.gridx = 0;
|
||
238 | GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); |
||
239 | gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL; |
||
240 | gridBagConstraints4.gridx = 1;
|
||
241 | gridBagConstraints4.gridy = 0;
|
||
242 | gridBagConstraints4.insets = new java.awt.Insets(0,0,0,0); |
||
243 | pSelect = new JPanel(); |
||
244 | pSelect.setLayout(new GridBagLayout()); |
||
245 | pSelect.setPreferredSize(new java.awt.Dimension(width, HSELECT));
|
||
246 | pSelect.add(getCActive(), gridBagConstraints5); |
||
247 | pSelect.add(getCbList(), gridBagConstraints6); |
||
248 | pSelect.add(lPredefinidas, gridBagConstraints4); |
||
249 | } |
||
250 | return pSelect;
|
||
251 | } |
||
252 | |||
253 | /**
|
||
254 | * This method initializes jPanel5
|
||
255 | *
|
||
256 | * @return javax.swing.JPanel
|
||
257 | */
|
||
258 | private JPanel getPOptions() { |
||
259 | if (pOptions == null) { |
||
260 | FlowLayout flowLayout1 = new FlowLayout(); |
||
261 | flowLayout1.setAlignment(java.awt.FlowLayout.LEFT); |
||
262 | flowLayout1.setVgap(0);
|
||
263 | flowLayout1.setHgap(3);
|
||
264 | pOptions = new JPanel(); |
||
265 | pOptions.setLayout(flowLayout1); |
||
266 | pOptions.setPreferredSize(new java.awt.Dimension(width, HOPTIONS));
|
||
267 | pOptions.add(getCbInterpolar(), null);
|
||
268 | } |
||
269 | return pOptions;
|
||
270 | } |
||
271 | |||
272 | /**
|
||
273 | * This method initializes bApply
|
||
274 | *
|
||
275 | * @return javax.swing.JButton
|
||
276 | */
|
||
277 | public JButton getBAccept() { |
||
278 | if (bAccept == null) { |
||
279 | bAccept = new JButton(Messages.getText("aceptar")); |
||
280 | } |
||
281 | return bAccept;
|
||
282 | } |
||
283 | |||
284 | /**
|
||
285 | * This method initializes bApply
|
||
286 | *
|
||
287 | * @return javax.swing.JButton
|
||
288 | */
|
||
289 | public JButton getBApply() { |
||
290 | if (bApply == null) { |
||
291 | bApply = new JButton(Messages.getText("aplicar")); |
||
292 | } |
||
293 | return bApply;
|
||
294 | } |
||
295 | |||
296 | /**
|
||
297 | * This method initializes bCancel
|
||
298 | *
|
||
299 | * @return javax.swing.JButton
|
||
300 | */
|
||
301 | public JButton getBCancel() { |
||
302 | if (bCancel == null) { |
||
303 | bCancel = new JButton(Messages.getText("cancelar")); |
||
304 | } |
||
305 | return bCancel;
|
||
306 | } |
||
307 | |||
308 | /**
|
||
309 | * This method initializes bSave
|
||
310 | *
|
||
311 | * @return javax.swing.JButton
|
||
312 | */
|
||
313 | public JButton getBSave() { |
||
314 | if (bSave == null) { |
||
315 | bSave = new JButton(Messages.getText("salvar")); |
||
316 | } |
||
317 | return bSave;
|
||
318 | } |
||
319 | |||
320 | /**
|
||
321 | 10645 | nacho | * This method initializes bSaveAs
|
322 | *
|
||
323 | * @return javax.swing.JButton
|
||
324 | */
|
||
325 | public JButton getBSaveAs() { |
||
326 | if (bSaveAs == null) { |
||
327 | bSaveAs = new JButton(Messages.getText("salvar_como")); |
||
328 | } |
||
329 | return bSaveAs;
|
||
330 | } |
||
331 | |||
332 | /**
|
||
333 | 8026 | nacho | * Asigna el tama?o del componente
|
334 | * @param w Nuevo ancho del componente padre
|
||
335 | * @param h Nuevo alto del componente padre
|
||
336 | */
|
||
337 | public void setComponentSize(int w, int h){ |
||
338 | this.width = w;
|
||
339 | this.height = h;
|
||
340 | this.setSize(w, h);
|
||
341 | this.setPreferredSize(new java.awt.Dimension(w, h)); |
||
342 | this.getPMain().setPreferredSize(new java.awt.Dimension(w, h)); |
||
343 | this.getPNorth().setPreferredSize(new java.awt.Dimension(w, h - HBUTTONS)); |
||
344 | this.getPSelect().setPreferredSize(new java.awt.Dimension(width, HSELECT)); |
||
345 | this.getPTable().setComponentSize(width -MARGIN, height - HSELECT - HOPTIONS - HBUTTONS - MARGIN);
|
||
346 | this.getPOptions().setPreferredSize(new java.awt.Dimension(width, HOPTIONS)); |
||
347 | this.getPButtons().setPreferredSize(new java.awt.Dimension(width, HBUTTONS)); |
||
348 | } |
||
349 | |||
350 | /**
|
||
351 | * This method initializes jCheckBox
|
||
352 | *
|
||
353 | * @return javax.swing.JCheckBox
|
||
354 | */
|
||
355 | public JCheckBox getCActive() { |
||
356 | if (cActive == null) { |
||
357 | cActive = new JCheckBox(); |
||
358 | cActive.setText(Messages.getText("activar"));
|
||
359 | cActive.setPreferredSize(new java.awt.Dimension(180,23)); |
||
360 | cActive.addActionListener(listener); |
||
361 | cActive.setSelected(true);
|
||
362 | } |
||
363 | return cActive;
|
||
364 | } |
||
365 | |||
366 | /**
|
||
367 | * This method initializes jComboBox
|
||
368 | *
|
||
369 | * @return javax.swing.JComboBox
|
||
370 | */
|
||
371 | public JComboBox getCbList() { |
||
372 | if (cbList == null) { |
||
373 | cbList = new JComboBox(); |
||
374 | } |
||
375 | return cbList;
|
||
376 | } |
||
377 | |||
378 | /**
|
||
379 | * This method initializes jCheckBox
|
||
380 | *
|
||
381 | * @return javax.swing.JCheckBox
|
||
382 | */
|
||
383 | public JCheckBox getCbInterpolar() { |
||
384 | if (cbInterpolar == null) { |
||
385 | cbInterpolar = new JCheckBox(); |
||
386 | cbInterpolar.setText(Messages.getText("interpolar"));
|
||
387 | } |
||
388 | return cbInterpolar;
|
||
389 | } |
||
390 | |||
391 | //----------------------------------------------------------------
|
||
392 | //M?TODOS DEL CONTROL
|
||
393 | |||
394 | /**
|
||
395 | * Convierte la tabla en un array de objetos para poder crear con el el objeto Palette
|
||
396 | * @return
|
||
397 | * @throws NotInitializeException
|
||
398 | */
|
||
399 | public Object[][] getTablePalette() throws NotInitializeException{ |
||
400 | int cols = 6; |
||
401 | if(getPTable().getRowCount() <= 0) |
||
402 | return null; |
||
403 | Object[][] pal = new Object[getPTable().getRowCount()][cols]; |
||
404 | TableButtonModel model = (TableButtonModel)getPTable().getModel(); |
||
405 | for(int iRow = 0; iRow < getPTable().getRowCount(); iRow ++){ |
||
406 | pal[iRow][0] = model.getValueAt(iRow, 1); |
||
407 | String[] rgb = ((String)model.getValueAt(iRow, 2)).split(","); |
||
408 | pal[iRow][1] = Integer.valueOf(rgb[0]); |
||
409 | pal[iRow][2] = Integer.valueOf(rgb[1]); |
||
410 | pal[iRow][3] = Integer.valueOf(rgb[2]); |
||
411 | pal[iRow][4] = Double.valueOf((String)model.getValueAt(iRow, 3)); |
||
412 | pal[iRow][5] = Integer.valueOf((String)model.getValueAt(iRow, 4)); |
||
413 | } |
||
414 | return pal;
|
||
415 | } |
||
416 | |||
417 | /**
|
||
418 | * Carga la tabla de datos desde un objeto Palette.
|
||
419 | * @param pal
|
||
420 | */
|
||
421 | public void loadPanelFromPalette(Palette pal){ |
||
422 | deleteAllRows(); |
||
423 | int[] paleta = pal.getPalette(); |
||
424 | String[] names = pal.getNameClass(); |
||
425 | |||
426 | //Obtenemos los datos de la clase paleta para construir cada fila de la tabla
|
||
427 | for(int iEntry = 0; iEntry < paleta.length; iEntry ++){ |
||
428 | int[] argb = Utilities.getARGBFromIntToIntArray(paleta[iEntry]); |
||
429 | Color color = new Color(argb[0], argb[1], argb[2]); |
||
430 | |||
431 | if( pal.getType() == RasterBuf.TYPE_BYTE |
|
||
432 | pal.getType() == RasterBuf.TYPE_SHORT | |
||
433 | pal.getType() == RasterBuf.TYPE_INT){ |
||
434 | int[] range = pal.getIntRange(); |
||
435 | addRowToTable(color, names[iEntry], range[iEntry]+"", String.valueOf(argb[3])); |
||
436 | }else if( pal.getType() == RasterBuf.TYPE_FLOAT | |
||
437 | pal.getType() == RasterBuf.TYPE_DOUBLE ){ |
||
438 | double[] range = pal.getDoubleRange(); |
||
439 | addRowToTable(color, names[iEntry], range[iEntry]+"", String.valueOf(argb[3])); |
||
440 | } |
||
441 | } |
||
442 | } |
||
443 | |||
444 | /**
|
||
445 | * A?ade una fila a la tabla asignando el color por par?metro. Este
|
||
446 | * color asignado ser? el que aparezca en el bot?n y en el texto RGB
|
||
447 | * @param color
|
||
448 | */
|
||
449 | public void addRowToTable(Color color, String name, String range, String alpha){ |
||
450 | getBSave().setEnabled(true);
|
||
451 | 10645 | nacho | getBSaveAs().setEnabled(true);
|
452 | 8026 | nacho | |
453 | try {
|
||
454 | getPTable().addRow(new Object[]{color, name, color.getRed()+","+color.getGreen()+","+color.getBlue(), range, alpha}); |
||
455 | } catch (NotInitializeException e1) {
|
||
456 | return;
|
||
457 | } |
||
458 | |||
459 | } |
||
460 | |||
461 | /**
|
||
462 | * Asigna valores a una fila de la tabla
|
||
463 | * @param row fila de la tabla a asignar valores
|
||
464 | * @param color Color del bot?n y RGB
|
||
465 | * @param name Nombre de la clase
|
||
466 | * @param range Valor de la entrada
|
||
467 | * @param alpha Valor de la transparencia
|
||
468 | */
|
||
469 | public void setValues(int row, Color color, String name, String range, String alpha){ |
||
470 | TableButtonModel model = (TableButtonModel)getPTable().getModel(); |
||
471 | |||
472 | model.setValueAt(color, row, 0);
|
||
473 | model.setValueAt(name, row, 1);
|
||
474 | model.setValueAt(color.getRed()+","+color.getGreen()+","+color.getBlue(), row, 2); |
||
475 | model.setValueAt(range, row, 3);
|
||
476 | model.setValueAt(alpha, row, 4);
|
||
477 | } |
||
478 | |||
479 | /**
|
||
480 | * Elimina la fila seleccionada de la tabla
|
||
481 | */
|
||
482 | public void deleteSelectedRow(){ |
||
483 | int index = 0; |
||
484 | try {
|
||
485 | index = getPTable().getSelectedIndex(); |
||
486 | getPTable().delRow(index); |
||
487 | 10645 | nacho | if(getPTable().getRowCount() == 0){ |
488 | 8026 | nacho | getBSave().setEnabled(false);
|
489 | 10645 | nacho | getBSaveAs().setEnabled(false);
|
490 | } |
||
491 | 8026 | nacho | } catch (NotInitializeException e1) {
|
492 | return;
|
||
493 | } |
||
494 | } |
||
495 | |||
496 | /**
|
||
497 | * Elimina todas las filas de la tabla
|
||
498 | */
|
||
499 | public void deleteAllRows(){ |
||
500 | try {
|
||
501 | getPTable().removeAllRows(); |
||
502 | } catch (NotInitializeException e1) {
|
||
503 | return;
|
||
504 | } |
||
505 | getBSave().setEnabled(false);
|
||
506 | 10645 | nacho | getBSaveAs().setEnabled(false);
|
507 | 8026 | nacho | } |
508 | |||
509 | /**
|
||
510 | * Obtiene el indice de la fila seleccionada
|
||
511 | * @return Indice de la fila
|
||
512 | */
|
||
513 | public int getSelectedIndex(){ |
||
514 | try {
|
||
515 | return getPTable().getSelectedIndex();
|
||
516 | } catch (NotInitializeException e) {
|
||
517 | return -1; |
||
518 | } |
||
519 | } |
||
520 | |||
521 | /**
|
||
522 | * Obtiene el valor del alpha de la fila seleccionada en la tabla
|
||
523 | * @return Entero que representa el alpha
|
||
524 | */
|
||
525 | public int getAlpha(){ |
||
526 | TableButtonModel model = (TableButtonModel)getPTable().getModel(); |
||
527 | String value;
|
||
528 | try {
|
||
529 | value = ((JButton)model.getValueAt(getPTable().getSelectedIndex(), 4)).getText(); |
||
530 | return Integer.valueOf(value).intValue(); |
||
531 | } catch (NotInitializeException e) {
|
||
532 | return -1; |
||
533 | } catch (NumberFormatException e) { |
||
534 | return -1; |
||
535 | } |
||
536 | } |
||
537 | |||
538 | /**
|
||
539 | * Ordena las filas de la paleta de color con valores decrecientes de rango. En las primeras
|
||
540 | * filas coloca los valores mayores y en la ?ltima los menores. Esta funci?n es utilizada
|
||
541 | * cada vez que se da aplicar o aceptar ya que es necesario cuando se pasa el filtro que los
|
||
542 | * valores esten ordenados.
|
||
543 | */
|
||
544 | public void orderRows(){ |
||
545 | class Row{ |
||
546 | public String name = null; |
||
547 | public Color rgb = null; |
||
548 | public String value = null; |
||
549 | public String alpha = null; |
||
550 | } |
||
551 | |||
552 | try {
|
||
553 | Row row = null;
|
||
554 | Hashtable map = new Hashtable(); |
||
555 | TableButtonModel model = (TableButtonModel)getPTable().getModel(); |
||
556 | |||
557 | //Comprobamos que el tipo de datos sea igual para todas las filas.
|
||
558 | boolean intType = true; |
||
559 | for(int iRow = 0; iRow < getPTable().getRowCount(); iRow ++){ |
||
560 | try{
|
||
561 | Integer.valueOf((String)model.getValueAt(iRow, 3)); |
||
562 | }catch(NumberFormatException exc){ |
||
563 | //Si alguno de los datos da un formatException es que no es entero por lo
|
||
564 | //que suponemos que es decimal. Ojo!, ya se ha validado que sea numerico
|
||
565 | intType = false;
|
||
566 | } |
||
567 | } |
||
568 | |||
569 | double[] doubleRange = null; |
||
570 | int[] intRange = null; |
||
571 | if(intType)
|
||
572 | intRange = new int[getPTable().getRowCount()]; |
||
573 | else
|
||
574 | doubleRange = new double[getPTable().getRowCount()]; |
||
575 | |||
576 | int lenght = getPTable().getRowCount();
|
||
577 | |||
578 | //Cargamos las filas en un treemap cuya clave es el rango
|
||
579 | for(int iRow = 0; iRow < lenght; iRow ++){ |
||
580 | row = new Row();
|
||
581 | row.name = (String)model.getValueAt(iRow, 1); |
||
582 | row.rgb = (Color)model.getValueAt(iRow, 0); |
||
583 | row.value = (String)model.getValueAt(iRow, 3); |
||
584 | row.alpha = (String)model.getValueAt(iRow, 4); |
||
585 | if(intType){
|
||
586 | intRange[iRow] = Integer.valueOf(row.value).intValue();
|
||
587 | map.put(intRange[iRow]+" ", row);
|
||
588 | }else{
|
||
589 | doubleRange[iRow] = Double.valueOf(row.value).intValue();
|
||
590 | map.put(doubleRange[iRow]+" ", row);
|
||
591 | } |
||
592 | } |
||
593 | |||
594 | //Ordenamos la lista por el valor del rango
|
||
595 | if(intType)
|
||
596 | Arrays.sort(intRange);
|
||
597 | else
|
||
598 | Arrays.sort(doubleRange);
|
||
599 | |||
600 | int rowAssigned = lenght - 1; |
||
601 | for(int iRow = 0; iRow < lenght; iRow ++){ |
||
602 | if(intType)
|
||
603 | row = (Row)map.get(intRange[iRow]+" ");
|
||
604 | else
|
||
605 | row = (Row)map.get(doubleRange[iRow]+" ");
|
||
606 | setValues(rowAssigned, row.rgb, row.name, row.value, row.alpha); |
||
607 | rowAssigned --; |
||
608 | } |
||
609 | } catch (NotInitializeException e) {
|
||
610 | // TODO Auto-generated catch block
|
||
611 | e.printStackTrace(); |
||
612 | } |
||
613 | } |
||
614 | |||
615 | /**
|
||
616 | * Carga en el listBox de paletas predefinidas los nombres que hay en la cabecera del fichero
|
||
617 | * de paletas predefinidas.
|
||
618 | *
|
||
619 | * @param palettesPath Camino al ficher de paletas predefinidas.
|
||
620 | *
|
||
621 | */
|
||
622 | public void loadPaletteList(String palettesPath){ |
||
623 | |||
624 | File palettesFile = new File(palettesPath); |
||
625 | if(!palettesFile.exists())
|
||
626 | return;
|
||
627 | |||
628 | ArrayList nameList = new ArrayList(); |
||
629 | |||
630 | try {
|
||
631 | KXmlParser parser = new KXmlParser();
|
||
632 | FileInputStream fileInputStream = new FileInputStream(palettesPath); |
||
633 | parser.setInput(fileInputStream, null);
|
||
634 | int tag = parser.nextTag();
|
||
635 | parser.require(KXmlParser.START_TAG, null, "palettes"); |
||
636 | tag = parser.nextTag(); |
||
637 | parser.require(KXmlParser.START_TAG, null, "palette_list"); |
||
638 | |||
639 | tag = parser.nextTag(); |
||
640 | while (tag == KXmlParser.START_TAG){
|
||
641 | parser.require(KXmlParser.START_TAG, null, "name"); |
||
642 | nameList.add(parser.nextText()); |
||
643 | parser.require(KXmlParser.END_TAG, null, "name"); |
||
644 | tag = parser.nextTag(); |
||
645 | } |
||
646 | parser.require(KXmlParser.END_TAG, null, "palette_list"); |
||
647 | |||
648 | getCbList().removeAllItems(); //
|
||
649 | for (int i = 0; i<nameList.size(); i++) // Solo se a?aden elementos al combo si se ha leido la lista |
||
650 | getCbList().addItem(nameList.get(i)); // completa de nombres de paleta sin errores.
|
||
651 | fileInputStream.close(); |
||
652 | return;
|
||
653 | |||
654 | |||
655 | }catch (FileNotFoundException fnfEx) { |
||
656 | fnfEx.printStackTrace(); |
||
657 | }catch (XmlPullParserException xmlEx) {
|
||
658 | System.out.println("El fichero de paletas predeterminadas no tiene la estructura correcta:\n " + xmlEx.getMessage()); |
||
659 | }catch (IOException e) { |
||
660 | } |
||
661 | } |
||
662 | |||
663 | public String getPalettesPath() { |
||
664 | return palettesPath;
|
||
665 | } |
||
666 | |||
667 | /**
|
||
668 | * Obtiene el listener
|
||
669 | * @return PalettePanelListener
|
||
670 | */
|
||
671 | public PalettePanelListener getListener() {
|
||
672 | return listener;
|
||
673 | } |
||
674 | |||
675 | /**
|
||
676 | * Activar o desactivar los componentes del panel
|
||
677 | */
|
||
678 | public void setEnabled(boolean enabled){ |
||
679 | getCbList().setEnabled(enabled); |
||
680 | getCbInterpolar().setEnabled(enabled); |
||
681 | getPTable().setEnabled(enabled); |
||
682 | } |
||
683 | |||
684 | /**
|
||
685 | * Funci?n que informa de si est? acivo o no la selecci?n de paleta en el panel
|
||
686 | * @return true si est? activo y false si no lo est?.
|
||
687 | */
|
||
688 | public boolean isActivePanel(){ |
||
689 | return this.getCActive().isSelected(); |
||
690 | } |
||
691 | } // @jve:decl-index=0:visual-constraint="10,10"
|