root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / colortable / ui / ColorTablePanel.java @ 22364
History | View | Annotate | Download (13.2 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 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.gvsig.rastertools.colortable.ui; |
20 |
|
21 |
import java.awt.BorderLayout; |
22 |
import java.io.IOException; |
23 |
import java.util.ArrayList; |
24 |
|
25 |
import javax.swing.JPanel; |
26 |
import javax.swing.event.ChangeEvent; |
27 |
import javax.swing.event.ChangeListener; |
28 |
|
29 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
30 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
31 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent; |
32 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener; |
33 |
import org.gvsig.raster.beans.previewbase.PreviewBasePanel; |
34 |
import org.gvsig.raster.dataset.RasterDataset; |
35 |
import org.gvsig.raster.datastruct.ColorTable; |
36 |
import org.gvsig.raster.grid.GridPalette; |
37 |
import org.gvsig.raster.grid.GridTransparency; |
38 |
import org.gvsig.raster.grid.filter.RasterFilterList; |
39 |
import org.gvsig.raster.grid.filter.bands.ColorTableFilter; |
40 |
import org.gvsig.raster.grid.render.Rendering; |
41 |
import org.gvsig.raster.util.RasterToolsUtil; |
42 |
import org.gvsig.rastertools.colortable.data.ColorTableData; |
43 |
import org.gvsig.rastertools.colortable.ui.library.ColorTableLibraryEvent; |
44 |
import org.gvsig.rastertools.colortable.ui.library.ColorTableLibraryListener; |
45 |
import org.gvsig.rastertools.colortable.ui.library.ColorTableLibraryPanel; |
46 |
import org.gvsig.rastertools.colortable.ui.tabs.IColorTableUI; |
47 |
import org.gvsig.rastertools.colortable.ui.tabs.TabInterpolated; |
48 |
import org.gvsig.rastertools.colortable.ui.tabs.TabTable; |
49 |
|
50 |
import com.iver.andami.PluginServices; |
51 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
52 |
/**
|
53 |
* ColorTablePanel es el panel padre que contendra todos los paneles del interfaz
|
54 |
* de tablas de color.
|
55 |
*
|
56 |
* @version 26/06/2007
|
57 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
58 |
*/
|
59 |
public class ColorTablePanel extends JPanel implements ChangeListener, ButtonsPanelListener, ColorTableLibraryListener { |
60 |
private static final long serialVersionUID = 6028780107787443656L; |
61 |
|
62 |
private ColorTableListener colorTableListener = null; |
63 |
private FLayer fLayer = null; |
64 |
private ArrayList filterStatus = null; |
65 |
private ColorTableData colorTableData = null; |
66 |
|
67 |
private IColorTableUI tabTable = null; |
68 |
private IColorTableUI tabInterpolated = null; |
69 |
|
70 |
private ColorTableLibraryPanel colorTableLibraryPanel = null; |
71 |
|
72 |
private JPanel jPanelListView = null; |
73 |
private ColorTableGlobalPanel panelGeneral = null; |
74 |
private ColorTableDialog colorTableDialog = null; |
75 |
private PreviewBasePanel previewBasePanel = null; |
76 |
|
77 |
/**
|
78 |
* Controla si se ha presionado el boton aceptar para el cerrado de la ventana
|
79 |
*/
|
80 |
boolean accepted = false; |
81 |
|
82 |
/**
|
83 |
* Construir un nuevo ColorTablePanel
|
84 |
*/
|
85 |
public ColorTablePanel(FLayer layer, ColorTableDialog colorTableDialog) {
|
86 |
this.colorTableDialog = colorTableDialog;
|
87 |
fLayer = layer; |
88 |
initialize(); |
89 |
setLayer(layer); |
90 |
} |
91 |
|
92 |
public PreviewBasePanel getPreviewBasePanel() {
|
93 |
if (previewBasePanel == null) { |
94 |
ArrayList list = new ArrayList(); |
95 |
list.add(getTabTable()); |
96 |
list.add(getTabInterpolated()); |
97 |
getColorTableListener().setLastColorTableUI(getTabTable()); |
98 |
|
99 |
previewBasePanel = new PreviewBasePanel(ButtonsPanel.BUTTONS_NONE, list, (JPanel) getGeneralPanel(), getPanelListView(), getColorTableListener(), (FLyrRasterSE) fLayer); |
100 |
previewBasePanel.getTabbedPane().addChangeListener(this);
|
101 |
previewBasePanel.getButtonsPanel().addButton(RasterToolsUtil.getText(this, "equidistar"), ButtonsPanel.BUTTON_LAST + 3); |
102 |
previewBasePanel.getButtonsPanel().addButton(RasterToolsUtil.getText(this, "guardar_predeterminado"), ButtonsPanel.BUTTON_LAST + 2); |
103 |
previewBasePanel.getButtonsPanel().addApply(); |
104 |
previewBasePanel.getButtonsPanel().addAccept(); |
105 |
previewBasePanel.getButtonsPanel().addCancel(); |
106 |
previewBasePanel.addButtonPressedListener(this);
|
107 |
} |
108 |
return previewBasePanel;
|
109 |
} |
110 |
|
111 |
|
112 |
private void initialize() { |
113 |
setLayout(new BorderLayout()); |
114 |
add(getPreviewBasePanel(), BorderLayout.CENTER);
|
115 |
} |
116 |
|
117 |
private IColorTableUI getTabTable() {
|
118 |
if (tabTable == null) { |
119 |
tabTable = new TabTable();
|
120 |
tabTable.addColorTableUIChangedListener(getColorTableListener()); |
121 |
} |
122 |
return tabTable;
|
123 |
} |
124 |
|
125 |
|
126 |
private IColorTableUI getTabInterpolated() {
|
127 |
if (tabInterpolated == null) { |
128 |
tabInterpolated = new TabInterpolated();
|
129 |
tabInterpolated.addColorTableUIChangedListener(getColorTableListener()); |
130 |
} |
131 |
return tabInterpolated;
|
132 |
} |
133 |
|
134 |
public JPanel getPanelListView() { |
135 |
if (jPanelListView == null) { |
136 |
jPanelListView = new JPanel(); |
137 |
jPanelListView.setBorder(javax.swing.BorderFactory.createTitledBorder(null, RasterToolsUtil.getText(this, "libreria"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
138 |
jPanelListView.setLayout(new BorderLayout()); |
139 |
|
140 |
jPanelListView.add(getColorTableLibraryPanel()); |
141 |
} |
142 |
return jPanelListView;
|
143 |
} |
144 |
|
145 |
public ColorTableLibraryPanel getColorTableLibraryPanel() {
|
146 |
if (colorTableLibraryPanel == null) { |
147 |
colorTableLibraryPanel = new ColorTableLibraryPanel();
|
148 |
colorTableLibraryPanel.addColorTableLibraryListener(this);
|
149 |
colorTableLibraryPanel.selectDefault(); |
150 |
} |
151 |
return colorTableLibraryPanel;
|
152 |
} |
153 |
|
154 |
/**
|
155 |
* Obtener el componente que hara la carga pesada de los listeners
|
156 |
* @return
|
157 |
*/
|
158 |
private ColorTableListener getColorTableListener() {
|
159 |
if (colorTableListener == null) { |
160 |
colorTableListener = new ColorTableListener(this, getColorTableData()); |
161 |
} |
162 |
return colorTableListener;
|
163 |
} |
164 |
|
165 |
public ColorTableGlobalPanel getGeneralPanel() {
|
166 |
if (panelGeneral == null) { |
167 |
panelGeneral = new ColorTableGlobalPanel(getColorTableData());
|
168 |
} |
169 |
return panelGeneral;
|
170 |
} |
171 |
|
172 |
/**
|
173 |
* Volvemos todo a la normalidad cuando se cancela
|
174 |
*/
|
175 |
private boolean restoreFilters() { |
176 |
if (getLayer() != null) { |
177 |
Rendering rendering = ((FLyrRasterSE) getLayer()).getRender(); |
178 |
if(rendering.getFilterList() == null) |
179 |
return false; |
180 |
rendering.getFilterList().setStatus(getFilterStatus()); |
181 |
((FLyrRasterSE) getLayer()).setRenderFilterList(rendering.getFilterList()); |
182 |
} |
183 |
return true; |
184 |
} |
185 |
|
186 |
/**
|
187 |
* Devuelve el arrayList de filtros inicial
|
188 |
* @return
|
189 |
*/
|
190 |
private ArrayList getFilterStatus() { |
191 |
return filterStatus;
|
192 |
} |
193 |
|
194 |
/**
|
195 |
* Carga la tabla de color que hay seleccionada en el componente
|
196 |
* ListViewComponent
|
197 |
*/
|
198 |
public void reloadPanelsFromLibraryPanel() { |
199 |
ColorTable colorTable = getColorTableLibraryPanel().getColorTableSelected(); |
200 |
|
201 |
getColorTableData().setColorTable((ColorTable) colorTable.clone()); |
202 |
if (colorTableData.isLimitsEnabled()) {
|
203 |
double min = getColorTableData().getMinim();
|
204 |
double max = getColorTableData().getMaxim();
|
205 |
getColorTableData().getColorTable().createColorTableInRange(min, max, false);
|
206 |
} |
207 |
getColorTableListener().refreshItems(true);
|
208 |
getColorTableData().refreshPreview(); |
209 |
} |
210 |
|
211 |
/**
|
212 |
* Salva la tabla de color al fichero rmf.
|
213 |
* @param fName
|
214 |
* @throws IOException
|
215 |
*/
|
216 |
private void saveColorTable() { |
217 |
RasterDataset dataset = ((FLyrRasterSE) getLayer()).getDataSource().getDataset(0)[0]; |
218 |
if (getColorTableData().isEnabled())
|
219 |
dataset.saveObjectToRmf(ColorTable.class, getColorTableData().getColorTable()); |
220 |
else
|
221 |
dataset.saveObjectToRmf(ColorTable.class, null);
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* Restaura el estado de los filtros como estaban al abrir la ventana y quita
|
226 |
* la leyenda del TOC
|
227 |
*/
|
228 |
private void restoreSettings() { |
229 |
if (getLayer() == null) |
230 |
return;
|
231 |
|
232 |
// Restauramos la vista con los filtros originales antes de cualquier acci?n
|
233 |
if(!restoreFilters())
|
234 |
return;
|
235 |
|
236 |
((FLyrRasterSE) getLayer()).setLastLegend(null);
|
237 |
|
238 |
RasterFilterList rasterFilterList = ((FLyrRasterSE) getLayer()).getRenderFilterList(); |
239 |
|
240 |
ColorTableFilter colorTableFilter = (ColorTableFilter) rasterFilterList.getByName(ColorTableFilter.names[0]);
|
241 |
if (colorTableFilter != null) { |
242 |
GridPalette gridPalette = new GridPalette((ColorTable) colorTableFilter.getColorTable().clone());
|
243 |
((FLyrRasterSE) getLayer()).setLastLegend(gridPalette); |
244 |
} |
245 |
|
246 |
// Quitamos la banda de transparencia por si el filtro de color la ha activado
|
247 |
try {
|
248 |
GridTransparency transparency = ((FLyrRasterSE) getLayer()).getRender().getLastTransparency(); |
249 |
transparency.setTransparencyBand(-1);
|
250 |
transparency.activeTransparency(); |
251 |
transparency.setAlphaBand(null);
|
252 |
} catch (NullPointerException ex) { |
253 |
//No quitamos la banda de transparencia
|
254 |
} |
255 |
} |
256 |
|
257 |
/**
|
258 |
* Devuelve el componente que trata los datos
|
259 |
* @return
|
260 |
*/
|
261 |
private ColorTableData getColorTableData() {
|
262 |
if (colorTableData == null) { |
263 |
colorTableData = new ColorTableData();
|
264 |
} |
265 |
return colorTableData;
|
266 |
} |
267 |
|
268 |
/**
|
269 |
* Especificar el layer para la tabla de color
|
270 |
* @param layer
|
271 |
*/
|
272 |
private void setLayer(FLayer fLayer) { |
273 |
this.fLayer = fLayer;
|
274 |
|
275 |
getGeneralPanel().setLayer(fLayer); |
276 |
|
277 |
getColorTableListener().setLayer(fLayer); |
278 |
|
279 |
RasterFilterList rasterFilterList = ((FLyrRasterSE) getLayer()).getRenderFilterList(); |
280 |
|
281 |
filterStatus = rasterFilterList.getStatusCloned(); |
282 |
|
283 |
ColorTableFilter colorTableFilter = (ColorTableFilter) rasterFilterList.getByName(ColorTableFilter.names[0]);
|
284 |
if (colorTableFilter != null) { |
285 |
GridPalette gridPalette = new GridPalette((ColorTable) colorTableFilter.getColorTable().clone());
|
286 |
if (gridPalette.isCompressible()) {
|
287 |
if (RasterToolsUtil.messageBoxYesOrNot("comprimir_paleta", this)) { |
288 |
gridPalette.compressPalette(); |
289 |
gridPalette.setInterpolated(true);
|
290 |
} |
291 |
} |
292 |
|
293 |
ColorTable colorTable = new ColorTable();
|
294 |
colorTable.setName(RasterToolsUtil.getText(this, "tabla_actual")); |
295 |
colorTable.createPaletteFromColorItems(gridPalette.getColorItems(), false);
|
296 |
colorTable.setInterpolated(gridPalette.isInterpolated()); |
297 |
getGeneralPanel().setCheckBoxInterpolated(gridPalette.isInterpolated()); |
298 |
|
299 |
getColorTableLibraryPanel().addColorTable(0, colorTable);
|
300 |
|
301 |
getGeneralPanel().setLimitsEnabled(false);
|
302 |
getGeneralPanel().setCheckBoxEnabled(true);
|
303 |
} else {
|
304 |
getColorTableLibraryPanel().selectDefault(); |
305 |
|
306 |
getGeneralPanel().setLimitsEnabled(true);
|
307 |
getGeneralPanel().setCheckBoxEnabled(false);
|
308 |
} |
309 |
reloadPanelsFromLibraryPanel(); |
310 |
} |
311 |
|
312 |
/**
|
313 |
* Especificar el layer para el recorte
|
314 |
* @param layer
|
315 |
*/
|
316 |
public FLayer getLayer() {
|
317 |
return fLayer;
|
318 |
} |
319 |
|
320 |
/**
|
321 |
* Cuando se aceptan los cambios en el panel ejecutaremos el aceptar del
|
322 |
* listener
|
323 |
*/
|
324 |
private void accept() { |
325 |
getColorTableListener().accept(); |
326 |
} |
327 |
|
328 |
/**
|
329 |
* Volvemos todo a la normalidad cuando se cancela
|
330 |
*/
|
331 |
private void cancel() { |
332 |
getColorTableListener().closePreviewLayer(); |
333 |
getLayer().getMapContext().invalidate(); |
334 |
} |
335 |
|
336 |
/**
|
337 |
* Se dispara cuando se selecciona una pesta?a en el panel
|
338 |
*/
|
339 |
public void stateChanged(ChangeEvent e) { |
340 |
switch (getPreviewBasePanel().getTabbedPane().getSelectedIndex()) {
|
341 |
case 1: |
342 |
getColorTableListener().setLastColorTableUI(getTabInterpolated()); |
343 |
break;
|
344 |
default:
|
345 |
getColorTableListener().setLastColorTableUI(getTabTable()); |
346 |
break;
|
347 |
} |
348 |
|
349 |
getColorTableListener().refreshItems(true);
|
350 |
} |
351 |
|
352 |
/*
|
353 |
* (non-Javadoc)
|
354 |
* @see org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener#actionButtonPressed(org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent)
|
355 |
*/
|
356 |
public void actionButtonPressed(ButtonsPanelEvent e) { |
357 |
restoreSettings(); |
358 |
|
359 |
// Al pulsar Aceptar o Aplicar se ejecuta el aceptar del panel
|
360 |
if (e.getButton() == ButtonsPanel.BUTTON_APPLY || e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
|
361 |
accept(); |
362 |
} |
363 |
|
364 |
// Al pulsar Cancelar la ventana se cierra y se refresca la vista
|
365 |
if (e.getButton() == ButtonsPanel.BUTTON_CANCEL) {
|
366 |
cancel(); |
367 |
close(); |
368 |
} |
369 |
|
370 |
// Al pulsar Aceptar simplemente la ventana se cierra
|
371 |
if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
|
372 |
accepted = true;
|
373 |
close(); |
374 |
} |
375 |
|
376 |
if (e.getButton() == (ButtonsPanel.BUTTON_LAST + 2)) { |
377 |
if (RasterToolsUtil.messageBoxYesOrNot("guardar_como_predeterminado", this)) { |
378 |
saveColorTable(); |
379 |
accept(); |
380 |
} |
381 |
} |
382 |
|
383 |
if (e.getButton() == (ButtonsPanel.BUTTON_LAST + 3)) { |
384 |
getColorTableListener().equidistar(); |
385 |
} |
386 |
} |
387 |
|
388 |
/**
|
389 |
* Acciones a ejecutar cuando se cancela
|
390 |
*/
|
391 |
private void close() { |
392 |
if (colorTableDialog == null) |
393 |
return;
|
394 |
try {
|
395 |
PluginServices.getMDIManager().closeWindow(colorTableDialog); |
396 |
} catch (ArrayIndexOutOfBoundsException e) { |
397 |
// Si la ventana no se puede eliminar no hacemos nada
|
398 |
} |
399 |
} |
400 |
|
401 |
public void windowClosed() { |
402 |
if (!accepted) {
|
403 |
restoreSettings(); |
404 |
cancel(); |
405 |
} |
406 |
} |
407 |
|
408 |
/*
|
409 |
* (non-Javadoc)
|
410 |
* @see org.gvsig.rastertools.colortable.library.ColorTableLibraryListener#actionColorTableChanged(org.gvsig.rastertools.colortable.library.ColorTableLibraryEvent)
|
411 |
*/
|
412 |
public void actionColorTableChanged(ColorTableLibraryEvent e) { |
413 |
reloadPanelsFromLibraryPanel(); |
414 |
} |
415 |
} |