/* ========================
* 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-2008, by :
* Corporate:
* EADS Astrium
* Individual:
* Claude Cazenave
*
* $Id: TextureEdit.java,v 1.3 2008/10/16 17:14:55 cazenave Exp $
*
* Changes
* -------
* 13 oct. 08 : Initial public release
*
*/
package jsynoptic.plugins.java3d.edit;
import java.io.File;
import javax.media.j3d.Appearance;
import javax.media.j3d.ImageComponent;
import javax.media.j3d.Texture;
import com.sun.j3d.utils.image.TextureLoader;
/**
*
*/
public class TextureEdit extends PropertyEdit<Appearance, Object> {
public static final String TextureEdit = "TextureEdit";
/**
* @param object
*/
public TextureEdit(Appearance object) {
super(object, TextureEdit);
}
@Override
public Object getPropertyValue() {
return _object.getTexture();
}
@Override
public void setPropertyValue(Object value) {
boolean changed;
if(value instanceof File){
changed=true;
}
else if(value==null && _object.getTexture()!=null){
changed=true;
}
else if(value instanceof Texture && value.equals(_object.getTexture())){
changed=false;
}
else{
changed=true;
}
if(changed){
if(value instanceof File){
File newFile=(File)value;
try{
TextureLoader tex = new TextureLoader(newFile.getAbsolutePath(), null);
setTexture(tex.getTexture());
_object.getTexture().setUserData(newFile);
}catch (Exception e) {
String emsg=e.getLocalizedMessage();
if(emsg==null){
emsg=e.getMessage();
}
if(emsg==null){
emsg=e.toString();
}
// TODO i18n
System.err.println("Can not load texture "+newFile+" : "+emsg);
}
}
else if(value!=null && value instanceof Texture){
setTexture((Texture)value);
}
else{
setTexture(null);
}
}
}
private void setTexture(Texture t){
boolean forced=forceCapability(Appearance.ALLOW_TEXTURE_WRITE);
// also set image read to enable serialization
if(t!=null) t.getImage(0).setCapability(ImageComponent.ALLOW_IMAGE_READ);
// java3d 1.5.1 bug workaround
// set texture to null does not remove the retained
// texture when scene is not living so first attach
if(t==null && _ad!=null){
_ad.attach();
}
_object.setTexture(t);
// java3d 1.5.1 bug workaround
// detach again to restore capability
if(t==null && _ad!=null){
_ad.detach();
}
if(forced) restoreCapability(Appearance.ALLOW_TEXTURE_WRITE);
}
@Override
public String getDisplayClassName() {
return "jsynoptic.plugins.java3d.panels.TextureSelectorPanel";
}
}