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.Point;
19  import java.util.StringTokenizer;
20  
21  import org.apache.commons.beanutils.Converter;
22  
23  /***
24   * A Converter that turns Strings in the form "x, y" into Point objects
25   *
26   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
27   * @version $Revision: 155420 $
28   */
29  public class PointConverter implements Converter {
30  
31      public Object convert(Class type, Object value) {
32          if ( value != null ) {
33              String text = value.toString();
34              StringTokenizer pointEnum = new StringTokenizer( text, "," );
35              int x = 0;
36              int y = 0;
37              if ( pointEnum.hasMoreTokens() ) {
38                  x = parseNumber( pointEnum.nextToken() );
39              }
40              if ( pointEnum.hasMoreTokens() ) {
41                  y = parseNumber( pointEnum.nextToken() );
42              }
43  
44              // now lets parse the Point...
45              return new Point( x, y );
46          }
47          return null;
48      }
49  
50      protected int parseNumber(String text) {
51          text = text.trim();
52          return Integer.parseInt(text);
53      }
54  
55  }