Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / tags / org.gvsig.tools-3.0.13 / org.gvsig.tools.lib / src / test / java / org / gvsig / tools / persistence / case1 / model / AwtFactory.java

History | View | Annotate | Download (3.7 KB)

1 802 cordinyana
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24 73 jjdelcerro
package org.gvsig.tools.persistence.case1.model;
25
26
import java.awt.Color;
27
import java.awt.geom.Point2D;
28
29
import org.gvsig.tools.dynobject.DynStruct;
30
import org.gvsig.tools.persistence.AbstractMultiPersistenceFactory;
31
import org.gvsig.tools.persistence.PersistentState;
32
import org.gvsig.tools.persistence.exception.PersistenceException;
33
34
public class AwtFactory extends AbstractMultiPersistenceFactory{
35
36
        private static final String DYNCLASS_COLOR_NAME = "Color";
37
        private static final String DYNCLASS_COLOR_DESCRIPTION = "Color";
38
        private static final String RED_FIELD = "red";
39
        private static final String GREEN_FIELD = "green";
40
        private static final String BLUE_FIELD = "blue";
41
        private static final String ALPHA_FIELD = "alpha";
42
43
        private static final String DYNCLASS_POINT2D_NAME = "Point2D";
44
        private static final String DYNCLASS_POINT2D_DESCRIPTION = "Point2D";
45
        private static final String FIELD_X = "x";
46
        private static final String FIELD_Y = "y";
47
48
        public AwtFactory() {
49
                super();
50
        }
51
52
        protected void makeDefinitions() {
53
                DynStruct color_definition = this.addDefinition(Color.class, DYNCLASS_COLOR_NAME, DYNCLASS_COLOR_DESCRIPTION);
54
55
                color_definition.addDynFieldInt(RED_FIELD).setMandatory(true);
56
                color_definition.addDynFieldInt(GREEN_FIELD).setMandatory(true);
57
                color_definition.addDynFieldInt(BLUE_FIELD).setMandatory(true);
58
                color_definition.addDynFieldInt(ALPHA_FIELD).setMandatory(true);
59
60
                DynStruct point_definition = this.addDefinition(Point2D.class, DYNCLASS_POINT2D_NAME, DYNCLASS_POINT2D_DESCRIPTION);
61
                point_definition.addDynFieldDouble(FIELD_X).setMandatory(true);
62
                point_definition.addDynFieldDouble(FIELD_Y).setMandatory(true);
63
64
        }
65
66
        public Object createFromState(PersistentState state)
67
                        throws PersistenceException {
68
                if( this.getManagedClass(state).isAssignableFrom(Color.class) ) {
69
                        int red = state.getInt(RED_FIELD);
70
                        int green = state.getInt(GREEN_FIELD);
71
                        int blue = state.getInt(BLUE_FIELD);
72
                        int alpha = state.getInt(ALPHA_FIELD);
73
74
                        return new Color(red, green, blue, alpha);
75
                }
76
77
                if( this.getManagedClass(state).isAssignableFrom(Point2D.class) ) {
78
                        double x = state.getDouble(FIELD_X);
79
                        double y = state.getDouble(FIELD_Y);
80
81
                        return new Point2D.Double(x, y);
82
                }
83
84
                throw new RuntimeException(); //FIXME
85
        }
86
87
        public void saveToState(PersistentState state, Object obj)
88
                        throws PersistenceException {
89
                if( obj instanceof Color ) {
90
                        Color color = (Color) obj;
91
                        state.set(RED_FIELD, color.getRed());
92
                        state.set(GREEN_FIELD, color.getGreen());
93
                        state.set(BLUE_FIELD, color.getBlue());
94
                        state.set(ALPHA_FIELD, color.getAlpha());
95
                        return;
96
                }
97
                if( obj instanceof Point2D ) {
98
                        Point2D point = (Point2D) obj;
99
                        state.set(FIELD_X, point.getX());
100
                        state.set(FIELD_Y, point.getY());
101
                        return;
102
                }
103
                throw new RuntimeException(); //FIXME
104
        }
105
106
}