Statistics
| Revision:

svn-gvsig-desktop / branches / simbologia / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / styles / SimpleLineStyle.java @ 10274

History | View | Annotate | Download (5.17 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 10274 2007-02-12 15:15:20Z jaume $
45
* $Log$
46
* Revision 1.1.2.2  2007-02-12 15:15:20  jaume
47
* refactored interval legend and added graduated symbol legend
48
*
49
* Revision 1.1.2.1  2007/02/09 07:47:04  jaume
50
* Isymbol moved
51
*
52
*
53
*/
54
package com.iver.cit.gvsig.fmap.core.styles;
55

    
56
import java.awt.BasicStroke;
57
import java.awt.Color;
58
import java.awt.Graphics2D;
59
import java.awt.Rectangle;
60
import java.awt.Stroke;
61

    
62
import com.iver.cit.gvsig.fmap.core.ISymbol;
63
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
64
import com.iver.utiles.XMLEntity;
65

    
66
public class SimpleLineStyle extends AbstractStyle implements ILineStyle {
67
        private final static Color PREVIEW_COLOR_1= new Color(150, 255, 200); //light blue
68
        private final static Color PREVIEW_COLOR_2 = new Color(255, 200, 100); //orange
69
        private final static int COLOR1_STROKE = 1;
70
        private final static int COLOR2_STROKE = 3;
71
        private float[] dashArray;
72
        private float dashPhase;
73
        private int endCap;
74
        private int lineJoin;
75
        private float miterlimit;
76
        private float lineWidth;
77

    
78

    
79

    
80
        public SimpleLineStyle() {
81
                BasicStroke dummy = new BasicStroke();
82
                dashArray = dummy.getDashArray();
83
                dashPhase = dummy.getDashPhase();
84
                endCap = dummy.getEndCap();
85
                lineJoin = dummy.getLineJoin();
86
                miterlimit = dummy.getMiterLimit();
87
        }
88

    
89

    
90
        public SimpleLineStyle(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) {
91
                this.lineWidth = width;
92
                this.endCap = cap;
93
                this.lineJoin = join;
94
                this.miterlimit = miterlimit;
95
                this.dashArray = dash;
96
                this.dashPhase = dash_phase;
97
        }
98

    
99
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
100
                Stroke oldStroke = g.getStroke();
101
                int h = (int) (r.getHeight() / 2);
102
                int margins = 2;
103

    
104
                BasicStroke stroke;
105
                stroke = new BasicStroke(COLOR1_STROKE, endCap, lineJoin, miterlimit, dashArray, dashPhase);
106
                g.setStroke(stroke);
107
                g.setColor(PREVIEW_COLOR_1);
108
                g.drawLine(margins, h, r.width-margins-margins, h);
109

    
110
                stroke = new BasicStroke(COLOR2_STROKE, endCap, lineJoin, miterlimit, dashArray, dashPhase);                g.setStroke(stroke);
111
                g.setColor(PREVIEW_COLOR_2);
112
                g.drawLine(margins, h, r.width-margins-margins, h);
113
                g.setStroke(oldStroke);
114
        }
115

    
116
        public String getClassName() {
117
                return getClass().getName();
118
        }
119

    
120
        public XMLEntity getXMLEntity() {
121
                XMLEntity xml = new XMLEntity();
122
                xml.putProperty("className", getClassName());
123
                xml.putProperty("desc", getDescription());
124

    
125
                if (dashArray != null) {
126
                        StringBuffer sb = new StringBuffer();
127
                        for (int i = 0; i < dashArray.length; i++) {
128
                                sb.append(dashArray[i]);
129
                                if (i< dashArray.length)
130
                                        sb.append(",");
131
                        }
132
                        xml.putProperty("dashArray", sb.toString());
133

    
134
                }
135
                xml.putProperty("lineWidth", lineWidth);
136
                xml.putProperty("dashPhase", dashPhase);
137
                xml.putProperty("endCap", endCap);
138
                xml.putProperty("lineJoin", lineJoin);
139
                xml.putProperty("miterLimit", miterlimit);
140
                return xml;
141
        }
142

    
143
        public void setXMLEntity(XMLEntity xml) {
144

    
145
                setDescription(xml.getStringProperty("desc"));
146
                if (xml.contains("dashArray")) {
147
                        String[] dashProp = xml.getStringArrayProperty("dashArray");
148
                        dashArray = new float[dashProp.length];
149
                        for (int i = 0; i < dashProp.length; i++) {
150
                                dashArray[i] = Float.parseFloat(dashProp[i]);
151
                        }
152
                }
153
                lineWidth = xml.getFloatProperty("lineWidth");
154
                dashPhase = xml.getFloatProperty("dashPhase");
155
                endCap = xml.getIntProperty("endCap");
156
                lineJoin = xml.getIntProperty("lineJoin");
157
                miterlimit = xml.getFloatProperty("miterLimit");
158
        }
159

    
160
        public Stroke getStroke() {
161
                return new BasicStroke(lineWidth, endCap, lineJoin, miterlimit, dashArray, dashPhase);
162
        }
163

    
164
        public float getLineWidth() {
165
                return lineWidth;
166
        }
167

    
168
        public void setLineWidth(float width) {
169
                this.lineWidth = width;
170
        }
171

    
172
        public boolean isSuitableFor(ISymbol symbol) {
173
                return symbol instanceof ILineSymbol;
174
        }
175

    
176
        public void setStroke(Stroke stroke) {
177
                if (stroke != null) {
178
                        BasicStroke dummy = (BasicStroke) stroke;
179
                        dashArray = dummy.getDashArray();
180
                        dashPhase = dummy.getDashPhase();
181
                        endCap = dummy.getEndCap();
182
                        lineJoin = dummy.getLineJoin();
183
                        miterlimit = dummy.getMiterLimit();
184
                }
185
        }
186

    
187

    
188
}