Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.impl / src / main / java / org / gvsig / raster / lib / legend / impl / DefaultTransparencyRange.java @ 43803

History | View | Annotate | Download (8.86 KB)

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

    
3
import org.gvsig.raster.lib.legend.api.TransparencyRange;
4
import org.gvsig.tools.ToolsLocator;
5
import org.gvsig.tools.dynobject.DynStruct;
6
import org.gvsig.tools.persistence.PersistenceManager;
7
import org.gvsig.tools.persistence.PersistentState;
8
import org.gvsig.tools.persistence.exception.PersistenceException;
9

    
10
/**
11
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
12
 *
13
 */
14
public class DefaultTransparencyRange implements TransparencyRange {
15

    
16
    private static final String PERSISTENCE_NAME = "transparencyRange";
17
    private static final String PERSISTENCE_DESCRIPTION = "transparencyRange";
18

    
19
    private static final String IS_AND_PERSISTENCE_FIELD = "isAnd";
20
    private static final String RED_PERSISTENCE_FIELD = "red";
21
    private static final String GREEN_PERSISTENCE_FIELD = "green";
22
    private static final String BLUE_PERSISTENCE_FIELD = "blue";
23
    private static final String ALPHA_PERSISTENCE_FIELD = "aplha";
24

    
25
    private boolean isAnd;
26
    private int[] red;
27
    private int[] green;
28
    private int[] blue;
29
    private int alpha;
30

    
31
    public DefaultTransparencyRange() {
32
        this.isAnd = false;
33
        this.red = new int[2];
34
        this.red[0] = -1;
35
        this.red[1] = -1;
36
        this.green = new int[2];
37
        this.green[0] = -1;
38
        this.green[1] = -1;
39
        this.blue = new int[2];
40
        this.blue[0] = -1;
41
        this.blue[1] = -1;
42
        this.alpha = -1;
43
    }
44

    
45
    public DefaultTransparencyRange(int[] redRange, int[] greenRange, int[] blueRange, int alpha, boolean isAnd) {
46
        this.isAnd = isAnd;
47
        setRGB(redRange, greenRange, blueRange);
48
        setAlpha(alpha);
49
    }
50

    
51
    @Override
52
    public boolean isAnd() {
53
        return this.isAnd;
54
    }
55

    
56
    @Override
57
    public void setAnd(boolean flag) {
58
        this.isAnd = flag;
59
    }
60

    
61
    @Override
62
    public int[] getBlue() {
63
        return this.blue;
64
    }
65

    
66
    @Override
67
    public void setRGB(int[] red, int[] green, int[] blue) {
68

    
69
        if (red.length != 2 || green.length != 2 || blue.length != 2) {
70
            throw new IllegalArgumentException(
71
                "Transparency range values only accepts 2 values for each color");
72
        }
73

    
74
        this.red = red;
75
        this.green = green;
76
        this.blue = blue;
77
    }
78

    
79
    @Override
80
    public void setBlue(int[] blue) {
81
        if (blue.length != 2) {
82
            throw new IllegalArgumentException(
83
                "Transparency range values only accepts 2 values for each color");
84
        }
85

    
86
        this.blue = blue;
87
    }
88

    
89
    @Override
90
    public int[] getGreen() {
91
        return this.green;
92
    }
93

    
94
    @Override
95
    public void setGreen(int[] green) {
96
        if (green.length != 2) {
97
            throw new IllegalArgumentException(
98
                "Transparency range values only accepts 2 values for each color");
99
        }
100

    
101
        this.green = green;
102
    }
103

    
104
    @Override
105
    public int[] getRed() {
106
        return this.red;
107
    }
108

    
109
    @Override
110
    public void setRed(int[] red) {
111
        if (red.length != 2) {
112
            throw new IllegalArgumentException(
113
                "Transparency range values only accepts 2 values for each color");
114
        }
115

    
116
        this.red = red;
117
    }
118

    
119
    @Override
120
    public int getAlpha() {
121
        return this.alpha;
122
    }
123

    
124
    @Override
125
    public void setAlpha(int alpha) {
126

    
127
        if (alpha < 0 || alpha > 255) {
128
            throw new IllegalArgumentException("Alpha value has to be between 0 and 255");
129
        }
130

    
131
        this.alpha = alpha;
132
    }
133

    
134
    public static void registerPersistence() {
135

    
136
        PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
137
        DynStruct definition = persistenceManager.getDefinition(PERSISTENCE_NAME);
138
        if (definition == null) {
139
            definition =
140
                persistenceManager.addDefinition(DefaultTransparencyRange.class, PERSISTENCE_NAME,
141
                    PERSISTENCE_DESCRIPTION, null, null);
142
            definition.addDynFieldArray(RED_PERSISTENCE_FIELD).setClassOfValue(Integer.class)
143
                .setMandatory(false);
144
            definition.addDynFieldArray(GREEN_PERSISTENCE_FIELD).setClassOfValue(Integer.class)
145
                .setMandatory(false);
146
            definition.addDynFieldArray(BLUE_PERSISTENCE_FIELD).setClassOfValue(Integer.class)
147
                .setMandatory(false);
148
            definition.addDynFieldBoolean(IS_AND_PERSISTENCE_FIELD).setMandatory(false);
149
            definition.addDynFieldInt(ALPHA_PERSISTENCE_FIELD).setMandatory(false);
150
        }
151
    }
152

    
153
    @Override
154
    public void saveToState(PersistentState state) throws PersistenceException {
155
        state.set(RED_PERSISTENCE_FIELD, this.getRed());
156
        state.set(BLUE_PERSISTENCE_FIELD, this.getBlue());
157
        state.set(GREEN_PERSISTENCE_FIELD, this.getGreen());
158
        state.set(IS_AND_PERSISTENCE_FIELD, isAnd);
159
        state.set(ALPHA_PERSISTENCE_FIELD, alpha);
160
    }
161

    
162
    @Override
163
    public void loadFromState(PersistentState state) throws PersistenceException {
164
        this.red = state.getIntArray(RED_PERSISTENCE_FIELD);
165
        this.green = state.getIntArray(GREEN_PERSISTENCE_FIELD);
166
        this.blue = state.getIntArray(BLUE_PERSISTENCE_FIELD);
167
        this.alpha = state.getInt(ALPHA_PERSISTENCE_FIELD);
168
        this.isAnd = state.getBoolean(IS_AND_PERSISTENCE_FIELD);
169
    }
170

    
171
    @Override
172
    public String toString() {
173

    
174
        StringBuilder builder = new StringBuilder();
175
        int[] redValues = getRed();
176
        if (redValues[0] < 0) {
177
            builder.append("*");
178
        } else if (redValues[0] == redValues[1]) {
179
            builder.append(redValues[0]);
180
        } else if (redValues.length > 1) {
181
            builder.append("[");
182
            for (int i = 0; i < redValues.length; i++) {
183
                builder.append(redValues[i]);
184
                if (i < (redValues.length - 1)) {
185
                    builder.append(",");
186
                }
187
            }
188
            builder.append("]");
189
        }
190

    
191
        if (isAnd()) {
192
            builder.append(" & ");
193
        } else {
194
            builder.append(" | ");
195
        }
196

    
197
        int[] greenValues = getGreen();
198
        if (greenValues[0] < 0) {
199
            builder.append("*");
200
        } else if (greenValues[0] == greenValues[1]) {
201
            builder.append(greenValues[0]);
202
        } else if (greenValues.length > 1) {
203
            builder.append("[");
204
            for (int i = 0; i < greenValues.length; i++) {
205
                builder.append(greenValues[i]);
206
                if (i < (greenValues.length - 1)) {
207
                    builder.append(",");
208
                }
209
            }
210
            builder.append("]");
211
        }
212

    
213
        if (isAnd()) {
214
            builder.append(" & ");
215
        } else {
216
            builder.append(" | ");
217
        }
218

    
219
        int[] blueValues = getBlue();
220
        if (blueValues[0] < 0) {
221
            builder.append("*");
222
        } else if (blueValues[0] == blueValues[1]) {
223
            builder.append(blueValues[0]);
224
        } else if (blueValues.length > 1) {
225
            builder.append("[");
226
            for (int i = 0; i < blueValues.length; i++) {
227
                builder.append(blueValues[i]);
228
                if (i < (blueValues.length - 1)) {
229
                    builder.append(",");
230
                }
231
            }
232
            builder.append("]");
233
        }
234

    
235
        int alpha = getAlpha();
236
        if (alpha >= 0) {
237
            builder.append(" (alpha = ");
238
            builder.append(alpha);
239
            builder.append(")");
240
        }
241
        return builder.toString();
242

    
243
    }
244

    
245
//    @Override
246
//    public boolean equals(Object obj) {
247
//        if(!(obj instanceof TransparencyRange)){
248
//            return false;
249
//        }
250
//        TransparencyRange other = (TransparencyRange)obj;
251
//        if(other.getAlpha()!=this.getAlpha()){
252
//            return false;
253
//        }
254
//
255
//        return isSameRange(other);
256
//    }
257

    
258
    @Override
259
    public boolean isSameRange(TransparencyRange otherRange) {
260
//        if(!(otherRange instanceof TransparencyRange)){
261
//            return false;
262
//        }
263
//        if(otherRange.getAlpha()!=this.getAlpha()){
264
//            return false;
265
//        }
266

    
267
        if(otherRange.getRed().length!=this.getRed().length){
268
            return false;
269
        }
270
        if(otherRange.getGreen().length!=this.getGreen().length){
271
            return false;
272
        }
273
        if(otherRange.getBlue().length!=this.getBlue().length){
274
            return false;
275
        }
276

    
277
        if(otherRange.getRed()[0]!=this.getRed()[0]){
278
            return false;
279
        }
280
        if(otherRange.getRed()[1]!=this.getRed()[1]){
281
            return false;
282
        }
283

    
284
        if(otherRange.getGreen()[0]!=this.getGreen()[0]){
285
            return false;
286
        }
287
        if(otherRange.getGreen()[1]!=this.getGreen()[1]){
288
            return false;
289
        }
290

    
291
        if(otherRange.getBlue()[0]!=this.getBlue()[0]){
292
            return false;
293
        }
294
        if(otherRange.getBlue()[1]!=this.getBlue()[1]){
295
            return false;
296
        }
297

    
298
        return true;
299
    }
300
}