View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.convert;
20  
21  import java.awt.Color;
22  
23  import java.text.FieldPosition;
24  import java.text.Format;
25  import java.text.ParseException;
26  import java.text.ParsePosition;
27  import org.apache.myfaces.trinidad.logging.TrinidadLogger;
28  
29  /**
30   * Format for colors.
31   * <p>
32   * @version $Name:  $ ($Revision: adfrt/faces/adf-faces-api/src/main/java/oracle/adf/view/faces/convert/ColorFormat.java#0 $) $Date: 10-nov-2005.19:09:10 $
33   * @todo This class needs to be part of the public API - need to move it to 
34   *        place where our public API exists, once we have figured it out.
35   */
36  abstract class ColorFormat extends Format
37  {
38    /**
39     * Returns the value as a Color.
40     */
41    public final Color parse(
42      String        source, 
43      ParsePosition status)
44    {
45      return (Color)parseObject(source, status);
46    }
47    
48    /**
49     * Returns the value as a Color.
50     */
51    public final Color parse(
52      String source) throws ParseException
53    {
54      return (Color)parseObject(source);
55    }
56    
57    /**
58     * Parses a string to produce an object.
59     *
60     * @exception ParseException if the specified string is invalid.
61     */
62    @Override
63    public Object parseObject(
64      String source) throws ParseException 
65    {
66      ParsePosition status = new ParsePosition(0);
67      Object result = null;
68      boolean illegalArgument = false;
69      
70      // We expect a IllegalArgumentException in case either of
71      // R/G/B color comps were to be out of range.
72      try
73      {
74        result = parseObject(source, status);
75      }
76      catch(IllegalArgumentException ie)
77      {
78        _LOG.warning(ie);
79        illegalArgument = true;
80      }
81  
82      int index = status.getIndex();
83      if (illegalArgument || index == 0 ||
84          (source != null && index < source.length())) 
85      {
86          throw new ParseException("Format.parseObject(String) failed",
87              status.getErrorIndex());
88      }
89      return result;
90    }
91  
92    /**
93     * Returns the value as a Color.
94     */
95    @Override
96    abstract public Object parseObject(
97      String        source, 
98      ParsePosition status);
99      
100   /**
101    * Formats a Color into a color string.
102    * 
103    * @param color  the color value to be formatted into a color string
104    * 
105    * @return the formatted color string
106    */
107   public final String format(
108     Color color)
109   {
110     return format(color, new StringBuffer(),new FieldPosition(0)).toString();
111   }
112 
113   @Override
114   public final StringBuffer format(
115     Object obj, 
116     StringBuffer toAppendTo,
117     FieldPosition fieldPosition)
118   {
119     if (obj instanceof Color)
120     {
121       return format((Color)obj, toAppendTo, fieldPosition);
122     }
123     else if (obj instanceof Number)
124     {
125       return format(new Color(((Number)obj).intValue()),
126                     toAppendTo, fieldPosition);
127     }
128     else 
129     {
130       throw 
131         new IllegalArgumentException(_LOG.getMessage(
132           "CANNOT_FORMAT_GIVEN_OBJECT_AS_COLOR"));
133     }
134   }
135   
136   abstract public StringBuffer format(
137     Color color,
138     StringBuffer toAppendTo,
139     FieldPosition pos);
140   private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
141     ColorFormat.class);
142 
143   private static final long serialVersionUID = 1L;
144 }