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