Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / attributes / DefaultAttributes.java @ 24261

History | View | Annotate | Download (3.79 KB)

1
package org.gvsig.tools.attributes;
2

    
3
import java.util.HashMap;
4
import java.util.Iterator;
5
import java.util.Map;
6

    
7
import org.gvsig.tools.persistence.AbstractPersistenceManager;
8
import org.gvsig.tools.persistence.PersistenceException;
9
import org.gvsig.tools.persistence.PersistenceValueNotFoundException;
10
import org.gvsig.tools.persistence.Persistent;
11
import org.gvsig.tools.persistence.PersistentState;
12

    
13
public class DefaultAttributes implements Attributes, Persistent {
14

    
15
        private Map params;
16
        private Map alias;
17
        private boolean allowAnonimousAttributes;
18

    
19
        public DefaultAttributes() {
20
                this.params = new HashMap();
21
                this.alias = new HashMap();
22
                this.allowAnonimousAttributes = false;
23
        }
24

    
25
        public DefaultAttributes(boolean allowAnonimousAttributes) {
26
                this.params = new HashMap();
27
                this.alias = new HashMap();
28
                this.allowAnonimousAttributes = allowAnonimousAttributes;
29
        }
30

    
31
        final protected void setAllowAnonimousAttributes(
32
                        boolean allowAnonimousAttributes) {
33
                this.allowAnonimousAttributes = allowAnonimousAttributes;
34
        }
35

    
36
        public boolean allowAnonimousAttributes() {
37
                return this.allowAnonimousAttributes;
38
        }
39

    
40
        public AttributeDescriptor addAttribute(String name, int dataType,
41
                        String description, Object defaultValue) {
42
                AttributeDescriptor attr = new DefaultAttributeDescriptor(name,
43
                                dataType, description, defaultValue);
44
                this.params.put(name.toLowerCase(), attr);
45
                return attr;
46
        }
47

    
48
        public AttributeDescriptor addAttribute(String name, int dataType,
49
                        String description, Object defaultValue, Object[] avaliableValues) {
50
                AttributeDescriptor attr = new DefaultAttributeDescriptor(name,
51
                                dataType, description, defaultValue, avaliableValues);
52
                this.params.put(name.toLowerCase(), attr);
53
                return attr;
54
        }
55

    
56
        public AttributeDescriptor addAttribute(String name, int dataType,
57
                        String description, Object defaultValue, Object minValue,
58
                        Object maxValue) {
59
                AttributeDescriptor attr = new DefaultAttributeDescriptor(name,
60
                                dataType, description, defaultValue, minValue, maxValue);
61
                this.params.put(name.toLowerCase(), attr);
62
                return attr;
63
        }
64

    
65
        public Object getAttribute(String name) {
66
                DefaultAttributeDescriptor descriptor = (DefaultAttributeDescriptor) this.params
67
                                .get(name.toLowerCase());
68
                if (descriptor == null) {
69
                        throw new IllegalArgumentException(name);
70
                }
71
                return descriptor.getValue();
72
        }
73

    
74
        public Attributes setAttribute(String name, Object value) {
75
                DefaultAttributeDescriptor descriptor = (DefaultAttributeDescriptor) this.params
76
                                .get(name.toLowerCase());
77
                if (descriptor == null) {
78
                        throw new IllegalArgumentException(name);
79
                }
80
                descriptor.setValue(value);
81
                return this;
82
        }
83

    
84
        public PersistentState getState() throws PersistenceException {
85
                return AbstractPersistenceManager.getState(this);
86
        }
87

    
88
        public void loadState(PersistentState state) throws PersistenceException {
89
                state.set("Attributes.allowAnonimousAttributes",
90
                                allowAnonimousAttributes);
91
                Iterator it = params.values().iterator();
92
                while (it.hasNext()) {
93
                        DefaultAttributeDescriptor p = ((DefaultAttributeDescriptor) it
94
                                        .next());
95
                        state.set(p.getName(), p.getValue());
96
                }
97
        }
98

    
99
        public void setState(PersistentState state) throws PersistenceException {
100
                Iterator it = state.getNames();
101
                while (it.hasNext()) {
102
                        String name = (String) it.next();
103
                        if (name.equals("Attributes.allowAnonimousAttributes")) {
104
                                this.allowAnonimousAttributes = state.getBoolean(name);
105
                        } else {
106
                                try {
107
                                        this.setAttribute(name, state.get(name));
108
                                } catch (PersistenceValueNotFoundException e) {
109
                                        // Ignore
110
                                }
111
                        }
112
                }
113
        }
114

    
115

    
116
        public AttributeDescriptor addAttributeAlias(String name, String alias) {
117
                Object param = this.params.get(name);
118
                if (param == null) {
119
                        return null;
120
                }
121
                this.alias.put(alias, param);
122
                return (AttributeDescriptor) param;
123
        }
124

    
125
        public Iterator getAttributes() {
126
                return params.values().iterator();
127
        }
128

    
129
}