View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.settings;
21  
22  import java.awt.Font;
23  import java.util.StringTokenizer;
24  
25  /***
26   * Provides a setting for <code>Font</code> objects.
27   */
28  public class FontSetting extends AbstractSetting<Font> {
29  
30      public FontSetting(SettingResource backend, String key, Font defaultValue, Validator validator)
31      {
32      	super(backend, key, defaultValue, validator);
33      }
34  
35      public FontSetting(SettingResource backend, String key, Font defaultValue)
36      {
37      	super(backend, key, defaultValue, null);
38      }
39  
40      protected Font fromString(String value)
41  	{
42  		// this could be way easier but there seems to be no serialize
43  		// method that can create the format recognized by getFont()
44  		//return Font.getFont(key);
45  
46  		StringTokenizer t = new StringTokenizer(value, ARRAY_SEPARATOR);
47  		if (t.countTokens() >= 3) {
48  			try {
49  				String name = t.nextToken();
50  				int style = Integer.parseInt(t.nextToken());
51  				int size = Integer.parseInt(t.nextToken());
52  
53  				return new Font(name, style, size);
54  			}
55  			catch (NumberFormatException e) {
56  			}
57  		}
58  
59  		return null;
60  	}
61  
62  	protected String toString(Font value)
63  	{
64  		Font font = (Font)value;
65  		
66  		// encode Font
67  		StringBuilder sb = new StringBuilder();
68  		sb.append(font.getName());
69  		sb.append(ARRAY_SEPARATOR);
70  		sb.append(font.getStyle());
71  		sb.append(ARRAY_SEPARATOR);
72  		sb.append(font.getSize());
73  		return sb.toString();
74  	}
75  
76  }