gvsig-derived-geometries / org.gvsig.derivedgeometries / trunk / org.gvsig.derivedgeometries / org.gvsig.derivedgeometries.main / src / main / java / org / gvsig / derivedgeometries / main / Main.java @ 62
History | View | Annotate | Download (8.37 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2014 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.derivedgeometries.main; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.io.File; |
29 |
|
30 |
import javax.swing.AbstractAction; |
31 |
import javax.swing.JButton; |
32 |
import javax.swing.JComponent; |
33 |
import javax.swing.JFrame; |
34 |
import javax.swing.JMenu; |
35 |
import javax.swing.JMenuBar; |
36 |
import javax.swing.JMenuItem; |
37 |
import javax.swing.JOptionPane; |
38 |
import javax.swing.JToolBar; |
39 |
import javax.swing.WindowConstants; |
40 |
|
41 |
import org.cresques.cts.IProjection; |
42 |
|
43 |
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesLocator; |
44 |
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesManager; |
45 |
import org.gvsig.fmap.crs.CRSFactory; |
46 |
import org.gvsig.fmap.dal.DALLocator; |
47 |
import org.gvsig.fmap.dal.DataManager; |
48 |
import org.gvsig.fmap.dal.DataStoreParameters; |
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.mapcontext.MapContextLocator; |
54 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
55 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
56 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
57 |
import org.gvsig.fmap.mapcontrol.MapControl; |
58 |
import org.gvsig.fmap.mapcontrol.MapControlCreationException; |
59 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
60 |
import org.gvsig.fmap.mapcontrol.MapControlManager; |
61 |
import org.gvsig.fmap.mapcontrol.tools.PanListenerImpl; |
62 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.MoveBehavior; |
63 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
64 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
65 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
66 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
67 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE; |
68 |
|
69 |
public class Main { |
70 |
|
71 |
final String DEFAULT_CRS_CODE = "EPSG:23030"; |
72 |
|
73 |
private DerivedGeometriesManager derivedGeometriesManager;
|
74 |
private MapControlManager mapControlManager;
|
75 |
private DataManager dataManager;
|
76 |
private MapContextManager mapContextManager;
|
77 |
private WindowManager windowManager;
|
78 |
|
79 |
private MapControl mapControl;
|
80 |
|
81 |
private JFrame mainFrame; |
82 |
|
83 |
// Actions
|
84 |
private AbstractAction derivedGeometries; |
85 |
|
86 |
private AbstractAction openAddLayerDialog; |
87 |
|
88 |
private AbstractAction exit; |
89 |
|
90 |
public static void main(String[] args) { |
91 |
new DefaultLibrariesInitializer().fullInitialize();
|
92 |
Main main = new Main();
|
93 |
main.doMain(); |
94 |
|
95 |
} |
96 |
|
97 |
public Main() {
|
98 |
mapControlManager = MapControlLocator.getMapControlManager(); |
99 |
derivedGeometriesManager = DerivedGeometriesLocator.getManager(); |
100 |
dataManager = DALLocator.getDataManager(); |
101 |
mapContextManager = MapContextLocator.getMapContextManager(); |
102 |
windowManager = ToolsSwingLocator.getWindowManager(); |
103 |
mapContextManager.getSymbolManager().getSymbolPreferences() |
104 |
.setDefaultSymbolFillColorAleatory(true);
|
105 |
|
106 |
// Register our DummyLayerOrderManger needed to add sample layer
|
107 |
MapContextLocator |
108 |
.registerDefaultOrderManager(DummyLayerOrderManager.class); |
109 |
} |
110 |
|
111 |
private void doMain() { |
112 |
try {
|
113 |
mapControl = mapControlManager.createJMapControlPanel(mapContextManager.createMapContext()); |
114 |
} catch (MapControlCreationException e) {
|
115 |
// TODO Auto-generated catch block
|
116 |
} |
117 |
|
118 |
mapControl.addBehavior("pan", new MoveBehavior(new PanListenerImpl( |
119 |
mapControl))); |
120 |
|
121 |
mapControl.setTool("pan");
|
122 |
|
123 |
IProjection defaultProjection = CRSFactory.getCRS(DEFAULT_CRS_CODE); |
124 |
mapControl.getViewPort().setProjection(defaultProjection); |
125 |
|
126 |
// Create JFrame to show data
|
127 |
mainFrame = new JFrame("Editing test app"); |
128 |
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
129 |
mainFrame.setPreferredSize(new Dimension(800, 680)); |
130 |
mainFrame.add(mapControl, BorderLayout.CENTER);
|
131 |
|
132 |
// Create actions
|
133 |
createActions(); |
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(500, 0); |
144 |
mainFrame.setVisible(true);
|
145 |
} |
146 |
|
147 |
private void createToolBar() { |
148 |
JToolBar toolBar = new JToolBar(); |
149 |
|
150 |
toolBar.add(new JButton(openAddLayerDialog)); |
151 |
toolBar.add(new JButton(derivedGeometries)); |
152 |
|
153 |
mainFrame.add(toolBar, BorderLayout.PAGE_START);
|
154 |
|
155 |
} |
156 |
|
157 |
private void createMenu() { |
158 |
// Create the menu bar.
|
159 |
JMenuBar menuBar = new JMenuBar(); |
160 |
|
161 |
// Build the menu.
|
162 |
JMenu menuFile = new JMenu("File"); |
163 |
menuFile.add(openAddLayerDialog); |
164 |
menuFile.add(new JMenuItem(exit)); |
165 |
|
166 |
mainFrame.setJMenuBar(menuBar); |
167 |
|
168 |
} |
169 |
|
170 |
@SuppressWarnings("serial") |
171 |
private void createActions() { |
172 |
|
173 |
derivedGeometries = new AbstractAction("DerivedGeometries") { |
174 |
|
175 |
public void actionPerformed(ActionEvent e) { |
176 |
JComponent panel =
|
177 |
derivedGeometriesManager.getDerivedGeometriesPanel( |
178 |
mapControl).asJComponent(); |
179 |
|
180 |
windowManager.showWindow(panel, "derived_geometries",
|
181 |
MODE.WINDOW); |
182 |
} |
183 |
}; |
184 |
|
185 |
openAddLayerDialog = new AbstractAction("Add sample layers") { |
186 |
|
187 |
public void actionPerformed(ActionEvent e) { |
188 |
try {
|
189 |
addLayer("src/main/resources/sample-cartography/puertos_andalucia.shp");
|
190 |
addLayer("src/main/resources/sample-cartography/hidro_andalucia.shp");
|
191 |
addLayer("src/main/resources/sample-cartography/Provincias andalucia.shp");
|
192 |
|
193 |
ThreadSafeDialogsManager dlgManager = |
194 |
ToolsSwingLocator.getThreadSafeDialogsManager(); |
195 |
|
196 |
StringBuilder stb = new StringBuilder(); |
197 |
stb.append("Layer: Provincias_andalucia Type: multisurface\n");
|
198 |
stb.append("Layer: hidro_andalucia Type: multicurve\n");
|
199 |
stb.append("Layer: puertos_andalucia Type: point\n");
|
200 |
|
201 |
dlgManager.messageDialog(stb.toString(), |
202 |
"Sample layers added succesfully",
|
203 |
JOptionPane.INFORMATION_MESSAGE);
|
204 |
} catch (Exception e1) { |
205 |
// TODO Auto-generated catch block
|
206 |
e1.printStackTrace(); |
207 |
} |
208 |
} |
209 |
}; |
210 |
|
211 |
exit = new AbstractAction("Exit") { |
212 |
|
213 |
public void actionPerformed(ActionEvent e) { |
214 |
System.exit(0); |
215 |
} |
216 |
}; |
217 |
|
218 |
} |
219 |
|
220 |
public void addLayer(String shpPath) throws InitializeException, |
221 |
ProviderNotRegisteredException, ValidateDataParametersException, |
222 |
LoadLayerException { |
223 |
DataStoreParameters params; |
224 |
params = dataManager.createStoreParameters("Shape");
|
225 |
|
226 |
File shpFile = new File(shpPath); |
227 |
|
228 |
params.setDynValue("shpFile", shpFile.getPath());
|
229 |
params.setDynValue("CRS", CRSFactory.getCRS(DEFAULT_CRS_CODE));
|
230 |
params.validate(); |
231 |
|
232 |
FeatureStore store = |
233 |
(FeatureStore) dataManager.openStore("Shape", params);
|
234 |
|
235 |
FLayer layer = mapContextManager.createLayer(store.getName(), store); |
236 |
|
237 |
mapControl.getMapContext().getLayers().addLayer(layer); |
238 |
mapControl.getMapContext().getLayers().setActive(true);
|
239 |
layer.dispose(); |
240 |
|
241 |
} |
242 |
|
243 |
} |