1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: GuiConvert.java 2223 2012-01-26 21:28:02Z dmsmith $
21   */
22  package org.crosswire.common.swing;
23  
24  import java.awt.Color;
25  import java.awt.Font;
26  
27  import org.crosswire.common.util.Logger;
28  import org.crosswire.common.util.StringUtil;
29  
30  /**
31   * Conversions between various types and Strings.
32   * 
33   * @see gnu.lgpl.License for license details.<br>
34   *      The copyright to this program is held by it's authors.
35   * @author Joe Walker [joe at eireneh dot com]
36   */
37  public final class GuiConvert {
38      /**
39       * We don't want anyone doing this ...
40       */
41      private GuiConvert() {
42      }
43  
44      /**
45       * Convert a String to a Font. Accepts one of two inputs:
46       * FamilyName-STYLE-size, where STYLE is either PLAIN, BOLD, ITALIC or
47       * BOLDITALIC<br/>
48       * or<br/>
49       * FamilyName,style,size, where STYLE is 0 for PLAIN, 1 for BOLD, 2 for
50       * ITALIC or 3 for BOLDITALIC.
51       * 
52       * @param value
53       *            the thing to convert
54       * @return the converted data
55       */
56      public static Font string2Font(String value) {
57          if (value == null || "".equals(value)) {
58              return null;
59          }
60  
61          // new way
62          if (value.indexOf(',') == -1) {
63              return Font.decode(value);
64          }
65  
66          // old way
67          String[] values = StringUtil.split(value, ",");
68          if (values.length != 3) {
69              log.warn("Illegal font name: " + value);
70              return null;
71          }
72          return new Font(values[0], Integer.parseInt(values[1]), Integer.parseInt(values[2]));
73      }
74  
75      /**
76       * Convert a Font to a String. Produces a format that can be read with
77       * <code>Font.decode(String)</code>.
78       * 
79       * @param font
80       *            the thing to convert
81       * @return the converted data
82       */
83      public static String font2String(Font font) {
84          if (font == null) {
85              return "";
86          }
87  
88          String strStyle = "plain";
89  
90          if (font.isBold()) {
91              strStyle = font.isItalic() ? "bolditalic" : "bold";
92          } else if (font.isItalic()) {
93              strStyle = "italic";
94          }
95  
96          return font.getName() + "-" + strStyle + "-" + font.getSize();
97      }
98  
99      /**
100      * Create a font just like the another with regard to style and size, but
101      * differing in font family.
102      * 
103      * @param fontspec
104      *            the font to model
105      * @param fontName
106      *            the font to use
107      * @return the font
108      */
109     public static Font deriveFont(String fontspec, String fontName) {
110         Font font = string2Font(fontspec);
111         Font derived = null;
112         if (font != null && fontName != null) {
113             derived = new Font(fontName, font.getStyle(), font.getSize());
114         }
115         return derived;
116     }
117 
118     /**
119      * Convert a String to a Color
120      * 
121      * @param value
122      *            the thing to convert
123      * @return the converted data
124      */
125     public static Color string2Color(String value) {
126         if (value == null || "".equals(value)) {
127             return null;
128         }
129 
130         if (value.length() != 7) {
131             log.warn("Illegal colour name: " + value);
132             return null;
133         }
134 
135         // log.fine("input=" + value);
136         String red = value.substring(1, 3);
137         String green = value.substring(3, 5);
138         String blue = value.substring(5, 7);
139         // log.fine("red=" + red + " green=" + green + " blue=" + blue);
140 
141         return new Color(Integer.parseInt(red, 16), Integer.parseInt(green, 16), Integer.parseInt(blue, 16));
142     }
143 
144     /**
145      * Convert a Color to a String
146      * 
147      * @param color
148      *            the thing to convert
149      * @return the converted data
150      */
151     public static String color2String(Color color) {
152         if (color == null) {
153             return "";
154         }
155 
156         String red = "00" + Integer.toHexString(color.getRed());
157         String green = "00" + Integer.toHexString(color.getGreen());
158         String blue = "00" + Integer.toHexString(color.getBlue());
159 
160         red = red.substring(red.length() - 2);
161         green = green.substring(green.length() - 2);
162         blue = blue.substring(blue.length() - 2);
163 
164         return "#" + red + green + blue;
165     }
166 
167     /**
168      * The log stream
169      */
170     private static final Logger log = Logger.getLogger(GuiConvert.class);
171 }
172