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