root / branches / gvSIG_19_ext3D_osgVP_2_2_0 / libraries / lib3DMap / src / org / gvsig / gvsig3d / cacheservices / OSGCacheService.java @ 32391
History | View | Annotate | Download (12.2 KB)
1 |
package org.gvsig.gvsig3d.cacheservices; |
---|---|
2 |
|
3 |
import java.awt.geom.Rectangle2D; |
4 |
import java.io.File; |
5 |
import java.io.FileNotFoundException; |
6 |
|
7 |
import javax.swing.JOptionPane; |
8 |
|
9 |
import org.apache.log4j.Logger; |
10 |
import org.cresques.cts.IProjection; |
11 |
import org.gvsig.cacheservice.CacheService; |
12 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
13 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
14 |
import org.gvsig.fmap.geom.primitive.AbstractPrimitive; |
15 |
import org.gvsig.geometries3D.MultiGeometry; |
16 |
import org.gvsig.geometries3D.MultiSolid; |
17 |
import org.gvsig.gvsig3d.drivers.GvsigDriverOSG; |
18 |
import org.gvsig.gvsig3d.listener.EditorListener; |
19 |
import org.gvsig.gvsig3d.navigation.NavigationMode; |
20 |
import org.gvsig.operations3D.Draw3DMultiSolid; |
21 |
import org.gvsig.operations3D.context.Draw3DContext; |
22 |
import org.gvsig.osgvp.core.osg.Group; |
23 |
import org.gvsig.osgvp.core.osg.Node; |
24 |
import org.gvsig.osgvp.core.osg.PositionAttitudeTransform; |
25 |
import org.gvsig.osgvp.core.osg.Vec3; |
26 |
import org.gvsig.osgvp.core.osgdb.osgDB; |
27 |
import org.gvsig.osgvp.exceptions.node.NodeException; |
28 |
import org.gvsig.osgvp.manipulator.EditionManager; |
29 |
import org.gvsig.osgvp.manipulator.ManipulatorHandler; |
30 |
import org.gvsig.osgvp.manipulator.RemoveAllSelectionCommand; |
31 |
import org.gvsig.osgvp.terrain.Terrain; |
32 |
import org.gvsig.osgvp.terrain.TerrainViewer; |
33 |
import org.gvsig.osgvp.util.ActionCommand; |
34 |
import org.gvsig.osgvp.viewer.IViewerContainer; |
35 |
|
36 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
37 |
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps; |
38 |
import com.iver.andami.PluginServices; |
39 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
40 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
41 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
42 |
|
43 |
public class OSGCacheService extends CacheService { |
44 |
|
45 |
private IViewerContainer _canvas3D;
|
46 |
private TerrainViewer _terrainViewer;
|
47 |
private FLayer _layer;
|
48 |
// vector caching for libJOSG
|
49 |
private Group _layerNode; |
50 |
private EditionManager _editionManager;
|
51 |
private EditorListener _editionListener;
|
52 |
private ManipulatorHandler _manipulatorHandler;
|
53 |
private RemoveAllSelectionCommand _command;
|
54 |
private String _filePath; |
55 |
private static Logger _logger = Logger.getLogger(OSGCacheService.class |
56 |
.getName()); |
57 |
|
58 |
public OSGCacheService(IViewerContainer canvas3D, Terrain terrain,
|
59 |
String name, FLayer layer, IProjection viewProj) {
|
60 |
super(terrain.getTerrainName(), name);
|
61 |
|
62 |
_canvas3D = canvas3D; |
63 |
_terrainViewer = (TerrainViewer) canvas3D.getOSGViewer(); |
64 |
_layer = layer; |
65 |
_layerNode = new Group(); |
66 |
_layerNode.setNodeName("layer3DOSG");
|
67 |
try {
|
68 |
_editionManager = new EditionManager();
|
69 |
_layerNode.addChild(_editionManager); |
70 |
_manipulatorHandler = new ManipulatorHandler();
|
71 |
_manipulatorHandler.setActive(false);
|
72 |
|
73 |
_editionListener = new EditorListener(_editionManager,
|
74 |
_manipulatorHandler, _layer, _canvas3D, _terrainViewer); |
75 |
|
76 |
_canvas3D.getOSGViewer().addEventHandler(_manipulatorHandler); |
77 |
|
78 |
} catch (NodeException e2) {
|
79 |
// TODO Auto-generated catch block
|
80 |
e2.printStackTrace(); |
81 |
} |
82 |
|
83 |
try {
|
84 |
_terrainViewer.addFeature(_layerNode); |
85 |
} catch (NodeException e1) {
|
86 |
_logger.error("Command: "
|
87 |
+ "Error adding new child node to the special node.", e1);
|
88 |
} |
89 |
|
90 |
int cacheType = CacheService.GLOBAL;
|
91 |
if (terrain.getCoordinateSystemType() == Terrain.CoordinateSystemType.GEOCENTRIC)
|
92 |
cacheType += SPHERIC; |
93 |
else
|
94 |
cacheType += PLANE; |
95 |
setCacheType(cacheType); |
96 |
|
97 |
} |
98 |
|
99 |
public Group getLayerNode() { |
100 |
|
101 |
if (!Layer3DProps.getLayer3DProps(this._layer).isEditing()) { |
102 |
try {
|
103 |
return (Group) _editionManager.getScene(); |
104 |
} catch (NodeException e) {
|
105 |
// TODO Auto-generated catch block
|
106 |
e.printStackTrace(); |
107 |
} |
108 |
} |
109 |
|
110 |
else {
|
111 |
|
112 |
try {
|
113 |
return _editionManager.getTransformedScene();
|
114 |
} catch (NodeException e) {
|
115 |
// TODO Auto-generated catch block
|
116 |
e.printStackTrace(); |
117 |
} |
118 |
|
119 |
} |
120 |
return _layerNode;
|
121 |
} |
122 |
|
123 |
// uses a strategy to get all features from layer and their symbols
|
124 |
// and add them to the terrain
|
125 |
|
126 |
public void AddFeaturesToTerrain() { |
127 |
|
128 |
boolean newLayer = Layer3DProps.getLayer3DProps(_layer).isNewLayerOSG();
|
129 |
|
130 |
if (!newLayer) {
|
131 |
GvsigDriverOSG osgD = (GvsigDriverOSG) ((FLyrVect) _layer) |
132 |
.getSource().getDriver(); |
133 |
try {
|
134 |
File file = osgD.getFile();
|
135 |
_filePath = file.getAbsolutePath(); |
136 |
Node n = null;
|
137 |
|
138 |
try {
|
139 |
n = osgDB.readNodeFile(file.getAbsolutePath()); |
140 |
} catch (FileNotFoundException e) { |
141 |
// TODO Auto-generated catch block
|
142 |
e.printStackTrace(); |
143 |
} |
144 |
|
145 |
// Insert the geometry in the root node of the graph.
|
146 |
Group g = new Group(n.getCPtr()); |
147 |
// buildGeometry(root, g);
|
148 |
// g.addChild(n);
|
149 |
for (int i = 0; i < g.getNumChildren(); i++) { |
150 |
|
151 |
_editionManager.setScene(g.getChild(i)); |
152 |
|
153 |
} |
154 |
|
155 |
// _layerNode.addChild(n);
|
156 |
// startEditing();
|
157 |
// _layerNode.addChild(g);
|
158 |
} catch (NodeException e) {
|
159 |
_logger |
160 |
.error( |
161 |
"Command: "
|
162 |
+ "Error loading OSG file. File must be generated with gvSIG",
|
163 |
e); |
164 |
|
165 |
} |
166 |
} |
167 |
//this._canvas3D.repaint();
|
168 |
} |
169 |
|
170 |
public void RefreshFeaturesToTerrain() { |
171 |
this.DeleteFeaturesToTerrain();
|
172 |
this.AddFeaturesToTerrain();
|
173 |
} |
174 |
|
175 |
public void DeleteFeaturesToTerrain() { |
176 |
try {
|
177 |
endEditing(); |
178 |
_terrainViewer.removeFeature(_layerNode); |
179 |
} catch (NodeException e) {
|
180 |
_logger.error("Command: "
|
181 |
+ "Error removing new child node to the special node.", e);
|
182 |
} |
183 |
} |
184 |
|
185 |
public void refreshFeaturesToTerrain(boolean visible) { |
186 |
_layerNode.setNodeMask(visible ? 0xffffffff : 0x00000000); |
187 |
} |
188 |
|
189 |
public void AddGeometryToLayer(MultiGeometry multiGeometry, Vec3 position, |
190 |
Vec3 rotation, Vec3 scale) { |
191 |
Group group = new Group(); |
192 |
group.setNodeName("GROUP-PAT");
|
193 |
|
194 |
PositionAttitudeTransform posAttTrasn = null;
|
195 |
try {
|
196 |
posAttTrasn = new PositionAttitudeTransform();
|
197 |
posAttTrasn.setPosition(position); |
198 |
posAttTrasn.setScale(scale); |
199 |
posAttTrasn.setNodeName("PAT");
|
200 |
// posAttTrasn.setAttitude((float) rotation.x(), new Vec3(1,0,0));
|
201 |
// posAttTrasn.setAttitude((float) rotation.y(), new Vec3(0,1,0));
|
202 |
// posAttTrasn.setAttitude((float) rotation.z(), new Vec3(0,0,1));
|
203 |
buildGeometry(multiGeometry, group); |
204 |
posAttTrasn.addChild(group); |
205 |
|
206 |
// if (props3D.isEditing()){
|
207 |
_editionManager.setScene(posAttTrasn); |
208 |
// }else{
|
209 |
// _layerNode.addChild(posAttTrasn);
|
210 |
// }
|
211 |
// _layerNode.addChild(group);
|
212 |
} catch (NodeException e1) {
|
213 |
// TODO Auto-generated catch block
|
214 |
e1.printStackTrace(); |
215 |
} |
216 |
|
217 |
} |
218 |
public void AddNodeToLayer(Node node, Vec3 position, |
219 |
Vec3 rotation, Vec3 scale) { |
220 |
|
221 |
|
222 |
PositionAttitudeTransform posAttTrasn = null;
|
223 |
try {
|
224 |
posAttTrasn = new PositionAttitudeTransform();
|
225 |
posAttTrasn.setPosition(position); |
226 |
posAttTrasn.setScale(scale); |
227 |
posAttTrasn.setNodeName("PAT");
|
228 |
// posAttTrasn.setAttitude((float) rotation.x(), new Vec3(1,0,0));
|
229 |
// posAttTrasn.setAttitude((float) rotation.y(), new Vec3(0,1,0));
|
230 |
// posAttTrasn.setAttitude((float) rotation.z(), new Vec3(0,0,1));
|
231 |
// buildGeometry(multiGeometry, group);
|
232 |
|
233 |
posAttTrasn.addChild(node); |
234 |
|
235 |
// if (props3D.isEditing()){
|
236 |
_editionManager.setScene(posAttTrasn); |
237 |
// }else{
|
238 |
// _layerNode.addChild(posAttTrasn);
|
239 |
// }
|
240 |
// _layerNode.addChild(group);
|
241 |
} catch (NodeException e1) {
|
242 |
// TODO Auto-generated catch block
|
243 |
e1.printStackTrace(); |
244 |
} |
245 |
|
246 |
} |
247 |
private void buildGeometry(AbstractPrimitive geom, Group group) |
248 |
throws NodeException {
|
249 |
|
250 |
int i;
|
251 |
|
252 |
if (geom instanceof MultiGeometry) { |
253 |
MultiGeometry multiGeometry = (MultiGeometry) geom; |
254 |
for (i = 0; i < multiGeometry.getGeometries().size(); i++) { |
255 |
|
256 |
Group child = new Group(); |
257 |
group.addChild(child); |
258 |
buildGeometry(multiGeometry.getGeometries().get(i), child); |
259 |
} |
260 |
} else if (geom instanceof MultiSolid) { |
261 |
// Getting the geometry
|
262 |
MultiSolid multiSolid = (MultiSolid) geom; |
263 |
// Creating the context and adding parameters
|
264 |
Draw3DContext ctx3D = new Draw3DContext();
|
265 |
ctx3D.setGroup(group); |
266 |
// Creating the drawing operation
|
267 |
Draw3DMultiSolid d3DMultiSolid = new Draw3DMultiSolid();
|
268 |
|
269 |
try {
|
270 |
// Invoking the operation for the multisolid
|
271 |
multiSolid.invokeOperation(d3DMultiSolid.getOperationIndex(), |
272 |
ctx3D); |
273 |
} catch (GeometryOperationNotSupportedException e) {
|
274 |
// TODO Auto-generated catch block
|
275 |
e.printStackTrace(); |
276 |
} catch (GeometryOperationException e) {
|
277 |
// TODO Auto-generated catch block
|
278 |
e.printStackTrace(); |
279 |
} |
280 |
} |
281 |
|
282 |
} |
283 |
|
284 |
public void startEditing() { |
285 |
|
286 |
Layer3DProps props3D = Layer3DProps.getLayer3DProps(this._layer);
|
287 |
if ((props3D.getType() == Layer3DProps.layer3DOSG)
|
288 |
&& (!props3D.isEditing())) { |
289 |
props3D.setEditing(true);
|
290 |
props3D.setNewLayerOSG(true);
|
291 |
JOptionPane.showMessageDialog(null, |
292 |
PluginServices.getText( |
293 |
this, "edited_layer")); |
294 |
|
295 |
// editionManager = new EditionManager();
|
296 |
// editionManager.setNodeName("EDITIONMANAGER");
|
297 |
// editionManager.getChild(0).setNodeName("GROUP");
|
298 |
// for (int i = 0; i < _layerNode.getNumChildren(); i++) {
|
299 |
// Node node = _layerNode.getChild(i);
|
300 |
// editionManager.setScene(node);
|
301 |
// _layerNode.removeChild(node);
|
302 |
// }
|
303 |
// _layerNode.addChild(editionManager);
|
304 |
// _layerNode.setNodeName("LAYERNODE");
|
305 |
// Group parent = _layerNode.getParent(0);
|
306 |
// parent.removeChild(_layerNode);
|
307 |
// parent.addChild(editionManager);
|
308 |
|
309 |
// ManipulatorHandler manipulatorHandler = new ManipulatorHandler();
|
310 |
// manipulatorHandler.setActive(true);
|
311 |
// public void changeDragger(String draggerType)
|
312 |
// editionManager.changeDragger(DraggerType.ROTATE_SPHERE_DRAGGER
|
313 |
// );
|
314 |
// editionManager.changeDragger(DraggerType.TABBOX_DRAGGER);
|
315 |
// editionManager.changeDragger(DraggerType.TRANSLATE_AXIS_DRAGGER
|
316 |
// );
|
317 |
|
318 |
} |
319 |
} |
320 |
|
321 |
public void endEditing() { |
322 |
|
323 |
Layer3DProps props3D = Layer3DProps.getLayer3DProps(this._layer);
|
324 |
if ((props3D.getType() == Layer3DProps.layer3DOSG)
|
325 |
&& (props3D.isEditing())) { |
326 |
props3D.setEditing(false);
|
327 |
|
328 |
if (props3D.isNewLayerOSG())
|
329 |
JOptionPane
|
330 |
.showMessageDialog(null,PluginServices.getText(
|
331 |
this, "modified_layer") |
332 |
); |
333 |
|
334 |
_command = new RemoveAllSelectionCommand(_editionManager);
|
335 |
_command.execute(); |
336 |
|
337 |
// Group scene = editionManager.getTransformedScene();
|
338 |
|
339 |
// for (int i = 0; i < _layerNode.getNumChildren(); i++) {
|
340 |
// Node node = _layerNode.getChild(i);
|
341 |
// _layerNode.removeChild(node);
|
342 |
//
|
343 |
// }
|
344 |
// _layerNode.removeChildren();
|
345 |
|
346 |
// for (int i =0; i < scene.getNumChildren(); i++){
|
347 |
//
|
348 |
// _layerNode.addChild(scene.getChild(i));
|
349 |
//
|
350 |
// }
|
351 |
//
|
352 |
|
353 |
// Group parent = _layerNode.getParent(0);
|
354 |
// parent.removeChild(_layerNode);
|
355 |
// parent.addChild(scene);
|
356 |
// public void changeDragger(String draggerType)
|
357 |
// editionManager.changeDragger(DraggerType.ROTATE_SPHERE_DRAGGER
|
358 |
// );
|
359 |
// editionManager.changeDragger(DraggerType.TABBOX_DRAGGER);
|
360 |
// editionManager.changeDragger(DraggerType.TRANSLATE_AXIS_DRAGGER
|
361 |
// );
|
362 |
|
363 |
_manipulatorHandler.setActive(false);
|
364 |
} |
365 |
|
366 |
} |
367 |
|
368 |
public EditionManager getEditionManager() {
|
369 |
return _editionManager;
|
370 |
} |
371 |
|
372 |
public ManipulatorHandler getManipulatorHandler(){
|
373 |
|
374 |
return _manipulatorHandler;
|
375 |
|
376 |
} |
377 |
|
378 |
public void enableEditionListener(){ |
379 |
|
380 |
_canvas3D.addKeyListener(_editionListener); |
381 |
_canvas3D.addMouseListener(_editionListener); |
382 |
_manipulatorHandler.setActive(true);
|
383 |
NavigationMode.removeAllNavigationModes(_terrainViewer |
384 |
.getTerrainCameraManipulator()); |
385 |
|
386 |
} |
387 |
|
388 |
public void disableEditionListener(){ |
389 |
|
390 |
_canvas3D.removeKeyListener(_editionListener); |
391 |
_canvas3D.removeMouseListener(_editionListener); |
392 |
_manipulatorHandler.setActive(false);
|
393 |
NavigationMode.restoreAllNavigationModes(_terrainViewer |
394 |
.getTerrainCameraManipulator()); |
395 |
ActionCommand command; |
396 |
command = new RemoveAllSelectionCommand(_editionManager);
|
397 |
command.execute(); |
398 |
|
399 |
} |
400 |
|
401 |
|
402 |
public void setEditionManager(EditionManager editionManager) { |
403 |
this._editionManager = editionManager;
|
404 |
} |
405 |
|
406 |
public String getLayerPath() { |
407 |
return _filePath;
|
408 |
} |
409 |
|
410 |
} |