root / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / gridmath / gui / listener / GridMathPanelListener.java @ 17903
History | View | Annotate | Download (11.6 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Instituto de Desarrollo Regional 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 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
|
34 |
* Campus Universitario s/n
|
35 |
* 02071 Alabacete
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 967 599 200
|
39 |
*/
|
40 |
|
41 |
package org.gvsig.remotesensing.gridmath.gui.listener; |
42 |
|
43 |
import java.io.File; |
44 |
|
45 |
import javax.swing.JFileChooser; |
46 |
import javax.swing.event.TableModelEvent; |
47 |
import javax.swing.event.TableModelListener; |
48 |
|
49 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
50 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
51 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent; |
52 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener; |
53 |
import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
54 |
import org.gvsig.raster.buffer.BufferFactory; |
55 |
import org.gvsig.raster.buffer.RasterBuffer; |
56 |
import org.gvsig.raster.dataset.GeoRasterWriter; |
57 |
import org.gvsig.raster.dataset.InvalidSetViewException; |
58 |
import org.gvsig.raster.dataset.io.RasterDriverException; |
59 |
import org.gvsig.raster.util.ExtendedFileFilter; |
60 |
import org.gvsig.raster.util.RasterToolsUtil; |
61 |
import org.gvsig.rastertools.RasterModule; |
62 |
import org.gvsig.remotesensing.gridmath.ErrorSintaxisException; |
63 |
import org.gvsig.remotesensing.gridmath.GridMathProcess; |
64 |
import org.gvsig.remotesensing.gridmath.NoAssignedVarsException; |
65 |
import org.gvsig.remotesensing.gridmath.NoVarsException; |
66 |
import org.gvsig.remotesensing.gridmath.gui.GridMathPanel; |
67 |
|
68 |
import com.iver.andami.PluginServices; |
69 |
import com.iver.andami.Utilities; |
70 |
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard; |
71 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
72 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
73 |
|
74 |
|
75 |
/**
|
76 |
* Listener para el panel de la calculadora de bandas
|
77 |
*
|
78 |
* @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
|
79 |
* @author Alejandro Mu?oz S?nchez (alejandro.munoz@uclm.es)
|
80 |
* @version 19/10/2007
|
81 |
*/
|
82 |
public class GridMathPanelListener implements ButtonsPanelListener, TableModelListener { |
83 |
|
84 |
private GridMathPanel gridMathPanel = null; |
85 |
private boolean canClose = false; |
86 |
|
87 |
|
88 |
/**
|
89 |
* Constructor
|
90 |
* @param calculatorPanel
|
91 |
*/
|
92 |
public GridMathPanelListener(GridMathPanel calculatorPanel) {
|
93 |
this.gridMathPanel = calculatorPanel;
|
94 |
|
95 |
} |
96 |
|
97 |
|
98 |
/*
|
99 |
* (non-Javadoc)
|
100 |
* @see org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener#actionButtonPressed(org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent)
|
101 |
*/
|
102 |
public void actionButtonPressed(ButtonsPanelEvent e) { |
103 |
// Bot?n de Aceptar
|
104 |
|
105 |
if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
|
106 |
aplicar(); |
107 |
if(canClose)
|
108 |
close(); |
109 |
} |
110 |
// Bot?n de Aplicar
|
111 |
if (e.getButton() == ButtonsPanel.BUTTON_APPLY) {
|
112 |
aplicar(); |
113 |
} |
114 |
|
115 |
|
116 |
// Bot?n de Cerrar
|
117 |
if (e.getButton() == ButtonsPanel.BUTTON_CANCEL) {
|
118 |
close(); |
119 |
} |
120 |
} |
121 |
|
122 |
|
123 |
/**
|
124 |
* Acciones al aplicar. Comprobar que la lista de variables no es vacia, que todas las variables
|
125 |
* estan asignadas, y que la expresi?n es correcta. Si se cumplen los requisitos se lanza el calculo.
|
126 |
*/
|
127 |
private void aplicar(){ |
128 |
|
129 |
try{
|
130 |
|
131 |
canClose = false;
|
132 |
if (gridMathPanel.getCalculatorPanel().getJTableVariables().getTableFormat().getRowCount()>0){ |
133 |
boolean allAsigned=true; |
134 |
|
135 |
// Comprobar que todas las variables estan asignadas
|
136 |
for (int i=0; i<gridMathPanel.getCalculatorPanel().getJTableVariables().getTableFormat().getRowCount(); i++){ |
137 |
if (gridMathPanel.getCalculatorPanel().getJTableVariables().getTableFormat().getValueAt(i,1).toString().equals("")) |
138 |
{ allAsigned=false; break;} |
139 |
} |
140 |
|
141 |
if (allAsigned){
|
142 |
if(!gridMathPanel.getCalculatorPanel().getParser().hasError()){
|
143 |
canClose = true;
|
144 |
calculate(); |
145 |
} |
146 |
else{
|
147 |
throw new ErrorSintaxisException(); |
148 |
} |
149 |
}else{
|
150 |
throw new NoAssignedVarsException(); |
151 |
} |
152 |
|
153 |
}else {
|
154 |
throw new NoVarsException(); |
155 |
} |
156 |
} catch (ErrorSintaxisException exc) {
|
157 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "bad_expresion"), this); |
158 |
}catch (NoAssignedVarsException exc){
|
159 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this,"variables_sin_asignar"),this); |
160 |
}catch (NoVarsException exc){
|
161 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this,"no_variables"),this); |
162 |
} |
163 |
} |
164 |
|
165 |
|
166 |
/**
|
167 |
* M?todo que construye el HasMap con los params y lanza el proceso para el
|
168 |
* calculo del raster resultante.
|
169 |
*
|
170 |
*/
|
171 |
private void calculate(){ |
172 |
|
173 |
int nBand;
|
174 |
String layerBand;
|
175 |
String layerName;
|
176 |
FLyrRasterSE rasterLayer; |
177 |
|
178 |
String path = getFileSelected();
|
179 |
if (path == null) |
180 |
return;
|
181 |
FLayers layers = gridMathPanel.getView().getModel().getMapContext().getLayers(); |
182 |
|
183 |
// Extent de salida personalizado.
|
184 |
if (gridMathPanel.getOptionsPanel().getRButtom2().isSelected()){
|
185 |
try{
|
186 |
gridMathPanel.getOutputExtent().setXRange(Double.parseDouble(gridMathPanel.getOptionsPanel().getJTextRangoX1().getText()),Double.parseDouble(gridMathPanel.getOptionsPanel().getJTextRangoX2().getText())); |
187 |
gridMathPanel.getOutputExtent().setYRange(Double.parseDouble(gridMathPanel.getOptionsPanel().getJTextRangoY1().getText()), Double.parseDouble(gridMathPanel.getOptionsPanel().getJTextRangoY2().getText())); |
188 |
gridMathPanel.getOutputExtent().setCellSize(Double.parseDouble(gridMathPanel.getOptionsPanel().getJTextCellSize().getText()));
|
189 |
}catch (NumberFormatException e) { |
190 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "invalid_number"), this); |
191 |
return;
|
192 |
} |
193 |
} |
194 |
// Extent de salida a partir de una capa.
|
195 |
else if(gridMathPanel.getOptionsPanel().getRButtom4().isSelected()){ |
196 |
try {
|
197 |
FLayer layer = layers.getLayer(gridMathPanel.getOptionsPanel().getJComboCapas().getSelectedIndex()); |
198 |
gridMathPanel.getOutputExtent().setXRange(layer.getFullExtent().getMinX(), |
199 |
layer.getFullExtent().getMaxX()); |
200 |
gridMathPanel.getOutputExtent().setYRange(layer.getFullExtent().getMinY(), |
201 |
layer.getFullExtent().getMaxY()); |
202 |
gridMathPanel.getOutputExtent().setCellSize(Double.parseDouble(gridMathPanel.getOptionsPanel().getJTextCellSize().getText()));
|
203 |
gridMathPanel.getOptionsPanel().extentHasChanged(); |
204 |
} catch (Exception ex) { |
205 |
ex.printStackTrace(); |
206 |
} |
207 |
} |
208 |
gridMathPanel.getGridMath().setResultExtent(gridMathPanel.getOutputExtent()); |
209 |
|
210 |
//Rellenar el HasMap con el buffer corespondiente a cada variable
|
211 |
|
212 |
RasterBuffer valor=null;
|
213 |
for (int i=0;i<gridMathPanel.getCalculatorPanel().getJTableVariables().getTableFormat().getRowCount();i++){ |
214 |
|
215 |
layerBand= gridMathPanel.getCalculatorPanel().getJTableVariables().getTableFormat().getValueAt(i,1).toString();
|
216 |
layerName = layerBand.substring(0,layerBand.indexOf("[")); |
217 |
nBand = Integer.valueOf(layerBand.substring(layerBand.lastIndexOf("Band")+4,layerBand.lastIndexOf("]"))).intValue(); |
218 |
rasterLayer = (FLyrRasterSE)layers.getLayer(layerName); |
219 |
BufferFactory bufferFactory= rasterLayer.getBufferFactory(); |
220 |
|
221 |
double minX=0,minY=0,maxX=0,maxY=0; |
222 |
minX= gridMathPanel.getOutputExtent().getMin().getX(); |
223 |
minY= gridMathPanel.getOutputExtent().getMin().getY(); |
224 |
maxX= gridMathPanel.getOutputExtent().getMax().getX(); |
225 |
maxY =gridMathPanel.getOutputExtent().getMax().getY(); |
226 |
|
227 |
try {
|
228 |
bufferFactory.setAdjustToExtent(false);
|
229 |
bufferFactory.setDrawableBands(new int[]{nBand-1}); |
230 |
bufferFactory.setAreaOfInterest(minX,minY,maxX,maxY,gridMathPanel.getOutputExtent().getNX(),gridMathPanel.getOutputExtent().getNY()); |
231 |
valor=(RasterBuffer) bufferFactory.getRasterBuf(); |
232 |
|
233 |
} catch (ArrayIndexOutOfBoundsException e) { |
234 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e); |
235 |
} catch (InvalidSetViewException e) {
|
236 |
e.printStackTrace(); |
237 |
} catch (InterruptedException e) { |
238 |
Thread.currentThread().interrupt();
|
239 |
} catch (RasterDriverException e) {
|
240 |
e.printStackTrace(); |
241 |
} |
242 |
|
243 |
gridMathPanel.getCalculatorPanel().getQWindowsHash().put(gridMathPanel.getCalculatorPanel().getJTableVariables().getTableFormat().getValueAt(i,0).toString(),new Object[]{valor,new Integer(valor.getDataType())}); |
244 |
|
245 |
} |
246 |
gridMathPanel.getGridMath().setParams(gridMathPanel.getCalculatorPanel().getQWindowsHash()); |
247 |
|
248 |
// Lanzar el proceso de c?lculo.
|
249 |
GridMathProcess process = new GridMathProcess();
|
250 |
process.addParam("expresion",gridMathPanel.getCalculatorPanel().getJTextExpression().getText());
|
251 |
process.addParam("params",gridMathPanel.getGridMath().getParams());
|
252 |
process.addParam("extent",gridMathPanel.getOutputExtent());
|
253 |
process.addParam("mapcontext",gridMathPanel.getView().getModel().getMapContext());
|
254 |
process.addParam("filepath", path);
|
255 |
process.start(); |
256 |
|
257 |
} |
258 |
|
259 |
|
260 |
/**
|
261 |
* Acciones al cerrar
|
262 |
*/
|
263 |
private void close() { |
264 |
PluginServices.getMDIManager().closeWindow(gridMathPanel.getCalculatorDialog()); |
265 |
} |
266 |
|
267 |
|
268 |
public void tableChanged(TableModelEvent e) { |
269 |
gridMathPanel.getOptionsPanel().InicializarOpcion(); |
270 |
} |
271 |
|
272 |
/**
|
273 |
* Devuelve la ruta del fichero donde se va a guardar, en caso de guardarse
|
274 |
* en memoria, calcula el nombre sin preguntar y devuelve la ruta.
|
275 |
* @return string con la ruta de salida
|
276 |
*/
|
277 |
public String getFileSelected() { |
278 |
String path = ""; |
279 |
if (gridMathPanel.getOptionsPanel().getRadioFile().isSelected()) {
|
280 |
JFileChooser chooser = new JFileChooser(FileOpenWizard.getLastPath()); |
281 |
chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero")); |
282 |
//A?adimos las extensiones que hayan sido registradas en el driver
|
283 |
String[] extList = GeoRasterWriter.getDriversExtensions(); |
284 |
for(int i=0;i<extList.length;i++) |
285 |
chooser.addChoosableFileFilter(new ExtendedFileFilter(extList[i]));
|
286 |
|
287 |
if (chooser.showOpenDialog(gridMathPanel.getOptionsPanel()) != JFileChooser.APPROVE_OPTION) |
288 |
return null; |
289 |
|
290 |
FileOpenWizard.setLastPath(chooser.getSelectedFile().getPath().substring(0, chooser.getSelectedFile().getPath().lastIndexOf(File.separator))); |
291 |
ExtendedFileFilter fileFilter = (ExtendedFileFilter) chooser.getFileFilter(); |
292 |
path = fileFilter.getNormalizedFilename(chooser.getSelectedFile()); |
293 |
} else {
|
294 |
String file = gridMathPanel.getOptionsPanel().getJTextNombreCapa().getText();
|
295 |
path = Utilities.createTempDirectory() + File.separator + gridMathPanel.getOptionsPanel().getJTextNombreCapa().getText() + ".tif"; |
296 |
if(file.compareTo(RasterModule.getOnlyLayerName()) == 0) |
297 |
RasterModule.usesOnlyLayerName(); |
298 |
gridMathPanel.getOptionsPanel().updateNewLayerText(); |
299 |
} |
300 |
|
301 |
return path;
|
302 |
} |
303 |
|
304 |
} |
305 |
|
306 |
/**
|
307 |
* @author Nacho Brodin <brodin_ign@gva.es>
|
308 |
* Filtro para el selector de formatos de escritura
|
309 |
*/
|
310 |
class WriterFilter extends javax.swing.filechooser.FileFilter { |
311 |
private String filter; |
312 |
|
313 |
public WriterFilter(String fil) { |
314 |
this.filter = fil;
|
315 |
} |
316 |
|
317 |
public boolean accept(File f) { |
318 |
return f.isDirectory() || f.getName().toLowerCase().endsWith("." + filter); |
319 |
} |
320 |
|
321 |
public String getDescription() { |
322 |
return "." + filter; |
323 |
} |
324 |
} |