javauis/m2g_qt/javasrc/com/nokia/microedition/m2g/M2GSVGPath.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.svg.*;
       
    21 import org.w3c.dom.DOMException;
       
    22 import java.io.IOException;
       
    23 import com.nokia.mj.impl.utils.Logger;
       
    24 
       
    25 /**
       
    26  * This interface represents an "SVGPath" datatype used to define the path geometry.
       
    27  * Corresponds to SVG path specification or the "d" attribute.
       
    28  */
       
    29 public class M2GSVGPath extends M2GObject implements SVGPath
       
    30 {
       
    31     //--------------------------------------------------
       
    32     // STATIC CONTANTS
       
    33     //--------------------------------------------------
       
    34     /* Optimization: static finals changed to local variables
       
    35     private static final String SEGMENT_INDEX_SIZE_ESTR =
       
    36     "The segment index is out of bounds.";
       
    37     private static final String PARAMETER_INDEX_SIZE_ESTR =
       
    38     "The parameter index is out of bounds for this segment's type.";
       
    39     */
       
    40     private static final int CLOSE_PARAMETER_COUNT = 0;
       
    41     private static final int MOVE_TO_PARAMETER_COUNT = 2;
       
    42     private static final int LINE_TO_PARAMETER_COUNT = 2;
       
    43     private static final int CURVE_TO_PARAMETER_COUNT = 6;
       
    44     private static final int QUAD_TO_PARAMETER_COUNT = 4;
       
    45 
       
    46     //--------------------------------------------------
       
    47     // VARIABLES
       
    48     //--------------------------------------------------
       
    49 
       
    50     //--------------------------------------------------
       
    51     // METHODS
       
    52     //--------------------------------------------------
       
    53 
       
    54     /**
       
    55      * Constructor
       
    56      * @param aHandle - native path object handle
       
    57      * @param aDoCleanup - true if the native object needed to be deleted
       
    58      */
       
    59     protected M2GSVGPath(int aHandle)
       
    60     {
       
    61         super();
       
    62         setHandle(aHandle);
       
    63         doConstruct();
       
    64     }
       
    65 
       
    66     /**
       
    67     * @see com.nokia.microedition.m2g.M2GObject#doCheckValidity()
       
    68      */
       
    69     public boolean doCheckValidity()
       
    70     {
       
    71         // SVGPath is invalid if it begins with any segment other
       
    72         // than MOVE_TO segment. Note that an empty SVGPath is still a valid value.
       
    73         try
       
    74         {
       
    75             if (!super.doCheckValidity() ||
       
    76                     ((getNumberOfSegments() != 0) && (getSegment(0) != MOVE_TO)))
       
    77             {
       
    78                 return false;
       
    79             }
       
    80         }
       
    81         catch (IOException e)
       
    82         {
       
    83             return false;
       
    84         }
       
    85         return true;
       
    86     }
       
    87 
       
    88     /**
       
    89      * @see com.nokia.microedition.m2g.M2GObject#doCleanup()
       
    90      */
       
    91     protected void doCleanup()
       
    92     {
       
    93         _destroyPath(getNativeSVGProxyHandle(),
       
    94             getHandle());
       
    95         resetHandles();
       
    96     }
       
    97 
       
    98     /**
       
    99      * @see com.nokia.microedition.m2g.M2GObject#doConstruct()
       
   100      */
       
   101     protected void doConstruct()
       
   102     {
       
   103         super.doConstruct();
       
   104         register(this);
       
   105     }
       
   106 
       
   107 
       
   108     /**
       
   109      * @see org.w3c.dom.svg.SVGPath#getNumberOfSegments()
       
   110      */
       
   111     public int getNumberOfSegments()
       
   112     {
       
   113         return _getNumberOfSegments(getNativeSVGProxyHandle(),
       
   114                                     getHandle() );
       
   115     }
       
   116 
       
   117     /**
       
   118      * @see org.w3c.dom.svg.SVGPath#getSegment()
       
   119      */
       
   120     public short getSegment(int index) throws DOMException
       
   121     {
       
   122         if ((index < 0) ||
       
   123                 (index >= getNumberOfSegments()))
       
   124         {
       
   125             Logger.ELOG(Logger.EJavaUI, "getSegment() - exception:"
       
   126                         + /*SF*/"The segment index is out of bounds."/*SF*/);
       
   127             throw new DOMException(
       
   128                 DOMException.INDEX_SIZE_ERR,
       
   129                 /*SF*/"The segment index is out of bounds."/*SF*/);
       
   130         }
       
   131         return M2GSVGConstants.parsePathCommand(
       
   132                    _getSegmentType(getNativeSVGProxyHandle(),
       
   133                                    getHandle(),
       
   134                                    index)
       
   135                );
       
   136     }
       
   137 
       
   138     /**
       
   139      * Checks that parameter index is valid
       
   140      * @param aCommand -
       
   141      * @param aParameterIndex -
       
   142      */
       
   143     protected void checkParameterIndex(int aCommand, int aParameterIndex) throws DOMException
       
   144     {
       
   145         if ((aCommand == CLOSE && aParameterIndex >= CLOSE_PARAMETER_COUNT) ||
       
   146                 (aCommand == MOVE_TO && aParameterIndex >= MOVE_TO_PARAMETER_COUNT)
       
   147                 || (aCommand == LINE_TO && aParameterIndex >= LINE_TO_PARAMETER_COUNT)
       
   148                 || (aCommand == CURVE_TO && aParameterIndex >= CURVE_TO_PARAMETER_COUNT)
       
   149                 || (aCommand == QUAD_TO && aParameterIndex >= QUAD_TO_PARAMETER_COUNT))
       
   150         {
       
   151             throw new DOMException(
       
   152                 DOMException.INDEX_SIZE_ERR,
       
   153                 /*SF*/"The parameter index is out of bounds for this segment's type."/*SF*/);
       
   154         }
       
   155     }
       
   156 
       
   157     /**
       
   158     * @see org.w3c.dom.svg.SVGPath#close()
       
   159      */
       
   160     public void close()
       
   161     {
       
   162         _addClose(getNativeSVGProxyHandle(), getHandle());
       
   163     }
       
   164 
       
   165     /**
       
   166      * @see org.w3c.dom.svg.SVGPath#curveTo()
       
   167      */
       
   168     public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3)
       
   169     {
       
   170         _addCurveTo(getNativeSVGProxyHandle(),
       
   171             getHandle(), x1, y1, x2, y2, x3, y3);
       
   172     }
       
   173 
       
   174     /**
       
   175      * @see org.w3c.dom.svg.SVGPath#getSegmentParam()
       
   176      */
       
   177     public float getSegmentParam(int cmdIndex, int paramIndex)
       
   178     {
       
   179         if ((cmdIndex >= getNumberOfSegments()) ||
       
   180                 (cmdIndex < 0) ||
       
   181                 (paramIndex < 0))
       
   182         {
       
   183             Logger.ELOG(Logger.EJavaUI, "getSegmentParam() - end, exception:"
       
   184                         + /*SF*/"The segment index is out of bounds."/*SF*/);
       
   185             throw new DOMException(
       
   186                 DOMException.INDEX_SIZE_ERR,
       
   187                 /*SF*/"The segment index is out of bounds."/*SF*/);
       
   188         }
       
   189         short command = getSegment(cmdIndex);
       
   190         // Checks the validity of the parameter index.
       
   191         checkParameterIndex(command, paramIndex);
       
   192         return _getSegmentParameter(getNativeSVGProxyHandle(),
       
   193                                     getHandle(),
       
   194                                     cmdIndex,
       
   195                                     paramIndex
       
   196                                    );
       
   197     }
       
   198 
       
   199     /**
       
   200      * @see org.w3c.dom.svg.SVGPath#lineTo()
       
   201      */
       
   202     public void lineTo(float x, float y)
       
   203     {
       
   204         _addLineTo(getNativeSVGProxyHandle(), getHandle(), x, y);
       
   205     }
       
   206 
       
   207     /**
       
   208      * @see org.w3c.dom.svg.SVGPath#moveTo()
       
   209      */
       
   210     public void moveTo(float x, float y)
       
   211     {
       
   212         _addMoveTo(getNativeSVGProxyHandle(), getHandle(), x, y );
       
   213     }
       
   214 
       
   215     /**
       
   216      * @see org.w3c.dom.svg.SVGPath#quadTo()
       
   217      */
       
   218     public void quadTo(float x1, float y1, float x2, float y2)
       
   219     {
       
   220         _addQuadTo(getNativeSVGProxyHandle(),
       
   221                    getHandle(),
       
   222                    x1,
       
   223                    y1,
       
   224                    x2,
       
   225                    y2 );
       
   226     }
       
   227 
       
   228     //--------------------------------------------------
       
   229     // NATIVE METHODS
       
   230     //--------------------------------------------------
       
   231     private static native void _addClose(
       
   232         int aSVGProxyHandle, int aPathHandle);
       
   233 
       
   234     private static native void _addCurveTo(
       
   235         int aSVGProxyHandle, int aPathHandle,
       
   236         float aX1, float aY1, float aX2, float aY2, float aX3, float aY3);
       
   237 
       
   238     private static native void _addLineTo(
       
   239         int aSVGProxyHandle, int aPathHandle, float aX, float aY);
       
   240 
       
   241     private static native void _addMoveTo(
       
   242         int aSVGProxyHandle, int aPathHandle, float aX, float aY);
       
   243 
       
   244     private static native void _addQuadTo(
       
   245         int aSVGProxyHandle, int aPathHandle,
       
   246         float aX1, float aY1, float aX2, float aY2);
       
   247 
       
   248     static native int _createPath(
       
   249         int aSVGProxyHandle);
       
   250 
       
   251     private static native void _destroyPath(
       
   252         int aSVGProxyHandle, int aPathHandle);
       
   253 
       
   254     private static native int _getNumberOfSegments(
       
   255         int aSVGProxyHandle, int aPathHandle);
       
   256 
       
   257     private static native float _getSegmentParameter(
       
   258         int aSVGProxyHandle, int aPathHandle, int aCmdIndex, int aParamIndex);
       
   259 
       
   260     private static native short _getSegmentType(
       
   261         int aSVGProxyHandle, int aPathHandle, int aIndex);
       
   262 }
       
   263