/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2007, by :
* Corporate:
* EADS Astrium
* Individual:
* Claude Cazenave
*
* $Id: ShapeNode.java,v 1.9 2008/10/22 16:02:10 cazenave Exp $
*
* Changes
* -------
* 9 janv. 08 : Initial public release
*
*/
package jsynoptic.plugins.java3d.tree;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.media.j3d.Appearance;
import javax.media.j3d.Geometry;
import javax.media.j3d.SceneGraphObject;
import javax.media.j3d.Shape3D;
/**
* A Node to hold a Shape
*/
public class ShapeNode extends SceneGraphTreeNode {
/**
* Register nodes resources and actions
*/
static void loadResources(){
addResources("javax.media.j3d.Shape3D", "Shape");
addResources("javax.media.j3d.Text3D", "Text");
addResources("javax.media.j3d.Geometry", "Geometry");
AppearanceNode.loadResources();
addActions("javax.media.j3d.Shape3D");
}
// required for dynamic node creation
public ShapeNode(Tree tree, Object graphObject, boolean getChildren) {
super(tree, graphObject, getChildren);
}
@Override
protected void getSceneGraphChildren(ArrayList<Object> list) {
Shape3D s=(Shape3D)getGraphObject();
Enumeration<?> e=s.getAllGeometries();
while(e.hasMoreElements()){
list.add(e.nextElement());
}
list.add(s.getAppearance());
}
@Override
public SceneGraphObject addSceneGraphObject(SceneGraphObject obj){
Shape3D s=(Shape3D)getGraphObject();
if(obj instanceof Geometry){
boolean forced=forceCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
s.addGeometry((Geometry)obj);
if(forced) restoreCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
return null;
}
else if (obj instanceof Appearance){
Appearance a=s.getAppearance();
boolean forced=forceCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
s.setAppearance(a);
if(forced) restoreCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
return a;
}
throw new RuntimeException("addSceneGraphObject invalid argument ="+obj.getClass());
}
public boolean canRemoveSceneGraphObject(SceneGraphObject obj, SceneGraphObject oldObj){
if (obj instanceof Appearance){
if(oldObj!=null && (oldObj instanceof Appearance)){
return true;
}
return false;
}
return super.canRemoveSceneGraphObject(obj,oldObj);
}
@Override
public void removeSceneGraphObject(SceneGraphObject obj, SceneGraphObject oldObj){
Shape3D s=(Shape3D)getGraphObject();
if(obj instanceof Geometry){
boolean forced=forceCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
s.removeGeometry((Geometry)obj);
if(forced) restoreCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
return;
}
else if (obj instanceof Appearance){
if(oldObj==null || !(oldObj instanceof Appearance)){
throw new RuntimeException("removeSceneGraphObject invalid argument old="+oldObj.getClass());
}
boolean forced=forceCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
s.setAppearance((Appearance)oldObj);
if(forced) restoreCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
return;
}
throw new RuntimeException("removeSceneGraphObject invalid argument ="+obj.getClass());
}
}