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