InterposerListener.java :  » 6.0-JDK-Modules » java-3d » org » jdesktop » j3dedit » scenegrapheditor » Java Open Source

Java Open Source » 6.0 JDK Modules » java 3d 
java 3d » org » jdesktop » j3dedit » scenegrapheditor » InterposerListener.java
/*
 *  $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/InterposerListener.java,v 1.1 2005/04/20 22:20:44 paulby Exp $
 *
 *                         Sun Public License Notice
 *
 *  The contents of this file are subject to the Sun Public License Version
 *  1.0 (the "License"). You may not use this file except in compliance with
 *  the License. A copy of the License is available at http://www.sun.com/
 *  
 *  The Original Code is the Java 3D(tm) Scene Graph Editor.
 *  The Initial Developer of the Original Code is Paul Byrne.
 *  Portions created by Paul Byrne are Copyright (C) 2002.
 *  All Rights Reserved.
 *  
 *  Contributor(s): Paul Byrne.
 *  
 **/
package org.jdesktop.j3dedit.scenegrapheditor;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Group;
import javax.media.j3d.Locale;

import org.jdesktop.j3dedit.interposerext.InterposerListenerInterface;

import org.jdesktop.j3dedit.J3dEditContext;
import org.jdesktop.j3dedit.interposer.InterposerLocale;
import org.jdesktop.j3dedit.scenegraph.SGGroup;
import org.jdesktop.j3dfly.utils.developmenttools.DevelopmentLocale;

/**
 *
 * @author  Paul Byrne
 * @version $Id: InterposerListener.java,v 1.1 2005/04/20 22:20:44 paulby Exp $
 */
public class InterposerListener implements InterposerListenerInterface {
    
    private final static boolean DEBUG_CALLS = false;
    
    private J3dEditContext context;
    
    /** Creates a new instance of InterposerListener */
    public InterposerListener() {
    }
    
    public void setContext( J3dEditContext context ) {
        this.context = context;
    }
    
    public void localeAddBranchGraph(Object locale, Object bg) {
        if (DEBUG_CALLS)
            System.out.println("localeAddBranchGraph "+locale+"   "+bg);
        
        if (context==null) {
            System.out.println("Context is null in localeAddBranchGraph*************");
            return;
        }
        
        DevelopmentLocale interposerLocale = getInterposerLocale( (Locale)locale );
        
        if (interposerLocale==null) {
           System.out.println("MULTIPLE LOCALES*********************");
           interposerLocale = new InterposerLocale( (javax.media.j3d.Locale)locale );
           context.addLocale( interposerLocale );
           interposerLocale.setSceneGraphChangeListeners( ((InterposerLocale)context.getLocale(0)).getSceneGraphChangeListeners() );
        } 
        
        // Need the if check because the locale may be a DevelopmentLocale
        if (interposerLocale instanceof InterposerLocale)
            interposerLocale.addBranchGraph( (BranchGroup)bg );
    }
    
    private DevelopmentLocale getInterposerLocale( Locale realLocale ) {
        if (realLocale==null)
            return null;
        
        if (realLocale instanceof DevelopmentLocale)
            return (DevelopmentLocale)realLocale;
        
        Locale locale;
        
        for( int i=0; i<context.numLocales(); i++) {
            locale = context.getLocale(i);
            if ( locale instanceof InterposerLocale && 
                 ((InterposerLocale)locale).getActualLocale()==realLocale)
                return (InterposerLocale)locale;
        }
        
        return null;
    }
    
    public void localeRemoveBranchGraph( Object locale, Object bg ) {
        if (DEBUG_CALLS)
            System.out.println("localeRemoveBranchGraph "+bg);
        
        DevelopmentLocale interposerLocale = getInterposerLocale( (Locale)locale );
        if (interposerLocale!=null && interposerLocale instanceof InterposerLocale) {
            interposerLocale.removeBranchGraph( (BranchGroup)bg );
        }
    }
    
    
    public void groupAddChild(Object group, Object child) {
        // Only interested in add's to live nodes.
        if (!((Group)group).isLive())
            return;
        
        if (DEBUG_CALLS)
            System.out.println("groupAddChild "+child);
        
        DevelopmentLocale locale = getLocale( group );
        if (locale==null)
            return;
        // This method is called before before the actual addChild
        // takes place, so group is live, but child is not live
        locale.notifyGroupAddChild( 
                (Group)group, 
                (BranchGroup)child);
    }
    
    public void groupSetChild(Object group, Object child, int index) {
        if (!((Group)group).isLive())
            return;
        
        if (DEBUG_CALLS)
            System.out.println("groupSetChild "+child);
        
        DevelopmentLocale locale = getLocale( group );
        if (locale==null)
            return;
        
        locale.notifyGroupSetChild( (Group)group, 
                                                 (BranchGroup)child, 
                                                 index );
    }
    
    public void groupRemoveAllChildren(Object group) {
        if (!((Group)group).isLive())
            return;
        
        if (DEBUG_CALLS)
            System.out.println("groupRemoveAllChildren "+group);
        
        DevelopmentLocale locale = getLocale( group );
        if (locale==null)
            return;
        
        locale.notifyGroupRemoveAllChildren( (Group)group );
    }
    
    public void groupRemoveChild(Object group, Object child) {
        if (!((Group)group).isLive())
            return;
        
        if (DEBUG_CALLS)
            System.out.println("groupRemoveChild "+child);
        
        DevelopmentLocale locale = getLocale( group );
        if (locale==null)
            return;
        
        locale.notifyGroupRemoveChild( (Group)group, (BranchGroup)child );
    }
    
    public void groupRemoveChild(Object group, int childIndex) {
        if (!((Group)group).isLive())
            return;
        
        if (DEBUG_CALLS)
            System.out.println("groupAddChild "+((Group)group).getChild(childIndex) );
        
        DevelopmentLocale locale = getLocale( group );
        if (locale==null)
            return;
        
        // TODO - figure out which child is being removed
        locale.notifyGroupRemoveChild( (Group)group, (BranchGroup)((Group)group).getChild(childIndex) );
    }
    
    private DevelopmentLocale getLocale( Object node ) {
        Locale l = context.getSceneGraphControl().getLocale((javax.media.j3d.Node)node);
        
        return getInterposerLocale( l );
    }
    
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.