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 javax.swing.DebugGraphics;
19  import java.util.StringTokenizer;
20  
21  import org.apache.commons.beanutils.Converter;
22  import org.apache.commons.beanutils.ConvertUtils;
23  
24  /***
25   * A Converter that turns Strings in one of the constants of
26   *    {@link DebugGraphics} to their appropriate integer constant.
27   *
28   * @author <a href="mailto:paul@activemath.org">Paul Libbrecht</a>
29   * @version $Revision: 155420 $
30   */
31  public class DebugGraphicsConverter implements Converter {
32  
33      private static String usageText =
34          "DebugGraphics options are set as a \"|\" separated list of words using one of the constants of DebugGraphics: log, flash, or buffered.";
35  
36      public static void register() {
37          ConvertUtils.register(
38              new DebugGraphicsConverter(),
39              java.lang.Integer.class);
40      }
41  
42      /*** Part of the Converter interface.
43       * @see org.apache.commons.beanutils.Converter#convert(java.lang.Class, java.lang.Object)
44       */
45      public Object convert(Class type, Object value) {
46          return convert(value);
47      }
48      
49      /*** This is not part of the converter interface, it's for use by
50       * classes that don't use DebugGraphicsConverter through BeanUtils.
51       * @param from
52       * @return
53       */
54      public Object convert(Object value) {
55          if (value != null) {
56              int result = 0;
57              StringTokenizer stok =
58                  new StringTokenizer(value.toString(), ", \t|", false);
59              while (stok.hasMoreTokens()) {
60                  String tok = stok.nextToken();
61                  result = result | recognizeOption(tok);
62              }
63              return new Integer(result);
64          }
65          return null;
66      }
67  
68      protected int recognizeOption(String value) {
69          value = value.toString().toLowerCase();
70  
71          if ("log".equals(value) || "log_option".equals(value)) {
72              return DebugGraphics.LOG_OPTION;
73          }
74          else if ("flash".equals(value) || "flash_option".equals(value)) {
75                  return DebugGraphics.FLASH_OPTION;
76          }
77          else if ("buffered".equals(value) || "buffered_option".equals(value)) {
78              return DebugGraphics.BUFFERED_OPTION;
79          }
80          else {
81              throw new IllegalArgumentException(usageText);
82          }
83      }
84  }