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.swt.converters;
17  
18  import java.util.StringTokenizer;
19  
20  import org.apache.commons.beanutils.Converter;
21  
22  import org.eclipse.swt.graphics.Point;
23  
24  /***
25   * A Converter that turns Strings in the form "x, y" into Point objects
26   *
27   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
28   * @version $Revision: 155420 $
29   */
30  public class PointConverter implements Converter {
31  
32      private static final PointConverter instance = new PointConverter();
33  
34      public static PointConverter getInstance() {
35          return instance;
36      }
37  
38      /***
39       * Parsers a String in the form "x, y" into an SWT Point class
40       * @param text
41       * @return Point
42       */
43      public Point parse(String text) {
44          StringTokenizer items = new StringTokenizer( text, "," );
45          int x = 0;
46          int y = 0;
47          if ( items.hasMoreTokens() ) {
48              x = parseNumber( items.nextToken() );
49          }
50          if ( items.hasMoreTokens() ) {
51              y = parseNumber( items.nextToken() );
52          }
53          return new Point( x, y );
54      }
55  
56      // Converter interface
57      //-------------------------------------------------------------------------
58      public Object convert(Class type, Object value) {
59          Object answer = null;
60          if ( value != null ) {
61              String text = value.toString();
62              answer = parse(text);
63          }
64  
65          System.out.println("Converting value: " + value + " into: " + answer);
66  
67          return answer;
68      }
69  
70      protected int parseNumber(String text) {
71          text = text.trim();
72          return Integer.parseInt(text.trim());
73      }
74  
75  }