svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / util / RasterToolsUtil.java @ 18014
History | View | Annotate | Download (14.5 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 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.raster.util; |
20 |
|
21 |
import java.awt.Component; |
22 |
import java.io.File; |
23 |
import java.util.ArrayList; |
24 |
|
25 |
import javax.swing.ImageIcon; |
26 |
import javax.swing.JOptionPane; |
27 |
|
28 |
import org.apache.log4j.Logger; |
29 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
30 |
import org.gvsig.gui.beans.propertiespanel.PropertiesComponent; |
31 |
import org.gvsig.gui.beans.propertiespanel.PropertyStruct; |
32 |
import org.gvsig.raster.Configuration; |
33 |
import org.gvsig.raster.dataset.Params; |
34 |
import org.gvsig.raster.dataset.Params.Param; |
35 |
|
36 |
import com.iver.andami.PluginServices; |
37 |
import com.iver.andami.ui.mdiManager.IWindow; |
38 |
import com.iver.cit.gvsig.ProjectExtension; |
39 |
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException; |
40 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
41 |
import com.iver.cit.gvsig.project.Project; |
42 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
43 |
/**
|
44 |
* Herramientas de uso general y que son dependientes de gvSIG, FMap o de
|
45 |
* libUIComponents. En caso de no serlo existe una clase independiente de
|
46 |
* cualquier proyecto dentro de libRaster para este tipo de funciones.
|
47 |
*
|
48 |
* @version 31/05/2007
|
49 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
50 |
*/
|
51 |
public class RasterToolsUtil { |
52 |
|
53 |
/**
|
54 |
* Obtiene el nombre de la vista donde est? cargada la capa que se pasa por par?metro
|
55 |
* @param layer Capa cargada en una vista
|
56 |
* @return Nombre de la vista donde est? cargada la capa.
|
57 |
*/
|
58 |
public static String getView(FLayer layer) { |
59 |
Project p = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject(); |
60 |
return p.getView(layer);
|
61 |
} |
62 |
|
63 |
/**
|
64 |
* Devuelve la traducci?n de un texto. En caso de no existir la traducci?n al idioma seleccionado
|
65 |
* devolver? la cadena de entrada.
|
66 |
* @param parent Ventana padre que contiene el objeto con la traducci?n
|
67 |
* @param text Texto a traducir
|
68 |
* @return Texto traducido o cadena de entrada en caso de no encontrar una traducci?n
|
69 |
*/
|
70 |
public static String getText(Object parent, String text) { |
71 |
return PluginServices.getText(parent, text);
|
72 |
} |
73 |
|
74 |
/**
|
75 |
* Obtiene un icono definido por la etiqueta que se especifica en el
|
76 |
* par?metro
|
77 |
* @param ico Etiqueta del icono
|
78 |
* @return Icono
|
79 |
*/
|
80 |
public static ImageIcon getIcon(String ico) { |
81 |
return PluginServices.getIconTheme().get(ico);
|
82 |
} |
83 |
|
84 |
/**
|
85 |
* Selecciona los controles del panel de propiedades a partir de los par?mtros
|
86 |
* obtenidos del driver. Este m?todo realiza una transformaci?n entre Params
|
87 |
* obtenido del driver de escritura y los par?metros del panel de propiedades.
|
88 |
* @param panel Panel de propiedades
|
89 |
* @param params Par?metros del driver
|
90 |
* @param notTakeIntoAccount Nombre de par?metros que no hay que tener en cuenta. Si es null se tienen en cuenta todos.
|
91 |
*/
|
92 |
public static void loadPropertiesFromWriterParams(PropertiesComponent pComp, Params params, String[] notTakeIntoAccount) { |
93 |
for (int i = 0; i < params.getNumParams(); i++) { |
94 |
Param p = params.getParam(i); |
95 |
String name = getText(null, p.id); |
96 |
String key = p.id;
|
97 |
|
98 |
//Miramos si el par?metro coincide con alguno en la lista de parametros que no hay que
|
99 |
//tener en cuenta. Si es as? no lo a?adimos
|
100 |
if(notTakeIntoAccount != null && notTakeIntoAccount.length > 0) { |
101 |
boolean jump = false; |
102 |
for (int j = 0; j < notTakeIntoAccount.length; j++) { |
103 |
if (key.equals(notTakeIntoAccount[j]))
|
104 |
jump = true;
|
105 |
} |
106 |
if(jump)
|
107 |
continue;
|
108 |
} |
109 |
|
110 |
Object[] types = null; |
111 |
int selectedValue = 0; |
112 |
|
113 |
switch (p.type) {
|
114 |
case Params.CHECK:
|
115 |
pComp.addValue(name, key, p.defaultValue, types); |
116 |
break;
|
117 |
case Params.CHOICE:
|
118 |
ArrayList list = new ArrayList(); |
119 |
for (int j = 0; j < p.list.length; j++) { |
120 |
list.add(p.list[j]); |
121 |
if (p.defaultValue instanceof Integer) |
122 |
if (((Integer) p.defaultValue).intValue() == j) |
123 |
selectedValue = j; |
124 |
} |
125 |
types = new Object[] { new Integer(PropertiesComponent.TYPE_COMBO), list }; |
126 |
pComp.addValue(name, key, new Integer(selectedValue), types); |
127 |
break;
|
128 |
case Params.SLIDER:
|
129 |
types = new Object[] { new Integer(PropertiesComponent.TYPE_SLIDER), new Integer(p.list[0]), new Integer(p.list[1]) }; |
130 |
pComp.addValue(name, key, p.defaultValue, types); |
131 |
break;
|
132 |
default:
|
133 |
pComp.addValue(getText(null, params.getParam(i).id), params.getParam(i).id, params.getParam(i).defaultValue, null); |
134 |
break;
|
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
/**
|
140 |
* Carga los par?metros del escritor WriterParams con los valores obtenidos
|
141 |
* de la ventana de propiedades.
|
142 |
*/
|
143 |
public static void loadWriterParamsFromPropertiesPanel(PropertiesComponent pComp, Params params) { |
144 |
ArrayList values = pComp.getValues();
|
145 |
for (int iParam = 0; iParam < params.getNumParams(); iParam++) { |
146 |
Param p = (Param) params.getParam(iParam); |
147 |
for (int iValue = 0; iValue < values.size(); iValue++) { |
148 |
PropertyStruct prop = ((PropertyStruct) values.get(iValue)); |
149 |
if (p.id.compareTo(prop.getKey()) == 0) { |
150 |
switch (p.type) {
|
151 |
case Params.CHECK:
|
152 |
p.defaultValue = (Boolean) prop.getNewValue();
|
153 |
break;
|
154 |
case Params.CHOICE:
|
155 |
p.defaultValue = ((Integer) prop.getNewValue());//p.list[((Integer) prop.getNewValue()).intValue()]; |
156 |
break;
|
157 |
case Params.SLIDER:
|
158 |
try {
|
159 |
p.defaultValue = (Integer)prop.getNewValue();
|
160 |
} catch (NumberFormatException e) {} |
161 |
} |
162 |
break;
|
163 |
} |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
/**
|
169 |
* Funci?n que devuelve true si se tiene permiso de escritura en la ruta
|
170 |
* indicada en el par?metro path y false si no los tiene.
|
171 |
* @param path Ruta a comprobar los permisosv
|
172 |
* @param pluginObject si es distinto de null se obtiene un mensaje de
|
173 |
* advertencia y sirve como par?metro para getText de la traducci?n.
|
174 |
* Si es null no se mostrar? ventana de advertencia
|
175 |
* @return true si se tiene permiso de escritura en la ruta indicada en el
|
176 |
* par?metro path y false si no los tiene.
|
177 |
*/
|
178 |
public static boolean canWrite(String path, Object pluginObject) { |
179 |
File f = new File(path); |
180 |
if(f.exists() && f.canWrite())
|
181 |
return true; |
182 |
else {
|
183 |
if(pluginObject != null) |
184 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
185 |
PluginServices.getText(pluginObject, "error_escritura"));
|
186 |
return false; |
187 |
} |
188 |
} |
189 |
|
190 |
/**
|
191 |
* Muestra un dialogo con un texto y un bot?n Si o No.
|
192 |
* @param msg Mensaje a mostrar en el dialogo.
|
193 |
* @param parentWindow Ventana desde la que se lanza el dialogo
|
194 |
* @return El bot?n seleccionado por el usuario. true si ha seleccionado "si"
|
195 |
* y false si ha seleccionado "no"
|
196 |
*/
|
197 |
public static boolean messageBoxYesOrNot(String msg, Object parentWindow){ |
198 |
String string1 = PluginServices.getText(parentWindow, "yes"); |
199 |
String string2 = PluginServices.getText(parentWindow, "no"); |
200 |
Object[] options = {string1, string2}; |
201 |
int n = JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(), |
202 |
"<html>" + PluginServices.getText(parentWindow, msg).replaceAll("\n", "<br>") + "</html>", |
203 |
PluginServices.getText(parentWindow, "confirmacion"),
|
204 |
JOptionPane.YES_NO_OPTION,
|
205 |
JOptionPane.QUESTION_MESSAGE,
|
206 |
null,
|
207 |
options, |
208 |
string1); |
209 |
if (n == JOptionPane.YES_OPTION) |
210 |
return true; |
211 |
else
|
212 |
return false; |
213 |
} |
214 |
|
215 |
/**
|
216 |
* Muestra un dialogo de error con un texto y un bot?n de aceptar.
|
217 |
* @param msg Mensaje a mostrar en el dialogo.
|
218 |
* @param parentWindow Ventana desde la que se lanza el dialogo
|
219 |
*/
|
220 |
public static void messageBoxError(String msg, Object parentWindow){ |
221 |
String string = PluginServices.getText(parentWindow, "accept"); |
222 |
Object[] options = {string}; |
223 |
JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(), |
224 |
"<html>" + PluginServices.getText(parentWindow, msg).replaceAll("\n", "<br>") + "</html>", |
225 |
PluginServices.getText(parentWindow, "confirmacion"),
|
226 |
JOptionPane.OK_OPTION,
|
227 |
JOptionPane.ERROR_MESSAGE,
|
228 |
null,
|
229 |
options, |
230 |
string); |
231 |
} |
232 |
|
233 |
/**
|
234 |
* Muestra un dialogo de informaci?n con un texto y un bot?n de aceptar.
|
235 |
* @param msg Mensaje a mostrar en el dialogo. Identificador de la cadena a traducir
|
236 |
* @param parentWindow Ventana desde la que se lanza el dialogo
|
237 |
*/
|
238 |
public static void messageBoxInfo(String msg, Object parentWindow){ |
239 |
String string = PluginServices.getText(parentWindow, "accept"); |
240 |
Object[] options = {string}; |
241 |
JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(), |
242 |
"<html>" + PluginServices.getText(parentWindow, msg).replaceAll("\n", "<br>") + "</html>", |
243 |
PluginServices.getText(parentWindow, "confirmacion"),
|
244 |
JOptionPane.OK_OPTION,
|
245 |
JOptionPane.INFORMATION_MESSAGE,
|
246 |
null,
|
247 |
options, |
248 |
string); |
249 |
} |
250 |
|
251 |
/**
|
252 |
* Registra un mensaje de error en el log de gvSIG
|
253 |
* @param msg Mensaje a guardar en el log
|
254 |
* @param parentWindow Objeto que hizo disparar el mensaje
|
255 |
* @param exception Excepcion que ha sido recogida
|
256 |
*/
|
257 |
public static void debug(String msg, Object parentWindow, Exception exception) { |
258 |
Logger.getLogger(parentWindow.getClass().getName()).debug(PluginServices.getText(parentWindow, msg), exception);
|
259 |
} |
260 |
|
261 |
/**
|
262 |
* Muestra un dialogo de error con un texto y un bot?n de aceptar. El error es
|
263 |
* registrado en el log de gvSIG con la excepcion que se le pase por parametro
|
264 |
* @param msg Mensaje a mostrar en el dialogo.
|
265 |
* @param parentWindow Ventana desde la que se lanza el dialogo
|
266 |
* @param exception Excepcion que ha sido recogida
|
267 |
*/
|
268 |
public static void messageBoxError(String msg, Object parentWindow, Exception exception) { |
269 |
debug(msg, parentWindow, exception); |
270 |
messageBoxError(msg, parentWindow); |
271 |
} |
272 |
|
273 |
/**
|
274 |
* Muestra un dialogo de error con un texto y un bot?n de aceptar. Se le pasa como ?ltimo par?metros
|
275 |
* una lista de excepciones que ser?n guardadas en el log
|
276 |
* @param msg Mensaje a mostrar en el dialogo.
|
277 |
* @param parentWindow Ventana desde la que se lanza el dialogo
|
278 |
* @param exception Excepcion que ha sido recogida
|
279 |
*/
|
280 |
public static void messageBoxError(String msg, Object parentWindow, ArrayList exception) { |
281 |
for (int i = 0; i < exception.size(); i++) { |
282 |
if(exception.get(i) instanceof Exception) |
283 |
debug(msg, parentWindow, (Exception)exception.get(i));
|
284 |
} |
285 |
messageBoxError(msg, parentWindow); |
286 |
} |
287 |
|
288 |
/**
|
289 |
* Muestra un dialogo de informaci?n con un texto y un bot?n de aceptar. El
|
290 |
* mensaje informativo es registrado en el log de gvSIG con la excepcion que
|
291 |
* se le pase por parametro.
|
292 |
* @param msg Mensaje a mostrar en el dialogo. Identificador de la cadena a
|
293 |
* traducir
|
294 |
* @param parentWindow Ventana desde la que se lanza el dialogo
|
295 |
* @param exception Excepcion que ha sido recogida
|
296 |
*/
|
297 |
public static void messageBoxInfo(String msg, Object parentWindow, Exception exception) { |
298 |
debug(msg, parentWindow, exception); |
299 |
messageBoxInfo(msg, parentWindow); |
300 |
} |
301 |
|
302 |
/**
|
303 |
* Muestra un dialogo con un texto y un bot?n Si o No. El mensaje es
|
304 |
* registrado en el log de gvSIG con la excepcion que se le pase por
|
305 |
* parametro.
|
306 |
* @param msg Mensaje a mostrar en el dialogo.
|
307 |
* @param parentWindow Ventana desde la que se lanza el dialogo
|
308 |
* @return El bot?n seleccionado por el usuario. true si ha seleccionado "si"
|
309 |
* y false si ha seleccionado "no"
|
310 |
*/
|
311 |
public static boolean messageBoxYesOrNot(String msg, Object parentWindow, Exception exception) { |
312 |
debug(msg, parentWindow, exception); |
313 |
return messageBoxYesOrNot(msg, parentWindow);
|
314 |
} |
315 |
|
316 |
/**
|
317 |
* Carga una capa raster en una vista de gvSIG.
|
318 |
* @param viewName Nombre de la vista donde ha de cargarse. Si vale null se cargar? en la
|
319 |
* primera vista que encuentre que est? activa. Si no hay ninguna saltar? una excepci?n.
|
320 |
* @param fileName Nombre del fichero a cargar. No debe ser nulo nunca.
|
321 |
* @param layerName Nombre de la capa. Si es null se asignar? el nombre del
|
322 |
* fichero sin extensi?n.
|
323 |
* @throws RasterNotLoadException Excepci?n que se lanza cuando no se ha podido cargar la capa
|
324 |
* por alg?n motivo.
|
325 |
*/
|
326 |
public static FLayer loadLayer(String viewName, String fileName, String layerName) throws RasterNotLoadException { |
327 |
if(fileName == null) |
328 |
return null; |
329 |
|
330 |
//Seleccionamos la vista de gvSIG
|
331 |
View theView = null; |
332 |
try {
|
333 |
IWindow[] allViews = PluginServices.getMDIManager().getAllWindows();
|
334 |
if(viewName != null) { |
335 |
for (int i = 0; i < allViews.length; i++) { |
336 |
if (allViews[i] instanceof View |
337 |
&& PluginServices.getMDIManager().getWindowInfo((View) allViews[i]).getTitle().equals(viewName))
|
338 |
theView = (View) allViews[i];
|
339 |
} |
340 |
} else {
|
341 |
IWindow activeWindow = PluginServices.getMDIManager().getActiveWindow(); |
342 |
for (int i = 0; i < allViews.length; i++) { |
343 |
if (allViews[i] instanceof View && ((View)allViews[i]) == activeWindow) //En la primera vista activa |
344 |
theView = (View) allViews[i];
|
345 |
} |
346 |
if(theView == null) { |
347 |
for (int i = 0; i < allViews.length; i++) { |
348 |
if (allViews[i] instanceof View) //En la primera vista |
349 |
theView = (View) allViews[i];
|
350 |
} |
351 |
} |
352 |
} |
353 |
|
354 |
if (theView == null) |
355 |
throw new RasterNotLoadException("Imposible cargar la capa."); |
356 |
} catch (ClassCastException ex) { |
357 |
throw new RasterNotLoadException("No se puede hacer un casting de esa IWindow a View."); |
358 |
} |
359 |
|
360 |
theView.getMapControl().getMapContext().beginAtomicEvent(); |
361 |
|
362 |
FLayer lyr = null;
|
363 |
try {
|
364 |
if(layerName == null) { |
365 |
int endIndex = fileName.lastIndexOf("."); |
366 |
if (endIndex < 0) |
367 |
endIndex = fileName.length(); |
368 |
lyr = FLyrRasterSE.createLayer( |
369 |
fileName.substring(fileName.lastIndexOf(File.separator) + 1, endIndex), |
370 |
new File(fileName), |
371 |
theView.getMapControl().getProjection(), |
372 |
Configuration.getSingleton());
|
373 |
} else {
|
374 |
lyr = FLyrRasterSE.createLayer( |
375 |
layerName, |
376 |
new File(fileName), |
377 |
theView.getMapControl().getProjection(), |
378 |
Configuration.getSingleton());
|
379 |
} |
380 |
|
381 |
} catch (LoadLayerException e) {
|
382 |
throw new RasterNotLoadException("Error al cargar la capa."); |
383 |
} |
384 |
theView.getMapControl().getMapContext().getLayers().addLayer(lyr); |
385 |
theView.getMapControl().getMapContext().invalidate(); |
386 |
theView.getMapControl().getMapContext().endAtomicEvent(); |
387 |
return lyr;
|
388 |
} |
389 |
} |