KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > graphic > FontStyle


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.graphic;
20
21 import jcckit.util.ConfigParameters;
22 import jcckit.util.FactoryException;
23
24 import java.util.Hashtable JavaDoc;
25
26 /**
27  * Font style constants.
28  * This class is based on the typesafe enumeration pattern.
29  *
30  * @author Franz-Josef Elmer
31  */

32 public class FontStyle {
33   private static final Hashtable JavaDoc REPOSITORY = new Hashtable JavaDoc();
34   static final String JavaDoc NORMAL_TXT = "normal",
35                       BOLD_TXT = "bold",
36                       ITALIC_TXT = "italic",
37                       BOLD_ITALIC_TXT = "bold italic";
38   /** Font style constant. */
39   public static final FontStyle NORMAL = new FontStyle(NORMAL_TXT),
40                                 BOLD = new FontStyle(BOLD_TXT),
41                                 ITALIC = new FontStyle(ITALIC_TXT),
42                                 BOLD_ITALIC = new FontStyle(BOLD_ITALIC_TXT);
43
44   private final String JavaDoc _description;
45
46   /** Non-public constructor to control the number of instances. */
47   private FontStyle(String JavaDoc description) {
48     _description = description;
49     REPOSITORY.put(description, this);
50   }
51
52   /**
53    * Returns from the specified configuration parameters the font style
54    * defined by the specified key or the specified default value.
55    * @param config Configuration parameters.
56    * @param key The key of the font style.
57    * @param defaultValue The default value.
58    * @return one of the four instances of <tt>FontStyle</tt>.
59    * @throws FactoryException if the value of the key-value pair denoted
60    * by <tt>key</tt> is neither <tt>normal</tt>, <tt>bold</tt>,
61    * <tt>italic</tt>, nor <tt>bold italic</tt>,
62    * Note, that {@link FactoryException#getClassName()}
63    * returns the invalid value.
64    */

65   public static FontStyle getFontStyle(ConfigParameters config, String JavaDoc key,
66                                        FontStyle defaultValue) {
67     FontStyle result = defaultValue;
68     String JavaDoc value = config.get(key, null);
69     if (value != null) {
70       result = (FontStyle) REPOSITORY.get(value);
71       if (result == null) {
72         throw new FactoryException(config, key, "Invalid font style.");
73       }
74     }
75     return result;
76   }
77
78   /** Returns a human readable description for pretty printing. */
79   public String JavaDoc toString() {
80     return _description;
81   }
82 }
83
Popular Tags