View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.swing.converters;
17  
18  import java.awt.Dimension;
19  import java.util.StringTokenizer;
20  
21  import org.apache.commons.beanutils.Converter;
22  
23  /***
24   * A Converter that turns Strings in the form "width, height" into Dimension objects
25   *
26   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
27   * @version $Revision: 155420 $
28   */
29  public class DimensionConverter implements Converter {
30  
31      public Object convert(Class type, Object value) {
32          if ( value != null ) {
33              String text = value.toString();
34              StringTokenizer dimensionEnum = new StringTokenizer( text, "," );
35              int width = 0;
36              int height = 0;
37              if ( dimensionEnum.hasMoreTokens() ) {
38                  width = parseNumber( dimensionEnum.nextToken() );
39              }
40              if ( dimensionEnum.hasMoreTokens() ) {
41                  height = parseNumber( dimensionEnum.nextToken() );
42              }
43  
44              // now lets parse the dimension...
45              return new Dimension( width, height );
46          }
47          return null;
48      }
49  
50      protected int parseNumber(String text) {
51          text = text.trim();
52          return Integer.parseInt(text);
53      }
54  
55  }