svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / gui / Panels / SelectFilePanel.java @ 2977
History | View | Annotate | Download (14.4 KB)
1 |
package com.iver.cit.gvsig.gui.Panels; |
---|---|
2 |
|
3 |
import java.awt.FlowLayout; |
4 |
import java.awt.GridBagConstraints; |
5 |
import java.awt.GridBagLayout; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.geom.Rectangle2D; |
8 |
import java.io.File; |
9 |
|
10 |
import javax.swing.JButton; |
11 |
import javax.swing.JCheckBox; |
12 |
import javax.swing.JDialog; |
13 |
import javax.swing.JFileChooser; |
14 |
import javax.swing.JLabel; |
15 |
import javax.swing.JPanel; |
16 |
import javax.swing.JTextField; |
17 |
import javax.swing.filechooser.FileFilter; |
18 |
|
19 |
import org.cresques.cts.IProjection; |
20 |
import org.cresques.io.GeoRasterFile; |
21 |
import org.cresques.px.Extent; |
22 |
|
23 |
import com.hardcode.driverManager.DriverLoadException; |
24 |
import com.iver.andami.PluginServices; |
25 |
import com.iver.andami.messages.NotificationManager; |
26 |
import com.iver.cit.gvsig.fmap.DriverException; |
27 |
import com.iver.cit.gvsig.fmap.ViewPort; |
28 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
29 |
import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
30 |
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster; |
31 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
32 |
import com.iver.cit.gvsig.fmap.layers.RasterAdapter; |
33 |
import com.iver.cit.gvsig.fmap.layers.RasterFileAdapter; |
34 |
import com.iver.cit.gvsig.gui.FOpenDialog; |
35 |
import com.iver.cit.gvsig.gui.View; |
36 |
import com.iver.cit.gvsig.gui.Dialogs.GeoreferencingDialog; |
37 |
public class SelectFilePanel extends JPanel { |
38 |
|
39 |
private JPanel pGeneral = null; |
40 |
private JPanel pFileSelection = null; |
41 |
private JPanel pControls = null; |
42 |
private JTextField tFile = null; |
43 |
private JButton bSelectFile = null; |
44 |
//private ProjChooserPanel jPanelProj = null;
|
45 |
private JPanel pProyection = null; |
46 |
/**
|
47 |
* Lista de formatos soportados
|
48 |
*/
|
49 |
private String[] fileFilters = {"ecw", "sid", "tif", "jpg", "png"}; |
50 |
/**
|
51 |
* Nombre del fichero seleccionado
|
52 |
*/
|
53 |
private String fName = ""; |
54 |
|
55 |
/**
|
56 |
* Nombre de la capa
|
57 |
*/
|
58 |
private String lyrName = ""; |
59 |
/**
|
60 |
* Recuerda la ?ltima ruta seleccionada por el usuario
|
61 |
*/
|
62 |
private String lastPath = "./"; |
63 |
private GeoreferencingDialog parent = null; |
64 |
private FLyrGeoRaster lyrGeoRaster = null; |
65 |
|
66 |
private JPanel pCheckUseGeoref = null; |
67 |
private JCheckBox cbUseGeoref = null; |
68 |
private JLabel lUseGeoref = null; |
69 |
public static double widthPxImg, heightPxImg; |
70 |
|
71 |
/**
|
72 |
* This is the default constructor
|
73 |
*/
|
74 |
public SelectFilePanel(GeoreferencingDialog georef) {
|
75 |
super();
|
76 |
initialize(); |
77 |
this.parent = georef;
|
78 |
} |
79 |
|
80 |
/**
|
81 |
* This method initializes this
|
82 |
*
|
83 |
* @return void
|
84 |
*/
|
85 |
private void initialize() { |
86 |
this.setPreferredSize(new java.awt.Dimension(400,240)); |
87 |
this.setSize(new java.awt.Dimension(400,240)); |
88 |
this.setLocation(new java.awt.Point(0,0)); |
89 |
this.add(getPGeneral(), null); |
90 |
this.getBSelectFile().addActionListener(new java.awt.event.ActionListener() { |
91 |
public void actionPerformed(java.awt.event.ActionEvent evt) { |
92 |
acceptButtonActionPerformed(evt); |
93 |
} |
94 |
}); |
95 |
} |
96 |
|
97 |
/**
|
98 |
* This method initializes jPanel
|
99 |
*
|
100 |
* @return javax.swing.JPanel
|
101 |
*/
|
102 |
private JPanel getPGeneral() { |
103 |
if (pGeneral == null) { |
104 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
105 |
gridBagConstraints1.insets = new java.awt.Insets(5,0,0,0); |
106 |
gridBagConstraints1.gridy = 1;
|
107 |
gridBagConstraints1.gridx = 0;
|
108 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
109 |
gridBagConstraints.insets = new java.awt.Insets(0,0,2,0); |
110 |
gridBagConstraints.gridy = 0;
|
111 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; |
112 |
gridBagConstraints.gridx = 0;
|
113 |
pGeneral = new JPanel(); |
114 |
pGeneral.setLayout(new GridBagLayout()); |
115 |
pGeneral.setPreferredSize(new java.awt.Dimension(400,240)); |
116 |
gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH; |
117 |
pGeneral.add(getPFileSelection(), gridBagConstraints); |
118 |
pGeneral.add(getPControls(), gridBagConstraints1); |
119 |
} |
120 |
return pGeneral;
|
121 |
} |
122 |
|
123 |
/**
|
124 |
* This method initializes jPanel
|
125 |
*
|
126 |
* @return javax.swing.JPanel
|
127 |
*/
|
128 |
private JPanel getPFileSelection() { |
129 |
if (pFileSelection == null) { |
130 |
FlowLayout flowLayout = new FlowLayout(); |
131 |
flowLayout.setVgap(10);
|
132 |
pFileSelection = new JPanel(); |
133 |
pFileSelection.setBorder(javax.swing.BorderFactory.createTitledBorder(null, PluginServices.getText(this,"cargar"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
134 |
pFileSelection.setLayout(flowLayout); |
135 |
pFileSelection.setPreferredSize(new java.awt.Dimension(380,70)); |
136 |
pFileSelection.add(getTFile(), null);
|
137 |
pFileSelection.add(getBSelectFile(), null);
|
138 |
} |
139 |
return pFileSelection;
|
140 |
} |
141 |
|
142 |
private JPanel getPControls() { |
143 |
if (pControls == null) { |
144 |
GridBagConstraints gridBagConstraints11 = new GridBagConstraints(); |
145 |
GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
146 |
gridBagConstraints2.gridx = 0;
|
147 |
gridBagConstraints2.gridy = 1;
|
148 |
pControls = new JPanel(); |
149 |
pControls.setLayout(new GridBagLayout()); |
150 |
pControls.setPreferredSize(new java.awt.Dimension(380,135)); |
151 |
pControls.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
152 |
gridBagConstraints11.gridx = 0;
|
153 |
gridBagConstraints11.gridy = 0;
|
154 |
pControls.add(getPCheckUseGeoref(), gridBagConstraints11); |
155 |
pControls.add(getPProyection(), gridBagConstraints2); |
156 |
} |
157 |
return pControls;
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* This method initializes jTextField
|
162 |
*
|
163 |
* @return javax.swing.JTextField
|
164 |
*/
|
165 |
private JTextField getTFile() { |
166 |
if (tFile == null) { |
167 |
tFile = new JTextField(); |
168 |
tFile.setPreferredSize(new java.awt.Dimension(280,25)); |
169 |
} |
170 |
return tFile;
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* This method initializes jButton
|
175 |
*
|
176 |
* @return javax.swing.JButton
|
177 |
*/
|
178 |
private JButton getBSelectFile() { |
179 |
if (bSelectFile == null) { |
180 |
bSelectFile = new JButton(); |
181 |
bSelectFile.setText(PluginServices.getText(this, "cargar")); |
182 |
} |
183 |
return bSelectFile;
|
184 |
} |
185 |
|
186 |
/**
|
187 |
* This method initializes jPanel
|
188 |
*
|
189 |
* @return javax.swing.JPanel
|
190 |
*/
|
191 |
private ProjChooserPanel getPProyection() {
|
192 |
if (pProyection == null) { |
193 |
pProyection = new ProjChooserPanel(GeoreferencingDialog.getLastProjection());
|
194 |
((ProjChooserPanel)pProyection).addActionListener(new java.awt.event.ActionListener() {
|
195 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
196 |
if (((ProjChooserPanel)pProyection).isOkPressed()) {
|
197 |
FOpenDialog.setLastProjection(((ProjChooserPanel)pProyection).getCurProj()); |
198 |
} |
199 |
} |
200 |
}); |
201 |
} |
202 |
return ((ProjChooserPanel)pProyection);
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* Crea una capa Raster a partir del nombre driver, fichero y proyecci?n y
|
207 |
* coordenadas de georeferenciaci?n. Esta funci?n es para georeferenciar
|
208 |
* capas raster. Para imagenes que no tienen georeferenciaci?n hay que asignarle
|
209 |
* una temporal, normalmente a partir de la vista activa.
|
210 |
*
|
211 |
* @param layerName Nombre de la capa.
|
212 |
* @param d RasterDriver.
|
213 |
* @param f Fichero.
|
214 |
* @param proj Proyecci?n.
|
215 |
* @param extent Extent de la vista activa
|
216 |
*
|
217 |
* @return Nueva capa de tipo raster.
|
218 |
*
|
219 |
* @throws DriverIOException
|
220 |
*/
|
221 |
private FLyrGeoRaster createLayer(String layerName, RasterDriver d, |
222 |
File f, IProjection proj) throws DriverException { |
223 |
RasterAdapter adapter = new RasterFileAdapter(f);
|
224 |
adapter.setDriver(d); |
225 |
FLyrGeoRaster capa = new FLyrGeoRaster();
|
226 |
if (capa != null) { |
227 |
capa.setName(layerName); |
228 |
capa.setSource(adapter); |
229 |
capa.setProjection(proj); |
230 |
try {
|
231 |
capa.load(); |
232 |
} catch (DriverIOException e) {
|
233 |
throw new DriverException(e); |
234 |
} |
235 |
|
236 |
capa.setVisible(true);
|
237 |
}else
|
238 |
return null; |
239 |
return capa;
|
240 |
} |
241 |
|
242 |
/**
|
243 |
* Calcula un extent posible para la imagen a partir del extent de la vista.
|
244 |
* En este caso centra la imagen en la vista.
|
245 |
* @param w Ancho de la imagen
|
246 |
* @param h Alto de la imagen
|
247 |
* @return Extent para la imagen
|
248 |
*/
|
249 |
protected Extent calcTempExtent(String file, ViewPort vp, IProjection proj){ |
250 |
|
251 |
//Obtenemos el ancho y alto de la imagen y obtenemos un extent a partir
|
252 |
//del viewport.
|
253 |
Extent tempExtent = null;
|
254 |
GeoRasterFile grf = GeoRasterFile.openFile(proj, file); |
255 |
double w = widthPxImg = grf.getWidth();
|
256 |
double h = heightPxImg = grf.getHeight();
|
257 |
if(vp == null || vp.getAdjustedExtent() == null){ |
258 |
vp = new ViewPort(proj);
|
259 |
Rectangle2D r2d = new Rectangle2D.Double(0, 0, w, h); |
260 |
vp.setExtent(r2d); |
261 |
tempExtent = new Extent(0, 0, w, h); |
262 |
}else
|
263 |
tempExtent = new Extent(vp.getAdjustedExtent());
|
264 |
grf.close(); |
265 |
|
266 |
|
267 |
double ulX = 0D, ulY = 0D, lrX = 0D, lrY = 0D; |
268 |
if(w > h){
|
269 |
double widthView = tempExtent.maxX() - tempExtent.minX();
|
270 |
ulX = tempExtent.minX() + (widthView / 4);
|
271 |
lrX = ulX + (widthView / 2);
|
272 |
double newAlto = ((h * (widthView / 2)) / w); |
273 |
double centroY = tempExtent.minY()+((tempExtent.maxY() - tempExtent.minY())/2); |
274 |
ulY = centroY - (newAlto / 2);
|
275 |
lrY = centroY + (newAlto / 2);
|
276 |
}else{
|
277 |
double heightView = tempExtent.maxY() - tempExtent.minY();
|
278 |
ulY = tempExtent.minY() + (heightView / 4);
|
279 |
lrY = ulY + (heightView / 2);
|
280 |
double newAncho = ((w * (heightView / 2)) / h); |
281 |
double centroX = tempExtent.minX()+((tempExtent.maxX() - tempExtent.minX())/2); |
282 |
ulX = centroX - (newAncho / 2);
|
283 |
lrX = centroX + (newAncho / 2);
|
284 |
} |
285 |
return new Extent(ulX, ulY, lrX, lrY); |
286 |
} |
287 |
|
288 |
/**
|
289 |
* Evento de pulsado del bot?n de seleccionar fichero
|
290 |
* @param e
|
291 |
*/
|
292 |
private void acceptButtonActionPerformed(ActionEvent e) { |
293 |
//Selector de Fichero que se quiere georeferenciar
|
294 |
|
295 |
if( e.getSource().equals(this.getBSelectFile())){ |
296 |
JFileChooser chooser = new JFileChooser(lastPath); |
297 |
chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero")); |
298 |
FileFilter f = null; |
299 |
for(int i=0; i<this.fileFilters.length;i++){ |
300 |
f = new SelectFileFilter(chooser, this.fileFilters[i]); |
301 |
chooser.addChoosableFileFilter(f); |
302 |
} |
303 |
int returnVal = chooser.showOpenDialog(this); |
304 |
if(returnVal == JFileChooser.APPROVE_OPTION){ |
305 |
this.fName = chooser.getSelectedFile().toString();
|
306 |
FileFilter filter = chooser.getFileFilter();
|
307 |
this.getTFile().setText(fName);
|
308 |
lastPath = chooser.getCurrentDirectory().getAbsolutePath(); |
309 |
|
310 |
if (PluginServices.getMainFrame() == null) { |
311 |
((JDialog) (getParent().getParent().getParent()
|
312 |
.getParent())).dispose(); |
313 |
} else {
|
314 |
//Creamos la capa y la cargamos
|
315 |
IProjection proj = this.getPProyection().getCurProj();
|
316 |
try{
|
317 |
View theView = (View) PluginServices.getMDIManager().getActiveView(); |
318 |
|
319 |
com.hardcode.driverManager.Driver driver = LayerFactory.getDM().getDriver("gvSIG Image Driver");
|
320 |
|
321 |
//Si la ventana que hemos abierto es Singleton necesitamos esto para recuperar la vista.
|
322 |
//Si definitivamente no va a ser singleton se puede borrar este c?digo.
|
323 |
/*com.iver.andami.ui.mdiManager.View[] v = PluginServices.getMDIManager().getAllViews();
|
324 |
com.iver.cit.gvsig.gui.View theView = null;
|
325 |
for(int i=0; i<v.length;i++){
|
326 |
if(v[i] instanceof com.iver.cit.gvsig.gui.View &&
|
327 |
((com.iver.cit.gvsig.gui.View)v[i]).isFocusable())
|
328 |
theView = (com.iver.cit.gvsig.gui.View)v[i];
|
329 |
}
|
330 |
if(theView == null)
|
331 |
return;*/
|
332 |
|
333 |
File fich = new File(fName); |
334 |
|
335 |
ViewPort viewPort = theView.getMapControl().getMapContext().getViewPort(); |
336 |
|
337 |
Extent ext = calcTempExtent(fName, |
338 |
viewPort, |
339 |
proj); |
340 |
|
341 |
this.setLyrName("*"+fName.substring(fName.lastIndexOf("/")+1)); |
342 |
lyrGeoRaster = this.createLayer("*"+fName.substring(fName.lastIndexOf("/")+1, fName.length()), |
343 |
(RasterDriver) driver, fich, proj); |
344 |
|
345 |
if(!this.getCbUseGeoref().isSelected()) |
346 |
lyrGeoRaster.setAssignExtent(ext); |
347 |
|
348 |
|
349 |
if(lyrGeoRaster != null){ |
350 |
theView.getMapControl().getMapContext().getLayers().addLayer(lyrGeoRaster); |
351 |
lyrGeoRaster.getStackZoom().setInitExtent(ext); |
352 |
} |
353 |
|
354 |
}catch(DriverLoadException exc){
|
355 |
NotificationManager.addError("No se pudo acceder a los drivers", exc);
|
356 |
}catch(DriverException exc){
|
357 |
NotificationManager.addError("El driver ha producido un error", exc);
|
358 |
} |
359 |
parent.getTabPanel().setSelectedIndex(1);
|
360 |
} |
361 |
} |
362 |
} |
363 |
|
364 |
} |
365 |
|
366 |
/**
|
367 |
* @return Returns the lyrRaster.
|
368 |
*/
|
369 |
public FLyrGeoRaster getFLyrGeoRaster() {
|
370 |
return lyrGeoRaster;
|
371 |
} |
372 |
|
373 |
/**
|
374 |
* @param lyrRaster The lyrRaster to set.
|
375 |
*/
|
376 |
public void setFLyrGeoRaster(FLyrGeoRaster lyrGeoRaster) { |
377 |
this.lyrGeoRaster = lyrGeoRaster;
|
378 |
} |
379 |
|
380 |
/**
|
381 |
*
|
382 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
383 |
*
|
384 |
*/
|
385 |
class SelectFileFilter extends javax.swing.filechooser.FileFilter { |
386 |
|
387 |
private JFileChooser chooser = null; |
388 |
private String file = null; |
389 |
|
390 |
public SelectFileFilter(JFileChooser ch, String file){ |
391 |
this.chooser = ch;
|
392 |
this.file = file;
|
393 |
} |
394 |
|
395 |
public boolean accept(File f) { |
396 |
|
397 |
return f.isDirectory() || f.getName().toLowerCase().endsWith("."+file); |
398 |
} |
399 |
|
400 |
public String getDescription() { |
401 |
return file;
|
402 |
} |
403 |
|
404 |
} |
405 |
|
406 |
/**
|
407 |
* @return Returns the fName.
|
408 |
*/
|
409 |
public String getLyrName() { |
410 |
return lyrName;
|
411 |
} |
412 |
/**
|
413 |
* @param name The fName to set.
|
414 |
*/
|
415 |
public void setLyrName(String name) { |
416 |
lyrName = name; |
417 |
} |
418 |
/**
|
419 |
* This method initializes jPanel
|
420 |
*
|
421 |
* @return javax.swing.JPanel
|
422 |
*/
|
423 |
private JPanel getPCheckUseGeoref() { |
424 |
if (pCheckUseGeoref == null) { |
425 |
FlowLayout flowLayout1 = new FlowLayout(); |
426 |
flowLayout1.setAlignment(java.awt.FlowLayout.LEFT); |
427 |
lUseGeoref = new JLabel(); |
428 |
pCheckUseGeoref = new JPanel(); |
429 |
pCheckUseGeoref.setLayout(flowLayout1); |
430 |
pCheckUseGeoref.setPreferredSize(new java.awt.Dimension(370,31)); |
431 |
lUseGeoref.setText("Usar georeferenciaci?n de la imagen");
|
432 |
pCheckUseGeoref.add(getCbUseGeoref(), null);
|
433 |
pCheckUseGeoref.add(lUseGeoref, null);
|
434 |
} |
435 |
return pCheckUseGeoref;
|
436 |
} |
437 |
/**
|
438 |
* This method initializes jCheckBox
|
439 |
*
|
440 |
* @return javax.swing.JCheckBox
|
441 |
*/
|
442 |
private JCheckBox getCbUseGeoref() { |
443 |
if (cbUseGeoref == null) { |
444 |
cbUseGeoref = new JCheckBox(); |
445 |
cbUseGeoref.setSelected(true);
|
446 |
} |
447 |
return cbUseGeoref;
|
448 |
} |
449 |
} |