Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / styles / SimpleLineStyle.java @ 12467

History | View | Annotate | Download (7.61 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
*
44
* $Id: SimpleLineStyle.java 12467 2007-07-03 10:58:29Z jaume $
45
* $Log$
46
* Revision 1.8  2007-07-03 10:58:29  jaume
47
* first refactor on CartographicSupport
48
*
49
* Revision 1.7  2007/05/22 10:05:30  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.6  2007/05/17 09:32:06  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.5  2007/05/09 16:07:26  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.4  2007/05/08 08:47:39  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.3  2007/04/04 15:41:05  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.2  2007/03/09 11:20:56  jaume
65
* Advanced symbology (start committing)
66
*
67
* Revision 1.1.2.4  2007/02/21 07:34:09  jaume
68
* labeling starts working
69
*
70
* Revision 1.1.2.3  2007/02/15 16:23:44  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.1.2.2  2007/02/12 15:15:20  jaume
74
* refactored interval legend and added graduated symbol legend
75
*
76
* Revision 1.1.2.1  2007/02/09 07:47:04  jaume
77
* Isymbol moved
78
*
79
*
80
*/
81
package com.iver.cit.gvsig.fmap.core.styles;
82

    
83
import java.awt.BasicStroke;
84
import java.awt.Color;
85
import java.awt.Graphics2D;
86
import java.awt.Rectangle;
87
import java.awt.Stroke;
88

    
89
import com.iver.cit.gvsig.fmap.MapContext;
90
import com.iver.cit.gvsig.fmap.ViewPort;
91
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
92
import com.iver.cit.gvsig.fmap.core.FShape;
93
import com.iver.cit.gvsig.fmap.core.CartographicSupport.CartographicSupportToolkit;
94
import com.iver.cit.gvsig.fmap.core.symbols.ArrowMarkerSymbol;
95
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
96
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
97
import com.iver.utiles.XMLEntity;
98

    
99
/**
100
 * @see http://www.oreilly.com/catalog/java2d/chapter/ch04.html
101
 * @author  jaume dominguez faus - jaume.dominguez@iver.es
102
 */
103
public class SimpleLineStyle extends AbstractStyle implements ILineStyle, CartographicSupport {
104
        private final static Color PREVIEW_COLOR_1= new Color(150, 255, 200); //light blue
105
        private final static Color PREVIEW_COLOR_2 = new Color(255, 200, 100); //orange
106
        private final static int COLOR1_STROKE = 1;
107
        private final static int COLOR2_STROKE = 3;
108
        private float[] dashArray;
109
        private float dashPhase;
110
        private int endCap = BasicStroke.CAP_BUTT;
111
        private int lineJoin;
112
        private float miterlimit;
113
        private float lineWidth;
114
        private int measureUnit;// for the offset distance
115
        private double offset;
116
        private int referenceSystem;
117

    
118
        public SimpleLineStyle() {
119
                BasicStroke dummy = new BasicStroke();
120
                dashArray = dummy.getDashArray();
121
                dashPhase = dummy.getDashPhase();
122
                endCap = BasicStroke.CAP_BUTT;
123
                lineJoin = dummy.getLineJoin();
124
                miterlimit = dummy.getMiterLimit();
125
        }
126

    
127
        public SimpleLineStyle(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) {
128
                this.lineWidth = width;
129
                this.endCap = cap;
130
                this.lineJoin = join;
131
                this.miterlimit = miterlimit;
132
                this.dashArray = dash;
133
                this.dashPhase = dash_phase;
134
        }
135

    
136
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
137
                Stroke oldStroke = g.getStroke();
138
                int h = (int) (r.getHeight() / 2);
139
                int margins = 2;
140

    
141
                BasicStroke stroke;
142
                stroke = new BasicStroke(COLOR1_STROKE, endCap, lineJoin, miterlimit, dashArray, dashPhase);
143
                g.setStroke(stroke);
144
                g.setColor(PREVIEW_COLOR_1);
145
                g.drawLine(margins, h, r.width-margins-margins, h);
146

    
147
                stroke = new BasicStroke(COLOR2_STROKE, endCap, lineJoin, miterlimit, dashArray, dashPhase);                g.setStroke(stroke);
148
                g.setColor(PREVIEW_COLOR_2);
149
                g.drawLine(margins, h, r.width-margins-margins, h);
150
                g.setStroke(oldStroke);
151
        }
152

    
153
        public String getClassName() {
154
                return getClass().getName();
155
        }
156

    
157
        public XMLEntity getXMLEntity() {
158
                XMLEntity xml = new XMLEntity();
159
                xml.putProperty("className", getClassName());
160
                xml.putProperty("desc", getDescription());
161

    
162
                if (dashArray != null) {
163
                        StringBuffer sb = new StringBuffer();
164
                        for (int i = 0; i < dashArray.length; i++) {
165
                                sb.append(dashArray[i]);
166
                                if (i< dashArray.length)
167
                                        sb.append(",");
168
                        }
169
                        xml.putProperty("dashArray", sb.toString());
170

    
171
                }
172
                xml.putProperty("lineWidth", lineWidth);
173
                xml.putProperty("dashPhase", dashPhase);
174
                xml.putProperty("endCap", endCap);
175
                xml.putProperty("lineJoin", lineJoin);
176
                xml.putProperty("miterLimit", miterlimit);
177
                xml.putProperty("offset", offset);
178
                xml.putProperty("unit", measureUnit);
179
                return xml;
180
        }
181

    
182
        public void setXMLEntity(XMLEntity xml) {
183

    
184
                setDescription(xml.getStringProperty("desc"));
185
                if (xml.contains("dashArray")) {
186
                        String[] dashProp = xml.getStringProperty("dashArray").split(",");
187
                        dashArray = new float[dashProp.length];
188
                        for (int i = 0; i < dashProp.length; i++) {
189
                                dashArray[i] = Float.parseFloat(dashProp[i]);
190
                        }
191
                }
192
                lineWidth = xml.getFloatProperty("lineWidth");
193
                dashPhase = xml.getFloatProperty("dashPhase");
194
                endCap = xml.getIntProperty("endCap");
195
                lineJoin = xml.getIntProperty("lineJoin");
196
                miterlimit = xml.getFloatProperty("miterLimit");
197
                offset = xml.getFloatProperty("offset");
198
                measureUnit = xml.getIntProperty("unit");
199
        }
200

    
201
        public Stroke getStroke() {
202
                return new BasicStroke((float) lineWidth, endCap, lineJoin, miterlimit, dashArray, dashPhase);
203
        }
204

    
205
        /**
206
         * @return
207
         * @uml.property  name="lineWidth"
208
         */
209
        public float getLineWidth() {
210
                return lineWidth;
211
        }
212

    
213
        /**
214
         * @param width
215
         * @uml.property  name="lineWidth"
216
         */
217
        public void setLineWidth(float width) {
218
                this.lineWidth = width;
219
        }
220

    
221
        public boolean isSuitableFor(ISymbol symbol) {
222
                return symbol instanceof ILineSymbol;
223
        }
224

    
225
        public void setStroke(Stroke stroke) {
226
                if (stroke != null) {
227
                        BasicStroke dummy = (BasicStroke) stroke;
228
                        dashArray = dummy.getDashArray();
229
                        dashPhase = dummy.getDashPhase();
230
                        endCap = dummy.getEndCap();
231
                        lineJoin = dummy.getLineJoin();
232
                        miterlimit = dummy.getMiterLimit();
233
                }
234
        }
235

    
236
        public void drawOutline(Graphics2D g, Rectangle r) {
237
                drawInsideRectangle(g, r);
238
        }
239

    
240
        public double getOffset() {
241
                return offset;
242
        }
243

    
244
        public void setOffset(double offset) {
245
                this.offset = offset;
246
        }
247

    
248
        public ArrowMarkerSymbol getArrowDecorator() {
249
//                 TODO Implement it
250
                return null;
251

    
252
        }
253

    
254
        public void setArrowDecorator(ArrowMarkerSymbol arrowSym) {
255
                // TODO Implement it
256
                throw new Error("Not yet implemented!");
257

    
258
        }
259

    
260
        public void setUnit(int unitIndex) {
261
                this.measureUnit = unitIndex;
262
        }
263

    
264
        public int getUnit() {
265
                return measureUnit;
266
        }
267

    
268
        public int getReferenceSystem() {
269
                return referenceSystem;
270
        }
271

    
272
        public void setReferenceSystem(int referenceSystem) {
273
                this.referenceSystem = referenceSystem;
274
        }
275

    
276
        public int getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
277
                return CartographicSupportToolkit.getCartographicLength(this, getOffset(), viewPort, dpi);
278
        }
279

    
280

    
281
}