Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / ValueType.java @ 1405

History | View | Annotate | Download (5.12 KB)

1
package org.gvsig.tools.dynobject.impl;
2

    
3
import org.gvsig.tools.ToolsLocator;
4
import org.gvsig.tools.dataTypes.DataType;
5
import org.gvsig.tools.dataTypes.DataTypes;
6
import org.gvsig.tools.dynobject.DynObject;
7
import org.gvsig.tools.dynobject.DynStruct;
8

    
9
public class ValueType implements org.gvsig.tools.lang.Cloneable {
10

    
11
    DataType type;
12
    Class theClass;
13
    DynStruct theDynClass;
14

    
15
    public ValueType(int type) {
16
        this.setType(type);
17
    }
18

    
19
    public void setType(int type) {
20
        if ( type == DataTypes.UNKNOWN ) {
21
            this.type = null;
22
        } else {
23
            DataType t = ToolsLocator.getDataTypesManager().get(type);
24
            setType(t);
25
        }
26
    }
27

    
28
    public void setType(DataType type) {
29
        if ( this.theDynClass != null ) {
30
            if ( type.getType() == DataTypes.DYNOBJECT ) {
31
                this.type = type;
32
            } else if ( type.getType() == DataTypes.UNKNOWN ) {
33
                this.type = ToolsLocator.getDataTypesManager().get(DataTypes.DYNOBJECT);
34
            } else {
35
                throw new IllegalArgumentException("Can't set type to non DYNOBJECT when have a dynclass assigned.");
36
            }
37
        } else {
38
            this.type = type;
39
            if( this.theClass==null ) {
40
                this.theClass = this.type.getDefaultClass();
41
            }
42
        }
43
    }
44

    
45
    public String getSubtype() {
46
        if ( this.type == null ) {
47
            return null;
48
        }
49
        return this.type.getSubtype();
50
    }
51

    
52
    public int getType() {
53
        if( this.type == null ) {
54
            return DataTypes.UNKNOWN;
55
        }
56
        return type.getType();
57
    }
58

    
59
    public DataType getDataType() {
60
        return type;
61
    }
62

    
63
    public void setClassOfValue(Class theClass) {
64
        if( theClass == null ) {
65
            this.theClass = null;
66
            return;
67
        }
68
        
69
        if ( this.type == null || type.getType() == DataTypes.UNKNOWN ) {
70
            this.type = ToolsLocator.getDataTypesManager().get(DataTypes.OBJECT);
71
        }
72
        if ( type.getType() == DataTypes.DYNOBJECT && theClass!=null) {
73
            if( !DynObject.class.isAssignableFrom(theClass) ) {                
74
                throw new IllegalArgumentException("Can't assign a Class '"+theClass.getName()+"' when type is '"+type.getName()+"'.");
75
            }
76
        }
77
        this.theClass = theClass;
78
    }
79

    
80
    public void setClassOfValue(DynStruct dynStrct) {
81
        if( dynStrct == null ) {
82
            this.theDynClass = null;
83
            return;
84
        }
85
        if ( this.type == null || type.getType() == DataTypes.UNKNOWN ) {
86
            this.type = ToolsLocator.getDataTypesManager().get(DataTypes.DYNOBJECT);
87
        }
88
        if ( type.getType() != DataTypes.DYNOBJECT && dynStrct!=null) {
89
            throw new IllegalArgumentException("Can't assign a DynClass '"+dynStrct.getFullName()+"' when type is '"+type.getName()+"'.");
90
        }
91
        this.theDynClass = dynStrct;
92
    }
93

    
94
    public void setClassOfValue(String theClassNameOfValue) {
95
        if( theClassNameOfValue == null ) {
96
            this.theDynClass = null;
97
            this.theClass = null;
98
            return;
99
        }
100
        if ( this.type == null ) {
101
            throw new IllegalStateException("Need to set type before assign classOfValue by name");
102
        }
103
        if ( type.getType() == DataTypes.DYNOBJECT ) {
104
            // Probablemente habria que posponer la busqueda de la clase hasta que se
105
            // invoque al metodo getDynClassOfValue, ya que si se esta cargando la 
106
            // DynClass desde un fichero xml puede que aun no esten disponibles las
107
            // clases del fichero xml para ser usadas en este momento.
108
            //             
109
            this.theDynClass = ToolsLocator.getDynObjectManager().get(theClassNameOfValue);
110
            if ( this.theDynClass == null ) {
111
                throw new IllegalArgumentException("DynClass of type '" + theClassNameOfValue + "' not found.");
112
            }
113
        } else {
114
            try {
115
                Class theClass = Class.forName(theClassNameOfValue);
116
                this.theClass = theClass;
117
            } catch (ClassNotFoundException ex) {
118
                throw new IllegalArgumentException("The class '" + theClassNameOfValue + "' not exists.");
119
            }
120
        }
121
    }
122

    
123
    public String getClassNameOfValue() {
124
        if ( type == null ) {
125
            return null;
126
        }
127
        if ( type.getType() == DataTypes.DYNOBJECT ) {
128
            if ( this.theDynClass == null ) {
129
                return null;
130
            }
131
            return this.theDynClass.getFullName();
132
        }
133
        if ( this.theClass == null ) {
134
            return null;
135
        }
136
        return this.theClass.getName();
137
    }
138

    
139
    public DynStruct getDynClassOfValue() {
140
        return this.theDynClass;
141
    }
142

    
143
    public Class getClassOfValue() {
144
        return this.theClass;
145
    }
146

    
147
    public String toString() {
148
        if ( this.type == null ) {
149
            return "(null)";
150
        }
151
        return this.type.toString();
152
    }
153

    
154
    @Override
155
    public Object clone() throws CloneNotSupportedException {
156
        ValueType other = (ValueType) super.clone();
157
        return other;
158
    }    
159
}