/* ========================
* 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: AttachDetach.java,v 1.2 2008/02/11 14:09:28 cazenave Exp $
*
* Changes
* -------
* 12 janv. 08 : Initial public release
*
*/
package jsynoptic.plugins.java3d;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Locale;
import javax.media.j3d.Node;
public class AttachDetach {
private int _detachKey;
private final Node _n;
private BranchGroup _branchGroup;
private Locale _locale;
// TODO remove ctor public access
// and make a factory to limit AttachDetach to one per BranchGroup and gets
// benefits of detach key
public AttachDetach(Node n) {
_n = n;
_detachKey = 0;
_locale = null;
}
public Node getNode() {
return _n;
}
/** Allow the scene to be displayed by attaching the main branchgroup */
public void attach() {
attach(0);
}
/** Allow the scene to be displayed by attaching the main branchgroup */
public void attach(int key) {
if (key != _detachKey)
return;
_locale.addBranchGraph(_branchGroup);
_detachKey = 0; // reset to allow default detach ops to work
}
/** Allow modifications to the scene by detaching the main branchgroup */
public void detach() {
detach(0);
}
/**
* Allow modifications to the scene by detaching the main branchgroup
* Attaching will be accepted only with the given key => this allows a code
* to detach once for all, and call functions that do attach/detach with
* other or no keys => the higher level detach/attach is the only one taken
* in account Tip: use Object.hashCode() to get a unique ID. Note: key 0 is
* the default. Thus, a current detachKey != 0 prevents default
* attach/detach ops.
*/
public void detach(int key) {
if ((_detachKey != 0) && (key != _detachKey))
return;
_branchGroup = getRootGroup();
_locale = _n.getLocale();
_locale.removeBranchGraph(_branchGroup);
_detachKey = key;
}
BranchGroup getRootGroup() {
Node n = _n;
while (n != null) {
if (n instanceof BranchGroup) {
BranchGroup bg = (BranchGroup) n;
if (bg.getParent() == null) {
return bg;
}
}
n = n.getParent();
}
return null;
}
}