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