View Javadoc

    1   /*
    2    * "Peko" Visual Novel System
    3    *
    4    * All Rights Reserved.
    5    * Copyright (c) 1999-2003 Tsukuba Bunko.
    6    *
    7    * $Id: InsetsDeserializer.java,v 1.1 2005/07/11 12:49:18 ppoi Exp $
    8    */
    9   package tsukuba_bunko.peko.resource;
    10
    11  import	java.awt.Insets;
    12
    13  import	tsukuba_bunko.resource.SimpleDeserializer;
    14
    15
    16  /***
    17   * {@link java.awt.Insets} 型のリソースに対する {@link tsukuba_bunko.resource.ResourceDeserializer} 実装です。
    18   * @author	$Author: ppoi $
    19   * @version	$Revision: 1.1 $
    20   * @see <a href="http://softlab.tsukuba-bunko.org/peko/userguide/resource.html#type-peko:insets">peko:insets 型のリソース</a>
    21   */
    22  public class InsetsDeserializer	extends SimpleDeserializer	{
    23
    24  	/***
    25  	 * <code>InsetsDeserializer</code> のインスタンスを作成します。
    26  	 */
    27  	public InsetsDeserializer()
    28  	{
    29  		super();
    30  	}
    31
    32
    33  //
    34  //	SimpleDeserializer の実装
    35  //
    36  	protected Object convertValue( String source )
    37  	{
    38  		return parseInsets( source );
    39  	}
    40
    41
    42  	/***
    43  	 * <code>source</code> を Insets として解析します。
    44  	 * @param	source	解析元の文字列
    45  	 * @return	解析結果
    46  	 */
    47  	public static Insets parseInsets( String source )
    48  	{
    49  		try	{
    50  			int	top, right, bottom, left;
    51  			int	begin = 0;
    52  			int	end = source.indexOf( ',' );
    53  			if( end == -1 )	{
    54  				top = Integer.parseInt( source.trim() );
    55  				return new Insets( top, top, top, top );
    56  			}
    57  			else	{
    58  				top = Integer.parseInt( source.substring(begin, end).trim() );
    59  			}
    60
    61  			begin = end + 1;
    62  			end = source.indexOf( ',', begin );
    63  			if( end == -1 )	{
    64  				right = Integer.parseInt( source.substring(begin).trim() );
    65  				return new Insets( top, right, top, right );
    66  			}
    67  			else	{
    68  				right = Integer.parseInt( source.substring(begin, end).trim() );
    69  			}
    70
    71  			begin = end + 1;
    72  			end = source.indexOf( ',', begin );
    73  			if( end == -1 )	{
    74  				bottom = Integer.parseInt( source.substring(begin).trim() );
    75  				return new Insets( top, right, bottom, right );
    76  			}
    77  			else	{
    78  				bottom = Integer.parseInt( source.substring(begin, end).trim() );
    79  			}
    80
    81  			begin = end + 1;
    82  			left = Integer.parseInt( source.substring(begin).trim() );
    83
    84  			return new Insets( top, left, bottom, right );
    85  		}
    86  		catch( Exception e )	{
    87  			throw new IllegalArgumentException( e.getMessage() );
    88  		}
    89  	}
    90  }