gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.main / src / main / java / org / gvsig / vectorediting / main / Main.java @ 80
History | View | Annotate | Download (15.4 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2012 gvSIG Association.
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.vectorediting.main; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Dimension; |
28 |
import java.awt.GridLayout; |
29 |
import java.awt.event.ActionEvent; |
30 |
import java.io.File; |
31 |
|
32 |
import javax.swing.AbstractAction; |
33 |
import javax.swing.JButton; |
34 |
import javax.swing.JFrame; |
35 |
import javax.swing.JLabel; |
36 |
import javax.swing.JMenu; |
37 |
import javax.swing.JMenuBar; |
38 |
import javax.swing.JMenuItem; |
39 |
import javax.swing.JTextField; |
40 |
import javax.swing.JToolBar; |
41 |
import javax.swing.WindowConstants; |
42 |
|
43 |
import org.cresques.cts.IProjection; |
44 |
import org.gvsig.fmap.crs.CRSFactory; |
45 |
import org.gvsig.fmap.dal.DALLocator; |
46 |
import org.gvsig.fmap.dal.DataManager; |
47 |
import org.gvsig.fmap.dal.DataStoreParameters; |
48 |
import org.gvsig.fmap.dal.exception.InitializeException; |
49 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
50 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
51 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
52 |
import org.gvsig.fmap.geom.primitive.Envelope; |
53 |
import org.gvsig.fmap.mapcontext.MapContext; |
54 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
55 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
56 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
57 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
58 |
import org.gvsig.fmap.mapcontext.layers.order.impl.TrivialLayerOrderManager; |
59 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
60 |
import org.gvsig.fmap.mapcontrol.MapControl; |
61 |
import org.gvsig.fmap.mapcontrol.MapControlCreationException; |
62 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
63 |
import org.gvsig.fmap.mapcontrol.MapControlManager; |
64 |
import org.gvsig.fmap.mapcontrol.tools.PanListenerImpl; |
65 |
import org.gvsig.fmap.mapcontrol.tools.ZoomInListenerImpl; |
66 |
import org.gvsig.fmap.mapcontrol.tools.ZoomOutRightButtonListener; |
67 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
68 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.MoveBehavior; |
69 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior; |
70 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.RectangleBehavior; |
71 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
72 |
import org.gvsig.vectorediting.lib.api.EditingLocator; |
73 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
74 |
import org.gvsig.vectorediting.lib.api.exceptions.CreateEditingBehaviorException; |
75 |
import org.gvsig.vectorediting.lib.prov.circlecr.CircleCREditingProviderFactory; |
76 |
import org.gvsig.vectorediting.lib.prov.insertpoint.InsertPointEditingProviderFactory; |
77 |
import org.gvsig.vectorediting.lib.prov.internalpolygon.InternalPolygonEditingProviderFactory; |
78 |
import org.gvsig.vectorediting.lib.prov.polyline.PolylineEditingProviderFactory; |
79 |
import org.gvsig.vectorediting.lib.prov.symmetry.SymmetryEditingProviderFactory; |
80 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
81 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
82 |
import org.gvsig.vectorediting.swing.api.EditingBehavior; |
83 |
import org.gvsig.vectorediting.swing.api.EditingSwingLocator; |
84 |
import org.gvsig.vectorediting.swing.api.EditingSwingManager; |
85 |
import org.gvsig.vectorediting.swing.impl.DefaultEditingBehavior; |
86 |
import org.gvsig.vectorediting.swing.impl.EditingCompoundBehavior; |
87 |
import org.slf4j.Logger; |
88 |
import org.slf4j.LoggerFactory; |
89 |
|
90 |
/**
|
91 |
* Main executable class for testing the VectorEditing library.
|
92 |
*
|
93 |
* @author gvSIG Team
|
94 |
* @version $Id$
|
95 |
*/
|
96 |
public class Main { |
97 |
|
98 |
private static final Logger logger = LoggerFactory.getLogger(Main.class); |
99 |
|
100 |
final EditingManager manager;
|
101 |
|
102 |
final EditingSwingManager swingManager;
|
103 |
|
104 |
final EditingProviderManager providerManager;
|
105 |
|
106 |
final MapControlManager mapControlManager;
|
107 |
|
108 |
final DataManager dataManager;
|
109 |
|
110 |
final MapContextManager mapContextManager;
|
111 |
|
112 |
EditingBehavior editingBehavior; |
113 |
|
114 |
MapControl mapControl; |
115 |
|
116 |
FeatureStore store; |
117 |
|
118 |
FLyrVect currentLayer; |
119 |
|
120 |
final String DEFAULT_CRS_CODE = "EPSG:23030"; |
121 |
|
122 |
IProjection defaultProjection; |
123 |
|
124 |
TrivialLayerOrderManager layerOrderManager; |
125 |
|
126 |
JFrame mainFrame;
|
127 |
|
128 |
JFrame addLayerFrame;
|
129 |
|
130 |
JFrame paletteTools;
|
131 |
|
132 |
JTextField shpField;
|
133 |
|
134 |
// Actions
|
135 |
AbstractAction pan;
|
136 |
|
137 |
AbstractAction zoom;
|
138 |
|
139 |
AbstractAction zoomall;
|
140 |
|
141 |
AbstractAction cleanMap;
|
142 |
|
143 |
AbstractAction openAddLayerDialog;
|
144 |
|
145 |
AbstractAction openPaletteTools;
|
146 |
|
147 |
AbstractAction editingExtensionAction;
|
148 |
|
149 |
AbstractAction endEditingExtensionAction;
|
150 |
|
151 |
AbstractAction insertPoint;
|
152 |
|
153 |
AbstractAction exit;
|
154 |
|
155 |
AbstractAction addLayer;
|
156 |
|
157 |
AbstractAction circleCr;
|
158 |
|
159 |
AbstractAction polyline;
|
160 |
|
161 |
AbstractAction symmetry;
|
162 |
|
163 |
AbstractAction internalPolygon;
|
164 |
|
165 |
public static void main(String args[]) { |
166 |
new DefaultLibrariesInitializer().fullInitialize();
|
167 |
Main main = new Main();
|
168 |
main.doMain(); |
169 |
} |
170 |
|
171 |
public Main() {
|
172 |
manager = EditingLocator.getManager(); |
173 |
swingManager = EditingSwingLocator.getSwingManager(); |
174 |
providerManager = EditingProviderLocator.getProviderManager(); |
175 |
mapControlManager = MapControlLocator.getMapControlManager(); |
176 |
dataManager = DALLocator.getDataManager(); |
177 |
mapContextManager = MapContextLocator.getMapContextManager(); |
178 |
defaultProjection = CRSFactory.getCRS(DEFAULT_CRS_CODE); |
179 |
layerOrderManager = new TrivialLayerOrderManager();
|
180 |
} |
181 |
|
182 |
public void doMain() { |
183 |
|
184 |
try {
|
185 |
mapControl = mapControlManager.createJMapControlPanel(); |
186 |
|
187 |
editingBehavior = new DefaultEditingBehavior(mapControl);
|
188 |
EditingCompoundBehavior editingCompoundBehavior = new EditingCompoundBehavior(editingBehavior);
|
189 |
mapControl.addBehavior("VectorEditing", editingCompoundBehavior);
|
190 |
|
191 |
mapControl.addBehavior("zoom", new Behavior[] { |
192 |
new RectangleBehavior(new ZoomInListenerImpl(mapControl)), |
193 |
new PointBehavior(new ZoomOutRightButtonListener(mapControl)) }); |
194 |
mapControl.addBehavior("pan", new MoveBehavior(new PanListenerImpl(mapControl))); |
195 |
|
196 |
|
197 |
IProjection defaultProjection = CRSFactory.getCRS(DEFAULT_CRS_CODE); |
198 |
mapControl.getViewPort().setProjection(defaultProjection); |
199 |
|
200 |
// Create actions
|
201 |
createActions(); |
202 |
} |
203 |
catch (MapControlCreationException e) {
|
204 |
logger.error("Can't create mapControl",e);
|
205 |
} catch (CreateEditingBehaviorException e) {
|
206 |
logger.error("Can't create EditingBehavior",e);
|
207 |
} |
208 |
|
209 |
// Create JFrame to show data
|
210 |
mainFrame = new JFrame("Editing test app"); |
211 |
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
212 |
mainFrame.setPreferredSize(new Dimension(800, 680)); |
213 |
mainFrame.add(mapControl, BorderLayout.CENTER);
|
214 |
|
215 |
// Create JFrame to add layer
|
216 |
createAddLayerJFrame(); |
217 |
|
218 |
// create JFrame palette tools
|
219 |
createShowPaletteTools(); |
220 |
|
221 |
// Create menu bar
|
222 |
createMenu(); |
223 |
|
224 |
// Create tools bar
|
225 |
createToolBar(); |
226 |
|
227 |
// Display the window.
|
228 |
mainFrame.pack(); |
229 |
mainFrame.setLocation(500, 0); |
230 |
mainFrame.setVisible(true);
|
231 |
} |
232 |
|
233 |
private void createAddLayerJFrame() { |
234 |
addLayerFrame = new JFrame("Add layer"); |
235 |
addLayerFrame.setLayout(new GridLayout(4, 2, 3, 3)); |
236 |
addLayerFrame.setPreferredSize(new Dimension(400, 150)); |
237 |
addLayerFrame.pack(); |
238 |
addLayerFrame.setLocation(250, 250); |
239 |
addLayerFrame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
|
240 |
|
241 |
JLabel shpLabel = new JLabel("Archivo SHP"); |
242 |
shpField = new JTextField(); |
243 |
|
244 |
addLayerFrame.add(shpLabel); |
245 |
addLayerFrame.add(shpField); |
246 |
addLayerFrame.add(new JLabel()); |
247 |
|
248 |
JButton aceptarButton = new JButton(addLayer); |
249 |
addLayerFrame.add(aceptarButton, BorderLayout.LINE_END);
|
250 |
|
251 |
} |
252 |
|
253 |
private void createShowPaletteTools() { |
254 |
paletteTools = new JFrame("Palette tools"); |
255 |
paletteTools.setLayout(new GridLayout(20, 1, 1, 5)); |
256 |
paletteTools.setPreferredSize(new Dimension(200, 600)); |
257 |
paletteTools.pack(); |
258 |
paletteTools.setLocation(0, 0); |
259 |
paletteTools.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
|
260 |
|
261 |
JButton insertPointButton = new JButton(insertPoint); |
262 |
JButton circleCrButton = new JButton(circleCr); |
263 |
JButton polylineButton = new JButton(polyline); |
264 |
JButton symmetryButton = new JButton(symmetry); |
265 |
JButton internalPolygonButton = new JButton(internalPolygon); |
266 |
|
267 |
paletteTools.add(new JLabel("Insert tools")); |
268 |
paletteTools.add(insertPointButton); |
269 |
paletteTools.add(circleCrButton); |
270 |
paletteTools.add(polylineButton); |
271 |
|
272 |
paletteTools.add(new JLabel("Modify tools")); |
273 |
paletteTools.add(symmetryButton); |
274 |
paletteTools.add(internalPolygonButton); |
275 |
} |
276 |
|
277 |
private void createToolBar() { |
278 |
JToolBar toolBar = new JToolBar(); |
279 |
|
280 |
toolBar.add(new JButton(openAddLayerDialog)); |
281 |
toolBar.add(new JButton(editingExtensionAction)); |
282 |
toolBar.add(new JButton(endEditingExtensionAction)); |
283 |
toolBar.add(new JButton(pan)); |
284 |
toolBar.add(new JButton(zoom)); |
285 |
toolBar.add(new JButton(zoomall)); |
286 |
toolBar.add(new JButton(cleanMap)); |
287 |
toolBar.add(new JButton(openPaletteTools)); |
288 |
|
289 |
mainFrame.add(toolBar, BorderLayout.PAGE_START);
|
290 |
} |
291 |
|
292 |
private void createMenu() { |
293 |
// Create the menu bar.
|
294 |
JMenuBar menuBar = new JMenuBar(); |
295 |
|
296 |
// Build the menu.
|
297 |
JMenu menuFile = new JMenu("File"); |
298 |
menuFile.add(new JMenuItem(exit)); |
299 |
|
300 |
JMenu menuLayer = new JMenu("Layer"); |
301 |
menuLayer.add(new JMenuItem(openAddLayerDialog)); |
302 |
menuLayer.add(new JMenuItem(pan)); |
303 |
menuLayer.add(new JMenuItem(zoom)); |
304 |
menuLayer.add(new JMenuItem(zoomall)); |
305 |
menuLayer.add(new JMenuItem(cleanMap)); |
306 |
|
307 |
JMenu menuTool = new JMenu("Tools"); |
308 |
menuTool.add(new JMenuItem(editingExtensionAction)); |
309 |
menuTool.add(new JMenuItem(endEditingExtensionAction)); |
310 |
menuTool.addSeparator(); |
311 |
menuTool.add(new JMenuItem(insertPoint)); |
312 |
menuTool.add(new JMenuItem(circleCr)); |
313 |
menuTool.add(new JMenuItem(polyline)); |
314 |
menuTool.add(new JMenuItem(symmetry)); |
315 |
|
316 |
menuBar.add(menuFile); |
317 |
menuBar.add(menuTool); |
318 |
|
319 |
mainFrame.setJMenuBar(menuBar); |
320 |
} |
321 |
|
322 |
@SuppressWarnings("serial") |
323 |
private void createActions() throws MapControlCreationException { |
324 |
|
325 |
pan = new AbstractAction("Pan") { |
326 |
|
327 |
public void actionPerformed(ActionEvent e) { |
328 |
mapControl.setTool("pan");
|
329 |
} |
330 |
}; |
331 |
|
332 |
zoom = new AbstractAction("Zoom") { |
333 |
|
334 |
public void actionPerformed(ActionEvent e) { |
335 |
mapControl.setTool("zoom");
|
336 |
} |
337 |
}; |
338 |
|
339 |
zoomall = new AbstractAction("Zoom all") { |
340 |
|
341 |
public void actionPerformed(ActionEvent e) { |
342 |
zoomAll(); |
343 |
} |
344 |
}; |
345 |
|
346 |
cleanMap = new AbstractAction("Clean Map") { |
347 |
|
348 |
public void actionPerformed(ActionEvent e) { |
349 |
MapContext mapContext = mapControl.getMapContext(); |
350 |
mapContext.beginAtomicEvent(); |
351 |
while (mapContext.getLayers().getLayersCount() > 0) { |
352 |
mapContext.getLayers().removeLayer(0);
|
353 |
} |
354 |
mapContext.endAtomicEvent(); |
355 |
|
356 |
} |
357 |
}; |
358 |
|
359 |
openAddLayerDialog = new AbstractAction("Add layer") { |
360 |
|
361 |
public void actionPerformed(ActionEvent e) { |
362 |
addLayerFrame.setVisible(true);
|
363 |
} |
364 |
}; |
365 |
|
366 |
openPaletteTools = new AbstractAction("Show palette tools") { |
367 |
|
368 |
public void actionPerformed(ActionEvent e) { |
369 |
paletteTools.setVisible(true);
|
370 |
} |
371 |
}; |
372 |
|
373 |
addLayer = new AbstractAction("OK") { |
374 |
|
375 |
public void actionPerformed(ActionEvent e) { |
376 |
String shpPath = shpField.getText();
|
377 |
|
378 |
try {
|
379 |
addLayer(shpPath); |
380 |
} |
381 |
catch (Exception e1) { |
382 |
logger.error("Problems adding layer");
|
383 |
e1.printStackTrace(); |
384 |
} |
385 |
addLayerFrame.setVisible(false);
|
386 |
} |
387 |
}; |
388 |
|
389 |
exit = new AbstractAction("Exit") { |
390 |
|
391 |
public void actionPerformed(ActionEvent e) { |
392 |
System.exit(0); |
393 |
} |
394 |
}; |
395 |
|
396 |
editingExtensionAction = new AbstractAction("EditingExtension") { |
397 |
|
398 |
public void actionPerformed(ActionEvent e) { |
399 |
swingManager.beginEdition(currentLayer, mapControl); |
400 |
} |
401 |
}; |
402 |
|
403 |
endEditingExtensionAction = new AbstractAction("EndEditingExtension") { |
404 |
|
405 |
public void actionPerformed(ActionEvent e) { |
406 |
swingManager.endEdition(currentLayer, mapControl); |
407 |
} |
408 |
}; |
409 |
|
410 |
insertPoint = new AbstractAction("InsertPointAction") { |
411 |
|
412 |
public void actionPerformed(ActionEvent e) { |
413 |
|
414 |
swingManager.activateService( |
415 |
InsertPointEditingProviderFactory.PROVIDER_NAME, mapControl); |
416 |
} |
417 |
|
418 |
}; |
419 |
|
420 |
circleCr = new AbstractAction("CircleCrAction") { |
421 |
|
422 |
public void actionPerformed(ActionEvent e) { |
423 |
swingManager.activateService(CircleCREditingProviderFactory.PROVIDER_NAME, |
424 |
mapControl); |
425 |
} |
426 |
}; |
427 |
|
428 |
|
429 |
polyline = new AbstractAction("PolylineAction") { |
430 |
|
431 |
public void actionPerformed(ActionEvent e) { |
432 |
swingManager.activateService(PolylineEditingProviderFactory.PROVIDER_NAME, |
433 |
mapControl); |
434 |
} |
435 |
}; |
436 |
|
437 |
|
438 |
symmetry = new AbstractAction("SymmetryAction") { |
439 |
|
440 |
public void actionPerformed(ActionEvent e) { |
441 |
swingManager.activateService(SymmetryEditingProviderFactory.PROVIDER_NAME, |
442 |
mapControl); |
443 |
} |
444 |
}; |
445 |
|
446 |
internalPolygon = new AbstractAction("InternalPolygonAction") { |
447 |
|
448 |
public void actionPerformed(ActionEvent e) { |
449 |
swingManager.activateService(InternalPolygonEditingProviderFactory.PROVIDER_NAME, |
450 |
mapControl); |
451 |
} |
452 |
}; |
453 |
|
454 |
} |
455 |
|
456 |
public void addLayer(String shpPath) throws InitializeException, |
457 |
ProviderNotRegisteredException, ValidateDataParametersException, |
458 |
LoadLayerException { |
459 |
DataStoreParameters params; |
460 |
params = dataManager.createStoreParameters("Shape");
|
461 |
if (shpPath.isEmpty()){
|
462 |
shpPath = "/home/paco/data/cartography/Andalucia/muni_andalucia.shp";
|
463 |
} |
464 |
|
465 |
String dbfPath = shpPath.substring(0, shpPath.length() - 3).concat("dbf"); |
466 |
String shxPath = shpPath.substring(0, shpPath.length() - 3).concat("shx"); |
467 |
|
468 |
File shpFile = new File(shpPath); |
469 |
File dbFile = new File(dbfPath); |
470 |
File shxFile = new File(shxPath); |
471 |
|
472 |
params.setDynValue("shpFile", shpFile.getPath());
|
473 |
params.setDynValue("dbfFile", dbFile.getPath());
|
474 |
params.setDynValue("shxFile", shxFile.getPath());
|
475 |
params.setDynValue("CRS", defaultProjection);
|
476 |
|
477 |
store = (FeatureStore) dataManager.openStore("Shape", params);
|
478 |
|
479 |
FLayer layer = mapContextManager.createLayer("prueba", store);
|
480 |
mapControl.getMapContext().setOrderManager(layerOrderManager); |
481 |
mapControl.getMapContext().getLayers().addLayer(layer); |
482 |
mapControl.getMapContext().getLayers().setActive(true);
|
483 |
try {
|
484 |
currentLayer = (FLyrVect) layer; |
485 |
} |
486 |
catch (Exception e) { |
487 |
logger.error("Can't load layer " + e.getMessage());
|
488 |
} |
489 |
|
490 |
} |
491 |
|
492 |
private void zoomAll() { |
493 |
MapContext mapContext = mapControl.getMapContext(); |
494 |
Envelope all = mapContext.getLayers().getFullEnvelope(); |
495 |
logger.info("Full extdents " + all.toString());
|
496 |
mapContext.getViewPort().setEnvelope(all); |
497 |
mapContext.invalidate(); |
498 |
} |
499 |
|
500 |
public JFrame getJFrame() { |
501 |
return this.addLayerFrame; |
502 |
} |
503 |
} |