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