1 26 27 package org.nightlabs.editor2d.iofilter.xstream; 28 29 import org.nightlabs.editor2d.DrawComponent; 30 import org.nightlabs.editor2d.impl.Editor2DFactoryImpl; 31 import com.thoughtworks.xstream.converters.Converter; 32 import com.thoughtworks.xstream.converters.MarshallingContext; 33 import com.thoughtworks.xstream.converters.UnmarshallingContext; 34 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 35 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 36 37 public abstract class DrawComponentConverter 38 implements Converter 39 { 40 public static final String ID = DrawComponent.PROP_ID; 41 public static final String AFFINE_TRANSFORM = DrawComponent.PROP_AFFINE_TRANSFORM; 42 public static final String HORIZONTAL_GUIDE = DrawComponent.PROP_HORIZONTAL_GUIDE; 43 public static final String VERTICAL_GUIDE = DrawComponent.PROP_VERTICAL_GUIDE; 44 public static final String X = DrawComponent.PROP_X; 45 public static final String Y = DrawComponent.PROP_Y; 46 public static final String WIDTH = DrawComponent.PROP_WIDTH; 47 public static final String HEIGHT = DrawComponent.PROP_HEIGHT; 48 public static final String RENDER_MODE = DrawComponent.PROP_RENDER_MODE; 49 public static final String ROTATION = DrawComponent.PROP_ROTATION; 50 public static final String ROTATION_X = DrawComponent.PROP_ROTATION_X; 51 public static final String ROTATION_Y = DrawComponent.PROP_ROTATION_Y; 52 53 public static final String TMP_ROTATION_X = DrawComponent.PROP_TMP_ROTATION_X; 54 public static final String TMP_ROTATION_Y = DrawComponent.PROP_TMP_ROTATION_Y; 55 56 public static final String PARENT_ID = "ParentID"; 57 public static final String NAME = DrawComponent.PROP_NAME; 58 59 public static final Editor2DFactoryImpl factory = new Editor2DFactoryImpl(); 60 61 public DrawComponentConverter() { 62 super(); 63 } 64 65 public boolean canConvert(Class type) 66 { 67 if (DrawComponent.class.isAssignableFrom(type)) { 68 return true; 69 } 70 return false; 71 } 72 73 protected void write(Object source, HierarchicalStreamWriter writer, MarshallingContext context) 74 { 75 DrawComponent dc = (DrawComponent) source; 76 writer.startNode(getNodeName()); 77 writer.addAttribute(ID, ""+dc.getId()); 78 writer.addAttribute(PARENT_ID, ""+dc.getParent().getId()); 79 writer.addAttribute(X, ""+dc.getX()); 80 writer.addAttribute(Y, ""+dc.getY()); 81 writer.addAttribute(WIDTH, ""+dc.getWidth()); 82 writer.addAttribute(HEIGHT, ""+dc.getHeight()); 83 writer.addAttribute(RENDER_MODE, ""+dc.getRenderMode()); 84 writer.addAttribute(NAME, ""+dc.getName()); 85 writer.addAttribute(ROTATION, ""+dc.getRotation()); 86 writer.addAttribute(ROTATION_X, ""+dc.getRotationX()); 87 writer.addAttribute(ROTATION_Y, ""+dc.getRotationY()); 88 writer.addAttribute(AFFINE_TRANSFORM, factory.convertAffineTransformToString(dc.getAffineTransform())); 89 90 writeSpecific(dc, writer, context); 91 92 writer.endNode(); 93 } 94 95 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) 96 { 97 write(source, writer, context); 98 } 99 100 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) 101 { 102 return read(reader, context); 103 } 104 105 protected Object read(HierarchicalStreamReader reader, UnmarshallingContext context) 106 { 107 DrawComponent dc; 109 try { 110 dc = (DrawComponent) getImpl().newInstance(); 111 String nodeName = reader.getNodeName(); 112 reader.moveDown(); 113 dc.setId(Long.parseLong(reader.getAttribute(ID))); 114 dc.setX(Integer.parseInt(reader.getAttribute(X))); 115 dc.setY(Integer.parseInt(reader.getAttribute(Y))); 116 dc.setRenderMode(Integer.parseInt(reader.getAttribute(RENDER_MODE))); 119 dc.setName(reader.getAttribute(NAME)); 120 dc.setRotationX(Integer.parseInt(reader.getAttribute(ROTATION_X))); 121 dc.setRotationY(Integer.parseInt(reader.getAttribute(ROTATION_Y))); 122 dc.setRotation(Double.parseDouble(reader.getAttribute(ROTATION))); 123 dc.setAffineTransform(factory.createAffineTransformFromString(reader.getAttribute(AFFINE_TRANSFORM))); 124 dc.setTmpRotationX(DrawComponent.ROTATION_X_DEFAULT); 125 dc.setTmpRotationY(DrawComponent.ROTATION_Y_DEFAULT); 126 127 dc = readSpecific(dc, reader, context); 128 129 reader.moveUp(); 130 } catch (InstantiationException e) { 131 throw new RuntimeException (e); 132 } catch (IllegalAccessException e) { 133 throw new RuntimeException (e); 134 } 135 return dc; 136 } 137 138 protected abstract DrawComponent readSpecific(DrawComponent dc, HierarchicalStreamReader reader, UnmarshallingContext context); 139 protected abstract void writeSpecific(DrawComponent dc, HierarchicalStreamWriter writer, MarshallingContext context); 140 protected abstract String getNodeName(); 141 protected abstract Class getImpl(); 142 } 143 | Popular Tags |