/* ========================
* 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: AnimatorButton.java,v 1.6 2008/11/14 17:03:15 cazenave Exp $
*
* Changes
* -------
* 1 avr. 08 : Initial public release
*
*/
package jsynoptic.plugins.java3d.panels;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.media.j3d.Node;
import javax.media.j3d.SceneGraphObject;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.UndoableEditEvent;
import javax.swing.undo.AbstractUndoableEdit;
import jsynoptic.plugins.java3d.SceneGraphObjectHolder;
import jsynoptic.plugins.java3d.Animator;
import jsynoptic.plugins.java3d.AnimatorFactory;
import jsynoptic.plugins.java3d.GraphObjectReference;
import jsynoptic.plugins.java3d.Universe;
import jsynoptic.plugins.java3d.edit.PropertyEdit;
import jsynoptic.plugins.java3d.panels.SceneGraphObjectDialog.RemoveListener;
/**
*
*/
public class AnimatorButton<T extends SceneGraphObject, E> extends JButton implements ActionListener{
final PropertyEdit<T, E> _p;
final AnimatorFactory.Parameters _param;
final Frame _owner;
final JComponent _editorComponent;
Animator _anim;
// TODO i18N
static final String animateText="Animate...";
public AnimatorButton(AnimatorFactory.Parameters param, PropertyEdit<T, E> p,
JComponent editorComponent, Frame owner){
_p=p;
_param=param;
_editorComponent=editorComponent;
_owner=owner;
setup();
addActionListener(this);
}
protected void setup(){
_anim=AnimatorFactory.getAnimator(_p.getObject());
setText(animateText);
if(_anim!=null){
_editorComponent.setEnabled(false);
}
else{
_editorComponent.setEnabled(true);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(_anim==null){
AnimatorListSelection dsel=new AnimatorListSelection();
dsel.setLocation(_owner.getLocationOnScreen());
dsel.pack();
dsel.setVisible(true);
}
if(_anim!=null){
SceneGraphObjectHolder animHolder=Universe.findHolder(_p.getHolder(), _anim);
if(animHolder==null){
throw new RuntimeException("Invalid Animator");
}
SceneGraphObjectDialog.createDialog(animHolder,
_owner.getLocationOnScreen(), new RemoveListener(){
@Override
public void remove() {
removeFromAnimator();
}
});
}
setup();
}
private void removeFromAnimator(){
GraphObjectReference ref=null;
for(GraphObjectReference gor : _anim.getGraphObjects()){
if(gor.getObject()==_p.getObject()){
ref=gor;
break;
}
}
if(ref==null){
throw new RuntimeException("Invalid Animator Reference");
}
SceneGraphObjectHolder animHolder=Universe.findHolder(_p.getHolder(), _anim);
if(animHolder==null){
throw new RuntimeException("Invalid Animator");
}
animHolder.removeSceneGraphObject(ref, null);
RemoveEdit re=new RemoveEdit(animHolder, ref);
animHolder.getUndoableEditListener().undoableEditHappened(
new UndoableEditEvent(this, re));
}
/**
* An undoable remove of one object reference
*/
public static class RemoveEdit extends AbstractUndoableEdit {
final GraphObjectReference _ref;
final SceneGraphObjectHolder _animHolder;
/**
* @see AbstractUndoableEdit#undo()
*/
public void undo() {
super.undo();
_animHolder.addSceneGraphObject(_ref);
}
/**
* @see AbstractUndoableEdit#redo()
*/
public void redo() {
super.redo();
_animHolder.removeSceneGraphObject(_ref, null);
}
/**
*
* @param _sourceShape The deleted Shape
*/
public RemoveEdit(SceneGraphObjectHolder owner, GraphObjectReference ref) {
super();
_animHolder=owner;
_ref=ref;
}
public String getPresentationName() {
return "remove";
}
}
public class AnimatorListSelection extends JDialog {
Animator _an;
public AnimatorListSelection(){
super(_owner, "Animator selection"); // TODO i18n
Animator anc=AnimatorFactory.create(_param);
final Node n=_p.getHolder().getSceneGraphNode();
AnimatorList list=new AnimatorList(AnimatorFactory.getCompatibleAnimators(anc, n));
getContentPane().add(list, CENTER);
JPanel pbutton=new JPanel();
// TODO i18n
JButton bcreate=new JButton("Create");
bcreate.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
_an=AnimatorFactory.create(_param);
dispose();
// get the first group node as a parent
// SceneGraphObjectHolder holder=_p.getHolder().getParentHolder();
// while(holder!=null && ! (holder.getSceneGraphObject() instanceof BranchGroup)){
// holder=holder.getParentHolder();
// }
// if(holder==null){
// throw new RuntimeException("Can not find a branch group to hold the new animator");
// }
// holder.addSceneGraphObject(_an);
// AddEdit ae=new AddEdit(holder,_an,null);
// holder.getUndoableEditListener().undoableEditHappened(
// new UndoableEditEvent(this, ae));
//
// ArrayList<SceneGraphObjectHolder> cl=new ArrayList<SceneGraphObjectHolder>();
// holder.getChildrenHolders(cl);
// holder=null;
// for(SceneGraphObjectHolder h : cl){
// if(h.getSceneGraphObject()==_an){
// holder=h;
// break;
// }
// }
// if(holder==null){
// throw new RuntimeException("Can not find animator node holder");
// }
// holder.addSceneGraphObject(_p.getHolder().getSceneGraphObject());
SceneGraphObjectHolder holder=_p.addAnimator(_an, this);
SceneGraphObjectDialog.createDialog(holder,
_owner.getLocationOnScreen(), new RemoveListener(){
@Override
public void remove() {
removeFromAnimator();
}
});
}
});
JButton bcancel=new JButton("Cancel");
bcancel.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
pbutton.add(bcreate);
pbutton.add(bcancel);
getContentPane().add(pbutton);
}
public class AnimatorList extends JList {
public AnimatorList(ArrayList<Animator> ans){
super(ans.toArray());
}
}
}
}