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