gvsig-3d / 2.1 / trunk / org.gvsig.view3d / org.gvsig.view3d.main / src / main / java / org / gvsig / view3d / main / Main.java @ 448
History | View | Annotate | Download (12.6 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2015 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 |
|
25 |
package org.gvsig.view3d.main; |
26 |
|
27 |
import java.awt.BorderLayout; |
28 |
import java.awt.Color; |
29 |
import java.awt.Dimension; |
30 |
import java.awt.event.MouseEvent; |
31 |
import java.awt.event.MouseListener; |
32 |
|
33 |
import javax.swing.JButton; |
34 |
import javax.swing.JFrame; |
35 |
import javax.swing.JMenu; |
36 |
import javax.swing.JMenuBar; |
37 |
import javax.swing.JMenuItem; |
38 |
import javax.swing.JOptionPane; |
39 |
import javax.swing.JPanel; |
40 |
import javax.swing.JToolBar; |
41 |
import javax.swing.JTree; |
42 |
import javax.swing.SwingUtilities; |
43 |
import javax.swing.WindowConstants; |
44 |
import javax.swing.tree.DefaultMutableTreeNode; |
45 |
import javax.swing.tree.DefaultTreeModel; |
46 |
import javax.swing.tree.TreePath; |
47 |
import javax.swing.tree.TreeSelectionModel; |
48 |
|
49 |
import org.cresques.cts.IProjection; |
50 |
import org.slf4j.Logger; |
51 |
import org.slf4j.LoggerFactory; |
52 |
|
53 |
import org.gvsig.fmap.crs.CRSFactory; |
54 |
import org.gvsig.fmap.dal.DALLocator; |
55 |
import org.gvsig.fmap.dal.DataManager; |
56 |
import org.gvsig.fmap.mapcontext.MapContext; |
57 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
58 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
59 |
import org.gvsig.fmap.mapcontext.ViewPort; |
60 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
61 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
62 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent; |
63 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener; |
64 |
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent; |
65 |
import org.gvsig.fmap.mapcontrol.MapControl; |
66 |
import org.gvsig.fmap.mapcontrol.MapControlCreationException; |
67 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
68 |
import org.gvsig.fmap.mapcontrol.MapControlManager; |
69 |
import org.gvsig.fmap.mapcontrol.tools.PanListenerImpl; |
70 |
import org.gvsig.fmap.mapcontrol.tools.PointSelectionListener; |
71 |
import org.gvsig.fmap.mapcontrol.tools.ZoomInListenerImpl; |
72 |
import org.gvsig.fmap.mapcontrol.tools.ZoomOutListenerImpl; |
73 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
74 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.MoveBehavior; |
75 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior; |
76 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.RectangleBehavior; |
77 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
78 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
79 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
80 |
import org.gvsig.view3d.swing.api.View3DSwingLocator; |
81 |
import org.gvsig.view3d.swing.api.View3DSwingManager; |
82 |
|
83 |
/**
|
84 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
85 |
*
|
86 |
*/
|
87 |
public class Main { |
88 |
|
89 |
private static final Logger logger = LoggerFactory.getLogger(Main.class); |
90 |
|
91 |
final View3DSwingManager view3DManager;
|
92 |
|
93 |
final MapControlManager mapControlManager;
|
94 |
|
95 |
final DataManager dataManager;
|
96 |
|
97 |
final MapContextManager mapContextManager;
|
98 |
|
99 |
final String DEFAULT_CRS_CODE = "EPSG:4326"; |
100 |
|
101 |
final PaletteActions paletteActions;
|
102 |
|
103 |
IProjection defaultProjection; |
104 |
|
105 |
MapControl mapControl; |
106 |
|
107 |
JFrame mainFrame;
|
108 |
|
109 |
JTree tree;
|
110 |
|
111 |
DefaultTreeModel treeModel;
|
112 |
|
113 |
DefaultMutableTreeNode rootNode;
|
114 |
|
115 |
MouseListener treeMouseListener;
|
116 |
|
117 |
public Main() {
|
118 |
view3DManager = View3DSwingLocator.getManager(); |
119 |
mapControlManager = MapControlLocator.getMapControlManager(); |
120 |
dataManager = DALLocator.getDataManager(); |
121 |
mapContextManager = MapContextLocator.getMapContextManager(); |
122 |
defaultProjection = CRSFactory.getCRS(DEFAULT_CRS_CODE); |
123 |
paletteActions = new PaletteActions(this); |
124 |
|
125 |
MapContextLocator |
126 |
.registerDefaultOrderManager(DummieLayerOrderManager.class); |
127 |
} |
128 |
|
129 |
public static void main(String[] args) { |
130 |
new DefaultLibrariesInitializer().fullInitialize();
|
131 |
Main main = new Main();
|
132 |
main.doMain(); |
133 |
} |
134 |
|
135 |
private void doMain() { |
136 |
|
137 |
try {
|
138 |
MapContext mapContext = |
139 |
new MapContext(new ViewPort(defaultProjection)); |
140 |
mapControl = mapControlManager.createJMapControlPanel(mapContext); |
141 |
|
142 |
addCollecionListener(); |
143 |
|
144 |
mapControl.addBehavior("zoom", new Behavior[] { |
145 |
new RectangleBehavior(new ZoomInListenerImpl(mapControl)), |
146 |
new PointBehavior(new ZoomOutListenerImpl(mapControl), |
147 |
Behavior.BUTTON_RIGHT) }); |
148 |
mapControl.addBehavior("pan", new MoveBehavior(new PanListenerImpl( |
149 |
mapControl))); |
150 |
|
151 |
mapControl.addBehavior("pointSelection",
|
152 |
new Behavior[] { new PointBehavior(new PointSelectionListener( |
153 |
mapControl)) }); |
154 |
|
155 |
IProjection defaultProjection = CRSFactory.getCRS(DEFAULT_CRS_CODE); |
156 |
mapControl.getViewPort().setProjection(defaultProjection); |
157 |
|
158 |
mapControl.setTool("pan");
|
159 |
|
160 |
} catch (MapControlCreationException e) {
|
161 |
logger.error("Can't create mapControl", e);
|
162 |
} |
163 |
|
164 |
// Create JFrame to show data
|
165 |
mainFrame = new JFrame("View 3D test app"); |
166 |
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
167 |
mainFrame.setPreferredSize(new Dimension(1055, 680)); |
168 |
mainFrame.add(mapControl, BorderLayout.CENTER);
|
169 |
|
170 |
// Create menu bar
|
171 |
createMenu(); |
172 |
|
173 |
// Create tools bar
|
174 |
createToolBar(); |
175 |
|
176 |
// Create simple TOC
|
177 |
createTOC(); |
178 |
|
179 |
// Display the window.
|
180 |
mainFrame.pack(); |
181 |
mainFrame.setLocation(100, 0); |
182 |
mainFrame.setVisible(true);
|
183 |
|
184 |
} |
185 |
|
186 |
private void addCollecionListener() { |
187 |
mapControl.getMapContext().getLayers() |
188 |
.addLayerCollectionListener(new LayerCollectionListener() {
|
189 |
|
190 |
public void visibilityChanged(LayerCollectionEvent e) |
191 |
throws CancelationException {
|
192 |
} |
193 |
|
194 |
public void layerRemoving(LayerCollectionEvent e) |
195 |
throws CancelationException {
|
196 |
} |
197 |
|
198 |
public void layerRemoved(LayerCollectionEvent e) { |
199 |
} |
200 |
|
201 |
public void layerMoving(LayerPositionEvent e) |
202 |
throws CancelationException {
|
203 |
} |
204 |
|
205 |
public void layerMoved(LayerPositionEvent e) { |
206 |
} |
207 |
|
208 |
public void layerAdding(LayerCollectionEvent e) |
209 |
throws CancelationException {
|
210 |
} |
211 |
|
212 |
public void layerAdded(LayerCollectionEvent e) { |
213 |
FLayer layer = e.getAffectedLayer(); |
214 |
DefaultMutableTreeNode layerNode =
|
215 |
new DefaultMutableTreeNode(layer.getName()); |
216 |
getRootNode().add(layerNode); |
217 |
getTreeModel().reload(getRootNode()); |
218 |
|
219 |
// Expand nodes
|
220 |
getTree().expandRow(0);
|
221 |
} |
222 |
}); |
223 |
} |
224 |
|
225 |
private void createMenu() { |
226 |
// Create the menu bar.
|
227 |
JMenuBar menuBar = new JMenuBar(); |
228 |
|
229 |
// Build the menu.
|
230 |
JMenu menuFile = new JMenu("File"); |
231 |
menuFile.add(new JMenuItem(paletteActions.getAction("exit"))); |
232 |
|
233 |
JMenu menuLayer = new JMenu("Layer"); |
234 |
menuLayer.add(new JMenuItem(paletteActions |
235 |
.getAction("add-vectorial-layer")));
|
236 |
menuLayer.add(new JMenuItem(paletteActions |
237 |
.getAction("add-raster-layer")));
|
238 |
menuLayer.add(new JMenuItem(paletteActions.getAction("remove-layer"))); |
239 |
|
240 |
JMenu menu3D = new JMenu("3D"); |
241 |
menu3D.add(new JMenuItem(paletteActions |
242 |
.getAction("create-sphere-view-3d")));
|
243 |
menu3D.add(new JMenuItem(paletteActions |
244 |
.getAction("create-flat-view-3d")));
|
245 |
menu3D.add(new JMenuItem(paletteActions |
246 |
.getAction("create-reload-view-3d")));
|
247 |
menu3D.add(new JMenuItem(paletteActions |
248 |
.getAction("create-synchronize-view-3d")));
|
249 |
|
250 |
menuBar.add(menuFile); |
251 |
menuBar.add(menuLayer); |
252 |
menuBar.add(menu3D); |
253 |
|
254 |
mainFrame.setJMenuBar(menuBar); |
255 |
} |
256 |
|
257 |
private void createToolBar() { |
258 |
JToolBar toolBar = new JToolBar(); |
259 |
|
260 |
toolBar |
261 |
.add(new JButton(paletteActions.getAction("add-vectorial-layer"))); |
262 |
toolBar.add(new JButton(paletteActions.getAction("add-raster-layer"))); |
263 |
toolBar.add(new JButton(paletteActions.getAction("remove-layer"))); |
264 |
toolBar.add(new JButton(paletteActions |
265 |
.getAction("create-sphere-view-3d")));
|
266 |
toolBar |
267 |
.add(new JButton(paletteActions.getAction("create-flat-view-3d"))); |
268 |
toolBar.add(new JButton(paletteActions |
269 |
.getAction("create-reload-view-3d")));
|
270 |
toolBar.add(new JButton(paletteActions |
271 |
.getAction("create-synchronize-view-3d")));
|
272 |
|
273 |
mainFrame.add(toolBar, BorderLayout.PAGE_START);
|
274 |
|
275 |
} |
276 |
|
277 |
private void createTOC() { |
278 |
|
279 |
JPanel toc = new JPanel(); |
280 |
toc.setBackground(Color.WHITE);
|
281 |
|
282 |
rootNode = getRootNode(); |
283 |
treeModel = getTreeModel(); |
284 |
tree = getTree(); |
285 |
|
286 |
toc.add(tree); |
287 |
mainFrame.add(toc, BorderLayout.WEST);
|
288 |
} |
289 |
|
290 |
private DefaultMutableTreeNode getRootNode() { |
291 |
if (rootNode == null) { |
292 |
rootNode = new DefaultMutableTreeNode("Layers"); |
293 |
} |
294 |
return rootNode;
|
295 |
} |
296 |
|
297 |
protected DefaultTreeModel getTreeModel() { |
298 |
if (treeModel == null) { |
299 |
treeModel = new DefaultTreeModel(getRootNode()); |
300 |
} |
301 |
return treeModel;
|
302 |
} |
303 |
|
304 |
protected JTree getTree() { |
305 |
if (tree == null) { |
306 |
tree = new JTree(getTreeModel()); |
307 |
tree.getSelectionModel().setSelectionMode( |
308 |
TreeSelectionModel.SINGLE_TREE_SELECTION);
|
309 |
tree.addMouseListener(getTreeMouseListener()); |
310 |
} |
311 |
return tree;
|
312 |
} |
313 |
|
314 |
private MouseListener getTreeMouseListener() { |
315 |
if (treeMouseListener == null) { |
316 |
treeMouseListener = new MouseListener() { |
317 |
|
318 |
public void mouseReleased(MouseEvent e) { |
319 |
} |
320 |
|
321 |
public void mousePressed(MouseEvent e) { |
322 |
int selRow = tree.getRowForLocation(e.getX(), e.getY());
|
323 |
TreePath selPath =
|
324 |
tree.getPathForLocation(e.getX(), e.getY()); |
325 |
if (selRow != -1) { |
326 |
if (e.getClickCount() == 2 |
327 |
&& SwingUtilities.isLeftMouseButton(e)) {
|
328 |
|
329 |
DefaultMutableTreeNode selectedNode =
|
330 |
(DefaultMutableTreeNode) selPath
|
331 |
.getLastPathComponent(); |
332 |
|
333 |
if(selectedNode == getRootNode()){
|
334 |
return;
|
335 |
} |
336 |
|
337 |
// TODO Open 3D properties dialog to set layer load
|
338 |
// mode.
|
339 |
ThreadSafeDialogsManager tsdm = |
340 |
ToolsSwingLocator.getThreadSafeDialogsManager(); |
341 |
tsdm.messageDialog( |
342 |
"Open " + selectedNode.toString()
|
343 |
+ " 3D properties"
|
344 |
+ " dialog to set layer load mode",
|
345 |
"3D properties",
|
346 |
JOptionPane.INFORMATION_MESSAGE);
|
347 |
} |
348 |
} |
349 |
} |
350 |
|
351 |
public void mouseExited(MouseEvent e) { |
352 |
} |
353 |
|
354 |
public void mouseEntered(MouseEvent e) { |
355 |
} |
356 |
|
357 |
public void mouseClicked(MouseEvent e) { |
358 |
} |
359 |
}; |
360 |
} |
361 |
return treeMouseListener;
|
362 |
} |
363 |
|
364 |
public MapControl getMapControl() {
|
365 |
return this.mapControl; |
366 |
} |
367 |
|
368 |
public MapContext getMapContext() {
|
369 |
return this.mapControl.getMapContext(); |
370 |
} |
371 |
|
372 |
public IProjection getDefaultProjection() {
|
373 |
return defaultProjection;
|
374 |
} |
375 |
} |