KickJava   Java API By Example, From Geeks To Geeks.

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


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.ShapeDrawComponent;
31 import org.nightlabs.editor2d.impl.Editor2DFactoryImpl;
32 import org.nightlabs.util.ColorUtil;
33 import com.thoughtworks.xstream.converters.MarshallingContext;
34 import com.thoughtworks.xstream.converters.UnmarshallingContext;
35 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
36 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
37
38 public abstract class ShapeDrawComponentConverter
39 //implements Converter
40
extends DrawComponentConverter
41 {
42     public ShapeDrawComponentConverter() {
43         super();
44         factory = new Editor2DFactoryImpl();
45     }
46
47     public boolean canConvert(Class JavaDoc type)
48     {
49         if (ShapeDrawComponent.class.isAssignableFrom(type)) {
50             return true;
51         }
52         return false;
53     }
54
55     public static final String JavaDoc GENERAL_SHAPE = ShapeDrawComponent.PROP_GENERAL_SHAPE;
56     public static final String JavaDoc FILL = ShapeDrawComponent.PROP_FILL;
57     public static final String JavaDoc FILL_COLOR = ShapeDrawComponent.PROP_FILL_COLOR;
58     public static final String JavaDoc LINE_COLOR = ShapeDrawComponent.PROP_LINE_COLOR;
59     public static final String JavaDoc LINE_STYLE = ShapeDrawComponent.PROP_LINE_STYLE;
60     public static final String JavaDoc LINE_WIDTH = ShapeDrawComponent.PROP_LINE_WIDTH;
61     
62     protected Editor2DFactoryImpl factory;
63     
64     protected String JavaDoc getNodeName() {
65         return "ShapeDrawComponent";
66     }
67     
68     protected DrawComponent readSpecific(DrawComponent dc, HierarchicalStreamReader reader, UnmarshallingContext context)
69     {
70         ShapeDrawComponent sdc = (ShapeDrawComponent) dc;
71         
72         // GeneralShape
73
// reader.moveDown();
74
// String generalShape = reader.getValue();
75
// sdc.setGeneralShape(factory.createGeneralShapeFromString(generalShape));
76
// reader.moveUp();
77
sdc.setGeneralShape(factory.createGeneralShapeFromString(reader.getAttribute(GENERAL_SHAPE)));
78         
79         // Fill
80
sdc.setFill(Boolean.parseBoolean(reader.getAttribute(FILL)));
81         
82         // Fill Color
83
// reader.moveDown();
84
// String fillColor = reader.getValue();
85
// sdc.setFillColor(ColorUtil.stringToColor(fillColor));
86
// reader.moveUp();
87
sdc.setFillColor(ColorUtil.stringToColor(reader.getAttribute(FILL_COLOR)));
88         
89         // Line Color
90
// reader.moveDown();
91
// String lineColor = reader.getValue();
92
// sdc.setLineColor(ColorUtil.stringToColor(lineColor));
93
// reader.moveUp();
94
sdc.setLineColor(ColorUtil.stringToColor(reader.getAttribute(LINE_COLOR)));
95                 
96         sdc.setLineWidth(Integer.parseInt(reader.getAttribute(LINE_WIDTH)));
97         sdc.setLineStyle(Integer.parseInt(reader.getAttribute(LINE_STYLE)));
98         
99         readAdditional(dc, reader, context);
100         
101         return sdc;
102     }
103
104     /**
105      * SubClasses should override this Method to get Additional Data
106      *
107      * @param dc the DrawComponent to read
108      * @param reader the reader to read
109      * @param context the context for the reader
110      */

111     protected void readAdditional(DrawComponent dc, HierarchicalStreamReader reader, UnmarshallingContext context)
112     {
113  
114     }
115     
116     protected void writeSpecific(DrawComponent dc, HierarchicalStreamWriter writer, MarshallingContext context)
117     {
118         ShapeDrawComponent sdc = (ShapeDrawComponent) dc;
119         
120 // writer.startNode(GENERAL_SHAPE);
121
// writer.setValue(factory.convertGeneralShapeToString(sdc.getGeneralShape()));
122
// writer.endNode();
123
writer.addAttribute(GENERAL_SHAPE, factory.convertGeneralShapeToString(sdc.getGeneralShape()));
124     writer.addAttribute(FILL, ""+sdc.isFill());
125     
126 // writer.startNode(FILL_COLOR);
127
// writer.setValue(ColorUtil.colorToString(sdc.getFillColor()));
128
// writer.endNode();
129
writer.addAttribute(FILL_COLOR, ColorUtil.colorToString(sdc.getFillColor()));
130     
131 // writer.startNode(LINE_COLOR);
132
// writer.setValue(ColorUtil.colorToString(sdc.getLineColor()));
133
// writer.endNode();
134
writer.addAttribute(LINE_COLOR, ColorUtil.colorToString(sdc.getLineColor()));
135     
136     writer.addAttribute(LINE_WIDTH, ""+sdc.getLineWidth());
137     writer.addAttribute(LINE_STYLE, ""+sdc.getLineStyle());
138     
139     writeAdditional(dc, writer, context);
140     }
141
142     /**
143      * SubClasses should override this Method to write Additional Data
144      *
145      * @param dc the DrawComponent to write
146      * @param writer the writer to write
147      * @param context the context for the writer
148      */

149     protected void writeAdditional(DrawComponent dc, HierarchicalStreamWriter writer, MarshallingContext context)
150     {
151         
152     }
153 }
154
Popular Tags