svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / saveraster / ui / listener / SaveRasterDialogListener.java @ 11342
History | View | Annotate | Download (10.8 KB)
1 | 11283 | nacho | /* 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.rastertools.saveraster.ui.listener; |
||
20 | |||
21 | import java.awt.Component; |
||
22 | import java.awt.Dimension; |
||
23 | import java.awt.event.ActionEvent; |
||
24 | import java.awt.geom.Rectangle2D; |
||
25 | import java.io.File; |
||
26 | 11337 | nacho | import java.util.ArrayList; |
27 | 11283 | nacho | |
28 | 11342 | nacho | import javax.swing.JFrame; |
29 | 11283 | nacho | import javax.swing.JOptionPane; |
30 | |||
31 | import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
||
32 | import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent; |
||
33 | import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener; |
||
34 | import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
||
35 | 11337 | nacho | import org.gvsig.gui.beans.propertiespanel.PropertiesPanel; |
36 | 11283 | nacho | import org.gvsig.raster.RasterLibrary; |
37 | 11307 | nacho | import org.gvsig.raster.buffer.IQueryableRaster; |
38 | 11283 | nacho | import org.gvsig.raster.dataset.GeoRasterWriter; |
39 | import org.gvsig.raster.dataset.NotSupportedExtensionException; |
||
40 | import org.gvsig.raster.dataset.RasterDriverException; |
||
41 | import org.gvsig.raster.dataset.WriterParams; |
||
42 | 11337 | nacho | import org.gvsig.raster.dataset.WriterParams.Param; |
43 | 11283 | nacho | import org.gvsig.raster.util.RasterUtilities; |
44 | import org.gvsig.rastertools.saveraster.operations.RasterizerLayer; |
||
45 | import org.gvsig.rastertools.saveraster.operations.SaveRasterProcess; |
||
46 | import org.gvsig.rastertools.saveraster.ui.SaveRasterDialog; |
||
47 | import org.gvsig.rastertools.saveraster.ui.SaveRasterPanel; |
||
48 | 11342 | nacho | import org.gvsig.rastertools.saveraster.ui.properties.WriterPropertiesDialog; |
49 | 11283 | nacho | |
50 | import com.iver.andami.PluginServices; |
||
51 | import com.iver.cit.gvsig.fmap.MapControl; |
||
52 | import com.iver.cit.gvsig.fmap.ViewPort; |
||
53 | import com.iver.cit.gvsig.fmap.layers.FLayers; |
||
54 | |||
55 | /**
|
||
56 | * Clase que gestiona los eventos de los botones del dialogo de Salvar a raster.
|
||
57 | *
|
||
58 | * @version 19/04/2007
|
||
59 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
60 | *
|
||
61 | */
|
||
62 | public class SaveRasterDialogListener implements ButtonsPanelListener{ |
||
63 | private SaveRasterDialog dialog = null; |
||
64 | private FLayers layers = null; |
||
65 | private MapControl mapCtrl = null; |
||
66 | private GeoRasterWriter writer = null; |
||
67 | |||
68 | /**
|
||
69 | * Constructor
|
||
70 | * @param dialog
|
||
71 | */
|
||
72 | public SaveRasterDialogListener(SaveRasterDialog dialog, FLayers layers, MapControl mapCtrl) {
|
||
73 | this.dialog = dialog;
|
||
74 | this.layers = layers;
|
||
75 | this.mapCtrl = mapCtrl;
|
||
76 | |||
77 | dialog.addButtonPressedListener(this);
|
||
78 | |||
79 | //Captura de eventos para el bot?n de propiedades
|
||
80 | ((SaveRasterPanel)dialog.getControlsPanel()).getBProperties().addActionListener(new java.awt.event.ActionListener() {
|
||
81 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
||
82 | propsButtonActionPerformed(evt); |
||
83 | } |
||
84 | }); |
||
85 | } |
||
86 | |||
87 | /**
|
||
88 | * Eventos para los botones de aplicar, aceptar y cancelar
|
||
89 | * @param e
|
||
90 | */
|
||
91 | public void actionButtonPressed(ButtonsPanelEvent e) { |
||
92 | switch (e.getButton()) {
|
||
93 | case ButtonsPanel.BUTTON_ACCEPT:
|
||
94 | acceptButtonActionPerformed(e); |
||
95 | dialog.closeJDialog(); |
||
96 | break;
|
||
97 | case ButtonsPanel.BUTTON_APPLY:
|
||
98 | acceptButtonActionPerformed(e); |
||
99 | break;
|
||
100 | case ButtonsPanel.BUTTON_CANCEL:
|
||
101 | dialog.closeJDialog(); |
||
102 | break;
|
||
103 | } |
||
104 | } |
||
105 | |||
106 | /**
|
||
107 | * Acciones realizadas al aceptar.
|
||
108 | * @param e
|
||
109 | */
|
||
110 | private boolean acceptButtonActionPerformed(ButtonsPanelEvent e) { |
||
111 | SaveRasterPanel controlPanel = dialog.getControlsPanel(); |
||
112 | |||
113 | String fName = dialog.getDataInputListener().getFileName();
|
||
114 | Dimension dimension = new Dimension(dialog.getDataInputListener().getWidthInPixels(), |
||
115 | dialog.getDataInputListener().getHeightInPixels() ); |
||
116 | |||
117 | //Comprobamos que el en la ruta de destino tengamos permisos de escritura
|
||
118 | File f = new File(fName); |
||
119 | 11307 | nacho | if(!f.exists()) {
|
120 | 11283 | nacho | if(!checkFileExists(fName))
|
121 | return false; |
||
122 | } |
||
123 | f = new File(fName.substring(0, fName.lastIndexOf(File.separator))); |
||
124 | 11307 | nacho | if(f.exists() && f.isDirectory() && !f.canWrite()) {
|
125 | 11283 | nacho | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
126 | PluginServices.getText(this, "error_file_not_writable")); |
||
127 | return false; |
||
128 | } |
||
129 | |||
130 | double lrX = Double.parseDouble(controlPanel.getTInfDerX().getText()); |
||
131 | double lrY = Double.parseDouble(controlPanel.getTInfDerY().getText()); |
||
132 | double ulX = Double.parseDouble(controlPanel.getTSupIzqX().getText()); |
||
133 | double ulY = Double.parseDouble(controlPanel.getTSupIzqY().getText()); |
||
134 | double width = 0; |
||
135 | 11307 | nacho | if(ulX > lrX) width = (double)(ulX - lrX); |
136 | else width = (double)(lrX - ulX); |
||
137 | 11283 | nacho | double height = 0; |
138 | 11307 | nacho | if(ulY > lrY) height = (double)(ulY - lrY); |
139 | else height = (double)(lrY - ulY); |
||
140 | 11283 | nacho | Rectangle2D ext = null; |
141 | |||
142 | 11307 | nacho | if(ulY < lrY)
|
143 | 11283 | nacho | ext = new Rectangle2D.Double(ulX, ulY, width, height); |
144 | else
|
||
145 | ext = new Rectangle2D.Double(ulX, lrY, width, height); |
||
146 | 11307 | nacho | |
147 | 11283 | nacho | WriterParams params = getWriterParams(fName); |
148 | 11307 | nacho | int blockSize = getBlockSize(params, layers);
|
149 | 11283 | nacho | |
150 | ViewPort vp = new ViewPort(layers.getProjection());
|
||
151 | vp.setBackColor(mapCtrl.getViewPort().getBackColor()); |
||
152 | vp.setImageSize(dimension); |
||
153 | vp.setExtent(ext); |
||
154 | |||
155 | //Creamos el servidor de datos de la vista
|
||
156 | 11325 | nacho | RasterizerLayer rasterizer = new RasterizerLayer(layers, vp, blockSize);
|
157 | 11283 | nacho | |
158 | 11307 | nacho | //Creamos la clase con el proceso y lo lanzamos
|
159 | SaveRasterProcess saveRasterProcess = new SaveRasterProcess(vp, dimension, rasterizer, fName, params);
|
||
160 | IncrementableTask incrementableTask = new IncrementableTask(rasterizer);
|
||
161 | saveRasterProcess.setIncrementableTask(incrementableTask); |
||
162 | incrementableTask.showWindow(); |
||
163 | saveRasterProcess.start(); |
||
164 | incrementableTask.start(); |
||
165 | |||
166 | 11283 | nacho | return true; |
167 | } |
||
168 | |||
169 | /**
|
||
170 | * Comprueba si existe el fichero pasado por par?metro existe solicitando confirmaci?n
|
||
171 | * de sobreescritura. Si el usuario acepta devolver? true y si no acepta devuelve false.
|
||
172 | * Si se acepta la sobreescritura el fichero es borrado.
|
||
173 | * @param file Cadena con el nombre del fichero a comprobar su existencia.
|
||
174 | * @return Devuelve true si el fichero existe y va a ser sobreescrito
|
||
175 | */
|
||
176 | private boolean checkFileExists(String file){ |
||
177 | File f = new File(file); |
||
178 | if(f.exists()){
|
||
179 | String string1 = PluginServices.getText(this, "yes"); |
||
180 | String string2 = PluginServices.getText(this, "no"); |
||
181 | Object[] options = {string1, string2}; |
||
182 | int n = JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(), |
||
183 | PluginServices.getText(this, "error_file_exists"), |
||
184 | PluginServices.getText(this, "confirmacion"), |
||
185 | JOptionPane.YES_NO_OPTION,
|
||
186 | JOptionPane.QUESTION_MESSAGE,
|
||
187 | null,
|
||
188 | options, |
||
189 | string1); |
||
190 | if (n == JOptionPane.YES_OPTION){ |
||
191 | f.delete(); |
||
192 | return true; |
||
193 | }else
|
||
194 | return false; |
||
195 | } |
||
196 | return true; |
||
197 | } |
||
198 | |||
199 | /**
|
||
200 | * Funci?n a ejecutar cuando se pulsa el bot?n de propiedades.
|
||
201 | * @param e ActionEvent
|
||
202 | */
|
||
203 | private void propsButtonActionPerformed(ActionEvent e){ |
||
204 | String name = dialog.getDataInputListener().getFileName();
|
||
205 | |||
206 | //Si no se ha seleccionado ning?n fichero salimos
|
||
207 | if(name == null || name.equals("")) |
||
208 | return;
|
||
209 | |||
210 | WriterParams params = getWriterParams(name); |
||
211 | 11342 | nacho | PropertiesPanel panel = new PropertiesPanel();
|
212 | WriterPropertiesDialog dialog = new WriterPropertiesDialog(panel, params);
|
||
213 | PluginServices.getMDIManager().addWindow(dialog); |
||
214 | 11283 | nacho | } |
215 | 11342 | nacho | |
216 | 11283 | nacho | /**
|
217 | * Obtiene los par?metros del driver de escritura. Si el driver no se ha creado a?n se obtienen
|
||
218 | * unos par?metros con la inicializaci?n por defecto. Si se ha creado ya y se han modificado se
|
||
219 | * devuelven los par?metros con las modificaciones. Si se cambia de driver se devolver? un WriterParams
|
||
220 | * como si fuera la primera vez que se abre.
|
||
221 | * @param name Nombre del fichero sobre el que se salva.
|
||
222 | * @return WriterParams
|
||
223 | */
|
||
224 | private WriterParams getWriterParams(String name) { |
||
225 | String ext = RasterUtilities.getExtensionFromFileName(name);
|
||
226 | try {
|
||
227 | if(writer == null) //La primera vez que se obtiene el driver |
||
228 | writer = GeoRasterWriter.getWriter(name); |
||
229 | else {
|
||
230 | String newType = GeoRasterWriter.getDriverType(ext);
|
||
231 | String oldType = writer.getDriverName();
|
||
232 | if(!newType.equals(oldType)) //Cambio de driver despu?s de haber seleccionado y modificado las propiedades de uno |
||
233 | writer = GeoRasterWriter.getWriter(name); |
||
234 | } |
||
235 | |||
236 | if(writer == null) |
||
237 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "no_driver_escritura")); |
||
238 | |||
239 | return writer.getParams();
|
||
240 | |||
241 | } catch (NotSupportedExtensionException e1) {
|
||
242 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "no_driver_escritura")); |
||
243 | return null; |
||
244 | } catch (RasterDriverException e1) {
|
||
245 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "no_driver_escritura")); |
||
246 | return null; |
||
247 | } |
||
248 | } |
||
249 | |||
250 | 11307 | nacho | /**
|
251 | 11317 | nacho | * Calculo del tama?o de bloque. Para ello comprueba si el raster est? siendo tileado. Si est? siendo
|
252 | * tileado se aplica el alto del tile menor para no pedir un bloque mayor que lo que un servidor puede
|
||
253 | * devolver. Si no est? siendo tileado se devuelve el tama?o de bloque de los par?metros.
|
||
254 | * @param flyrs Capas
|
||
255 | * @param params Par?metros del driver de escritura
|
||
256 | * @return tama?o de bloque
|
||
257 | 11307 | nacho | */
|
258 | private int getBlockSize(WriterParams params, FLayers flyrs) { |
||
259 | |||
260 | int blockSize = RasterLibrary.blockHeight;
|
||
261 | try {
|
||
262 | blockSize = Integer.parseInt(params.getParamById("blocksize").defaultValue); |
||
263 | } catch (NumberFormatException ex) { |
||
264 | //Se queda con el valor por defecto
|
||
265 | } |
||
266 | |||
267 | //Recorremos todas las capas comprobando si alguna de ellas implementa RasterOperations y tilea.
|
||
268 | //En ese caso se obtiene el ancho de bloque. El ancho de bloque total ser? el menor obtenido.
|
||
269 | //Esto lo hacemos para que las capas que tilean WMS, WCS, ... no hagan demasiadas peticiones al servidor
|
||
270 | //por tener un ancho de bloque muy peque?o de modo que el ancho del bloque se ajuste al Tile menor
|
||
271 | //soportado por los servidores que intervienen en el salvado.
|
||
272 | int[] wBlock = null; |
||
273 | boolean isTiling = false; |
||
274 | int block = Integer.MAX_VALUE; |
||
275 | for(int i = 0; i < flyrs.getLayersCount(); i++){ |
||
276 | if(flyrs.getLayer(i) instanceof IQueryableRaster){ |
||
277 | if(((IQueryableRaster)flyrs.getLayer(i)).isTiled()){
|
||
278 | wBlock = ((IQueryableRaster)flyrs.getLayer(i)).getTileSize(); |
||
279 | if((wBlock[0] - 1)< block){ |
||
280 | block = wBlock[0] - 1; |
||
281 | isTiling = true;
|
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | if(isTiling) {
|
||
287 | params.changeParamValue("blocksize", String.valueOf(block)); |
||
288 | return block;
|
||
289 | } |
||
290 | |||
291 | return blockSize;
|
||
292 | } |
||
293 | |||
294 | 11283 | nacho | } |