Statistics
| Revision:

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

History | View | Annotate | Download (6.92 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 11704 2007-05-17 09:43:35Z jaume $
45
* $Log$
46
* Revision 1.6  2007-05-17 09:32:06  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.5  2007/05/09 16:07:26  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.4  2007/05/08 08:47:39  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.3  2007/04/04 15:41:05  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.2  2007/03/09 11:20:56  jaume
59
* Advanced symbology (start committing)
60
*
61
* Revision 1.1.2.4  2007/02/21 07:34:09  jaume
62
* labeling starts working
63
*
64
* Revision 1.1.2.3  2007/02/15 16:23:44  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1.2.2  2007/02/12 15:15:20  jaume
68
* refactored interval legend and added graduated symbol legend
69
*
70
* Revision 1.1.2.1  2007/02/09 07:47:04  jaume
71
* Isymbol moved
72
*
73
*
74
*/
75
package com.iver.cit.gvsig.fmap.core.styles;
76

    
77
import java.awt.BasicStroke;
78
import java.awt.Color;
79
import java.awt.Graphics2D;
80
import java.awt.Rectangle;
81
import java.awt.Stroke;
82
import java.awt.geom.AffineTransform;
83

    
84
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
85
import com.iver.cit.gvsig.fmap.core.symbols.ArrowMarkerSymbol;
86
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
87
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
88
import com.iver.utiles.XMLEntity;
89

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

    
108
        public SimpleLineStyle() {
109
                BasicStroke dummy = new BasicStroke();
110
                dashArray = dummy.getDashArray();
111
                dashPhase = dummy.getDashPhase();
112
                endCap = BasicStroke.CAP_BUTT;
113
                lineJoin = dummy.getLineJoin();
114
                miterlimit = dummy.getMiterLimit();
115
        }
116

    
117
        public SimpleLineStyle(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) {
118
                this.lineWidth = width;
119
                this.endCap = cap;
120
                this.lineJoin = join;
121
                this.miterlimit = miterlimit;
122
                this.dashArray = dash;
123
                this.dashPhase = dash_phase;
124
        }
125

    
126
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
127
                Stroke oldStroke = g.getStroke();
128
                int h = (int) (r.getHeight() / 2);
129
                int margins = 2;
130

    
131
                BasicStroke stroke;
132
                stroke = new BasicStroke(COLOR1_STROKE, endCap, lineJoin, miterlimit, dashArray, dashPhase);
133
                g.setStroke(stroke);
134
                g.setColor(PREVIEW_COLOR_1);
135
                g.drawLine(margins, h, r.width-margins-margins, h);
136

    
137
                stroke = new BasicStroke(COLOR2_STROKE, endCap, lineJoin, miterlimit, dashArray, dashPhase);                g.setStroke(stroke);
138
                g.setColor(PREVIEW_COLOR_2);
139
                g.drawLine(margins, h, r.width-margins-margins, h);
140
                g.setStroke(oldStroke);
141
        }
142

    
143
        public String getClassName() {
144
                return getClass().getName();
145
        }
146

    
147
        public XMLEntity getXMLEntity() {
148
                XMLEntity xml = new XMLEntity();
149
                xml.putProperty("className", getClassName());
150
                xml.putProperty("desc", getDescription());
151

    
152
                if (dashArray != null) {
153
                        StringBuffer sb = new StringBuffer();
154
                        for (int i = 0; i < dashArray.length; i++) {
155
                                sb.append(dashArray[i]);
156
                                if (i< dashArray.length)
157
                                        sb.append(",");
158
                        }
159
                        xml.putProperty("dashArray", sb.toString());
160

    
161
                }
162
                xml.putProperty("lineWidth", lineWidth);
163
                xml.putProperty("dashPhase", dashPhase);
164
                xml.putProperty("endCap", endCap);
165
                xml.putProperty("lineJoin", lineJoin);
166
                xml.putProperty("miterLimit", miterlimit);
167
                xml.putProperty("offset", offset);
168
                xml.putProperty("unit", measureUnit);
169
                return xml;
170
        }
171

    
172
        public void setXMLEntity(XMLEntity xml) {
173

    
174
                setDescription(xml.getStringProperty("desc"));
175
                if (xml.contains("dashArray")) {
176
                        String[] dashProp = xml.getStringProperty("dashArray").split(",");
177
                        dashArray = new float[dashProp.length];
178
                        for (int i = 0; i < dashProp.length; i++) {
179
                                dashArray[i] = Float.parseFloat(dashProp[i]);
180
                        }
181
                }
182
                lineWidth = xml.getFloatProperty("lineWidth");
183
                dashPhase = xml.getFloatProperty("dashPhase");
184
                endCap = xml.getIntProperty("endCap");
185
                lineJoin = xml.getIntProperty("lineJoin");
186
                miterlimit = xml.getFloatProperty("miterLimit");
187
                offset = xml.getFloatProperty("offset");
188
                measureUnit = xml.getIntProperty("unit");
189
        }
190

    
191
        public Stroke getStroke() {
192
                return new BasicStroke(lineWidth, endCap, lineJoin, miterlimit, dashArray, dashPhase);
193
        }
194

    
195
        /**
196
         * @return
197
         * @uml.property  name="lineWidth"
198
         */
199
        public float getLineWidth() {
200
                return lineWidth;
201
        }
202

    
203
        /**
204
         * @param width
205
         * @uml.property  name="lineWidth"
206
         */
207
        public void setLineWidth(float width) {
208
                this.lineWidth = width;
209
        }
210

    
211
        public boolean isSuitableFor(ISymbol symbol) {
212
                return symbol instanceof ILineSymbol;
213
        }
214

    
215
        public void setStroke(Stroke stroke) {
216
                if (stroke != null) {
217
                        BasicStroke dummy = (BasicStroke) stroke;
218
                        dashArray = dummy.getDashArray();
219
                        dashPhase = dummy.getDashPhase();
220
                        endCap = dummy.getEndCap();
221
                        lineJoin = dummy.getLineJoin();
222
                        miterlimit = dummy.getMiterLimit();
223
                }
224
        }
225

    
226
        public void drawOutline(Graphics2D g, Rectangle r) {
227
                drawInsideRectangle(g, r);
228
        }
229

    
230
        public double getOffset() {
231
                return offset;
232
        }
233

    
234
        public void setOffset(double offset) {
235
                this.offset = offset;
236
        }
237

    
238
        public ArrowMarkerSymbol getArrowDecorator() {
239
//                 TODO Implement it
240
                return null;
241

    
242
        }
243

    
244
        public void setArrowDecorator(ArrowMarkerSymbol arrowSym) {
245
                // TODO Implement it
246
                throw new Error("Not yet implemented!");
247

    
248
        }
249

    
250
        public void setUnit(int unitIndex) {
251
                this.measureUnit = unitIndex;
252
        }
253

    
254
        public int getUnit() {
255
                return measureUnit;
256
        }
257

    
258
}