KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > iofilter > xstream > DrawComponentConverter


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

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 JavaDoc ID = DrawComponent.PROP_ID;
41     public static final String JavaDoc AFFINE_TRANSFORM = DrawComponent.PROP_AFFINE_TRANSFORM;
42     public static final String JavaDoc HORIZONTAL_GUIDE = DrawComponent.PROP_HORIZONTAL_GUIDE;
43     public static final String JavaDoc VERTICAL_GUIDE = DrawComponent.PROP_VERTICAL_GUIDE;
44     public static final String JavaDoc X = DrawComponent.PROP_X;
45     public static final String JavaDoc Y = DrawComponent.PROP_Y;
46     public static final String JavaDoc WIDTH = DrawComponent.PROP_WIDTH;
47     public static final String JavaDoc HEIGHT = DrawComponent.PROP_HEIGHT;
48     public static final String JavaDoc RENDER_MODE = DrawComponent.PROP_RENDER_MODE;
49     public static final String JavaDoc ROTATION = DrawComponent.PROP_ROTATION;
50     public static final String JavaDoc ROTATION_X = DrawComponent.PROP_ROTATION_X;
51     public static final String JavaDoc ROTATION_Y = DrawComponent.PROP_ROTATION_Y;
52     
53     public static final String JavaDoc TMP_ROTATION_X = DrawComponent.PROP_TMP_ROTATION_X;
54     public static final String JavaDoc TMP_ROTATION_Y = DrawComponent.PROP_TMP_ROTATION_Y;
55     
56     public static final String JavaDoc PARENT_ID = "ParentID";
57     public static final String JavaDoc 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 JavaDoc type)
66     {
67         if (DrawComponent.class.isAssignableFrom(type)) {
68             return true;
69         }
70         return false;
71     }
72
73     protected void write(Object JavaDoc 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 JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context)
96     {
97         write(source, writer, context);
98     }
99
100     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context)
101     {
102         return read(reader, context);
103     }
104
105     protected Object JavaDoc read(HierarchicalStreamReader reader, UnmarshallingContext context)
106     {
107 // DrawComponent dc = new DrawComponentImpl();
108
DrawComponent dc;
109         try {
110             dc = (DrawComponent) getImpl().newInstance();
111             String JavaDoc 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.setWidth(Integer.parseInt(reader.getAttribute(WIDTH)));
117
// dc.setHeight(Integer.parseInt(reader.getAttribute(HEIGHT)));
118
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 JavaDoc e) {
131             throw new RuntimeException JavaDoc(e);
132         } catch (IllegalAccessException JavaDoc e) {
133             throw new RuntimeException JavaDoc(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 JavaDoc getNodeName();
141     protected abstract Class JavaDoc getImpl();
142 }
143
Popular Tags