javauis/m3g_qt/javasrc/javax/microedition/m3g/Object3D.java
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package javax.microedition.m3g;
       
    20 
       
    21 import java.util.Enumeration;
       
    22 import java.util.Vector;
       
    23 import com.nokia.mj.impl.rt.support.Finalizer;
       
    24 
       
    25 /**
       
    26 */
       
    27 public abstract class Object3D
       
    28 {
       
    29     //------------------------------------------------------------------
       
    30     // Instance data
       
    31     //------------------------------------------------------------------
       
    32 
       
    33     int handle;
       
    34 
       
    35     private Object userObject;
       
    36     private Vector animTracks;
       
    37     private Interface iInterface;
       
    38 
       
    39     private Finalizer mFinalizer = new Finalizer()
       
    40     {
       
    41         public void finalizeImpl()
       
    42         {
       
    43             doFinalize();
       
    44         }
       
    45     };
       
    46 
       
    47     //------------------------------------------------------------------
       
    48     // Constructor(s)
       
    49     //------------------------------------------------------------------
       
    50 
       
    51     /**
       
    52      * <p>Only a package private constructor exists for this class.</p>
       
    53      */
       
    54     Object3D(int handle)
       
    55     {
       
    56         if (handle != 0)
       
    57         {
       
    58             this.handle = handle;
       
    59             _addRef(handle);
       
    60 
       
    61             // Get associated Interafece object and
       
    62             // register this instance with that
       
    63             iInterface = Interface.getInstance();
       
    64             iInterface.register(this);
       
    65 
       
    66             int n = _getAnimationTrackCount(handle);
       
    67             while (n-- > 0)
       
    68             {
       
    69                 linkAnimTrack((AnimationTrack) getInstance(_getAnimationTrack(handle, n)));
       
    70             }
       
    71         }
       
    72         else
       
    73         {
       
    74             System.out.println("Warning: Object3D constructor called with zero handle");
       
    75         }
       
    76     }
       
    77 
       
    78     //------------------------------------------------------------------
       
    79     // Public API
       
    80     //------------------------------------------------------------------
       
    81 
       
    82     public final Object3D duplicate()
       
    83     {
       
    84         int numRef = 1;
       
    85         if (this instanceof Node)
       
    86         {
       
    87             numRef = ((Node)this)._getSubtreeSize(handle);
       
    88         }
       
    89         int[] handles = new int[numRef * 2];
       
    90         Object3D obj = getInstance(_duplicate(handle, handles));
       
    91         for (int i = 0; i < numRef; i++)
       
    92         {
       
    93             Object userObj = getInstance(handles[i * 2]).getUserObject();
       
    94             Object3D duplicateObj = getInstance(handles[i * 2 + 1]);
       
    95             if (userObj != null)
       
    96             {
       
    97                 duplicateObj.setUserObject(userObj);
       
    98             }
       
    99         }
       
   100         return obj;
       
   101     }
       
   102 
       
   103     public int getReferences(Object3D[] references)
       
   104     {
       
   105         int[] handles = null;
       
   106         if (references != null)
       
   107         {
       
   108             handles = new int[references.length];
       
   109         }
       
   110         int num = _getReferences(handle, handles);
       
   111         if (references != null)
       
   112         {
       
   113             for (int i = 0; i < num; i++)
       
   114             {
       
   115                 references[i] = getInstance(handles[i]);
       
   116             }
       
   117         }
       
   118         return num;
       
   119     }
       
   120 
       
   121     public void setUserID(int userID)
       
   122     {
       
   123         _setUserID(handle, userID);
       
   124     }
       
   125 
       
   126     public int getUserID()
       
   127     {
       
   128         return _getUserID(handle);
       
   129     }
       
   130 
       
   131     public Object3D find(int userID)
       
   132     {
       
   133         return getInstance(_find(handle, userID));
       
   134     }
       
   135 
       
   136     public void addAnimationTrack(AnimationTrack animationTrack)
       
   137     {
       
   138         _addAnimationTrack(handle, animationTrack.handle);
       
   139         linkAnimTrack(animationTrack);
       
   140     }
       
   141 
       
   142     public AnimationTrack getAnimationTrack(int index)
       
   143     {
       
   144         /* Don't try to match the native indexing here -- just call
       
   145          * the native getter */
       
   146         return (AnimationTrack)getInstance(_getAnimationTrack(handle, index));
       
   147     }
       
   148 
       
   149     public void removeAnimationTrack(AnimationTrack animationTrack)
       
   150     {
       
   151         if (animationTrack != null)
       
   152         {
       
   153             _removeAnimationTrack(handle, animationTrack.handle);
       
   154 
       
   155             if (animTracks != null)
       
   156             {
       
   157                 animTracks.removeElement(animationTrack);
       
   158                 if (animTracks.isEmpty())
       
   159                 {
       
   160                     animTracks = null;
       
   161                 }
       
   162             }
       
   163         }
       
   164     }
       
   165 
       
   166     public int getAnimationTrackCount()
       
   167     {
       
   168         return _getAnimationTrackCount(handle);
       
   169     }
       
   170 
       
   171     public final int animate(int time)
       
   172     {
       
   173         return _animate(handle, time);
       
   174     }
       
   175 
       
   176     public void setUserObject(Object obj)
       
   177     {
       
   178         userObject = obj;
       
   179     }
       
   180 
       
   181     public Object getUserObject()
       
   182     {
       
   183         return userObject;
       
   184     }
       
   185 
       
   186     //------------------------------------------------------------------
       
   187     // Private methods
       
   188     //------------------------------------------------------------------
       
   189 
       
   190     static final Object3D getInstance(int handle)
       
   191     {
       
   192         return Interface.getObjectInstance(handle);
       
   193     }
       
   194 
       
   195     /**
       
   196      * Adds a reference to an animation track.
       
   197      */
       
   198     private void linkAnimTrack(AnimationTrack track)
       
   199     {
       
   200         if (animTracks == null)
       
   201         {
       
   202             animTracks = new Vector();
       
   203         }
       
   204         animTracks.addElement(track);
       
   205     }
       
   206 
       
   207     /**
       
   208      * Native peer finalization
       
   209      */
       
   210     private void doFinalize()
       
   211     {
       
   212         if (mFinalizer != null)
       
   213         {
       
   214             if (handle != 0)
       
   215             {
       
   216                 // finalize native peer
       
   217                 Platform.finalizeObject(handle, iInterface);
       
   218                 iInterface.deregister(this, iInterface);
       
   219 
       
   220                 // reset handles
       
   221                 iInterface = null;
       
   222                 handle= 0;
       
   223             }
       
   224             mFinalizer = null;
       
   225         }
       
   226     }
       
   227 
       
   228     // Native methods
       
   229     private static native int _addAnimationTrack(int hObject, int hAnimationTrack);
       
   230     private static native void _removeAnimationTrack(int hObject, int hAnimationTrack);
       
   231     private static native int _getAnimationTrackCount(int hObject);
       
   232     private static native int _animate(int hObject, int time);
       
   233     private static native void _setUserID(int hObject, int userID);
       
   234     private static native int _getUserID(int hObject);
       
   235 
       
   236     private static native void _addRef(int hObject);
       
   237     private static native int _getAnimationTrack(int hObject, int index);
       
   238     private static native int _duplicate(int hObject, int[] handles);
       
   239     private static native int _getReferences(int hObject, int[] handles);
       
   240     private static native int _find(int hObject, int userID);
       
   241 }