root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / Export.java @ 31496
History | View | Annotate | Download (10.4 KB)
1 |
/*
|
---|---|
2 |
* Created on 17-feb-2004
|
3 |
*
|
4 |
* To change the template for this generated file go to
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
6 |
*/
|
7 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
8 |
*
|
9 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or (at your option) any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
24 |
*
|
25 |
* For more information, contact:
|
26 |
*
|
27 |
* Generalitat Valenciana
|
28 |
* Conselleria d'Infraestructures i Transport
|
29 |
* Av. Blasco Ib??ez, 50
|
30 |
* 46010 VALENCIA
|
31 |
* SPAIN
|
32 |
*
|
33 |
* +34 963862235
|
34 |
* gvsig@gva.es
|
35 |
* www.gvsig.gva.es
|
36 |
*
|
37 |
* or
|
38 |
*
|
39 |
* IVER T.I. S.A
|
40 |
* Salamanca 50
|
41 |
* 46005 Valencia
|
42 |
* Spain
|
43 |
*
|
44 |
* +34 963163400
|
45 |
* dac@iver.es
|
46 |
*/
|
47 |
package org.gvsig.app.extension; |
48 |
|
49 |
import java.awt.Component; |
50 |
import java.awt.geom.AffineTransform; |
51 |
import java.awt.image.BufferedImage; |
52 |
import java.io.File; |
53 |
import java.io.IOException; |
54 |
import java.util.Hashtable; |
55 |
import java.util.Iterator; |
56 |
|
57 |
import javax.swing.JFileChooser; |
58 |
import javax.swing.JOptionPane; |
59 |
import javax.swing.filechooser.FileFilter; |
60 |
|
61 |
import org.gvsig.andami.PluginServices; |
62 |
import org.gvsig.andami.messages.NotificationManager; |
63 |
import org.gvsig.andami.plugins.Extension; |
64 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
65 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
66 |
import org.gvsig.raster.RasterLibrary; |
67 |
import org.gvsig.raster.dataset.GeoRasterWriter; |
68 |
import org.gvsig.raster.dataset.IBuffer; |
69 |
import org.gvsig.raster.dataset.IDataWriter; |
70 |
import org.gvsig.raster.dataset.NotSupportedExtensionException; |
71 |
import org.gvsig.raster.dataset.RasterDriverException; |
72 |
|
73 |
import com.sun.jimi.core.Jimi; |
74 |
import com.sun.jimi.core.JimiException; |
75 |
|
76 |
|
77 |
/**
|
78 |
* Extensi?n para exportar en algunos formatos raster la vista actual.
|
79 |
*
|
80 |
* @author Fernando Gonz?lez Cort?s
|
81 |
*/
|
82 |
public class Export extends Extension { |
83 |
private String lastPath = null; |
84 |
private Hashtable<String, MyFileFilter> cmsExtensionsSupported = null; |
85 |
private Hashtable<String, MyFileFilter> jimiExtensionsSupported = null; |
86 |
|
87 |
/**
|
88 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
89 |
*/
|
90 |
public boolean isEnabled() { |
91 |
DefaultViewPanel f = (DefaultViewPanel) PluginServices.getMDIManager().getActiveWindow(); |
92 |
|
93 |
if (f == null) { |
94 |
return false; |
95 |
} |
96 |
|
97 |
FLayers layers = f.getModel().getMapContext().getLayers(); |
98 |
for (int i=0;i< layers.getLayersCount();i++) { |
99 |
return layers.getLayer(i).isAvailable();
|
100 |
} |
101 |
return false; |
102 |
} |
103 |
|
104 |
/**
|
105 |
* @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
106 |
*/
|
107 |
public boolean isVisible() { |
108 |
org.gvsig.andami.ui.mdiManager.IWindow f = (org.gvsig.andami.ui.mdiManager.IWindow) PluginServices.getMDIManager() |
109 |
.getActiveWindow(); |
110 |
|
111 |
if (f == null) { |
112 |
return false; |
113 |
} |
114 |
|
115 |
return (f instanceof DefaultViewPanel); |
116 |
} |
117 |
|
118 |
/**
|
119 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
120 |
*/
|
121 |
public void initialize() { |
122 |
} |
123 |
|
124 |
/* (non-Javadoc)
|
125 |
* @see com.iver.andami.plugins.Extension#postInitialize()
|
126 |
*/
|
127 |
public void postInitialize() { |
128 |
cmsExtensionsSupported = new Hashtable<String, MyFileFilter>(); |
129 |
jimiExtensionsSupported = new Hashtable<String, MyFileFilter>(); |
130 |
cmsExtensionsSupported.put("jpg", new MyFileFilter("jpg", |
131 |
PluginServices.getText(this, "jpg"), "cms")); |
132 |
jimiExtensionsSupported.put("png",new MyFileFilter("png", |
133 |
PluginServices.getText(this, "png"), "jimi")); |
134 |
jimiExtensionsSupported.put("bmp",new MyFileFilter("bmp", |
135 |
PluginServices.getText(this, "bmp"), "jimi")); |
136 |
} |
137 |
|
138 |
public static boolean saveImageCMS(File fileDst,BufferedImage srcImage) throws IOException, NotSupportedExtensionException, RasterDriverException { |
139 |
RasterLibrary.wakeUp(); |
140 |
RasterizerImage data = new RasterizerImage(srcImage);
|
141 |
GeoRasterWriter writer = null;
|
142 |
writer = GeoRasterWriter.getWriter( |
143 |
data, |
144 |
fileDst.getAbsolutePath(), |
145 |
3,
|
146 |
new AffineTransform(), |
147 |
srcImage.getWidth(), |
148 |
srcImage.getHeight(), |
149 |
IBuffer.TYPE_IMAGE, |
150 |
GeoRasterWriter.getWriter(fileDst.getAbsolutePath()).getParams(), |
151 |
null,
|
152 |
false
|
153 |
); |
154 |
if (writer == null){ |
155 |
PluginServices.getLogger().error("No supported Format: " + fileDst.getAbsolutePath());
|
156 |
return false; |
157 |
} |
158 |
try {
|
159 |
writer.dataWrite(); |
160 |
} catch (InterruptedException e) { |
161 |
} |
162 |
writer.writeClose(); |
163 |
return true; |
164 |
} |
165 |
|
166 |
|
167 |
|
168 |
public static boolean saveImageJimi(File fileDst,BufferedImage srcImage) throws Exception{ |
169 |
try {
|
170 |
|
171 |
Jimi.putImage(srcImage, fileDst.getAbsolutePath()); |
172 |
|
173 |
} catch (JimiException e) {
|
174 |
throw new Exception(fileDst.getAbsolutePath(),e); |
175 |
} |
176 |
return true; |
177 |
|
178 |
} |
179 |
|
180 |
/**
|
181 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
182 |
*/
|
183 |
public void execute(String actionCommand) { |
184 |
JFileChooser jfc = new JFileChooser(lastPath); |
185 |
|
186 |
jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter()); |
187 |
|
188 |
Iterator<MyFileFilter> iter = cmsExtensionsSupported.values().iterator();
|
189 |
while (iter.hasNext()){
|
190 |
jfc.addChoosableFileFilter((FileFilter)iter.next());
|
191 |
} |
192 |
|
193 |
iter = jimiExtensionsSupported.values().iterator(); |
194 |
while (iter.hasNext()){
|
195 |
jfc.addChoosableFileFilter((FileFilter)iter.next());
|
196 |
} |
197 |
|
198 |
jfc.setFileFilter((FileFilter)jimiExtensionsSupported.get("png")); |
199 |
if (jfc.showSaveDialog(
|
200 |
(Component) PluginServices.getMainFrame()
|
201 |
) == JFileChooser.APPROVE_OPTION) {
|
202 |
|
203 |
BufferedImage tempImage;
|
204 |
|
205 |
tempImage = ((DefaultViewPanel) PluginServices.getMDIManager().getActiveWindow()).getImage(); |
206 |
|
207 |
File f = jfc.getSelectedFile();
|
208 |
|
209 |
lastPath = f.getParent(); |
210 |
|
211 |
MyFileFilter filter = (MyFileFilter)jfc.getFileFilter(); |
212 |
f = filter.normalizeExtension(f); |
213 |
|
214 |
|
215 |
if (f.exists()){
|
216 |
int resp = JOptionPane.showConfirmDialog( |
217 |
(Component) PluginServices.getMainFrame(),
|
218 |
PluginServices.getText(this,
|
219 |
"fichero_ya_existe_seguro_desea_guardarlo")+
|
220 |
"\n"+
|
221 |
f.getAbsolutePath(), |
222 |
PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
223 |
if (resp != JOptionPane.YES_OPTION) { |
224 |
return;
|
225 |
} |
226 |
|
227 |
} |
228 |
|
229 |
if (filter.getInfo().equalsIgnoreCase("cms")){ |
230 |
|
231 |
try {
|
232 |
saveImageCMS(f, tempImage); |
233 |
} catch (IOException e) { |
234 |
NotificationManager.addError("Error exportando la imagen", e);
|
235 |
} catch (NotSupportedExtensionException e) {
|
236 |
NotificationManager.addError("Error exportando la imagen: formato no soportado", e);
|
237 |
} catch (RasterDriverException e) {
|
238 |
NotificationManager.addError("Error exportando la imagen", e);
|
239 |
} |
240 |
}else if (filter.getInfo().equalsIgnoreCase("jimi")) { |
241 |
try {
|
242 |
saveImageJimi(f, tempImage); |
243 |
} catch (Exception e) { |
244 |
NotificationManager.addError("Error exportando la imagen", e);
|
245 |
} |
246 |
|
247 |
} |
248 |
|
249 |
|
250 |
} |
251 |
} |
252 |
} |
253 |
|
254 |
/**
|
255 |
* Servidor de datos desde un BufferedImage. Cada petici?n es de un tama?o
|
256 |
* de bloque indicado por el escritor en los par?metros de la llamada por lo
|
257 |
* que en la siguiente petici?n habr? que escribir el bloque siguiente.
|
258 |
*/
|
259 |
class RasterizerImage implements IDataWriter { |
260 |
private BufferedImage source = null; |
261 |
private int[] data = null; |
262 |
private int y = 0; |
263 |
|
264 |
public RasterizerImage(BufferedImage source) { |
265 |
this.source = source;
|
266 |
} |
267 |
|
268 |
/**
|
269 |
* Solo necesita implementar el m?todo RGB porque solo se
|
270 |
* utiliza para exportar la vista.
|
271 |
*/
|
272 |
public int[] readARGBData(int sX, int sY, int nBand){ |
273 |
return readData( sX, sY, nBand);
|
274 |
} |
275 |
|
276 |
public int[] readData(int sizeX, int sizeY, int nBand) { |
277 |
if(nBand == 0) { //Con nBand==0 se devuelven las 3 bandas |
278 |
this.data = this.source.getRGB(0, y, sizeX, sizeY, this.data, 0, sizeX); |
279 |
y += sizeY; |
280 |
return this.data; |
281 |
} |
282 |
return null; |
283 |
} |
284 |
|
285 |
public byte[][] readByteData(int sizeX, int sizeY) { |
286 |
return null; |
287 |
} |
288 |
|
289 |
public double[][] readDoubleData(int sizeX, int sizeY) { |
290 |
return null; |
291 |
} |
292 |
|
293 |
public float[][] readFloatData(int sizeX, int sizeY) { |
294 |
return null; |
295 |
} |
296 |
|
297 |
public int[][] readIntData(int sizeX, int sizeY) { |
298 |
return null; |
299 |
} |
300 |
|
301 |
public short[][] readShortData(int sizeX, int sizeY) { |
302 |
return null; |
303 |
} |
304 |
|
305 |
} |
306 |
|
307 |
class MyFileFilter extends FileFilter{ |
308 |
|
309 |
private String[] extensiones=new String[1]; |
310 |
private String description; |
311 |
private boolean dirs = true; |
312 |
private String info= null; |
313 |
|
314 |
public MyFileFilter(String[] ext, String desc) { |
315 |
extensiones = ext; |
316 |
description = desc; |
317 |
} |
318 |
|
319 |
public MyFileFilter(String[] ext, String desc,String info) { |
320 |
extensiones = ext; |
321 |
description = desc; |
322 |
this.info = info;
|
323 |
} |
324 |
|
325 |
public MyFileFilter(String ext, String desc) { |
326 |
extensiones[0] = ext;
|
327 |
description = desc; |
328 |
} |
329 |
|
330 |
public MyFileFilter(String ext, String desc,String info) { |
331 |
extensiones[0] = ext;
|
332 |
description = desc; |
333 |
this.info = info;
|
334 |
} |
335 |
|
336 |
public MyFileFilter(String ext, String desc, boolean dirs) { |
337 |
extensiones[0] = ext;
|
338 |
description = desc; |
339 |
this.dirs = dirs;
|
340 |
} |
341 |
|
342 |
public MyFileFilter(String ext, String desc, boolean dirs,String info) { |
343 |
extensiones[0] = ext;
|
344 |
description = desc; |
345 |
this.dirs = dirs;
|
346 |
this.info = info;
|
347 |
} |
348 |
|
349 |
public boolean accept(File f) { |
350 |
if (f.isDirectory()) {
|
351 |
if (dirs) {
|
352 |
return true; |
353 |
} else {
|
354 |
return false; |
355 |
} |
356 |
} |
357 |
for (int i=0;i<extensiones.length;i++){ |
358 |
if (extensiones[i].equals("")){ |
359 |
continue;
|
360 |
} |
361 |
if (getExtensionOfAFile(f).equalsIgnoreCase(extensiones[i])){
|
362 |
return true; |
363 |
} |
364 |
} |
365 |
|
366 |
return false; |
367 |
} |
368 |
|
369 |
/**
|
370 |
* @see javax.swing.filechooser.FileFilter#getDescription()
|
371 |
*/
|
372 |
public String getDescription() { |
373 |
return description;
|
374 |
} |
375 |
|
376 |
public String[] getExtensions() { |
377 |
return extensiones;
|
378 |
} |
379 |
|
380 |
public boolean isDirectory(){ |
381 |
return dirs;
|
382 |
} |
383 |
|
384 |
private String getExtensionOfAFile(File file){ |
385 |
String name;
|
386 |
int dotPos;
|
387 |
name = file.getName(); |
388 |
dotPos = name.lastIndexOf(".");
|
389 |
if (dotPos < 1){ |
390 |
return ""; |
391 |
} |
392 |
return name.substring(dotPos+1); |
393 |
} |
394 |
|
395 |
public File normalizeExtension(File file){ |
396 |
String ext = getExtensionOfAFile(file);
|
397 |
if (ext.equals("") || !(this.accept(file))){ |
398 |
return new File(file.getAbsolutePath() + "." + extensiones[0]); |
399 |
} |
400 |
return file;
|
401 |
} |
402 |
|
403 |
public String getInfo(){ |
404 |
return this.info; |
405 |
} |
406 |
|
407 |
public void setInfo(String info){ |
408 |
this.info = info;
|
409 |
} |
410 |
|
411 |
} |