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