themeinstaller/source/src/com/nokia/tools/themeinstaller/odtconverter/PropertyValue.java
branchRCL_3
changeset 17 fe49e33862e2
parent 16 b685c59de105
child 18 04b7640f6fb5
equal deleted inserted replaced
16:b685c59de105 17:fe49e33862e2
     1 /*
       
     2 * Copyright (c) 2007 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:  Single property value for property value list.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.odtconverter;
       
    20 
       
    21 import java.awt.Color;
       
    22 import java.io.IOException;
       
    23 
       
    24 /**
       
    25  * Property value stores style related data before it is externalized
       
    26  * to a binary format.
       
    27  */
       
    28 public class PropertyValue
       
    29     {
       
    30 
       
    31     // CONSTANTS
       
    32     // Ident types
       
    33     public static final String STRING_AUTO = "auto";
       
    34     public static final String STRING_NONE = "none";
       
    35     public static final String STRING_INHERIT = "inherit";
       
    36 
       
    37     public static final int E_NOT_SET = 0;
       
    38     public static final int E_AUTO = 1;
       
    39     public static final int E_NONE = 2;
       
    40     public static final int E_INHERIT = 3;
       
    41 
       
    42     // Property value type
       
    43     private Short iValueType;
       
    44 
       
    45     // Flag to identify ident
       
    46     private int iIdentType;
       
    47 
       
    48     // Rgb color value
       
    49     private Color iRgbValue;
       
    50 
       
    51     // Real value
       
    52     private double iRealValue;
       
    53 
       
    54     // String pool index to a string value
       
    55     private int iStringRef;
       
    56 
       
    57     // Reference to the String Pool
       
    58     private StringPool iStringPool;
       
    59 
       
    60     /**
       
    61      * Constructor.
       
    62      * @param aStringPool Reference to the String Pool
       
    63      */
       
    64     public PropertyValue( StringPool aStringPool )
       
    65         {
       
    66         iStringPool = aStringPool;
       
    67         }
       
    68 
       
    69     /**
       
    70      * Set rgb value.
       
    71      * @param aRgbValue the value to set
       
    72      * @param aValueType primitive value type
       
    73      * @throws ODTException
       
    74      * @throws ODTException if primitive value type is not a rgb color type
       
    75      */
       
    76     public void setRgbValue( Color aRgbValue )
       
    77         throws ODTException
       
    78         {
       
    79         iRgbValue = aRgbValue;
       
    80         iValueType = new Short( ValueTypeResolver.E_RGB_COLOR );
       
    81         }
       
    82 
       
    83     /**
       
    84      * Set real value.
       
    85      * @param aRealValue the value to set
       
    86      * @param aValueType primitive value type
       
    87      * @throws ODTException if primitive value type is not a real value type
       
    88      */
       
    89     public void setRealValue( double aRealValue, short aValueType )
       
    90         throws ODTException
       
    91         {
       
    92         // Verify the value type
       
    93         switch( aValueType )
       
    94             {
       
    95             case ValueTypeResolver.E_NUMBER:
       
    96             case ValueTypeResolver.E_PERCENTAGE:
       
    97             case ValueTypeResolver.E_EMS:
       
    98             case ValueTypeResolver.E_EXS:
       
    99             case ValueTypeResolver.E_PX:
       
   100             case ValueTypeResolver.E_CM:
       
   101             case ValueTypeResolver.E_MM:
       
   102             case ValueTypeResolver.E_IN:
       
   103             case ValueTypeResolver.E_PT:
       
   104             case ValueTypeResolver.E_PC:
       
   105             case ValueTypeResolver.E_DEG:
       
   106             case ValueTypeResolver.E_RAD:
       
   107             case ValueTypeResolver.E_GRAD:
       
   108             case ValueTypeResolver.E_MS:
       
   109             case ValueTypeResolver.E_S:
       
   110             case ValueTypeResolver.E_HZ:
       
   111             case ValueTypeResolver.E_KHZ:
       
   112             case ValueTypeResolver.E_UNIT_VALUE:
       
   113             break;
       
   114 
       
   115             default:
       
   116                 throw new ODTException(
       
   117                         "Property value type is not compatible with real: "
       
   118                         + aValueType );
       
   119             }
       
   120 
       
   121         iRealValue = aRealValue;
       
   122         iValueType = new Short( aValueType );
       
   123         }
       
   124 
       
   125     /**
       
   126      * Set string value.
       
   127      * @param aString the string to set
       
   128      * @param aValueType primitive value type
       
   129      * @throws ODTException if primitive value type is not a string type
       
   130      */
       
   131     public void setString( String aString, short aValueType )
       
   132         throws ODTException
       
   133         {
       
   134         // Verify the value type
       
   135         switch( aValueType )
       
   136             {
       
   137             case ValueTypeResolver.E_STRING:
       
   138             case ValueTypeResolver.E_IDENT:
       
   139             case ValueTypeResolver.E_URI:
       
   140             case ValueTypeResolver.E_ATTR:
       
   141             case ValueTypeResolver.E_UNKNOWN:
       
   142             break;
       
   143 
       
   144             default:
       
   145                 throw new ODTException(
       
   146                         "Property value type is not compatible with string: "
       
   147                         + aValueType );
       
   148             }
       
   149 
       
   150         // Add string to the pool
       
   151         iStringRef = iStringPool.addString( aString );
       
   152 
       
   153         // Resolve ident type
       
   154         if ( aString.equals( STRING_AUTO ) )
       
   155             {
       
   156             iIdentType = E_AUTO;
       
   157             }
       
   158         else if ( aString.equals( STRING_INHERIT ) )
       
   159             {
       
   160             iIdentType = E_INHERIT;
       
   161             }
       
   162         else if ( aString.equals( STRING_NONE ) )
       
   163             {
       
   164             iIdentType = E_NONE;
       
   165             }
       
   166         else
       
   167             {
       
   168             iIdentType = E_NOT_SET;
       
   169             }
       
   170         iValueType = new Short( aValueType );
       
   171         }
       
   172 
       
   173     /**
       
   174      * Externalize the property value to a stream.
       
   175      * @param aStream stream to use for externalization
       
   176      * @throws IOException if writing to a stream fails
       
   177      * @throws ODTException if value type can not be resolved
       
   178      */
       
   179     public void externalize( ODTDataOutputStream aStream )
       
   180         throws IOException, ODTException
       
   181         {
       
   182         // Write property value type - int8
       
   183         aStream.writeByte( iValueType.byteValue() );
       
   184 
       
   185         // Write property value data - real/int16/rgb
       
   186         switch( iValueType.intValue() )
       
   187             {
       
   188             // Real value
       
   189             case ValueTypeResolver.E_NUMBER:
       
   190             case ValueTypeResolver.E_PERCENTAGE:
       
   191             case ValueTypeResolver.E_EMS:
       
   192             case ValueTypeResolver.E_EXS:
       
   193             case ValueTypeResolver.E_PX:
       
   194             case ValueTypeResolver.E_CM:
       
   195             case ValueTypeResolver.E_MM:
       
   196             case ValueTypeResolver.E_IN:
       
   197             case ValueTypeResolver.E_PT:
       
   198             case ValueTypeResolver.E_PC:
       
   199             case ValueTypeResolver.E_DEG:
       
   200             case ValueTypeResolver.E_RAD:
       
   201             case ValueTypeResolver.E_GRAD:
       
   202             case ValueTypeResolver.E_MS:
       
   203             case ValueTypeResolver.E_S:
       
   204             case ValueTypeResolver.E_HZ:
       
   205             case ValueTypeResolver.E_KHZ:
       
   206             case ValueTypeResolver.E_UNIT_VALUE:
       
   207                 aStream.writeTReal64( iRealValue );
       
   208             break;
       
   209 
       
   210             // String value and ident type
       
   211             case ValueTypeResolver.E_STRING:
       
   212             case ValueTypeResolver.E_IDENT:
       
   213             case ValueTypeResolver.E_URI:
       
   214             case ValueTypeResolver.E_ATTR:
       
   215             case ValueTypeResolver.E_UNKNOWN:
       
   216                 aStream.writeInt16( iStringRef );
       
   217                 aStream.writeByte( iIdentType );
       
   218             break;
       
   219 
       
   220             // Rgb value
       
   221             case ValueTypeResolver.E_RGB_COLOR:
       
   222                 aStream.writeByte( iRgbValue.getRed() );
       
   223                 aStream.writeByte( iRgbValue.getGreen() );
       
   224                 aStream.writeByte( iRgbValue.getBlue() );
       
   225                 break;
       
   226             default:
       
   227                 throw new ODTException( "Property value can not be resolved: "
       
   228                         + iValueType );
       
   229             }
       
   230 
       
   231         }
       
   232 
       
   233     }