Statistics
| Revision:

gvsig-sldtools / org.gvsig.sld / org.gvsig.sldsupport / org.gvsig.sldsupport.lib / org.gvsig.sldsupport.lib.api / src / main / java / org / gvsig / sldsupport / sld / symbol / misc / SLDStroke.java @ 46

History | View | Annotate | Download (5.85 KB)

1
/*******************************************************************************
2
 *
3
 *   gvSIG. Desktop Geographic Information System.
4
 *  
5
 *   Copyright (C) 2007-2013 gvSIG Association.
6
 *  
7
 *   This program is free software; you can redistribute it and/or
8
 *   modify it under the terms of the GNU General Public License
9
 *   as published by the Free Software Foundation; either version 3
10
 *   of the License, or (at your option) any later version.
11
 *  
12
 *   This program is distributed in the hope that it will be useful,
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *   GNU General Public License for more details.
16
 *  
17
 *   You should have received a copy of the GNU General Public License
18
 *   along with this program; if not, write to the Free Software
19
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
 *   MA  02110-1301, USA.
21
 *  
22
 *   For any additional information, do not hesitate to contact us
23
 *   at info AT gvsig.com, or visit our website www.gvsig.com.
24
 *   
25
 *******************************************************************************/
26
package org.gvsig.sldsupport.sld.symbol.misc;
27

    
28
import java.awt.Color;
29
import java.text.DecimalFormat;
30
import java.text.DecimalFormatSymbols;
31
import java.util.ArrayList;
32
import java.util.List;
33

    
34
import org.gvsig.sldsupport.sld.filter.expression.SLDExpression;
35
import org.gvsig.sldsupport.sld.filter.expression.operator.SLDLiteral;
36
import org.gvsig.sldsupport.sld.graphic.SLDGraphic;
37

    
38

    
39
public class SLDStroke {
40
        
41
        public static final int STROKE_TYPE_SOLID = 0;
42
        public static final int STROKE_TYPE_GRAPHIC_FILL = 1;
43
        public static final int STROKE_TYPE_GRAPHIC_STROKE = 2;
44
        
45
        protected int strokeType = STROKE_TYPE_SOLID;
46
        
47
        public static DecimalFormat df = null;
48
        static {
49
                DecimalFormatSymbols dformater_rules = new DecimalFormatSymbols();
50
                dformater_rules.setDecimalSeparator ('.');
51
                df = new DecimalFormat("##########.0##########", dformater_rules);
52
        }
53
        
54
        protected SLDExpression width = null;
55
        protected SLDExpression color = null;
56
        
57
        /*
58
         * 1 is opaque
59
         * 0.5 is semi-transparent
60
         * 0 is transparent
61
         * 
62
         */
63
        protected SLDExpression opacity = null;
64
        protected SLDExpression lineJoin = null;
65
        protected SLDExpression lineCap = null;
66
        protected SLDExpression dashOffset = null;
67
        protected List<Float> dashArray = new ArrayList<Float>();
68
        
69
        /**
70
         * Used for STROKE_TYPE_GRAPHIC_STROKE
71
         */
72
        protected Double graphicStrokeInitialGap = null;
73
        
74
        /**
75
         * Used for STROKE_TYPE_GRAPHIC_STROKE
76
         */
77
        protected Double graphicStrokeGap = null;
78
        
79
        /**
80
         * Used when not STROKE_TYPE_SOLID
81
         * Means a graphicFill or graphicStroke depending on the type
82
         * of stroke
83
         */
84
        protected SLDGraphic graphic = null;
85
        // ============================================
86
        
87
        public SLDStroke() {
88
                /*
89
                 * Defaults
90
                 */
91
                setStrokeType(STROKE_TYPE_SOLID);
92
                this.setColor(Color.BLACK);
93
                this.setOpacity(1);
94
                this.setWidth(1);
95
                
96
                /*
97
                 * These are valid but not standard defaults
98
                 * (standard does not declare default value)
99
                 */
100
                this.setLineCap(new SLDLiteral("round"));
101
                this.setLineJoin(new SLDLiteral("round"));
102
                
103
        }
104

    
105
        public void setStrokeType(int t) {
106
                this.strokeType = t;
107
        }
108
        
109
        public int getStrokeType() {
110
                return strokeType;
111
        }
112
        
113
        public List<Float> getDashArray() {
114
                return dashArray;
115
        }
116
        // ==============================
117

    
118
        public SLDExpression getWidth() {
119
                return width;
120
        }
121

    
122
        public void setWidth(SLDExpression w) {
123
                this.width = w;
124
        }
125
        
126
        
127
        public void setWidth(double w) {
128
                if (w >= 0) {
129
                        setWidth(new SLDLiteral(df.format(w)));
130
                }
131
        }
132
        
133
        
134

    
135
        public SLDExpression getColor() {
136
                return color;
137
        }
138

    
139
        public void setColor(SLDExpression c) {
140
                this.color = c;
141
        }
142
        
143
        public void setColor(Color c) {
144
                
145
                if (c != null) {
146
                        String colstr = toHexadecimal(c, 6, "#");
147
                        setColor(new SLDLiteral(colstr));
148
                }
149
        }
150
        
151

    
152
        public SLDExpression getOpacity() {
153
                return opacity;
154
        }
155

    
156
        public void setOpacity(SLDExpression o) {
157
                this.opacity = o;
158
        }
159
        
160

    
161
        public void setOpacity(double opa) {
162
                if (opa >= 0 && opa <= 1) {
163
                        setOpacity(new SLDLiteral(df.format(opa)));
164
                }
165
        }
166
        
167

    
168
        public SLDExpression getLineJoin() {
169
                return lineJoin;
170
        }
171

    
172
        public void setLineJoin(SLDExpression j) {
173
                this.lineJoin = j;
174
        }
175

    
176
        public SLDExpression getLineCap() {
177
                return lineCap;
178
        }
179

    
180
        public void setLineCap(SLDExpression c) {
181
                this.lineCap = c;
182
        }
183

    
184
        public SLDExpression getDashOffset() {
185
                return dashOffset;
186
        }
187

    
188
        public void setDashOffset(SLDExpression doff) {
189
                this.dashOffset = doff;
190
        }
191

    
192
        public Double getGraphicStrokeInitialGap() {
193
                return graphicStrokeInitialGap;
194
        }
195

    
196
        public void setGraphicStrokeInitialGap(Double g) {
197
                this.graphicStrokeInitialGap = g;
198
        }
199

    
200
        public Double getGraphicStrokeGap() {
201
                return graphicStrokeGap;
202
        }
203

    
204
        public void setGraphicStrokeGap(Double g) {
205
                this.graphicStrokeGap = g;
206
        }
207

    
208
        public SLDGraphic getGraphic() {
209
                if (strokeType == STROKE_TYPE_SOLID) {
210
                        /*
211
                         * graphic used when not STROKE_TYPE_SOLID
212
                         */
213
                        return null;
214
                } else {
215
                        return graphic;
216
                }
217
        }
218

    
219
        public void setGraphic(SLDGraphic g) {
220
                this.graphic = g;
221
        }
222
        
223
        
224
        /**
225
         * Examples:
226
         * #ff0000
227
         * 0x000000cd9983
228
         * 
229
         * Returns null is color is null
230
         * 
231
         * @param co
232
         * @param length
233
         * @param prefix
234
         * @return
235
         */
236
        public static String toHexadecimal(Color co, int length, String prefix) {
237
                
238
                if (co == null) {
239
                        return null;
240
                }
241
                
242
                int v = (co.getRed() << 16) + (co.getGreen() << 8) + co.getBlue();
243
                String resp = Integer.toHexString(v);
244
                int len = resp.length();
245
                for (int i=0; i<(length-len); i++) {
246
                        resp = "0" + resp;
247
                }
248
                resp = resp.substring(resp.length() - length);
249
                if (prefix != null) {
250
                        resp = prefix + resp;
251
                }
252
                return resp;
253
        }
254
        
255

    
256

    
257
}