1   /*
2    *  Copyright (c) 1998-2005, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Valentin Tablan, 09/11/2001
10   *
11   *  $Id: OptionsMap.java,v 1.5 2005/01/11 13:51:37 ian Exp $
12   */
13  package gate.util;
14  
15  import java.awt.Font;
16  import java.awt.font.TextAttribute;
17  import java.util.*;
18  /**
19   * A map that stores values as strings and provides support for converting some
20   * frequently used types to and from string
21   */
22  public class OptionsMap extends HashMap {
23  
24    /**
25     * Converts the value to string using its toString() method and then stores it
26     */
27    public Object put(Object key, Object value){
28      if(value instanceof Font){
29        Font font = (Font)value;
30        String family = font.getFamily();
31        int size = font.getSize();
32        boolean italic = font.isItalic();
33        boolean bold = font.isBold();
34        value = family + "#" + size + "#" + italic + "#" + bold;
35      }
36  
37      Object res = super.put(key, value.toString());
38      return res;
39    }
40  
41    /**
42     * If the object stored under key is an Integer then returns its value
43     * otherwise returns null;
44     */
45    public Integer getInt(Object key){
46      String stringValue = getString(key);
47      Integer value = null;
48      try{
49        value = Integer.decode(stringValue);
50      }catch(Exception e){};
51      return value;
52    }
53  
54    /**
55     * If the object stored under key is a Boolean then returns its value
56     * otherwise returns null;
57     */
58    public Boolean getBoolean(Object key){
59      String stringValue = getString(key);
60      Boolean value = null;
61      try{
62        value = Boolean.valueOf(stringValue);
63      }catch(Exception e){};
64      return value;
65    }
66  
67    /**
68     * If the object stored under key is a String then returns its value
69     * otherwise returns null;
70     */
71    public String getString(Object key){
72      String stringValue = null;
73      try{
74        stringValue = (String)get(key);
75      }catch(Exception e){};
76      return stringValue;
77    }
78  
79    /**
80     * If the object stored under key is a String then returns its value
81     * otherwise returns null;
82     */
83    public Font getFont(Object key){
84      String stringValue = null;
85      try{
86        stringValue = (String)get(key);
87      }catch(Exception e){};
88      if(stringValue == null) return null;
89      StringTokenizer strTok = new StringTokenizer(stringValue, "#", false);
90      String family = strTok.nextToken();
91      int size = Integer.parseInt(strTok.nextToken());
92      boolean italic = Boolean.valueOf(strTok.nextToken()).booleanValue();
93      boolean bold = Boolean.valueOf(strTok.nextToken()).booleanValue();
94  
95      Map fontAttrs = new HashMap();
96      fontAttrs.put(TextAttribute.FAMILY, family);
97      fontAttrs.put(TextAttribute.SIZE, new Float(size));
98      if(bold) fontAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
99      else fontAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR);
100     if(italic) fontAttrs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
101     else fontAttrs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
102 
103     return new Font(fontAttrs);
104   }
105 
106 
107 
108 }