gvsig-raster / org.gvsig.raster.tools / branches / org.gvsig.raster.tools_dataaccess_refactoring / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / enhanced / ui / PreviewFiltering.java @ 2340
History | View | Annotate | Download (10.1 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.tools.app.basic.tool.enhanced.ui; |
23 |
|
24 |
import java.util.ArrayList; |
25 |
import java.util.List; |
26 |
|
27 |
import org.gvsig.andami.PluginServices; |
28 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
29 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
30 |
import org.gvsig.fmap.dal.coverage.exception.FilterManagerException; |
31 |
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException; |
32 |
import org.gvsig.fmap.dal.coverage.grid.RasterFilter; |
33 |
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList; |
34 |
import org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager; |
35 |
import org.gvsig.gui.beans.imagenavigator.ImageUnavailableException; |
36 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
37 |
import org.gvsig.raster.swing.RasterSwingLibrary; |
38 |
import org.gvsig.raster.swing.preview.DataSourcePreview; |
39 |
import org.gvsig.raster.swing.preview.PreviewRenderProcess; |
40 |
import org.gvsig.raster.tools.app.basic.RasterToolsUtil; |
41 |
import org.gvsig.raster.tools.app.basic.raster.bean.previewbase.ParamStruct; |
42 |
|
43 |
/**
|
44 |
* Procesado de la imagen para la previsualizaci?n. Para poder usar esta clase despu?s de instanciarla
|
45 |
* habr? que asignarle un valor a showFiltersSelected aunque por defecto est? a true por lo que se
|
46 |
* visualizaran los cambios sin problemas, asigna una lista de filtros inicial (que ya tenga el raster
|
47 |
* aplicado) con setFilterStatus y asignar los filtros que queramos aplicar con addNewParam (una llamada
|
48 |
* por cada filtro).
|
49 |
*
|
50 |
* 19/02/2008
|
51 |
* @author Nacho Brodin nachobrodin@gmail.com
|
52 |
*/
|
53 |
public class PreviewFiltering implements PreviewRenderProcess { |
54 |
|
55 |
private boolean showFiltersSelected = true; |
56 |
private List<ParamStruct> paramsList = new ArrayList<ParamStruct>(); |
57 |
private List<RasterFilter> filtersInit = new ArrayList<RasterFilter>(); |
58 |
private boolean showPreview = true; |
59 |
private boolean saveLayerFilters = true; |
60 |
|
61 |
public PreviewFiltering(boolean saveLayerFilters) { |
62 |
this.saveLayerFilters = saveLayerFilters;
|
63 |
} |
64 |
|
65 |
/**
|
66 |
* Flag de selecci?n de activaci?n y desactivaci?n
|
67 |
* @param show
|
68 |
*/
|
69 |
public void showFiltersSelected(boolean show) { |
70 |
this.showFiltersSelected = show;
|
71 |
} |
72 |
|
73 |
/**
|
74 |
* Obtiene la lista de par?metros
|
75 |
* @return the paramsList
|
76 |
*/
|
77 |
public List<ParamStruct> getParamsList() { |
78 |
return paramsList;
|
79 |
} |
80 |
|
81 |
/**
|
82 |
* Asigna la lista de par?metros
|
83 |
* @param params
|
84 |
*/
|
85 |
public void setParamsList(List<ParamStruct> params) { |
86 |
this.paramsList = params;
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* Asigna el arrayList de filtros inicial. Esto hace que aplique los filtros que ya
|
91 |
* existen en el raster. Estos pueden ser obtenidos del render de la capa de la forma
|
92 |
* lyr.getRender().getFilterList().getStatusCloned().
|
93 |
* @param params Lista de filtros aplicados.
|
94 |
*/
|
95 |
public void setFilterStatus(List<RasterFilter> filtersInit) { |
96 |
this.filtersInit = filtersInit;
|
97 |
} |
98 |
|
99 |
/**
|
100 |
* Devuelve el arrayList de filtros inicial
|
101 |
* @return
|
102 |
*/
|
103 |
public List<RasterFilter> getFilterStatus() { |
104 |
return filtersInit;
|
105 |
} |
106 |
|
107 |
/**
|
108 |
* A?adir un nuevo Params a la lista de Params que podemos manejar. Un Params
|
109 |
* equivale a un filtro cargado. El hecho de trabajar con Params y no con
|
110 |
* filtros, simplifica totalmente el panel. Sin tener que depender de los
|
111 |
* filtros nada m?s que para el momento de dibujado o guardado.
|
112 |
* @param name
|
113 |
* @param params
|
114 |
* @param classFilter
|
115 |
*/
|
116 |
public void addNewParam(String name, Params params, Class<?> classFilter) { |
117 |
ParamStruct param = new ParamStruct();
|
118 |
param.setFilterName(name); |
119 |
param.setFilterParam(params); |
120 |
param.setFilterClass(classFilter); |
121 |
paramsList.add(param); |
122 |
} |
123 |
|
124 |
/**
|
125 |
* Procesa la imagen con la lista de filtros si el flag showFilterSelected est? a true.
|
126 |
* Esta funci?n llama a addFilter por cada filtro a?adido pero es applyFilters la encargada
|
127 |
* de construir la lista.
|
128 |
*/
|
129 |
public void process(DataSourcePreview rendering) throws ImageUnavailableException { |
130 |
if(!showPreview)
|
131 |
throw new ImageUnavailableException(RasterToolsUtil.getText(this, "panel_preview_not_available")); |
132 |
|
133 |
if(!(rendering.getSource() instanceof FLyrRaster)) |
134 |
return;
|
135 |
FLyrRaster lyr = (FLyrRaster)rendering.getSource(); |
136 |
|
137 |
lyr.getRender().getFilterList().clear(); |
138 |
|
139 |
if (showFiltersSelected) {
|
140 |
RasterFilterList filterList = lyr.getRender().getFilterList(); |
141 |
filterList.setInitDataType(lyr.getDataStore().getDataType()[0]);
|
142 |
|
143 |
List<ParamStruct> listFilterUsed = null; |
144 |
if(saveLayerFilters)
|
145 |
listFilterUsed = applyAllFilters(lyr); |
146 |
else
|
147 |
listFilterUsed = applySelectedFilters(null);
|
148 |
ArrayList<Exception> exc = new ArrayList<Exception>(); |
149 |
for (int i = 0; i < listFilterUsed.size(); i++) { |
150 |
try {
|
151 |
RasterFilterListManager filterManager = filterList.getManagerByFilterClass(((ParamStruct) listFilterUsed.get(i)).getFilterClass()); |
152 |
Class<?> c = ((ParamStruct) listFilterUsed.get(i)).getFilterClass();
|
153 |
Params p = ((ParamStruct) listFilterUsed.get(i)).getFilterParam(); |
154 |
filterManager.addFilter(c, p); |
155 |
} catch (FilterTypeException e) {
|
156 |
exc.add(e); |
157 |
} catch (FilterManagerException e) {
|
158 |
exc.add(e); |
159 |
} |
160 |
} |
161 |
if(exc.size() != 0) { |
162 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, exc); |
163 |
exc.clear(); |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
/**
|
169 |
* Aqui se seleccionan que filtros se van a aplicar y se devuelven en forma
|
170 |
* de ArrayList tanto para el dibujado como cuando aceptan o aplican el panel.
|
171 |
* @param layerRaster
|
172 |
* @return
|
173 |
*/
|
174 |
public List<ParamStruct> applyAllFilters(FLyrRaster layerRaster) { |
175 |
List<ParamStruct> listFilterUsed = new ArrayList<ParamStruct>(); |
176 |
|
177 |
int initDataType = layerRaster.getRender().getFilterList().getInitDataType();
|
178 |
RasterFilterList filterList = RasterLocator.getManager().createEmptyFilterList(initDataType); |
179 |
filterList.setEnv(layerRaster.getRender().getFilterList().getEnv()); |
180 |
filterList.setInitDataType(initDataType); |
181 |
List<Class<?>> registeredFilters = filterList.getRegisteredFilterList(); |
182 |
|
183 |
if(filtersInit == null) |
184 |
return listFilterUsed;
|
185 |
|
186 |
// Conservamos filtros ya existentes
|
187 |
for (int i = 0; i < filtersInit.size(); i++) { |
188 |
|
189 |
RasterFilter obj = null;
|
190 |
for (int j = 0; j < registeredFilters.size(); j++) { |
191 |
Class<?> classFilter = (Class<?>) registeredFilters.get(j); |
192 |
try {
|
193 |
obj = (RasterFilter) classFilter.newInstance(); |
194 |
if (obj.getName().equals(((RasterFilter) filtersInit.get(i)).getName()))
|
195 |
break;
|
196 |
} catch (InstantiationException e) { |
197 |
RasterSwingLibrary.messageBoxError("error_creando_filtro", this, e); |
198 |
} catch (IllegalAccessException e) { |
199 |
RasterSwingLibrary.messageBoxError("error_creando_filtro", this, e); |
200 |
} |
201 |
} |
202 |
|
203 |
// Si no encontramos el filtro apropiado, nos olvidamos de el
|
204 |
if (obj == null) |
205 |
continue;
|
206 |
|
207 |
// Si no es visible tenemos que conservar el filtro
|
208 |
try {
|
209 |
Params params = (Params) ((RasterFilter) filtersInit.get(i)).getUIParams(((RasterFilter) filtersInit.get(i)).getName()).clone(); |
210 |
// A?ado el parametro RenderBands a los parametros del filtro
|
211 |
int[] renderBands = layerRaster.getRender().getRenderColorInterpretation().buildRenderBands(); |
212 |
String rgb = renderBands[0] + " " + renderBands[1] + " " + renderBands[2]; |
213 |
params.setParam("RenderBands", rgb, 0, null); |
214 |
|
215 |
ParamStruct newParam = new ParamStruct();
|
216 |
newParam.setFilterClass(obj.getClass()); |
217 |
newParam.setFilterName(((RasterFilter) filtersInit.get(i)).getName()); |
218 |
newParam.setFilterParam(params); |
219 |
listFilterUsed.add(newParam); |
220 |
} catch (CloneNotSupportedException e) { |
221 |
} |
222 |
} |
223 |
|
224 |
// Metemos los filtros seleccionados en listFilterUsed
|
225 |
listFilterUsed = applySelectedFilters(listFilterUsed); |
226 |
|
227 |
return listFilterUsed;
|
228 |
} |
229 |
|
230 |
/**
|
231 |
* Gets the list of filters selected for the preview (without the layer filters). If a list of filters is
|
232 |
* assigned, this method will replace the filter if exists in the list.
|
233 |
* @param listFilterUsed
|
234 |
* @return
|
235 |
*/
|
236 |
public List<ParamStruct> applySelectedFilters(List<ParamStruct> listFilterUsed) { |
237 |
if(listFilterUsed == null) |
238 |
return getParamStructListCopy();
|
239 |
|
240 |
for (int i = 0; i < paramsList.size(); i++) { |
241 |
// En caso de existir el filtro, lo reemplazamos
|
242 |
boolean finded = false; |
243 |
for (int j = 0; j < listFilterUsed.size(); j++) { |
244 |
if (((ParamStruct) listFilterUsed.get(j)).getFilterName().equals(((ParamStruct) paramsList.get(i)).getFilterName())) {
|
245 |
listFilterUsed.set(j, paramsList.get(i)); |
246 |
finded = true;
|
247 |
break;
|
248 |
} |
249 |
} |
250 |
if (!finded)
|
251 |
listFilterUsed.add(paramsList.get(i)); |
252 |
} |
253 |
|
254 |
return listFilterUsed;
|
255 |
} |
256 |
|
257 |
private List<ParamStruct> getParamStructListCopy() { |
258 |
List<ParamStruct> p = new ArrayList<ParamStruct>(); |
259 |
for (int i = 0; i < paramsList.size(); i++) { |
260 |
p.add(paramsList.get(i).clone()); |
261 |
} |
262 |
return p;
|
263 |
} |
264 |
|
265 |
/**
|
266 |
* Obtiene el flag que informa de si se est? mostrando la previsualizaci?n o no.
|
267 |
* En caso de no mostrarse se lanza una excepci?n ImageUnavailableExcepcion con el
|
268 |
* mensaje "La previsualizaci?n no est? disponible para este panel"
|
269 |
* @return
|
270 |
*/
|
271 |
public boolean isShowPreview() { |
272 |
return showPreview;
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* Asigna el flag para mostrar u ocultar la preview. En caso de no mostrarse se lanza una
|
277 |
* excepci?n ImageUnavailableExcepcion con el mensaje "La previsualizaci?n no est? disponible para
|
278 |
* este panel"
|
279 |
* @param showPreview
|
280 |
*/
|
281 |
public void setShowPreview(boolean showPreview) { |
282 |
this.showPreview = showPreview;
|
283 |
} |
284 |
} |