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/CoerceFontToString.java

View differences:

CoerceFontToString.java
27 27

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

  
34 35
/**
35 36
 * Convert a Font to String.
36
 * 
37
 *  
37
 *
38
 *
38 39
 * @author gvSIG Team
39 40
 * @version $Id$
40
 * 
41
 *
41 42
 */
42
public class CoerceFontToString implements Coercion {
43
	
44
	public CoerceFontToString() {
45
		// Do nothing
46
	}
47
	
48
	public Object coerce(Object value) throws CoercionException {
49
		try {
50
			if( value == null || value instanceof String ) {
51
				return value;
52
			}
53
			if( value instanceof Font ) {
54
				Font font = (Font) value;
55
				SimpleAttributesSerializer properties = new SimpleAttributesSerializer("Font");
56
				properties.put("name", font.getName());
57
				properties.put("style", new Integer(font.getStyle()));
58
				properties.put("size", new Integer(font.getSize()));
59
				return properties.toString();
60
			}
61
		} catch (Exception e) {
62
			throw new CoercionException(e);
63
		}
64
		throw new CoercionException();
65
	}
43
public class CoerceFontToString extends AbstractCoercion {
66 44

  
67
	public static void selfRegister() {
68
        DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
69
		if( dataTypesManager.getType("Font")== DataTypes.INVALID ) {
70
	        dataTypesManager.addtype(DataTypes.INVALID, null, "Font",Font.class, null);
71
		}
72
		int type = dataTypesManager.getType("Font");
73
        dataTypesManager.addCoercion(type, new CoerceStringToColor());
74
	}
45
  public CoerceFontToString() {
46
    // Do nothing
47
  }
48

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

  
69
  public static void selfRegister() {
70
    DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
71
    if (dataTypesManager.getType("Font") == DataTypes.INVALID) {
72
      dataTypesManager.addtype(DataTypes.INVALID, null, "Font", Font.class, null);
73
    }
74
    dataTypesManager.addCoercion(DataTypes.STRING, new CoerceFontToString());
75
  }
75 76
}
76

  

Also available in: Unified diff