gvsig-gdal / trunk / org.gvsig.gdal2 / org.gvsig.gdal2.app / org.gvsig.gdal2.app.gpx / src / main / java / org / gvsig / gdal / app / gpx / gui / GPXWizard.java @ 368
History | View | Annotate | Download (15 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.gdal.app.gpx.gui; |
23 |
|
24 |
import java.awt.FlowLayout; |
25 |
import java.awt.GridBagConstraints; |
26 |
import java.awt.GridBagLayout; |
27 |
import java.awt.event.MouseEvent; |
28 |
import java.awt.event.MouseListener; |
29 |
import java.io.File; |
30 |
import java.net.URL; |
31 |
import java.util.List; |
32 |
|
33 |
import javax.swing.JButton; |
34 |
import javax.swing.JLabel; |
35 |
import javax.swing.JPanel; |
36 |
import javax.swing.JScrollPane; |
37 |
import javax.swing.JTextArea; |
38 |
|
39 |
import org.gdal.gdal.gdal; |
40 |
import org.gdal.ogr.DataSource; |
41 |
import org.gdal.ogr.Feature; |
42 |
import org.gdal.ogr.FeatureDefn; |
43 |
import org.gdal.ogr.FieldDefn; |
44 |
import org.gdal.ogr.Layer; |
45 |
import org.gdal.ogr.ogr; |
46 |
import org.gvsig.andami.PluginServices; |
47 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
48 |
import org.gvsig.app.ApplicationLocator; |
49 |
import org.gvsig.app.ApplicationManager; |
50 |
import org.gvsig.app.gui.WizardPanel; |
51 |
import org.gvsig.app.gui.wizards.WizardListener; |
52 |
import org.gvsig.app.gui.wizards.WizardListenerSupport; |
53 |
import org.gvsig.app.project.ProjectManager; |
54 |
import org.gvsig.app.project.documents.view.ViewDocument; |
55 |
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
56 |
import org.gvsig.fmap.dal.DALLocator; |
57 |
import org.gvsig.fmap.dal.DataManager; |
58 |
import org.gvsig.fmap.dal.DataStoreParameters; |
59 |
import org.gvsig.fmap.dal.exception.InitializeException; |
60 |
import org.gvsig.fmap.dal.exception.OpenException; |
61 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
62 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
63 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
64 |
import org.gvsig.fmap.dal.store.gpx.OGRStoreParameters; |
65 |
import org.gvsig.fmap.dal.store.gpx.OGRStoreProvider; |
66 |
import org.gvsig.fmap.dal.store.gpx.OGRStoreProviderFactory; |
67 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
68 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
69 |
import org.gvsig.gdal.app.gpx.gui.utils.JFileField; |
70 |
import org.slf4j.Logger; |
71 |
import org.slf4j.LoggerFactory; |
72 |
|
73 |
/**
|
74 |
* Wizard panel to add a new OSM raster layer
|
75 |
* @author Jos? Bad?a (jbadia@scolab.es)
|
76 |
*/
|
77 |
public class GPXWizard extends WizardPanel implements MouseListener { |
78 |
private static final long serialVersionUID = 1L; |
79 |
private static final String TAB_NAME = "GDAL/OGR"; |
80 |
private static Logger logger = LoggerFactory.getLogger(GPXWizard.class.getName()); |
81 |
private AbstractViewPanel view = null; |
82 |
private WizardListenerSupport listenerSupport = null; |
83 |
|
84 |
private JPanel chooserPanel; |
85 |
private JPanel jPanel1; |
86 |
private JFileField cmbHost;
|
87 |
private JButton btnConnect; |
88 |
private JPanel jPanel; |
89 |
private JScrollPane jScrollPane; |
90 |
private JTextArea txtAbstract; |
91 |
private JPanel pnlName; |
92 |
private JLabel jLabel1; |
93 |
private JLabel lblTitle; |
94 |
|
95 |
private DataSource poDS = null; |
96 |
private File file = null; |
97 |
private OGRStoreParameters ogrStoreParameters;
|
98 |
|
99 |
|
100 |
public GPXWizard() {
|
101 |
super();
|
102 |
initialize(); |
103 |
} |
104 |
|
105 |
/**
|
106 |
* This method initializes this
|
107 |
*
|
108 |
* @return void
|
109 |
*/
|
110 |
private void initialize() { |
111 |
setTabName(PluginServices.getText(this, TAB_NAME));
|
112 |
setLayout(new GridBagLayout()); |
113 |
|
114 |
gdal.AllRegister(); |
115 |
ogr.RegisterAll(); |
116 |
|
117 |
IWindow iw = PluginServices.getMDIManager().getActiveWindow(); |
118 |
|
119 |
if (iw == null) { |
120 |
return;
|
121 |
} |
122 |
|
123 |
if (!(iw instanceof AbstractViewPanel)) { |
124 |
return;
|
125 |
} |
126 |
|
127 |
view = (AbstractViewPanel) iw; |
128 |
setMapCtrl(view.getMapControl()); |
129 |
|
130 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
131 |
gridBagConstraints.fill = GridBagConstraints.BOTH;
|
132 |
gridBagConstraints.weightx = 1.0;
|
133 |
gridBagConstraints.weighty = 1.0;
|
134 |
gridBagConstraints.gridx = 0;
|
135 |
gridBagConstraints.gridy = 0;
|
136 |
add(getFileChooserPanel(), gridBagConstraints); |
137 |
|
138 |
gridBagConstraints.weightx = 1.0;
|
139 |
gridBagConstraints.gridy = 1;
|
140 |
gridBagConstraints.weighty = 4;
|
141 |
add(getDescriptionPanel(), gridBagConstraints); |
142 |
} |
143 |
|
144 |
private WizardListenerSupport getWizardListenerSupport() {
|
145 |
if(listenerSupport == null) |
146 |
listenerSupport = new WizardListenerSupport();
|
147 |
return listenerSupport;
|
148 |
} |
149 |
|
150 |
/**
|
151 |
* This method checks for the options selected within the configuration
|
152 |
* dialog are correct.
|
153 |
*
|
154 |
* @return true if you're done, false if not.
|
155 |
*/
|
156 |
public boolean areSettingsValid() { |
157 |
return true; |
158 |
} |
159 |
|
160 |
public void addWizardListener(WizardListener listener) { |
161 |
getWizardListenerSupport().addWizardListener(listener); |
162 |
getWizardListenerSupport().callStateChanged(false);
|
163 |
} |
164 |
|
165 |
public void removeWizardListener(WizardListener listener) { |
166 |
getWizardListenerSupport().removeWizardListener(listener); |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public void close() { |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public void execute() { |
175 |
if(this.file != null){ |
176 |
isFileSupported(this.file);
|
177 |
|
178 |
// Si el fichero es soportado por OGR
|
179 |
if(poDS != null){ |
180 |
DataManager dataManager = DALLocator.getDataManager(); |
181 |
OGRStoreProviderFactory provFact = (OGRStoreProviderFactory) dataManager.getStoreProviderFactory(OGRStoreProvider.NAME); |
182 |
OGRStoreParameters params = (OGRStoreParameters) provFact.createParameters(); |
183 |
|
184 |
ApplicationManager application = ApplicationLocator.getManager(); |
185 |
ProjectManager projectManager = application.getProjectManager(); |
186 |
ViewDocument view = (ViewDocument) projectManager.getCurrentProject().getActiveDocument(); |
187 |
|
188 |
try {
|
189 |
|
190 |
FLyrVect layer = (FLyrVect) application.getMapContextManager().createLayer("prueba-ogr", openFile(this.file)); |
191 |
view.getMapContext().getLayers().addLayer(layer); |
192 |
} catch (LoadLayerException e) {
|
193 |
e.printStackTrace(); |
194 |
} |
195 |
} |
196 |
}else{
|
197 |
// TODO mensaje de error que no hay fichero escogido
|
198 |
} |
199 |
|
200 |
|
201 |
} |
202 |
|
203 |
private void isFileSupported(File path) { |
204 |
if(path != null){ |
205 |
lblTitle.setText(path.getAbsolutePath()); |
206 |
}else{
|
207 |
poDS = null;
|
208 |
lblTitle.setText("-");
|
209 |
return ;
|
210 |
} |
211 |
|
212 |
String aux = "Running under GDAL/OGR "+ gdal.VersionInfo() +" version.\n\n"; |
213 |
txtAbstract.setText(aux); |
214 |
|
215 |
if(ogr.GetDriverCount() != 0){ |
216 |
poDS = ogr.Open(path.getAbsolutePath(), false);
|
217 |
if(poDS != null){ |
218 |
aux = aux + "Opened file '"+ path.getName() +"' with "+ poDS.GetDriver().getName() +" driver.\n"; |
219 |
}else{
|
220 |
aux = aux + "ERROR: This file cannot be opened. Available drivers are the following:\n";
|
221 |
for(int i = 0; i<ogr.GetDriverCount(); i++){ |
222 |
aux = aux + ogr.GetDriver(i).getName(); |
223 |
if(i+1<ogr.GetDriverCount()){ |
224 |
aux = aux + ", ";
|
225 |
} |
226 |
} |
227 |
txtAbstract.setText(aux); |
228 |
return;
|
229 |
} |
230 |
}else{
|
231 |
aux = aux + "ERROR: GDAL/OGR driver are not initialized";
|
232 |
} |
233 |
txtAbstract.setText(aux); |
234 |
} |
235 |
|
236 |
private JPanel getFileChooserPanel() { |
237 |
if (chooserPanel == null) { |
238 |
chooserPanel = new javax.swing.JPanel();
|
239 |
chooserPanel.setLayout(null);
|
240 |
chooserPanel.setPreferredSize(new java.awt.Dimension(480, 220)); |
241 |
chooserPanel.setVisible(true);
|
242 |
chooserPanel.setBounds(15, 5, 480, 407); |
243 |
chooserPanel.add(getFilePanel(), null);
|
244 |
} |
245 |
|
246 |
return chooserPanel;
|
247 |
} |
248 |
|
249 |
private JPanel getFilePanel() { |
250 |
if (jPanel1 == null) { |
251 |
jPanel1 = new javax.swing.JPanel();
|
252 |
jPanel1.setLayout(null);
|
253 |
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder( |
254 |
null, PluginServices.getText(this, "file"), |
255 |
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
256 |
javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
257 |
jPanel1.setBounds(2, 5, 477, 85); |
258 |
jPanel1.add(getTxtHost(), null);
|
259 |
jPanel1.add(getBtnConnect(), null);
|
260 |
} |
261 |
|
262 |
return jPanel1;
|
263 |
} |
264 |
|
265 |
private JFileField getTxtHost() {
|
266 |
if (cmbHost == null) { |
267 |
cmbHost = new JFileField(null, "..."); |
268 |
cmbHost.setMode(JFileField.MODE_OPEN); |
269 |
cmbHost.getFileChooser().setCurrentDirectory(new File(System.getProperty("user.home"))); |
270 |
cmbHost.setPreferredSize(new java.awt.Dimension(350, 40)); |
271 |
cmbHost.setBounds(11, 26, 470, 30); |
272 |
//cmbHost.setEditable(true);
|
273 |
} |
274 |
return cmbHost;
|
275 |
} |
276 |
|
277 |
private JButton getBtnConnect() { |
278 |
if (btnConnect == null) { |
279 |
btnConnect = new JButton(); |
280 |
btnConnect.setPreferredSize(new java.awt.Dimension(100, 20)); |
281 |
btnConnect.setBounds(366, 60, 100, 20); |
282 |
btnConnect.setText(PluginServices.getText(this, "Cargar")); |
283 |
btnConnect.addActionListener(new java.awt.event.ActionListener() {
|
284 |
|
285 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
286 |
file = cmbHost.getFileChooser().getSelectedFile(); |
287 |
isFileSupported(file); |
288 |
|
289 |
String aux = txtAbstract.getText();
|
290 |
|
291 |
DataManager dataManager = DALLocator.getDataManager(); |
292 |
|
293 |
OGRStoreProviderFactory provFact = (OGRStoreProviderFactory) dataManager.getStoreProviderFactory(OGRStoreProvider.NAME); |
294 |
OGRStoreParameters params = (OGRStoreParameters) provFact.createParameters(); |
295 |
params.setFile(file); |
296 |
|
297 |
OGRStoreProvider provider = null;
|
298 |
try {
|
299 |
provider = (OGRStoreProvider) provFact.createProvider(params, null);
|
300 |
} catch (InitializeException e1) {
|
301 |
// TODO Auto-generated catch block
|
302 |
e1.printStackTrace(); |
303 |
} |
304 |
|
305 |
if(provider != null){ |
306 |
aux.concat("Running under GDAL/OGR "+ provider.getOGRVersion() +" version.\n"); |
307 |
try {
|
308 |
provider.open(); |
309 |
if(((OGRStoreParameters)provider.getParameters()).getDriver() != null){ |
310 |
aux.concat("Opened file '"+ file.getName() +"' with "+ ((OGRStoreParameters)provider.getParameters()).getDriver() +" driver.\n"); |
311 |
}else{
|
312 |
List<String> availableDrivers = provider.getAvailableDrivers(); |
313 |
aux.concat("ERROR: This file cannot be opened. Available drivers are the following:\n");
|
314 |
for(int i = 0; i<provider.getDriverCount(); i++){ |
315 |
aux.concat(provider.getDriver(i)+" ");
|
316 |
} |
317 |
} |
318 |
} catch (OpenException e1) {
|
319 |
// TODO Auto-generated catch block
|
320 |
e1.printStackTrace(); |
321 |
} |
322 |
} |
323 |
|
324 |
|
325 |
for(int index = 0; index < poDS.GetLayerCount();index++){ |
326 |
Layer layer = poDS.GetLayer(index); |
327 |
for(int feats = 0; feats < layer.GetFeatureCount();feats++){ |
328 |
Feature feat = layer.GetFeature(feats); |
329 |
FeatureDefn definition = feat.GetDefnRef(); |
330 |
for(int fields = 0; fields < definition.GetFieldCount();fields++){ |
331 |
FieldDefn field = definition.GetFieldDefn(fields); |
332 |
String fieldName = field.GetName();
|
333 |
int fieldType = field.GetFieldType();
|
334 |
switch(fieldType){
|
335 |
// Ejemplo -> Type 1 : Integer
|
336 |
case 1: |
337 |
// Crear una feature en la vista
|
338 |
int feature = feat.GetFieldAsInteger(field.GetFieldTypeName(fieldType));
|
339 |
|
340 |
// A?adirla
|
341 |
break;
|
342 |
default:
|
343 |
break;
|
344 |
} |
345 |
} |
346 |
} |
347 |
} |
348 |
|
349 |
|
350 |
|
351 |
} |
352 |
|
353 |
}); |
354 |
} |
355 |
|
356 |
return btnConnect;
|
357 |
} |
358 |
|
359 |
private JPanel getDescriptionPanel() { |
360 |
if (jPanel == null) { |
361 |
jPanel = new JPanel(); |
362 |
jPanel.setLayout(null);
|
363 |
jPanel.setPreferredSize(new java.awt.Dimension(480, 330)); |
364 |
jPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
|
365 |
PluginServices.getText(this, "descripcion"), |
366 |
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
367 |
javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
368 |
jPanel.setBounds(15, 5, 480, 407); |
369 |
jPanel.add(getJScrollPane(), null);
|
370 |
jPanel.add(getPnlName(), null);
|
371 |
} |
372 |
|
373 |
return jPanel;
|
374 |
} |
375 |
|
376 |
private JScrollPane getJScrollPane() { |
377 |
if (jScrollPane == null) { |
378 |
jScrollPane = new JScrollPane(); |
379 |
jScrollPane.setViewportView(getTxtAbstract()); |
380 |
jScrollPane.setPreferredSize(new java.awt.Dimension(450, 60)); |
381 |
jScrollPane.setBounds(10, 47, 457, 247); |
382 |
} |
383 |
|
384 |
return jScrollPane;
|
385 |
} |
386 |
|
387 |
private JTextArea getTxtAbstract() { |
388 |
if (txtAbstract == null) { |
389 |
txtAbstract = new JTextArea(); |
390 |
txtAbstract.setWrapStyleWord(true);
|
391 |
txtAbstract.setColumns(30);
|
392 |
txtAbstract.setLineWrap(true);
|
393 |
txtAbstract.setEditable(false);
|
394 |
} |
395 |
|
396 |
return txtAbstract;
|
397 |
} |
398 |
|
399 |
private JPanel getPnlName() { |
400 |
if (pnlName == null) { |
401 |
pnlName = new JPanel(); |
402 |
pnlName.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0)); |
403 |
pnlName.setBounds(new java.awt.Rectangle(9, 19, 452, 24)); |
404 |
pnlName.add(getJLabel1(), null);
|
405 |
pnlName.add(getLblTitle(), null);
|
406 |
} |
407 |
return pnlName;
|
408 |
} |
409 |
|
410 |
private JLabel getJLabel1() { |
411 |
if (jLabel1 == null) { |
412 |
jLabel1 = new javax.swing.JLabel();
|
413 |
jLabel1.setText(PluginServices.getText(this, "nombre") + ":"); |
414 |
jLabel1.setBounds(15, 26, 65, 15); |
415 |
} |
416 |
|
417 |
return jLabel1;
|
418 |
} |
419 |
|
420 |
private JLabel getLblTitle() { |
421 |
if (lblTitle == null) { |
422 |
lblTitle = new javax.swing.JLabel();
|
423 |
lblTitle.setText("-");
|
424 |
lblTitle.setPreferredSize(new java.awt.Dimension(350, 16)); |
425 |
lblTitle.setBounds(82, 26, 350, 16); |
426 |
} |
427 |
|
428 |
return lblTitle;
|
429 |
} |
430 |
|
431 |
@Override
|
432 |
public DataStoreParameters[] getParameters() { |
433 |
return null; |
434 |
} |
435 |
|
436 |
@Override
|
437 |
public void initWizard() { |
438 |
|
439 |
} |
440 |
|
441 |
public void mouseClicked(MouseEvent e) { |
442 |
|
443 |
} |
444 |
|
445 |
public void mouseEntered(MouseEvent e) { |
446 |
|
447 |
} |
448 |
|
449 |
public void mouseExited(MouseEvent e) { |
450 |
|
451 |
} |
452 |
|
453 |
public void mousePressed(MouseEvent e) { |
454 |
getWizardListenerSupport().callStateChanged(true);
|
455 |
} |
456 |
|
457 |
public void mouseReleased(MouseEvent e) { |
458 |
|
459 |
} |
460 |
|
461 |
/**
|
462 |
* Get a resource as a File from a path name in the
|
463 |
* class path.
|
464 |
*
|
465 |
* @param pathname
|
466 |
*
|
467 |
* @return resource as a File
|
468 |
*/
|
469 |
private File getResource(String pathname) { |
470 |
URL res = this.getClass().getClassLoader().getResource(pathname); |
471 |
return new File(res.getPath()); |
472 |
} |
473 |
|
474 |
private FeatureStore openFile(File shape) { |
475 |
// try {
|
476 |
//
|
477 |
// DataStoreParameters parameters;
|
478 |
// DataManager manager = DALLocator.getDataManager();
|
479 |
//
|
480 |
// parameters = manager.createStoreParameters("Shape");
|
481 |
// parameters.setDynValue("shpfile", shape);
|
482 |
// parameters.setDynValue("crs", "EPSG:23030");
|
483 |
// return (FeatureStore) manager.openStore("Shape", parameters);
|
484 |
//
|
485 |
// } catch (InitializeException e) {
|
486 |
// logger.error(e.getMessageStack());
|
487 |
// throw new RuntimeException(e);
|
488 |
// } catch (ProviderNotRegisteredException e) {
|
489 |
// logger.error(e.getMessageStack());
|
490 |
// throw new RuntimeException(e);
|
491 |
// } catch (ValidateDataParametersException e) {
|
492 |
// logger.error(e.getMessageStack());
|
493 |
// throw new RuntimeException(e);
|
494 |
// }
|
495 |
|
496 |
DataManager dataManager = DALLocator.getDataManager(); |
497 |
|
498 |
|
499 |
try {
|
500 |
if(this.ogrStoreParameters == null){ |
501 |
this.ogrStoreParameters =
|
502 |
(OGRStoreParameters) dataManager.createStoreParameters(OGRStoreProvider.NAME); |
503 |
} |
504 |
this.ogrStoreParameters.setFile(file);
|
505 |
return (FeatureStore) dataManager.openStore(
|
506 |
OGRStoreProvider.NAME, this.ogrStoreParameters);
|
507 |
} catch (ValidateDataParametersException e) {
|
508 |
e.printStackTrace(); |
509 |
} catch (InitializeException e) {
|
510 |
e.printStackTrace(); |
511 |
} catch (ProviderNotRegisteredException e) {
|
512 |
e.printStackTrace(); |
513 |
} |
514 |
return null; |
515 |
|
516 |
} |
517 |
} |