gvsig-3d / 2.1 / trunk / org.gvsig.gvsig3d / org.gvsig.gvsig3d.app / org.gvsig.gvsig3d.app.commons / src / main / java / org / gvsig / gvsig3d / app / extension / EnableManipulation3DExtension.java @ 385
History | View | Annotate | Download (9.35 KB)
1 |
/* gvSIG 3D extension for gvSIG
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2012 Prodevelop.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Prodevelop, S.L.
|
22 |
* Pza. Don Juan de Villarrasa, 14 - 5
|
23 |
* 46001 Valencia
|
24 |
* Spain
|
25 |
*
|
26 |
* +34 963 510 612
|
27 |
* +34 963 510 968
|
28 |
* prode@prodevelop.es
|
29 |
* http://www.prodevelop.es
|
30 |
*/
|
31 |
/*
|
32 |
* AUTHORS:
|
33 |
* 2012 AI2 - Instituto Universitario de Automatica e Informatica Industrial.
|
34 |
* Universitat Politecnica de Valencia (UPV)
|
35 |
* http://www.ai2.upv.es
|
36 |
*/
|
37 |
|
38 |
|
39 |
package org.gvsig.gvsig3d.app.extension; |
40 |
|
41 |
import java.awt.event.ComponentEvent; |
42 |
import java.awt.event.ComponentListener; |
43 |
import java.io.File; |
44 |
|
45 |
import javax.swing.Icon; |
46 |
import javax.swing.ImageIcon; |
47 |
|
48 |
import org.gvsig.andami.PluginServices; |
49 |
import org.gvsig.andami.plugins.Extension; |
50 |
import org.gvsig.andami.ui.mdiFrame.JToolBarButton; |
51 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
52 |
import org.gvsig.app.project.documents.view.ViewDocument; |
53 |
import org.gvsig.fmap.dal.DataStore; |
54 |
import org.gvsig.fmap.mapcontext.MapContext; |
55 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
56 |
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer; |
57 |
import org.gvsig.gvsig3d.map3d.Layer3DProps; |
58 |
import org.gvsig.gvsig3d.map3d.MapContext3D; |
59 |
import org.gvsig.gvsig3d.navigation.NavigationMode; |
60 |
import org.gvsig.gvsig3d.resources.ResourcesFactory; |
61 |
import org.gvsig.osgvp.core.osg.Vec4; |
62 |
import org.gvsig.osgvp.exceptions.node.NodeException; |
63 |
import org.gvsig.osgvp.features.Text; |
64 |
import org.gvsig.osgvp.manipulator.BasicInputHandler; |
65 |
import org.gvsig.osgvp.manipulator.EditionManager; |
66 |
import org.gvsig.osgvp.manipulator.RemoveAllSelectionCommand; |
67 |
import org.gvsig.osgvp.terrain.TerrainCameraManipulator; |
68 |
import org.gvsig.osgvp.terrain.TerrainViewer; |
69 |
import org.gvsig.osgvp.viewer.IViewer; |
70 |
import org.gvsig.osgvp.viewer.IViewerContainer; |
71 |
|
72 |
/**
|
73 |
* @author Jesus Zarzoso- jzarzoso@ai2.upv.es
|
74 |
* @version $Id$
|
75 |
*
|
76 |
*/
|
77 |
public class EnableManipulation3DExtension extends Extension { |
78 |
|
79 |
private IViewerContainer _canvas3d = null; |
80 |
private Icon iconActivar = null; |
81 |
private Icon iconDesactivar = null; |
82 |
private TerrainViewer _terrainViewer;
|
83 |
private Text _editionHud;
|
84 |
private FLayer _lastActiveLayer = null; |
85 |
private HudResizeListener _resizeListener;
|
86 |
private TerrainCameraManipulator _terrainCamera = null; |
87 |
private MapContext3D mc;
|
88 |
private Layer3DProps props;
|
89 |
private EditionManager em;
|
90 |
private BasicInputHandler bh;
|
91 |
private RemoveAllSelectionCommand _command;
|
92 |
|
93 |
public void execute(String actionCommand) { |
94 |
|
95 |
FLayer layer = osgLayerActive(); |
96 |
|
97 |
IViewer viewer = _canvas3d.getOSGViewer(); |
98 |
|
99 |
if (layer != null) { |
100 |
|
101 |
mc = ((MapContext3D) layer.getMapContext()); |
102 |
props = mc.getLayer3DProps(layer); |
103 |
|
104 |
em = ((MapContext3D) layer.getMapContext()) |
105 |
.getEditionManager(layer); |
106 |
|
107 |
bh = ((MapContext3D) layer.getMapContext()) |
108 |
.getEditionHandler(layer); |
109 |
|
110 |
if (actionCommand.equals("ENABLE_MANIPULATOR")) { |
111 |
|
112 |
JToolBarButton b = (JToolBarButton) PluginServices |
113 |
.getMainFrame().getComponentByName("ENABLE_MANIP");
|
114 |
|
115 |
if (props.isEditing()) {
|
116 |
|
117 |
endEditing(); |
118 |
b.setIcon(iconActivar); |
119 |
removeMessageFromHud(); |
120 |
|
121 |
} else {
|
122 |
|
123 |
startEditing(); |
124 |
b.setIcon(iconDesactivar); |
125 |
addMessageToHud(); |
126 |
|
127 |
} |
128 |
|
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
private void startEditing() { |
134 |
|
135 |
props.setEditing(true);
|
136 |
em.setEnabledHandlers(true);
|
137 |
bh.setActive(true);
|
138 |
IWindow view = PluginServices.getMDIManager().getActiveWindow(); |
139 |
if (!(view instanceof DefaultView3DPanel)) |
140 |
return;
|
141 |
|
142 |
DefaultView3DPanel vista3D = (DefaultView3DPanel) view; |
143 |
|
144 |
NavigationMode navMode = vista3D.getNavMode(); |
145 |
_terrainCamera = (TerrainCameraManipulator) vista3D.getCanvas3d() |
146 |
.getOSGViewer().getCameraManipulator(); |
147 |
navMode.removeAllModes(); |
148 |
|
149 |
} |
150 |
|
151 |
private void endEditing() { |
152 |
|
153 |
props.setEditing(false);
|
154 |
_command = new RemoveAllSelectionCommand(em);
|
155 |
_command.execute(); |
156 |
em.setEnabledHandlers(false);
|
157 |
bh.setActive(false);
|
158 |
IWindow view = PluginServices.getMDIManager().getActiveWindow(); |
159 |
if (!(view instanceof DefaultView3DPanel)) |
160 |
return;
|
161 |
|
162 |
DefaultView3DPanel vista3D = (DefaultView3DPanel) view; |
163 |
|
164 |
NavigationMode navMode = vista3D.getNavMode(); |
165 |
|
166 |
navMode.SetDefaultMode(); |
167 |
|
168 |
} |
169 |
|
170 |
public void initialize() { |
171 |
PluginServices.getIconTheme().registerDefault( |
172 |
"enable_manipulator",
|
173 |
this.getClass().getClassLoader()
|
174 |
.getResource("images/pick2.png"));
|
175 |
_resizeListener = new HudResizeListener();
|
176 |
|
177 |
} |
178 |
|
179 |
public FLayer osgLayerActive() {
|
180 |
|
181 |
FLayer activeLayer = null;
|
182 |
// Getting view3
|
183 |
IWindow view = PluginServices.getMDIManager().getActiveWindow(); |
184 |
if ((view instanceof DefaultView3DPanel)) { |
185 |
// Casting to View3D
|
186 |
DefaultView3DPanel vista3D = (DefaultView3DPanel) view; |
187 |
|
188 |
_canvas3d = vista3D.getCanvas3d(); |
189 |
|
190 |
ViewDocument model = vista3D.getModel(); |
191 |
MapContext mapa = model.getMapContext(); |
192 |
FLayer[] activeLayers = mapa.getLayers().getActives();
|
193 |
if (activeLayers.length == 1) { |
194 |
FLayer layer = activeLayers[0];
|
195 |
DataStore ds = ((SingleLayer) layer).getDataStore(); |
196 |
|
197 |
if (ds.getProviderName() == "OSG") { |
198 |
|
199 |
activeLayer = layer; |
200 |
} |
201 |
|
202 |
} |
203 |
} |
204 |
|
205 |
return activeLayer;
|
206 |
|
207 |
} |
208 |
|
209 |
public boolean isEnabled() { |
210 |
|
211 |
FLayer layer = osgLayerActive(); |
212 |
|
213 |
if (layer != null) { |
214 |
|
215 |
JToolBarButton b = (JToolBarButton) PluginServices.getMainFrame() |
216 |
.getComponentByName("ENABLE_MANIP");
|
217 |
Layer3DProps props3D = ((MapContext3D) layer.getMapContext()) |
218 |
.getLayer3DProps(layer); |
219 |
// First see if the layer has changed, then disable
|
220 |
if (_lastActiveLayer != layer && layer != null |
221 |
&& _lastActiveLayer != null) {
|
222 |
|
223 |
Layer3DProps lastProps3D = ((MapContext3D) layer |
224 |
.getMapContext()).getLayer3DProps(_lastActiveLayer); |
225 |
lastProps3D.setEditing(false);
|
226 |
// lastCache.disableEditionListener();
|
227 |
|
228 |
} |
229 |
|
230 |
if (!props3D.isEditing()) {
|
231 |
|
232 |
b.setIcon(iconActivar); |
233 |
removeMessageFromHud(); |
234 |
|
235 |
} else {
|
236 |
|
237 |
b.setIcon(iconDesactivar); |
238 |
addMessageToHud(); |
239 |
|
240 |
} |
241 |
_lastActiveLayer = layer; |
242 |
return true; |
243 |
|
244 |
} else {
|
245 |
removeMessageFromHud(); |
246 |
return false; |
247 |
|
248 |
} |
249 |
} |
250 |
|
251 |
public boolean isVisible() { |
252 |
|
253 |
FLayer layer = osgLayerActive(); |
254 |
|
255 |
if (layer != null) { |
256 |
return true; |
257 |
|
258 |
} else {
|
259 |
removeMessageFromHud(); |
260 |
return false; |
261 |
} |
262 |
} |
263 |
|
264 |
public void postInitialize() { |
265 |
|
266 |
File file = new File(this.getClass().getClassLoader() |
267 |
.getResource("images").getFile());
|
268 |
String path1 = file.getPath() + "/pick2.png"; |
269 |
String path2 = file.getPath() + "/nopick2.png"; |
270 |
|
271 |
iconActivar = new ImageIcon(path1); |
272 |
iconDesactivar = new ImageIcon(path2); |
273 |
|
274 |
initEditTextHud(); |
275 |
|
276 |
} |
277 |
|
278 |
private void initEditTextHud() { |
279 |
|
280 |
// Seting up text
|
281 |
try {
|
282 |
_editionHud = new Text();
|
283 |
} catch (NodeException e) {
|
284 |
// TODO Auto-generated catch block
|
285 |
e.printStackTrace(); |
286 |
} |
287 |
_editionHud.setCharacterSize(14);
|
288 |
_editionHud.setColor(new Vec4(1.0f, 1.0f, 0.0f, 1.0f)); |
289 |
_editionHud.setBackdropColor(1.0f, 1.0f, 0.0f, 1.0f); |
290 |
|
291 |
if (ResourcesFactory.exitsResouce("arial.ttf")) |
292 |
_editionHud.setFont(ResourcesFactory.getResourcePath("arial.ttf"));
|
293 |
else {
|
294 |
// TODO: This freeze the execution.. disable when working.
|
295 |
_editionHud.setFont("arial.ttf");
|
296 |
} |
297 |
|
298 |
_editionHud.setPosition(0, 0, 0); |
299 |
_editionHud.setBackdropType(Text.BackdropType.OUTLINE); |
300 |
_editionHud.setAlignment(Text.AlignmentType.LEFT_CENTER); |
301 |
_editionHud |
302 |
.setText(PluginServices.getText(this, "Hold_Control_Camera")); |
303 |
|
304 |
} |
305 |
|
306 |
public void addMessageToHud() { |
307 |
|
308 |
_terrainViewer = (TerrainViewer) _canvas3d.getOSGViewer(); |
309 |
if (_terrainViewer != null) { |
310 |
|
311 |
try {
|
312 |
if (_terrainViewer.getCameraHUD().getChildIndex(_editionHud) >= _terrainViewer
|
313 |
.getCameraHUD().getNumChildren()) { |
314 |
_terrainViewer.addNodeToCameraHUD(_editionHud); |
315 |
_editionHud.setPosition(10, _canvas3d.getHeight() - 10, 0); |
316 |
_canvas3d.addComponentListener(_resizeListener); |
317 |
|
318 |
} |
319 |
|
320 |
} catch (NodeException e) {
|
321 |
// TODO Auto-generated catch block
|
322 |
e.printStackTrace(); |
323 |
} |
324 |
} |
325 |
|
326 |
} |
327 |
|
328 |
public void removeMessageFromHud() { |
329 |
|
330 |
if (_terrainViewer != null) { |
331 |
try {
|
332 |
if (_terrainViewer.getCameraHUD().getChildIndex(_editionHud) < _terrainViewer
|
333 |
.getCameraHUD().getNumChildren()) { |
334 |
_terrainViewer.removeNodeFromCameraHUD(_editionHud); |
335 |
_canvas3d.removeComponentListener(_resizeListener); |
336 |
} |
337 |
|
338 |
} catch (NodeException e) {
|
339 |
// TODO Auto-generated catch block
|
340 |
e.printStackTrace(); |
341 |
} |
342 |
} |
343 |
|
344 |
} |
345 |
|
346 |
public class HudResizeListener implements ComponentListener { |
347 |
|
348 |
public void componentResized(java.awt.event.ComponentEvent evt) { |
349 |
_editionHud.setPosition(10, _canvas3d.getHeight() - 10, 0); |
350 |
} |
351 |
|
352 |
public void componentHidden(ComponentEvent arg0) { |
353 |
_editionHud.setPosition(10, _canvas3d.getHeight() - 10, 0); |
354 |
|
355 |
} |
356 |
|
357 |
public void componentMoved(ComponentEvent arg0) { |
358 |
_editionHud.setPosition(10, _canvas3d.getHeight() - 10, 0); |
359 |
|
360 |
} |
361 |
|
362 |
public void componentShown(ComponentEvent arg0) { |
363 |
_editionHud.setPosition(10, _canvas3d.getHeight() - 10, 0); |
364 |
|
365 |
} |
366 |
|
367 |
} |
368 |
|
369 |
} |