Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.legend / org.gvsig.raster.lib.legend.impl / src / main / java / org / gvsig / raster / lib / legend / impl / DefaultColorTableClass.java @ 6899

History | View | Annotate | Download (5.01 KB)

1
package org.gvsig.raster.lib.legend.impl;
2

    
3
import java.awt.Color;
4

    
5
import org.apache.commons.lang3.StringUtils;
6

    
7
import org.gvsig.raster.lib.legend.api.colortable.ColorTableClass;
8
import org.gvsig.tools.ToolsLocator;
9
import org.gvsig.tools.dynobject.DynStruct;
10
import org.gvsig.tools.persistence.PersistenceManager;
11
import org.gvsig.tools.persistence.PersistentState;
12
import org.gvsig.tools.persistence.exception.PersistenceException;
13

    
14
/**
15
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
16
 *
17
 */
18
public class DefaultColorTableClass implements ColorTableClass {
19

    
20
    /**
21
     * Persistence definition name
22
     */
23
    public static final String PERSISTENT_NAME = "ColorTableClassPersistent";
24
    /**
25
     * Description of persistence definition
26
     */
27
    public static final String PERSISTENT_DESCRIPTION =
28
        "Persistent definition of color table class";
29

    
30
    private static final String COLOR_PERSISTENCE_FIELD = "color";
31
    private static final String NAME_PERSISTENCE_FIELD = "name";
32
    private static final String VALUE_PERSISTENCE_FIELD = "value";
33
    private static final String INTERPOLATED_PERSISTENCE_FIELD = "interpolated";
34

    
35
    private Color color;
36
    private String name;
37
    private double value;
38
    private double interpolated;
39

    
40
    /**
41
     * Empty constructor
42
     */
43
    public DefaultColorTableClass() {
44

    
45
    }
46

    
47
    /**
48
     * Default constructor
49
     * 
50
     * @param name
51
     *            Name of class
52
     * @param value
53
     *            Value of class
54
     * @param color
55
     *            Color of class
56
     * @param interpolated
57
     *            Percent of interpolated
58
     */
59
    public DefaultColorTableClass(String name, double value, Color color, double interpolated) {
60

    
61
        if (color == null) {
62
            throw new IllegalArgumentException("Color can not be null");
63
        }
64

    
65
        this.name = name;
66
        this.value = value;
67
        this.color = color;
68
        this.interpolated = interpolated;
69
    }
70

    
71
    @Override
72
    public Color getColor() {
73
        return this.color;
74
    }
75

    
76
    @Override
77
    public void setColor(Color color) {
78

    
79
        if (color == null) {
80
            throw new IllegalArgumentException("Color can not be null");
81
        }
82

    
83
        this.color = color;
84
    }
85

    
86
    @Override
87
    public double getInterpolated() {
88
        return this.interpolated;
89
    }
90

    
91
    @Override
92
    public void setInterpolated(double interpolated) {
93
        this.interpolated = interpolated;
94
    }
95

    
96
    @Override
97
    public double getValue() {
98
        return this.value;
99
    }
100

    
101
    @Override
102
    public void setValue(double value) {
103
        this.value = value;
104
    }
105

    
106
    @Override
107
    public String getName() {
108
        return this.name;
109
    }
110

    
111
    @Override
112
    public void setName(String name) {
113
        this.name = name;
114
    }
115

    
116
    @Override
117
    public int compareTo(ColorTableClass otherClass) {
118
        Double value = Double.valueOf(this.getValue());
119
        Double otherValue = Double.valueOf(otherClass.getValue());
120

    
121
        if (value == null || otherValue == null) {
122
            throw new IllegalStateException("Value of ColorTableClass can not be null");
123
        }
124

    
125
        if (value.compareTo(otherValue) == 0) {
126
            if (StringUtils.isBlank(this.getName()) || StringUtils.isBlank(otherClass.getName())) {
127
                return value.compareTo(otherValue);
128
            }
129
            if (this.getName().compareTo(otherClass.getName()) == 0) {
130
                return 0;
131
            }
132
            return this.getName().compareTo(otherClass.getName());
133
        }
134
        return value.compareTo(otherValue);
135
    }
136

    
137
    public static void registerPersistence() {
138
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
139
        DynStruct definition = manager.getDefinition(PERSISTENT_NAME);
140
        if (definition == null) {
141
            definition =
142
                manager.addDefinition(DefaultColorTableClass.class, PERSISTENT_NAME,
143
                    PERSISTENT_DESCRIPTION, null, null);
144
            definition.addDynFieldString(NAME_PERSISTENCE_FIELD).setMandatory(false);
145
            definition.addDynFieldDouble(VALUE_PERSISTENCE_FIELD).setMandatory(false);
146
            definition.addDynFieldBoolean(INTERPOLATED_PERSISTENCE_FIELD).setMandatory(false);
147
            definition.addDynFieldObject(COLOR_PERSISTENCE_FIELD).setClassOfValue(Color.class)
148
                .setMandatory(false);
149
        }
150
    }
151

    
152
    @Override
153
    public void saveToState(PersistentState state) throws PersistenceException {
154
        state.set(NAME_PERSISTENCE_FIELD, this.getName());
155
        state.set(VALUE_PERSISTENCE_FIELD, this.getValue());
156
        state.set(INTERPOLATED_PERSISTENCE_FIELD, this.getInterpolated());
157
        state.set(COLOR_PERSISTENCE_FIELD, this.getColor());
158
    }
159

    
160
    @Override
161
    public void loadFromState(PersistentState state) throws PersistenceException {
162
        this.setName(NAME_PERSISTENCE_FIELD);
163
        this.setValue(state.getDouble(VALUE_PERSISTENCE_FIELD));
164
        this.setColor((Color) state.get(COLOR_PERSISTENCE_FIELD));
165
        this.setInterpolated(state.getDouble(INTERPOLATED_PERSISTENCE_FIELD));
166
    }
167

    
168
}