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 / RasterToolsUtil.java @ 2340
History | View | Annotate | Download (16.5 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; |
23 |
|
24 |
import java.awt.Component; |
25 |
import java.awt.Dimension; |
26 |
import java.awt.Point; |
27 |
import java.io.File; |
28 |
import java.util.ArrayList; |
29 |
|
30 |
import javax.swing.ImageIcon; |
31 |
import javax.swing.JOptionPane; |
32 |
|
33 |
import org.gvsig.andami.IconThemeHelper; |
34 |
import org.gvsig.andami.PluginServices; |
35 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
36 |
import org.gvsig.app.project.Project; |
37 |
import org.gvsig.app.project.ProjectManager; |
38 |
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
39 |
import org.gvsig.fmap.dal.DALLocator; |
40 |
import org.gvsig.fmap.dal.DataManager; |
41 |
import org.gvsig.fmap.dal.DataStore; |
42 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
43 |
import org.gvsig.fmap.dal.coverage.datastruct.Param; |
44 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
45 |
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters; |
46 |
import org.gvsig.fmap.dal.coverage.util.ProviderServices; |
47 |
import org.gvsig.fmap.dal.exception.InitializeException; |
48 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
49 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
50 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
51 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
52 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
53 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
54 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
55 |
import org.gvsig.gui.beans.propertiespanel.PropertiesComponent; |
56 |
import org.gvsig.gui.beans.propertiespanel.PropertyStruct; |
57 |
import org.gvsig.raster.fmap.layers.DefaultFLyrRaster; |
58 |
import org.gvsig.raster.util.RasterNotLoadException; |
59 |
|
60 |
/**
|
61 |
* Herramientas de uso general y que son dependientes de gvSIG, FMap o de
|
62 |
* libUIComponents. En caso de no serlo existe una clase independiente de
|
63 |
* cualquier proyecto dentro de libRaster para este tipo de funciones.
|
64 |
*
|
65 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
66 |
*/
|
67 |
public class RasterToolsUtil { |
68 |
|
69 |
/**
|
70 |
* Obtiene la lista de capas del TOC y devuelve las raster. Si hay capas agrupadas lo tiene en cuenta y mira
|
71 |
* dentro de la agrupaci?n.
|
72 |
* @param srcLyrs FLayers de la vista
|
73 |
* @param destLyrs Lista de capas
|
74 |
* @return ArrayList con la lista de capas raster agrupadas o no. El orden en que aparecen
|
75 |
* en la lista es de abajo a arriba las que aparecen en el TOC.
|
76 |
*/
|
77 |
public static ArrayList<FLayer> getRasterLayerList(FLayers srcLyrs, ArrayList<FLayer> destLyrs) { |
78 |
if(destLyrs == null) |
79 |
destLyrs = new ArrayList<FLayer>(); |
80 |
for (int i = 0; i < srcLyrs.getLayersCount(); i++) { |
81 |
if(srcLyrs.getLayer(i) instanceof DefaultFLyrRaster) |
82 |
destLyrs.add(srcLyrs.getLayer(i)); |
83 |
if(srcLyrs.getLayer(i) instanceof FLayers) |
84 |
destLyrs = getLayerList((FLayers)srcLyrs.getLayer(i), destLyrs); |
85 |
} |
86 |
return destLyrs;
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* Obtiene la lista de capas del TOC y devuelve las raster. Si hay capas agrupadas lo tiene en cuenta y mira
|
91 |
* dentro de la agrupaci?n.
|
92 |
* @param srcLyrs FLayers de la vista
|
93 |
* @param destLyrs Lista de capas
|
94 |
* @return ArrayList con la lista de capas raster agrupadas o no. El orden en que aparecen
|
95 |
* en la lista es de abajo a arriba las que aparecen en el TOC.
|
96 |
*/
|
97 |
public static ArrayList<FLayer> getLayerList(FLayers srcLyrs, ArrayList<FLayer> destLyrs) { |
98 |
if(srcLyrs == null) |
99 |
return null; |
100 |
if(destLyrs == null) |
101 |
destLyrs = new ArrayList<FLayer>(); |
102 |
for (int i = 0; i < srcLyrs.getLayersCount(); i++) { |
103 |
if(srcLyrs.getLayer(i) instanceof FLayers) |
104 |
destLyrs = getLayerList((FLayers)srcLyrs.getLayer(i), destLyrs); |
105 |
else
|
106 |
destLyrs.add(srcLyrs.getLayer(i)); |
107 |
} |
108 |
return destLyrs;
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* Obtiene el nombre de la vista donde est? cargada la capa que se pasa por par?metro
|
113 |
* @param layer Capa cargada en una vista
|
114 |
* @return Nombre de la vista donde est? cargada la capa.
|
115 |
*/
|
116 |
public static String getView(FLayer layer) { |
117 |
// Project p = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
|
118 |
Project p = ProjectManager.getInstance().getCurrentProject(); |
119 |
return p.getViewName(layer);
|
120 |
} |
121 |
|
122 |
/**
|
123 |
* Devuelve la traducci?n de un texto. En caso de no existir la traducci?n al idioma seleccionado
|
124 |
* devolver? la cadena de entrada.
|
125 |
* @param parent Ventana padre que contiene el objeto con la traducci?n
|
126 |
* @param text Texto a traducir
|
127 |
* @return Texto traducido o cadena de entrada en caso de no encontrar una traducci?n
|
128 |
*/
|
129 |
public static String getText(Object parent, String text) { |
130 |
return PluginServices.getText(parent, text);
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* Obtiene un icono definido por la etiqueta que se especifica en el
|
135 |
* par?metro
|
136 |
* @param ico Etiqueta del icono
|
137 |
* @return Icono
|
138 |
*/
|
139 |
public static ImageIcon getIcon(String ico) { |
140 |
return IconThemeHelper.getImageIcon(ico);
|
141 |
} |
142 |
|
143 |
/**
|
144 |
* A?ade una ventana al gestor de ventanas
|
145 |
* @param window
|
146 |
*/
|
147 |
public static void addWindow(IWindow window) { |
148 |
PluginServices.getMDIManager().addWindow(window); |
149 |
} |
150 |
|
151 |
/**
|
152 |
* Elimina una ventana al gestor de ventanas
|
153 |
* @param window
|
154 |
*/
|
155 |
public static void closeWindow(IWindow window) { |
156 |
PluginServices.getMDIManager().closeWindow(window); |
157 |
} |
158 |
|
159 |
/**
|
160 |
* Selecciona los controles del panel de propiedades a partir de los par?mtros
|
161 |
* obtenidos del driver. Este m?todo realiza una transformaci?n entre Params
|
162 |
* obtenido del driver de escritura y los par?metros del panel de propiedades.
|
163 |
* @param panel Panel de propiedades
|
164 |
* @param params Par?metros del driver
|
165 |
* @param notTakeIntoAccount Nombre de par?metros que no hay que tener en cuenta. Si es null se tienen en cuenta todos.
|
166 |
*/
|
167 |
public static void loadPropertiesFromWriterParams(PropertiesComponent pComp, Params params, String[] notTakeIntoAccount) { |
168 |
for (int i = 0; i < params.getNumParams(); i++) { |
169 |
Param p = params.getParam(i); |
170 |
String name = getText(null, p.getId()); |
171 |
String key = p.getId();
|
172 |
|
173 |
//Miramos si el par?metro coincide con alguno en la lista de parametros que no hay que
|
174 |
//tener en cuenta. Si es as? no lo a?adimos
|
175 |
if(notTakeIntoAccount != null && notTakeIntoAccount.length > 0) { |
176 |
boolean jump = false; |
177 |
for (int j = 0; j < notTakeIntoAccount.length; j++) { |
178 |
if (key.equals(notTakeIntoAccount[j]))
|
179 |
jump = true;
|
180 |
} |
181 |
if(jump)
|
182 |
continue;
|
183 |
} |
184 |
|
185 |
Object[] types = null; |
186 |
int selectedValue = 0; |
187 |
|
188 |
switch (p.getType()) {
|
189 |
case Params.CHECK:
|
190 |
pComp.addValue(name, key, p.getDefaultValue(), types); |
191 |
break;
|
192 |
case Params.CHOICE:
|
193 |
ArrayList<String> list = new ArrayList<String>(); |
194 |
for (int j = 0; j < p.getList().length; j++) { |
195 |
list.add(p.getList()[j]); |
196 |
if (p.getDefaultValue() instanceof Integer) |
197 |
if (((Integer) p.getDefaultValue()).intValue() == j) |
198 |
selectedValue = j; |
199 |
} |
200 |
types = new Object[] { new Integer(PropertiesComponent.TYPE_COMBO), list }; |
201 |
pComp.addValue(name, key, new Integer(selectedValue), types); |
202 |
break;
|
203 |
case Params.SLIDER:
|
204 |
types = new Object[] { new Integer(PropertiesComponent.TYPE_SLIDER), new Integer(p.getList()[0]), new Integer(p.getList()[1]) }; |
205 |
pComp.addValue(name, key, p.getDefaultValue(), types); |
206 |
break;
|
207 |
default:
|
208 |
pComp.addValue(getText(null, ((Param)params.getParam(i)).getId()), params.getParam(i).getId(), params.getParam(i).getDefaultValue(), null); |
209 |
break;
|
210 |
} |
211 |
} |
212 |
} |
213 |
|
214 |
/**
|
215 |
* Carga los par?metros del escritor WriterParams con los valores obtenidos
|
216 |
* de la ventana de propiedades.
|
217 |
*/
|
218 |
@SuppressWarnings("unchecked") |
219 |
public static void loadWriterParamsFromPropertiesPanel(PropertiesComponent pComp, Params params) { |
220 |
ArrayList<PropertyStruct> values = pComp.getValues();
|
221 |
for (int iParam = 0; iParam < params.getNumParams(); iParam++) { |
222 |
Param p = params.getParam(iParam); |
223 |
for (int iValue = 0; iValue < values.size(); iValue++) { |
224 |
PropertyStruct prop = values.get(iValue); |
225 |
if (p.getId().compareTo(prop.getKey()) == 0) { |
226 |
switch (p.getType()) {
|
227 |
case Params.CHECK:
|
228 |
p.setDefaultValue((Boolean) prop.getNewValue());
|
229 |
break;
|
230 |
case Params.CHOICE:
|
231 |
p.setDefaultValue(((Integer) prop.getNewValue()));//p.list[((Integer) prop.getNewValue()).intValue()]; |
232 |
break;
|
233 |
case Params.SLIDER:
|
234 |
try {
|
235 |
p.setDefaultValue((Integer)prop.getNewValue());
|
236 |
} catch (NumberFormatException e) {} |
237 |
} |
238 |
break;
|
239 |
} |
240 |
} |
241 |
} |
242 |
} |
243 |
|
244 |
/**
|
245 |
* Funci?n que devuelve true si se tiene permiso de escritura en la ruta
|
246 |
* indicada en el par?metro path y false si no los tiene.
|
247 |
* @param path Ruta a comprobar los permisosv
|
248 |
* @param pluginObject si es distinto de null se obtiene un mensaje de
|
249 |
* advertencia y sirve como par?metro para getText de la traducci?n.
|
250 |
* Si es null no se mostrar? ventana de advertencia
|
251 |
* @return true si se tiene permiso de escritura en la ruta indicada en el
|
252 |
* par?metro path y false si no los tiene.
|
253 |
*/
|
254 |
public static boolean canWrite(String path, Object pluginObject) { |
255 |
File f = new File(path); |
256 |
if(f.exists() && f.canWrite())
|
257 |
return true; |
258 |
else {
|
259 |
if(pluginObject != null) |
260 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
261 |
PluginServices.getText(pluginObject, "error_escritura"));
|
262 |
return false; |
263 |
} |
264 |
} |
265 |
|
266 |
/**
|
267 |
* Carga una capa raster en una vista de gvSIG.
|
268 |
* @param viewName Nombre de la vista donde ha de cargarse. Si vale null se cargar? en la
|
269 |
* primera vista que encuentre que est? activa. Si no hay ninguna saltar? una excepci?n.
|
270 |
* @param fileName Nombre del fichero a cargar. No debe ser nulo nunca.
|
271 |
* @param layerName Nombre de la capa. Si es null se asignar? el nombre del
|
272 |
* fichero sin extensi?n.
|
273 |
* @throws RasterNotLoadException Excepci?n que se lanza cuando no se ha podido cargar la capa
|
274 |
* por alg?n motivo.
|
275 |
*/
|
276 |
@SuppressWarnings("deprecation") |
277 |
public static FLayer loadLayer(String viewName, String fileName, String layerName) throws RasterNotLoadException { |
278 |
if(fileName == null) |
279 |
return null; |
280 |
|
281 |
//Seleccionamos la vista de gvSIG
|
282 |
AbstractViewPanel theView = null;
|
283 |
try {
|
284 |
IWindow[] allViews = PluginServices.getMDIManager().getAllWindows();
|
285 |
if(viewName != null) { |
286 |
for (int i = 0; i < allViews.length; i++) { |
287 |
if (allViews[i] instanceof AbstractViewPanel |
288 |
&& (((AbstractViewPanel) allViews[i]).getDocument().getName().equals(viewName) || |
289 |
PluginServices.getMDIManager().getWindowInfo((AbstractViewPanel) allViews[i]).getTitle().equals(viewName))) |
290 |
theView = (AbstractViewPanel) allViews[i]; |
291 |
} |
292 |
} else {
|
293 |
IWindow activeWindow = PluginServices.getMDIManager().getActiveWindow(); |
294 |
for (int i = 0; i < allViews.length; i++) { |
295 |
if (allViews[i] instanceof AbstractViewPanel && ((AbstractViewPanel)allViews[i]) == activeWindow) //En la primera vista activa |
296 |
theView = (AbstractViewPanel) allViews[i]; |
297 |
} |
298 |
if(theView == null) { |
299 |
for (int i = 0; i < allViews.length; i++) { |
300 |
if (allViews[i] instanceof AbstractViewPanel) //En la primera vista |
301 |
theView = (AbstractViewPanel) allViews[i]; |
302 |
} |
303 |
} |
304 |
} |
305 |
|
306 |
if (theView == null) |
307 |
throw new RasterNotLoadException("Imposible cargar la capa."); |
308 |
} catch (ClassCastException ex) { |
309 |
throw new RasterNotLoadException("No se puede hacer un casting de esa IWindow a View."); |
310 |
} |
311 |
|
312 |
theView.getMapControl().getMapContext().beginAtomicEvent(); |
313 |
|
314 |
DefaultFLyrRaster lyr = null;
|
315 |
try {
|
316 |
ProviderServices provServ = RasterLocator.getManager().getProviderServices(); |
317 |
RasterDataParameters storeParameters = provServ.createNotTiledParameters(fileName); |
318 |
storeParameters.setSRS(theView.getProjection()); |
319 |
|
320 |
MapContextManager mcm = MapContextLocator.getMapContextManager(); |
321 |
DataManager dataManager = DALLocator.getDataManager(); |
322 |
DataStore dataStore = null;
|
323 |
try {
|
324 |
dataStore = dataManager.createStore(storeParameters); |
325 |
} catch (ValidateDataParametersException e) {
|
326 |
throw new RasterNotLoadException("Error al cargar la capa."); |
327 |
} catch (InitializeException e) {
|
328 |
throw new RasterNotLoadException("Error al cargar la capa."); |
329 |
} catch (ProviderNotRegisteredException e) {
|
330 |
throw new RasterNotLoadException("Error al cargar la capa."); |
331 |
} |
332 |
|
333 |
lyr = new DefaultFLyrRaster();
|
334 |
|
335 |
if(layerName == null) { |
336 |
int endIndex = fileName.lastIndexOf("."); |
337 |
if (endIndex < 0) |
338 |
endIndex = fileName.length(); |
339 |
|
340 |
layerName = fileName.substring(fileName.lastIndexOf(File.separator) + 1, endIndex); |
341 |
} |
342 |
|
343 |
lyr = (DefaultFLyrRaster) mcm.createLayer(layerName, dataStore); |
344 |
|
345 |
} catch (LoadLayerException e) {
|
346 |
throw new RasterNotLoadException("Error al cargar la capa."); |
347 |
} catch (InitializeException e) {
|
348 |
throw new RasterNotLoadException("Error creating parameters."); |
349 |
} catch (ProviderNotRegisteredException e) {
|
350 |
throw new RasterNotLoadException("Error creating parameters."); |
351 |
} |
352 |
theView.getMapControl().getMapContext().getLayers().addLayer(lyr); |
353 |
theView.getMapControl().getMapContext().invalidate(); |
354 |
theView.getMapControl().getMapContext().endAtomicEvent(); |
355 |
return lyr;
|
356 |
} |
357 |
|
358 |
/**
|
359 |
* Carga una capa raster en una vista de gvSIG.
|
360 |
* @param viewName Nombre de la vista donde ha de cargarse. Si vale null se cargar? en la
|
361 |
* primera vista que encuentre que est? activa. Si no hay ninguna saltar? una excepci?n.
|
362 |
* @param fileName Nombre del fichero a cargar. No debe ser nulo nunca.
|
363 |
* @param layerName Nombre de la capa. Si es null se asignar? el nombre del
|
364 |
* fichero sin extensi?n.
|
365 |
* @throws RasterNotLoadException Excepci?n que se lanza cuando no se ha podido cargar la capa
|
366 |
* por alg?n motivo.
|
367 |
*/
|
368 |
public static FLayer loadLayer(String viewName, FLayer lyr) throws RasterNotLoadException { |
369 |
if(lyr == null) |
370 |
return null; |
371 |
|
372 |
//Seleccionamos la vista de gvSIG
|
373 |
AbstractViewPanel theView = null;
|
374 |
try {
|
375 |
IWindow[] allViews = PluginServices.getMDIManager().getAllWindows();
|
376 |
if(viewName != null) { |
377 |
for (int i = 0; i < allViews.length; i++) { |
378 |
if (allViews[i] instanceof AbstractViewPanel |
379 |
&& PluginServices.getMDIManager().getWindowInfo((AbstractViewPanel) allViews[i]).getTitle().equals(viewName)) |
380 |
theView = (AbstractViewPanel) allViews[i]; |
381 |
} |
382 |
} else {
|
383 |
IWindow activeWindow = PluginServices.getMDIManager().getActiveWindow(); |
384 |
for (int i = 0; i < allViews.length; i++) { |
385 |
if (allViews[i] instanceof AbstractViewPanel && ((AbstractViewPanel)allViews[i]) == activeWindow) //En la primera vista activa |
386 |
theView = (AbstractViewPanel) allViews[i]; |
387 |
} |
388 |
if(theView == null) { |
389 |
for (int i = 0; i < allViews.length; i++) { |
390 |
if (allViews[i] instanceof AbstractViewPanel) //En la primera vista |
391 |
theView = (AbstractViewPanel) allViews[i]; |
392 |
} |
393 |
} |
394 |
} |
395 |
|
396 |
if (theView == null) |
397 |
throw new RasterNotLoadException("Imposible cargar la capa."); |
398 |
} catch (ClassCastException ex) { |
399 |
throw new RasterNotLoadException("No se puede hacer un casting de esa IWindow a View."); |
400 |
} |
401 |
|
402 |
theView.getMapControl().getMapContext().beginAtomicEvent(); |
403 |
theView.getMapControl().getMapContext().getLayers().addLayer(lyr); |
404 |
theView.getMapControl().getMapContext().invalidate(); |
405 |
theView.getMapControl().getMapContext().endAtomicEvent(); |
406 |
return lyr;
|
407 |
} |
408 |
|
409 |
/**
|
410 |
* Calculo de las coordenadas de una ventana IWindow para que quede centrada sobre el
|
411 |
* MainFrame. Estas coordenadas solo valen para un IWindow ya que andami mete las ventanas
|
412 |
* con coordenadas relativas a su ventanta principal.
|
413 |
* @param widthWindow Ancho de la ventana a a?adir
|
414 |
* @param heightWindow Alto de la ventana a a?adir
|
415 |
* @return Array con el ancho y el alto
|
416 |
*/
|
417 |
public static Point iwindowPosition(int widthWindow, int heightWindow) { |
418 |
int posWindowX = 0; |
419 |
int posWindowY = 0; |
420 |
Dimension dim = null; |
421 |
Point pos = null; |
422 |
if(PluginServices.getMainFrame() instanceof Component) { |
423 |
dim = ((Component)PluginServices.getMainFrame()).getSize();
|
424 |
pos = ((Component)PluginServices.getMainFrame()).getLocation();
|
425 |
if(dim != null && pos != null) { |
426 |
posWindowX = ((dim.width >> 1) - (widthWindow >> 1)); |
427 |
posWindowY = ((dim.height >> 1) - (heightWindow >> 1) - 70); |
428 |
return new Point(posWindowX, posWindowY); |
429 |
} |
430 |
} |
431 |
return null; |
432 |
} |
433 |
} |