001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Valentin Tablan, 09/11/2001
011 *
012 * $Id: OptionsMap.java 12872 2010-07-08 11:29:23Z thomas_heitz $
013 */
014 package gate.util;
015
016 import java.awt.Font;
017 import java.awt.font.TextAttribute;
018 import java.util.*;
019 import java.io.Serializable;
020
021 /**
022 * A map that stores values as strings and provides support for converting some
023 * frequently used types to and from string.<br>
024 * Not very efficient as there is a lot of conversions from/to String.
025 * The conversion could happen only when loading/saving from/to a file.
026 */
027 public class OptionsMap extends TreeMap<Object, Object> {
028
029 /**
030 * Converts the value to string using {@link Strings#toString(Object)}
031 * method and then stores it.
032 * There is get methods for values that are a String, an Integer, a Boolean,
033 * a Font, a List of String and a Map of String*String.
034 */
035 public Object put(Object key, Object value) {
036 if(value instanceof Font){
037 Font font = (Font)value;
038 String family = font.getFamily();
039 int size = font.getSize();
040 boolean italic = font.isItalic();
041 boolean bold = font.isBold();
042 value = family + "#" + size + "#" + italic + "#" + bold;
043 }
044 return super.put(key, Strings.toString(value));
045 }
046
047 public Object put(Object key, List<String> value) {
048 return super.put(key, Strings.toString(value));
049 }
050
051 public Object put(Object key, Map<String, String> value) {
052 return super.put(key, Strings.toString(value));
053 }
054
055 /**
056 * If the object stored under key is an Integer then returns its value
057 * otherwise returns null.
058 * @param key key associated to the value to retrieve
059 * @return the associated integer
060 */
061 public Integer getInt(Object key) {
062 try {
063 return Integer.decode((String) get(key));
064 } catch (Exception e) {
065 return null;
066 }
067 }
068
069 /**
070 * If the object stored under key is an Double then returns its value
071 * otherwise returns null.
072 * @param key key associated to the value to retrieve
073 * @return the associated Double
074 */
075 public Double getDouble(Object key) {
076 try {
077 return Double.valueOf((String) get(key));
078 } catch (Exception e) {
079 return null;
080 }
081 }
082
083 /**
084 * If the object stored under key is a Boolean then returns its value
085 * otherwise returns false.
086 * @param key key associated to the value to retrieve
087 * @return the associated boolean
088 */
089 public Boolean getBoolean(Object key) {
090 try {
091 return Boolean.valueOf((String) get(key));
092 } catch (Exception e) {
093 return false;
094 }
095 }
096
097 /**
098 * If the object stored under key is a String then returns its value
099 * otherwise returns null.
100 * @param key key associated to the value to retrieve
101 * @return the associated string
102 */
103 public String getString(Object key) {
104 try {
105 return (String) get(key);
106 } catch (Exception e) {
107 return null;
108 }
109 }
110
111 /**
112 * If the object stored under key is a Font then returns its value
113 * otherwise returns null.
114 * @param key key associated to the value to retrieve
115 * @return the associated font
116 */
117 public Font getFont(Object key) {
118 try {
119 String stringValue = (String) get(key);
120 if (stringValue == null) { return null; }
121 StringTokenizer strTok = new StringTokenizer(stringValue, "#", false);
122 String family = strTok.nextToken();
123 int size = Integer.parseInt(strTok.nextToken());
124 boolean italic = Boolean.valueOf(strTok.nextToken());
125 boolean bold = Boolean.valueOf(strTok.nextToken());
126 HashMap<TextAttribute, Serializable> fontAttrs =
127 new HashMap<TextAttribute, Serializable>();
128 fontAttrs.put(TextAttribute.FAMILY, family);
129 fontAttrs.put(TextAttribute.SIZE, (float) size);
130 if(bold) fontAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
131 else fontAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR);
132 if(italic) fontAttrs.put(
133 TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
134 else fontAttrs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
135 return new Font(fontAttrs);
136 } catch (Exception e) {
137 return null;
138 }
139 }
140
141 /**
142 * If the object stored under key is a list then returns its value
143 * otherwise returns an empty list.
144 *
145 * @param key key associated to the value to retrieve
146 * @return the associated list
147 */
148 public List<String> getList(Object key) {
149 return Strings.toList((String) get(key), ", ");
150 }
151
152 /**
153 * If the object stored under key is a map then returns its value
154 * otherwise returns an empty map.
155 *
156 * @param key key associated to the value to retrieve
157 * @return the associated map
158 */
159 public Map<String, String> getMap(Object key) {
160 return Strings.toMap((String) get(key));
161 }
162 }
|