Revision 44669 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.mapcontext/org.gvsig.fmap.mapcontext.api/src/main/java/org/gvsig/fmap/mapcontext/tools/coerce/CoerceStringToFont.java

View differences:

CoerceStringToFont.java
26 26
import java.awt.Font;
27 27

  
28 28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.dataTypes.AbstractCoercion;
30
import org.gvsig.tools.dataTypes.CoercionContext;
29 31
import org.gvsig.tools.dataTypes.CoercionException;
32
import org.gvsig.tools.dataTypes.DataTypes;
30 33
import org.gvsig.tools.dataTypes.DataTypesManager;
31
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
32 34

  
33 35
/**
34 36
 * Convert a String to Font.
35
 * 
36
 *  
37
 *
38
 *
37 39
 * @author gvSIG Team
38 40
 * @version $Id$
39
 * 
41
 *
40 42
 */
41
public class CoerceStringToFont implements Coercion {
43
@SuppressWarnings("UseSpecificCatch")
44
public class CoerceStringToFont extends AbstractCoercion {
42 45

  
43
	public CoerceStringToFont() {
44
		// Do nothing
45
	}
46
	
47
	public Object coerce(Object value) throws CoercionException {
48
		try {
49
			if( value == null ) {
50
				return value;
51
			}
52
			if( value instanceof String ) {
53
				SimpleAttributesSerializer properties = new SimpleAttributesSerializer("Font");
54
				properties.load((String) value);
55
				String name = (String) properties.get("name");
56
				int style = Integer.parseInt((String) properties.get("size"));
57
				int size = Integer.parseInt((String) properties.get("style"));
58
				return new Font(name, style, size);
59
			}
60
		} catch (Exception e) {
61
			throw new CoercionException(e);
62
		}
63
		throw new CoercionException();
64
	}
65
	public static void selfRegister() {
66
        DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
67
		int id = dataTypesManager.getType("String");
68
        dataTypesManager.addCoercion(id, new CoerceStringToFont());
69
	}
46
  public CoerceStringToFont() {
47
    // Do nothing
48
  }
49

  
50
  @Override
51
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
52
    if (value == null || value instanceof Font) {
53
      return value;
54
    }
55
    try {
56
      if (value instanceof String) {
57
        SimpleAttributesSerializer properties = new SimpleAttributesSerializer("Font");
58
        properties.load((String) value);
59
        String name = (String) properties.get("name");
60
        int style = Integer.parseInt((String) properties.get("size"));
61
        int size = Integer.parseInt((String) properties.get("style"));
62
        return new Font(name, style, size);
63
      }
64
    } catch (Exception e) {
65
      throw new CoercionException(e);
66
    }
67
    throw new CoercionException();
68
  }
69

  
70
  public static void selfRegister() {
71
    DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
72
    int id = dataTypesManager.getType("Font");
73
    dataTypesManager.addCoercion(id, new CoerceStringToFont());
74
  }
70 75
}
71

  

Also available in: Unified diff