javauis/m2g_qt/javasrc/com/nokia/microedition/m2g/M2GSVGSVGElement.java
changeset 80 d6dafc5d983f
parent 56 abc41079b313
child 87 1627c337e51e
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
       
     1 /*
       
     2 * Copyright (c) 2005 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 package com.nokia.microedition.m2g;
       
    19 
       
    20 import org.w3c.dom.*;
       
    21 import org.w3c.dom.svg.*;
       
    22 import java.io.IOException;
       
    23 import com.nokia.mj.impl.utils.Logger;
       
    24 
       
    25 public class M2GSVGSVGElement extends M2GSVGLocatableElement implements SVGSVGElement
       
    26 {
       
    27     //--------------------------------------------------
       
    28     // STATIC CONSTANTS
       
    29     //--------------------------------------------------
       
    30     /* Optimization: static finals changed to local variables
       
    31     public static final String INVALID_SVG_VERSION_ESTR =
       
    32       "Invalid version.";
       
    33     public static final String INVALID_SVG_BASE_PROFILE_ESTR =
       
    34       "Invalid base profile.";
       
    35     */
       
    36 
       
    37     //------------------------------------------------------------------
       
    38     // VARIABLES
       
    39     //------------------------------------------------------------------
       
    40     // Exception text
       
    41     public static final String SCALE_BY_ZERO =
       
    42         "Cannot scale by 0.";
       
    43 
       
    44     //------------------------------------------------------------------
       
    45     // VARIABLES
       
    46     //------------------------------------------------------------------
       
    47     private Object iTimeGuard;
       
    48     private M2GSVGMatrix iMatrix;
       
    49     private M2GSVGPoint iCurrentTranslatePoint;
       
    50     private float iCurrentRotate;
       
    51     private float iCurrentScale;
       
    52     private float iCurrentTimeInSeconds;
       
    53 
       
    54     //------------------------------------------------------------------
       
    55     // METHODS
       
    56     //------------------------------------------------------------------
       
    57     /**
       
    58      * Constructor
       
    59      * @param aHandle -
       
    60      * @param aDocument -
       
    61      */
       
    62     protected M2GSVGSVGElement(int aHandle, M2GDocument aDocument)
       
    63     {
       
    64         super(aHandle, aDocument);
       
    65         iTimeGuard = new Object();
       
    66         iMatrix = (M2GSVGMatrix)createSVGMatrixComponents(1f, 0f, 0f, 1f, 0f, 0f);
       
    67         iCurrentTranslatePoint = new M2GSVGPoint(this);
       
    68         iCurrentRotate = 0.0f;
       
    69         iCurrentScale = 1.0f;
       
    70         iCurrentTimeInSeconds = 0.0f;
       
    71         refreshMatrix();
       
    72     }
       
    73 
       
    74     /**
       
    75      * @see org.w3c.dom.svg.SVGSVGElement#createSVGMatrixComponents()
       
    76      */
       
    77     public SVGMatrix createSVGMatrixComponents(
       
    78         float a, float b, float c, float d, float e, float f)
       
    79     {
       
    80         return new M2GSVGMatrix(a, b, c, d, e, f);
       
    81     }
       
    82 
       
    83     /**
       
    84        * @see org.w3c.dom.svg.SVGSVGElement#createSVGMatrixComponents()
       
    85      */
       
    86     public SVGPath createSVGPath()
       
    87     {
       
    88         return new M2GSVGPath(
       
    89                    M2GSVGPath._createPath(getNativeSVGProxyHandle()));
       
    90     }
       
    91 
       
    92     /**
       
    93        * @see org.w3c.dom.svg.SVGSVGElement#createSVGRect()
       
    94      */
       
    95     public SVGRect createSVGRect()
       
    96     {
       
    97         return new M2GSVGRect();
       
    98     }
       
    99 
       
   100     /**
       
   101        * @see org.w3c.dom.svg.SVGSVGElement#SVGRGBColor()
       
   102      */
       
   103     public SVGRGBColor createSVGRGBColor(int red, int green, int blue)
       
   104     {
       
   105         return new M2GSVGRGBColor(red, green, blue);
       
   106     }
       
   107 
       
   108     /**
       
   109        * @see com.nokia.microedition.m2g.M2GObject#doCleanup()
       
   110      */
       
   111     protected void doCleanup()
       
   112     {
       
   113         // Note this method is not automatically called if
       
   114         // doConstruct() method is not first called
       
   115         super.doCleanup();
       
   116     }
       
   117 
       
   118     /**
       
   119      * @see com.nokia.microedition.m2g.M2GObject#getCurrentRotate()
       
   120      */
       
   121     public float getCurrentRotate()
       
   122     {
       
   123         synchronized (this)
       
   124         {
       
   125             return iCurrentRotate;
       
   126         }
       
   127     }
       
   128 
       
   129     /**
       
   130      * @see com.nokia.microedition.m2g.M2GObject#getCurrentScale()
       
   131      */
       
   132     public float getCurrentScale()
       
   133     {
       
   134         synchronized (this)
       
   135         {
       
   136             return iCurrentScale;
       
   137         }
       
   138     }
       
   139 
       
   140     /**
       
   141      * @see com.nokia.microedition.m2g.M2GObject#getCurrentTime()
       
   142      */
       
   143     public float getCurrentTime()
       
   144     {
       
   145         synchronized (iTimeGuard)
       
   146         {
       
   147             return iCurrentTimeInSeconds;
       
   148         }
       
   149     }
       
   150 
       
   151     /**
       
   152        * @see com.nokia.microedition.m2g.M2GObject#getCurrentTranslate()
       
   153      */
       
   154     public SVGPoint getCurrentTranslate()
       
   155     {
       
   156         return iCurrentTranslatePoint;
       
   157     }
       
   158 
       
   159     /**
       
   160      * @org.w3c.dom.Node#getParentNode()
       
   161     */
       
   162     public Node getParentNode()
       
   163     {
       
   164         // Optimization: if(!getDocument().getConnectionPolicy().getAccessRight())
       
   165         if (!iDocument.iConnectionRight)
       
   166         {
       
   167             Logger.ELOG(Logger.EJavaUI, "getParentNode() - access rights failure");
       
   168             throw new SecurityException(M2GSVGConstants.ACCESS_RIGHTS_ESTR);
       
   169         }
       
   170         return null;
       
   171     }
       
   172 
       
   173     /**
       
   174      * Check svg version and base profile
       
   175      * @throws IOException If version or profile is invalid
       
   176     */
       
   177     public void checkVersionAndProfile() throws IOException
       
   178     {
       
   179         // Check version
       
   180         String trait = _getStringTrait(
       
   181                            getNativeSVGProxyHandle(),
       
   182                            getHandle(),
       
   183                            M2GSVGConstants.AT_VERSION);
       
   184         if (trait != null && (!trait.equals(
       
   185                                   M2GSVGConstants.getInstance().iSvgBaseProfileVersion)))
       
   186         {
       
   187             Logger.ELOG(Logger.EJavaUI, "doCheckValidity() - excption: svg version");
       
   188             throw new IOException(/*SF*/"Invalid version."/*SF*/);
       
   189         }
       
   190         // Check profile
       
   191         trait = _getStringTrait(
       
   192                     getNativeSVGProxyHandle(),
       
   193                     getHandle(),
       
   194                     M2GSVGConstants.AT_BASEPROFILE);
       
   195         if (trait != null && (!trait.equals(
       
   196                                   M2GSVGConstants.getInstance().iSvgBaseProfileName)))
       
   197         {
       
   198             Logger.ELOG(Logger.EJavaUI, "doCheckValidity() - excption: svg base profile");
       
   199             throw new IOException(/*SF*/"Invalid base profile."/*SF*/);
       
   200         }
       
   201     }
       
   202 
       
   203     /**
       
   204      * @javax.microedition.m2g.SVGImage#incrementTime()
       
   205      */
       
   206     public void incrementTime(float seconds)
       
   207     {
       
   208         incrementTimeNoUpdate(seconds);
       
   209         // update the media time to SVG engine
       
   210         updateTime();
       
   211     }
       
   212 
       
   213     /**
       
   214      * Increments the animation or media timeline for this SVGImage (in seconds)
       
   215      * but it does not update it to the SVG engine
       
   216      *
       
   217      * @note the time is updated to the engine during M2GScalableGraphics.RenderFrame()
       
   218      * @param seconds the value of time to increment in seconds
       
   219      *
       
   220      * @since S60 3.1
       
   221      */
       
   222     public void incrementTimeNoUpdate(float seconds)
       
   223     {
       
   224         if (seconds < 0)
       
   225         {
       
   226             throw new IllegalArgumentException();
       
   227         }
       
   228 
       
   229         if (Float.isNaN(iCurrentTimeInSeconds + seconds))
       
   230         {
       
   231             throw new IllegalArgumentException();
       
   232         }
       
   233 
       
   234         synchronized (iTimeGuard)
       
   235         {
       
   236             iCurrentTimeInSeconds += seconds;
       
   237         }
       
   238     }
       
   239 
       
   240     /**
       
   241      * Sets matrix trait
       
   242      */
       
   243     protected void refreshMatrix()
       
   244     {
       
   245         float matrixComponents[] = new float[M2GSVGMatrix.ARRAY_SIZE];
       
   246 
       
   247         synchronized (this)
       
   248         {
       
   249             iMatrix.getComponentsToArray(matrixComponents);
       
   250         }
       
   251 
       
   252         M2GSVGElement._setMatrixTrait(
       
   253              getNativeSVGProxyHandle(), getHandle(),
       
   254             M2GSVGConstants.AT_TRANSFORM, matrixComponents);
       
   255 
       
   256         // inform observer about changes in DOM
       
   257         iDocument.notifyDOMChangeObserver();
       
   258     }
       
   259 
       
   260     /**
       
   261      * Updates the time to engine
       
   262      */
       
   263     private void updateTime()
       
   264     {
       
   265         float currentTimeInSeconds = 0.0f;
       
   266 
       
   267         synchronized (iTimeGuard)
       
   268         {
       
   269             currentTimeInSeconds = iCurrentTimeInSeconds;
       
   270         }
       
   271 
       
   272         _setMediaTime(getNativeSVGProxyHandle(),
       
   273                       getDocument().getHandle(), currentTimeInSeconds);
       
   274 
       
   275         // inform observer about changes in DOM
       
   276         iDocument.notifyDOMChangeObserver();
       
   277     }
       
   278 
       
   279     /**
       
   280      * @see com.nokia.microedition.m2g.M2GObject#setCurrentRotate()
       
   281      */
       
   282     public void setCurrentRotate(float value)
       
   283     {
       
   284         String zoomAndPan = getTrait("zoomAndPan");
       
   285         if ((zoomAndPan != null) && zoomAndPan.equals("disable"))
       
   286         {
       
   287             Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   288                        "setCurrentRotate() - zoomAndPan:" + zoomAndPan + ", fail");
       
   289             return;
       
   290         }
       
   291         synchronized (this)
       
   292         {
       
   293             iCurrentRotate = value;
       
   294             updateTransformMatrix();
       
   295         }
       
   296     }
       
   297 
       
   298     /**
       
   299      * @see com.nokia.microedition.m2g.M2GObject#setCurrentScale()
       
   300      */
       
   301     public void setCurrentScale(float value)
       
   302     {
       
   303         String zoomAndPan = getTrait("zoomAndPan");
       
   304         if ((zoomAndPan != null) && zoomAndPan.equals("disable"))
       
   305         {
       
   306             Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   307                        "setCurrentScale() - zoomAndPan:" + zoomAndPan + ", fail");
       
   308             return;
       
   309         }
       
   310         if (value == 0)
       
   311         {
       
   312             throw new DOMException(DOMException.INVALID_ACCESS_ERR, SCALE_BY_ZERO);
       
   313         }
       
   314         synchronized (this)
       
   315         {
       
   316             iCurrentScale = value;
       
   317             updateTransformMatrix();
       
   318         }
       
   319     }
       
   320 
       
   321     /**
       
   322        * @see org.w3c.dom.svg.SVGSVGElement#setCurrentTime()
       
   323        * @note no support for setting the time backwards except for setting it to zero
       
   324        * @note engine resets the animation if current time is zero
       
   325      */
       
   326     public void setCurrentTime(float newSeconds)
       
   327     {
       
   328         synchronized (iTimeGuard)
       
   329         {
       
   330             if (newSeconds <= iCurrentTimeInSeconds && newSeconds != 0)
       
   331             {
       
   332                 // only forward time or zero time is accepted
       
   333                 return;
       
   334             }
       
   335 
       
   336             iCurrentTimeInSeconds = newSeconds;
       
   337         }
       
   338 
       
   339         updateTime();
       
   340     }
       
   341 
       
   342     /**
       
   343      * <pre>
       
   344      *  [currentScale      0       currentTranslate.x]   [cos(currentRotate) -sin(currentRotate 0]
       
   345      *  [     0      currentScale  currentTranslate.y] x [sin(currentRotate) cos(currentRotate) 0]
       
   346      *  [     0            0               1         ]   [         0                  0         1]
       
   347      * </pre>
       
   348      */
       
   349     public void updateTransformMatrix()
       
   350     {
       
   351         synchronized (this)
       
   352         {
       
   353             M2GSVGMatrix a = new M2GSVGMatrix(
       
   354                 iCurrentScale, 0f, 0f, iCurrentScale,
       
   355                 iCurrentTranslatePoint.getX(), iCurrentTranslatePoint.getY());
       
   356             M2GSVGMatrix b = new M2GSVGMatrix(
       
   357                 (float)Math.cos(iCurrentRotate *(float)Math.PI / 180.0f),
       
   358                 (float)Math.sin(iCurrentRotate *(float)Math.PI / 180.0f),
       
   359                 -(float)Math.sin(iCurrentRotate *(float)Math.PI / 180.0f),
       
   360                 (float)Math.cos(iCurrentRotate *(float)Math.PI / 180.0f),
       
   361                 0f, 0f);
       
   362             iMatrix = (M2GSVGMatrix)(a.mMultiply(b));
       
   363             refreshMatrix();
       
   364         }
       
   365     }
       
   366 
       
   367     //------------------------------------------------------------------
       
   368     // STATIC METHODS
       
   369     //------------------------------------------------------------------
       
   370     /**
       
   371      * Builds root element according to a document
       
   372      * @param aDocument
       
   373      * @return root element
       
   374      */
       
   375     public static M2GSVGSVGElement buildRootElement(M2GDocument aDocument)
       
   376     {
       
   377         // Get native handle
       
   378         int elementHandle = M2GSVGElement._getRootElement(
       
   379                                 aDocument.getNativeSVGProxyHandle(),
       
   380                                 aDocument.getHandle());
       
   381         // Check native handle
       
   382         if (!M2GObject.checkHandle(elementHandle))
       
   383         {
       
   384             return null;
       
   385         }
       
   386         // Create object
       
   387         M2GSVGSVGElement rootElement =
       
   388             new M2GSVGSVGElement(elementHandle, aDocument);
       
   389         if (rootElement != null)
       
   390         {
       
   391             // Register element
       
   392             aDocument.registerLiveElement(rootElement, new Integer(elementHandle));
       
   393         }
       
   394         return rootElement;
       
   395     }
       
   396 
       
   397     //------------------------------------------------------------------
       
   398     // NATIVE METHODS
       
   399     //------------------------------------------------------------------
       
   400     protected native static float _getMediaTime(
       
   401          int aSvgProxyHandle, int aDocumentHandle);
       
   402     private native static void _setMediaTime(
       
   403         int aSvgProxyHandle, int aDocumentHandle,
       
   404         float aSeconds );
       
   405 }
       
   406