gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.main / src / main / java / org / gvsig / vectorediting / main / Main.java @ 15
History | View | Annotate | Download (6.02 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.event.ActionEvent; |
29 |
|
30 |
import javax.swing.AbstractAction; |
31 |
import javax.swing.JButton; |
32 |
import javax.swing.JFrame; |
33 |
import javax.swing.JMenu; |
34 |
import javax.swing.JMenuBar; |
35 |
import javax.swing.JMenuItem; |
36 |
import javax.swing.JToolBar; |
37 |
import javax.swing.WindowConstants; |
38 |
|
39 |
import org.cresques.cts.IProjection; |
40 |
import org.gvsig.fmap.crs.CRSFactory; |
41 |
import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
42 |
import org.gvsig.fmap.mapcontrol.MapControl; |
43 |
import org.gvsig.fmap.mapcontrol.MapControlCreationException; |
44 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
45 |
import org.gvsig.fmap.mapcontrol.MapControlManager; |
46 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
47 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
48 |
import org.gvsig.vectorediting.lib.api.EditingBehavior; |
49 |
import org.gvsig.vectorediting.lib.api.EditingLocator; |
50 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
51 |
import org.gvsig.vectorediting.lib.impl.DefaultEditingBehavior; |
52 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
53 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
54 |
import org.slf4j.Logger; |
55 |
import org.slf4j.LoggerFactory; |
56 |
|
57 |
/**
|
58 |
* Main executable class for testing the VectorEditing library.
|
59 |
*
|
60 |
* @author gvSIG Team
|
61 |
* @version $Id$
|
62 |
*/
|
63 |
public class Main { |
64 |
|
65 |
private static final Logger logger = LoggerFactory.getLogger(Main.class); |
66 |
|
67 |
final EditingManager manager;
|
68 |
|
69 |
final EditingProviderManager providerManager;
|
70 |
|
71 |
final MapControlManager mapControlManager;
|
72 |
|
73 |
EditingBehavior editingBehavior; |
74 |
|
75 |
MapControl mapControl; |
76 |
|
77 |
LayerFactory layerFactory; |
78 |
|
79 |
JFrame mainFrame;
|
80 |
|
81 |
// Actions
|
82 |
AbstractAction editingExtensionAction;
|
83 |
|
84 |
AbstractAction insertPointAction;
|
85 |
|
86 |
AbstractAction exit;
|
87 |
|
88 |
AbstractAction addLayer;
|
89 |
|
90 |
final String DEFAULT_CRS_CODE = "EPSG:4326"; |
91 |
|
92 |
public static void main(String args[]) { |
93 |
new DefaultLibrariesInitializer().fullInitialize();
|
94 |
Main main = new Main();
|
95 |
main.doMain(); |
96 |
} |
97 |
|
98 |
public Main() {
|
99 |
manager = EditingLocator.getManager(); |
100 |
providerManager = EditingProviderLocator.getProviderManager(); |
101 |
mapControlManager = MapControlLocator.getMapControlManager(); |
102 |
} |
103 |
|
104 |
public void doMain() { |
105 |
|
106 |
try {
|
107 |
mapControl = mapControlManager.createJMapControlPanel(); |
108 |
editingBehavior = new DefaultEditingBehavior(mapControl);
|
109 |
layerFactory = LayerFactory.getInstance(); |
110 |
} |
111 |
catch (MapControlCreationException e) {
|
112 |
// TODO Auto-generated catch block
|
113 |
e.printStackTrace(); |
114 |
} |
115 |
|
116 |
IProjection defaultProjection = CRSFactory.getCRS(DEFAULT_CRS_CODE); |
117 |
mapControl.getViewPort().setProjection(defaultProjection); |
118 |
mapControl.addBehavior("VectorEditing", (Behavior) editingBehavior);
|
119 |
|
120 |
// Create actions
|
121 |
try {
|
122 |
createActions(); |
123 |
} |
124 |
catch (MapControlCreationException e) {
|
125 |
// TODO Auto-generated catch block
|
126 |
e.printStackTrace(); |
127 |
} |
128 |
|
129 |
// Create JFrame to show data
|
130 |
mainFrame = new JFrame("Editing test app"); |
131 |
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
132 |
mainFrame.setPreferredSize(new Dimension(600, 480)); |
133 |
mainFrame.add(mapControl, BorderLayout.CENTER);
|
134 |
|
135 |
// Create menu bar
|
136 |
createMenu(); |
137 |
|
138 |
// Create tools bar
|
139 |
createToolBar(); |
140 |
|
141 |
// Display the window.
|
142 |
mainFrame.pack(); |
143 |
mainFrame.setLocation(100, 100); |
144 |
mainFrame.setVisible(true);
|
145 |
} |
146 |
|
147 |
private void createToolBar() { |
148 |
JToolBar toolBar = new JToolBar(); |
149 |
|
150 |
toolBar.add(new JButton(editingExtensionAction)); |
151 |
toolBar.add(new JButton(insertPointAction)); |
152 |
|
153 |
mainFrame.add(toolBar, BorderLayout.PAGE_START);
|
154 |
} |
155 |
|
156 |
private void createMenu() { |
157 |
// Create the menu bar.
|
158 |
JMenuBar menuBar = new JMenuBar(); |
159 |
|
160 |
// Build the menu.
|
161 |
JMenu menuFile = new JMenu("File"); |
162 |
menuFile.add(new JMenuItem(addLayer)); |
163 |
menuFile.add(new JMenuItem(exit)); |
164 |
|
165 |
JMenu menuTool = new JMenu("Tools"); |
166 |
menuTool.add(new JMenuItem(editingExtensionAction)); |
167 |
menuTool.addSeparator(); |
168 |
menuTool.add(new JMenuItem(insertPointAction)); |
169 |
|
170 |
|
171 |
menuBar.add(menuFile); |
172 |
menuBar.add(menuTool); |
173 |
|
174 |
mainFrame.setJMenuBar(menuBar); |
175 |
} |
176 |
|
177 |
@SuppressWarnings("serial") |
178 |
private void createActions() throws MapControlCreationException { |
179 |
|
180 |
addLayer = new AbstractAction("Add layer"){ |
181 |
public void actionPerformed(ActionEvent e) { |
182 |
//Do Nothing
|
183 |
} |
184 |
}; |
185 |
|
186 |
exit = new AbstractAction("Exit") { |
187 |
|
188 |
public void actionPerformed(ActionEvent e) { |
189 |
System.exit(0); |
190 |
} |
191 |
}; |
192 |
|
193 |
editingExtensionAction = new AbstractAction("EditingExtension") { |
194 |
|
195 |
public void actionPerformed(ActionEvent e) { |
196 |
mapControl.setTool("VectorEditing");
|
197 |
|
198 |
updateControls(); |
199 |
} |
200 |
}; |
201 |
|
202 |
insertPointAction = new AbstractAction("InsertPointAction") { |
203 |
|
204 |
public void actionPerformed(ActionEvent e) { |
205 |
activateTool("InsertPoint");
|
206 |
} |
207 |
|
208 |
}; |
209 |
insertPointAction.setEnabled(false);
|
210 |
|
211 |
} |
212 |
|
213 |
private void updateControls() { |
214 |
insertPointAction.setEnabled(true);
|
215 |
} |
216 |
|
217 |
private void activateTool(String name) { |
218 |
manager.activateTool(name, mapControl); |
219 |
} |
220 |
} |