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 @ 6900

History | View | Annotate | Download (5.96 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.ColorTableNotification;
8
import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClass;
9
import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClassNotification;
10
import org.gvsig.raster.lib.legend.impl.colortable.colortableclass.DefaultColorTableClassNotification;
11
import org.gvsig.tools.ToolsLocator;
12
import org.gvsig.tools.dynobject.DynStruct;
13
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable;
14
import org.gvsig.tools.persistence.PersistenceManager;
15
import org.gvsig.tools.persistence.PersistentState;
16
import org.gvsig.tools.persistence.exception.PersistenceException;
17

    
18
/**
19
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
20
 *
21
 */
22
public class DefaultColorTableClass extends BaseWeakReferencingObservable implements ColorTableClass {
23

    
24
    /**
25
     * Persistence definition name
26
     */
27
    public static final String PERSISTENT_NAME = "ColorTableClassPersistent";
28
    /**
29
     * Description of persistence definition
30
     */
31
    public static final String PERSISTENT_DESCRIPTION =
32
        "Persistent definition of color table class";
33

    
34
    private static final String COLOR_PERSISTENCE_FIELD = "color";
35
    private static final String NAME_PERSISTENCE_FIELD = "name";
36
    private static final String VALUE_PERSISTENCE_FIELD = "value";
37
    private static final String INTERPOLATED_PERSISTENCE_FIELD = "interpolated";
38

    
39
    private Color color;
40
    private String name;
41
    private double value;
42
    private double interpolated;
43

    
44
    /**
45
     * Empty constructor
46
     */
47
    public DefaultColorTableClass() {
48

    
49
    }
50

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

    
65
        if (color == null) {
66
            throw new IllegalArgumentException("Color can not be null");
67
        }
68

    
69
        this.name = name;
70
        this.value = value;
71
        this.color = color;
72
        this.interpolated = interpolated;
73
    }
74

    
75
    @Override
76
    public Color getColor() {
77
        return this.color;
78
    }
79

    
80
    @Override
81
    public void setColor(Color color) {
82

    
83
        if (color == null) {
84
            throw new IllegalArgumentException("Color can not be null");
85
        }
86

    
87
        this.color = color;
88
        this.notifyObservers(new DefaultColorTableClassNotification(ColorTableClassNotification.SETTED_COLOR, new Object[] { color }));
89

    
90
    }
91

    
92
    @Override
93
    public double getInterpolated() {
94
        return this.interpolated;
95
    }
96

    
97
    @Override
98
    public void setInterpolated(double interpolated) {
99
        this.interpolated = interpolated;
100
        this.notifyObservers(new DefaultColorTableClassNotification(ColorTableClassNotification.SETTED_INTERPOLATED, new Object[] { new Double(interpolated) }));
101
    }
102

    
103
    @Override
104
    public double getValue() {
105
        return this.value;
106
    }
107

    
108
    @Override
109
    public void setValue(double value) {
110
        this.value = value;
111
        this.notifyObservers(new DefaultColorTableClassNotification(ColorTableClassNotification.SETTED_VALUE, new Object[] { new Double(value) }));
112
    }
113

    
114
    @Override
115
    public String getName() {
116
        return this.name;
117
    }
118

    
119
    @Override
120
    public void setName(String name) {
121
        this.name = name;
122
        this.notifyObservers(new DefaultColorTableClassNotification(ColorTableClassNotification.SETTED_NAME, new Object[] { name }));
123
    }
124

    
125
    @Override
126
    public int compareTo(ColorTableClass otherClass) {
127
        Double value = Double.valueOf(this.getValue());
128
        Double otherValue = Double.valueOf(otherClass.getValue());
129

    
130
        if (value == null || otherValue == null) {
131
            throw new IllegalStateException("Value of ColorTableClass can not be null");
132
        }
133

    
134
        if (value.compareTo(otherValue) == 0) {
135
            if (StringUtils.isBlank(this.getName()) || StringUtils.isBlank(otherClass.getName())) {
136
                return value.compareTo(otherValue);
137
            }
138
            if (this.getName().compareTo(otherClass.getName()) == 0) {
139
                return 0;
140
            }
141
            return this.getName().compareTo(otherClass.getName());
142
        }
143
        return value.compareTo(otherValue);
144
    }
145

    
146
    public static void registerPersistence() {
147
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
148
        DynStruct definition = manager.getDefinition(PERSISTENT_NAME);
149
        if (definition == null) {
150
            definition =
151
                manager.addDefinition(DefaultColorTableClass.class, PERSISTENT_NAME,
152
                    PERSISTENT_DESCRIPTION, null, null);
153
            definition.addDynFieldString(NAME_PERSISTENCE_FIELD).setMandatory(false);
154
            definition.addDynFieldDouble(VALUE_PERSISTENCE_FIELD).setMandatory(false);
155
            definition.addDynFieldBoolean(INTERPOLATED_PERSISTENCE_FIELD).setMandatory(false);
156
            definition.addDynFieldObject(COLOR_PERSISTENCE_FIELD).setClassOfValue(Color.class)
157
                .setMandatory(false);
158
        }
159
    }
160

    
161
    @Override
162
    public void saveToState(PersistentState state) throws PersistenceException {
163
        state.set(NAME_PERSISTENCE_FIELD, this.getName());
164
        state.set(VALUE_PERSISTENCE_FIELD, this.getValue());
165
        state.set(INTERPOLATED_PERSISTENCE_FIELD, this.getInterpolated());
166
        state.set(COLOR_PERSISTENCE_FIELD, this.getColor());
167
    }
168

    
169
    @Override
170
    public void loadFromState(PersistentState state) throws PersistenceException {
171
        this.setName(NAME_PERSISTENCE_FIELD);
172
        this.setValue(state.getDouble(VALUE_PERSISTENCE_FIELD));
173
        this.setColor((Color) state.get(COLOR_PERSISTENCE_FIELD));
174
        this.setInterpolated(state.getDouble(INTERPOLATED_PERSISTENCE_FIELD));
175
    }
176

    
177
}