svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / DEMO / ComandosListener.java @ 559
History | View | Annotate | Download (12.1 KB)
1 |
/*
|
---|---|
2 |
* Created on 17-jun-2003
|
3 |
*
|
4 |
* Copyright (c) 2003
|
5 |
* Francisco Jos?Pearrubia Mart?ez
|
6 |
* IVER Tecnolog?s de la Informacin S.A.
|
7 |
* Salamanca 50
|
8 |
* 46005 Valencia ( SPAIN )
|
9 |
* +34 963163400
|
10 |
* mailto:fran@iver.es
|
11 |
* http://www.iver.es
|
12 |
*/
|
13 |
package com.iver.cit.gvsig.DEMO; |
14 |
|
15 |
import java.awt.Color; |
16 |
import java.awt.event.ActionEvent; |
17 |
import java.awt.event.ActionListener; |
18 |
import java.io.File; |
19 |
import java.io.FileNotFoundException; |
20 |
import java.io.IOException; |
21 |
import java.net.URL; |
22 |
import java.util.HashMap; |
23 |
import java.util.Map; |
24 |
|
25 |
import javax.swing.JDialog; |
26 |
import javax.swing.JFileChooser; |
27 |
import javax.swing.JOptionPane; |
28 |
|
29 |
import org.cresques.cts.IProjection; |
30 |
import org.cresques.cts.ProjectionPool; |
31 |
import org.geotools.data.DataSourceException; |
32 |
import org.geotools.data.DataStore; |
33 |
import org.geotools.data.FeatureSource; |
34 |
import org.geotools.data.arcsde.ArcSDEDataStoreFactory; |
35 |
import org.geotools.data.jdbc.ConnectionPool; |
36 |
import org.geotools.data.jdbc.fidmapper.BasicFIDMapper; |
37 |
import org.geotools.data.jdbc.fidmapper.TypedFIDMapper; |
38 |
import org.geotools.data.postgis.PostgisConnectionFactory; |
39 |
import org.geotools.data.postgis.PostgisDataStore; |
40 |
import org.geotools.data.postgis.PostgisDataStoreFactory; |
41 |
import org.geotools.data.shapefile.ShapefileDataStore; |
42 |
import org.geotools.gui.swing.worker.BlockingSwingWorker; |
43 |
import org.geotools.map.DefaultMapLayer; |
44 |
import org.geotools.map.MapLayer; |
45 |
import org.geotools.styling.Style; |
46 |
import org.geotools.styling.StyleBuilder; |
47 |
|
48 |
import com.hardcode.driverManager.DriverLoadException; |
49 |
import com.hardcode.gdbms.engine.data.DBDriver; |
50 |
import com.iver.cit.gvsig.fmap.FMap; |
51 |
import com.iver.cit.gvsig.fmap.NewMapControl; |
52 |
import com.iver.cit.gvsig.fmap.ViewPort; |
53 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
54 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
55 |
import com.iver.cit.gvsig.fmap.layers.CancelationException; |
56 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
57 |
import com.iver.cit.gvsig.fmap.layers.FLyrGT2; |
58 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
59 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
60 |
import com.iver.cit.gvsig.fmap.rendering.LegendFactory; |
61 |
import com.iver.cit.gvsig.gui.DataBaseOpenDialog; |
62 |
import com.iver.cit.gvsig.gui.FOpenDialog; |
63 |
import com.iver.cit.gvsig.gui.FileOpenDialog; |
64 |
import com.iver.cit.gvsig.gui.legendmanager.FLegendManagerWindow; |
65 |
import com.iver.mdiApp.App; |
66 |
import com.vividsolutions.jts.geom.LineString; |
67 |
import com.vividsolutions.jts.geom.MultiLineString; |
68 |
import com.vividsolutions.jts.geom.MultiPoint; |
69 |
import com.vividsolutions.jts.geom.Point; |
70 |
|
71 |
|
72 |
|
73 |
//import java.awt.Frame;
|
74 |
|
75 |
/**
|
76 |
* @author Administrador
|
77 |
*
|
78 |
* To change the template for this generated type comment go to
|
79 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
80 |
*/
|
81 |
public class ComandosListener implements ActionListener { |
82 |
private NewMapControl m_MapControl;
|
83 |
private FMap m_Mapa;
|
84 |
private SingleView theView;
|
85 |
|
86 |
static PostgisDataStoreFactory postGisFactory = new PostgisDataStoreFactory(); |
87 |
static ArcSDEDataStoreFactory arcSdeFactory = new ArcSDEDataStoreFactory(); |
88 |
Map remote;
|
89 |
|
90 |
private static final boolean WKB_ENABLED = true; |
91 |
|
92 |
|
93 |
public ComandosListener(NewMapControl mapa, SingleView Owner) { |
94 |
m_Mapa = mapa.getMapContext(); |
95 |
theView = Owner;
|
96 |
m_MapControl = mapa; |
97 |
} |
98 |
|
99 |
/**
|
100 |
* Load the data from the specified dataStore and construct a {@linkPlain Context context} with
|
101 |
* a default style.
|
102 |
*
|
103 |
* @param url The url of the shapefile to load.
|
104 |
* @param name DOCUMENT ME!
|
105 |
*
|
106 |
* @throws IOException is a I/O error occured.
|
107 |
* @throws DataSourceException if an error occured while reading the data source.
|
108 |
* @throws FileNotFoundException DOCUMENT ME!
|
109 |
*/
|
110 |
protected void loadLayer(DataStore store, String layerName) |
111 |
throws IOException, DataSourceException { |
112 |
final FeatureSource features = store.getFeatureSource(layerName);
|
113 |
|
114 |
// Create the style
|
115 |
final StyleBuilder builder = new StyleBuilder(); |
116 |
final Style style; |
117 |
Class geometryClass = features.getSchema().getDefaultGeometry().getType();
|
118 |
|
119 |
if (LineString.class.isAssignableFrom(geometryClass)
|
120 |
|| MultiLineString.class.isAssignableFrom(geometryClass)) { |
121 |
style = builder.createStyle(builder.createLineSymbolizer()); |
122 |
} else if (Point.class.isAssignableFrom(geometryClass) |
123 |
|| MultiPoint.class.isAssignableFrom(geometryClass)) { |
124 |
style = builder.createStyle(builder.createPointSymbolizer()); |
125 |
} else {
|
126 |
style = builder.createStyle(builder.createPolygonSymbolizer(Color.ORANGE, Color.BLACK, 1)); |
127 |
} |
128 |
|
129 |
final MapLayer layer = new DefaultMapLayer(features, style); |
130 |
layer.setTitle(layerName); |
131 |
|
132 |
FLyrGT2 lyrGT2 = new FLyrGT2(layer);
|
133 |
try {
|
134 |
m_Mapa.getLayers().addLayer(lyrGT2); |
135 |
m_Mapa.fireInvalidEvent(); |
136 |
} catch (CancelationException e) {
|
137 |
// TODO Auto-generated catch block
|
138 |
e.printStackTrace(); |
139 |
} |
140 |
|
141 |
} |
142 |
|
143 |
|
144 |
/**
|
145 |
*
|
146 |
*/
|
147 |
protected void addLayerGT2_Shp() { |
148 |
JFileChooser fileChooser = new JFileChooser(); // lastFolder); |
149 |
fileChooser.addChoosableFileFilter(new SimpleFileFilter("shp", "Shapefile (*.shp)")); |
150 |
|
151 |
int result = fileChooser.showOpenDialog(theView);
|
152 |
|
153 |
if (result == JFileChooser.APPROVE_OPTION) { |
154 |
File file = fileChooser.getSelectedFile();
|
155 |
// lastFolder = file.getParentFile();
|
156 |
|
157 |
try {
|
158 |
// Load the file
|
159 |
URL url = file.toURL();
|
160 |
|
161 |
DataStore store; |
162 |
|
163 |
// Para shapes
|
164 |
store = new ShapefileDataStore(url);
|
165 |
|
166 |
loadLayer(store, store.getTypeNames()[0]);
|
167 |
} catch (Throwable t) { |
168 |
JOptionPane.showMessageDialog(null, "An error occurred while loading the file", |
169 |
"Demo GT2", JOptionPane.ERROR_MESSAGE); |
170 |
t.printStackTrace(); |
171 |
} |
172 |
} |
173 |
} |
174 |
protected void addLayerGT2_PostGIS() |
175 |
{ |
176 |
remote = new HashMap(); |
177 |
remote.put("dbtype","postgis"); |
178 |
remote.put("host","213.172.37.189"); //Jos? Miguel |
179 |
remote.put("port", new Integer(5432)); |
180 |
remote.put("database", "geomarketing"); |
181 |
remote.put("user", "Geomarketing"); |
182 |
remote.put("passwd", "geomarketing"); |
183 |
remote.put("charset", ""); |
184 |
remote.put("namespace", ""); |
185 |
|
186 |
DataStore store; |
187 |
try {
|
188 |
store = postGisFactory.createDataStore(remote); |
189 |
loadLayer(store, "ferrocarril");
|
190 |
|
191 |
} catch (IOException e) { |
192 |
// TODO Auto-generated catch block
|
193 |
e.printStackTrace(); |
194 |
} |
195 |
|
196 |
|
197 |
} |
198 |
protected void addLayerGT2_ArcSDE() |
199 |
{ |
200 |
remote = new HashMap(); |
201 |
remote.put("dbtype","arcsde"); |
202 |
remote.put("server","192.168.0.165"); //Jos? Miguel |
203 |
remote.put("port", new Integer(5151)); |
204 |
remote.put("instance", "sde"); |
205 |
remote.put("user", "sde"); |
206 |
remote.put("password", "aquilina"); |
207 |
|
208 |
|
209 |
DataStore store; |
210 |
try {
|
211 |
store = arcSdeFactory.createDataStore(remote); |
212 |
loadLayer(store, "SDE.VIAS");
|
213 |
|
214 |
} catch (IOException e) { |
215 |
// TODO Auto-generated catch block
|
216 |
e.printStackTrace(); |
217 |
} |
218 |
|
219 |
|
220 |
} |
221 |
|
222 |
/* (non-Javadoc)
|
223 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
224 |
*/
|
225 |
public void actionPerformed(ActionEvent e) { |
226 |
// TODO LWS Esto debe venir de fuera (proyeccion del fichero, o de la vista)
|
227 |
IProjection proj = ProjectionPool.get("ed50utm30");
|
228 |
ViewPort vp = m_Mapa.getViewPort(); |
229 |
|
230 |
if (e.getActionCommand() == "ZOOM_TODO") { |
231 |
try {
|
232 |
vp.setExtent(m_Mapa.getFullExtent()); |
233 |
m_MapControl.drawMap(); |
234 |
} catch (DriverIOException e1) {
|
235 |
// TODO Auto-generated catch block
|
236 |
e1.printStackTrace(); |
237 |
} |
238 |
m_MapControl.drawMap(); |
239 |
} |
240 |
if (e.getActionCommand() == "ZOOM_PREVIO") { |
241 |
vp.setExtentPrev(); |
242 |
} |
243 |
if (e.getActionCommand() == "ADD_GT2_POSTGIS") { |
244 |
addLayerGT2_PostGIS(); |
245 |
} |
246 |
if (e.getActionCommand() == "ADD_GT2_SHP") { |
247 |
addLayerGT2_Shp(); |
248 |
} |
249 |
if (e.getActionCommand() == "ADD_GT2_ARCSDE") { |
250 |
addLayerGT2_ArcSDE(); |
251 |
} |
252 |
|
253 |
/* if (e.getActionCommand() == "OPEN_LOCATOR") {
|
254 |
//Set up the dialog that the button brings up.
|
255 |
JDialog dialog = new JDialog();
|
256 |
FPanelLocConfig m_panelLoc = new FPanelLocConfig(m_Owner);
|
257 |
m_panelLoc.setPreferredSize(m_panelLoc.getSize());
|
258 |
dialog.getContentPane().add(m_panelLoc);
|
259 |
dialog.setModal(true);
|
260 |
dialog.pack();
|
261 |
dialog.show();
|
262 |
} */
|
263 |
|
264 |
if (e.getActionCommand() == "PRUEBA") { |
265 |
int i;
|
266 |
double valor;
|
267 |
FLegendManagerWindow dlg = new FLegendManagerWindow();
|
268 |
|
269 |
dlg.show(); |
270 |
} |
271 |
|
272 |
if (e.getActionCommand() == "ZOOM_MAS") { |
273 |
m_MapControl.setTool("zoomrect");
|
274 |
} |
275 |
|
276 |
if (e.getActionCommand() == "ZOOM_MENOS") { |
277 |
m_MapControl.setTool("zoomout");
|
278 |
} |
279 |
|
280 |
if (e.getActionCommand() == "PAN") { |
281 |
m_MapControl.setTool("pan");
|
282 |
} |
283 |
|
284 |
if (e.getActionCommand() == "INFO") { |
285 |
m_MapControl.setTool("info");
|
286 |
} |
287 |
if (e.getActionCommand() == "MEDICION") { |
288 |
m_MapControl.setTool("medicion");
|
289 |
} |
290 |
if (e.getActionCommand() == "MEDIRAREA") { |
291 |
m_MapControl.setTool("area");
|
292 |
} |
293 |
|
294 |
if ((e.getActionCommand() == "ADD_LAYER") || (e.getActionCommand() == "ADD_MEMORY_LAYER")) { |
295 |
JDialog dlg = new JDialog(); |
296 |
dlg.setModal(true);
|
297 |
FileOpenDialog fileDlg = new FileOpenDialog(new Class[]{VectorialFileDriver.class}); |
298 |
DataBaseOpenDialog dbop = new DataBaseOpenDialog();
|
299 |
dbop.setClasses(new Class[]{DBDriver.class}); |
300 |
|
301 |
FOpenDialog fopen = new FOpenDialog();
|
302 |
fopen.addTab("Fichero", fileDlg);
|
303 |
fopen.addTab("Base de datos", dbop);
|
304 |
// fileDlg.setPreferredSize(fopen.getSize());
|
305 |
dlg.setSize(fopen.getSize()); |
306 |
dlg.getContentPane().add(fopen); |
307 |
dlg.pack(); |
308 |
dlg.show(); |
309 |
|
310 |
|
311 |
// TODO: Hacer lo de la capa WMS tambi?n.
|
312 |
if (fileDlg.getFiles() == null) return; |
313 |
FLayer lyr = null;
|
314 |
File[] files = fileDlg.getFiles(); |
315 |
for (int iFile=0; iFile < files.length; iFile++) |
316 |
{ |
317 |
File fich = files[iFile];
|
318 |
String layerName = fich.getName();
|
319 |
String layerPath = fich.getAbsolutePath();
|
320 |
|
321 |
try {
|
322 |
lyr = (FLyrVect) LayerFactory.createLayer(fileDlg.getDriverName(), fich, proj); |
323 |
|
324 |
lyr.setVisible(true);
|
325 |
lyr.setName(layerName); |
326 |
if (lyr != null ) |
327 |
{ |
328 |
try {
|
329 |
theView.getMapControl().getMapContext().getLayers().addLayer(lyr); |
330 |
} catch (CancelationException ex) {
|
331 |
// TODO Auto-generated catch block
|
332 |
ex.printStackTrace(); |
333 |
} |
334 |
// TODO: Poner una variable y dibujar solo cuando
|
335 |
// todas las capas hayan sido cargadas.
|
336 |
theView.getMapControl().drawMap(); |
337 |
theView.getTOC().refresh(); |
338 |
} |
339 |
|
340 |
} catch (DriverLoadException ex) {
|
341 |
ex.printStackTrace(); |
342 |
} catch (DriverIOException ex) {
|
343 |
// TODO Auto-generated catch block
|
344 |
ex.printStackTrace(); |
345 |
} |
346 |
} // for
|
347 |
return;
|
348 |
|
349 |
} |
350 |
|
351 |
/**
|
352 |
|
353 |
String pathFich = fc.getDirectory() + fc.getFile();
|
354 |
|
355 |
|
356 |
System.out.println(pathFich);
|
357 |
FLyrVect lyr = new FLyrVect(fc.getFile(),pathFich);
|
358 |
|
359 |
m_Mapa.AddLayer(lyr);
|
360 |
m_Owner.jLeyenda.Refresh();
|
361 |
|
362 |
}
|
363 |
|
364 |
**/
|
365 |
if (e.getActionCommand() == "EXPORT") { |
366 |
// Export jExport = new Export(m_MapControl);
|
367 |
} |
368 |
|
369 |
if (e.getActionCommand() == "VIEW_TABLE") { |
370 |
/* FLyrVect lyrSelec = m_Owner.getTOC().getFirstLyrVectSelected();
|
371 |
Table nuevaTabla = new Table();
|
372 |
nuevaTabla.setModel(new FTable(lyrSelec.getRecordset()));
|
373 |
JDialog myDialog = new JDialog(m_Owner, "Tabla de Atributos ("
|
374 |
+ nuevaTabla.getModel().getRecordsCount() + " registros)" );
|
375 |
myDialog.setContentPane(nuevaTabla);
|
376 |
|
377 |
// myViewer.addAttributeTableViewerListener(m_Owner);
|
378 |
myDialog.pack();
|
379 |
myDialog.show(); */
|
380 |
} |
381 |
} |
382 |
} |