001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/graphics/sld/Halo.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.graphics.sld;
037    
038    import org.deegree.framework.xml.Marshallable;
039    import org.deegree.model.feature.Feature;
040    import org.deegree.model.filterencoding.FilterEvaluationException;
041    
042    /**
043     * Incarnation of a sld:Halo-element. A Halo is a type of Fill that is applied to the backgrounds of
044     * font glyphs. The use of halos greatly improves the readability of text labels.
045     * <p>
046     *
047     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
048     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
049     * @version $Revision: 26665 $ $Date: 2010-09-09 20:44:38 +0200 (Do, 09 Sep 2010) $
050     */
051    
052    public class Halo implements Marshallable {
053    
054        private Fill fill = null;
055    
056        private ParameterValueType radius = null;
057    
058        private Stroke stroke = null;
059    
060        /**
061         * Create a new <tt>Halo</tt> -instance.
062         * <p>
063         *
064         * @param radius
065         *            radius to be used for the halo, use null for a rectangle styled halo
066         * @param fill
067         *            defines the fill style, use null for default style
068         * @param stroke
069         *            defines the stroke style, use null for default style
070         */
071        public Halo( ParameterValueType radius, Fill fill, Stroke stroke ) {
072            setRadius( radius );
073            setFill( fill );
074            setStroke( stroke );
075        }
076    
077        /**
078         * A Fill allows area geometries to be filled. There are two types of fills: solid-color and
079         * repeated GraphicFill. In general, if a Fill element is omitted in its containing element, no
080         * fill will be rendered. The default is a solid 50%-gray (color "#808080") opaque fill.
081         * <p>
082         *
083         * @return the underlying <tt>Fill</tt> -object or null
084         *
085         */
086        public Fill getFill() {
087            return fill;
088        }
089    
090        /**
091         * Sets the underlying <tt>Fill</tt> -instance.
092         * <p>
093         *
094         * @param fill
095         *            defines the fill color and pattern
096         *
097         */
098        public void setFill( Fill fill ) {
099            this.fill = fill;
100        }
101    
102        /**
103         * The Radius element gives the absolute size of a halo radius in pixels encoded as a
104         * floating-point number. The radius is taken from the outside edge of a font glyph to extend
105         * the area of coverage of the glyph (and the inside edge of holes in the glyphs). The halo of a
106         * text label is considered to be a single shape. The default radius is one pixel. Negative
107         * values are not allowed.
108         * <p>
109         *
110         * @return the radius definition as <tt>ParameterValueType</tt>, or null if it has not been
111         *         specified
112         *
113         */
114        public ParameterValueType getRadius() {
115            return radius;
116        }
117    
118        /**
119         * Sets the value for the radius of the halo.
120         * <p>
121         *
122         * @param radius
123         *            radius to be used for the halo, use null for a rectangle styled halo
124         *
125         */
126        public void setRadius( ParameterValueType radius ) {
127            this.radius = radius;
128        }
129    
130        /**
131         * The Radius element gives the absolute size of a halo radius in pixels encoded as a
132         * floating-point number. The radius is taken from the outside edge of a font glyph to extend
133         * the area of coverage of the glyph (and the inside edge of holes in the glyphs). The halo of a
134         * text label is considered to be a single shape. The default radius is one pixel. Negative
135         * values are not allowed.
136         * <p>
137         *
138         * @param feature
139         *            specifies the <tt>Feature</tt> to be used for evaluation of the underlying
140         *            'sld:ParameterValueType'
141         * @return the radius value, or -1 if it has not been specified
142         * @throws FilterEvaluationException
143         *             if the evaluation fails
144         */
145        public double getRadius( Feature feature )
146                                throws FilterEvaluationException {
147            if ( radius == null ) {
148                return -1.0;
149            }
150    
151            String stringValue = null;
152            double radiusVal;
153    
154            try {
155                stringValue = radius.evaluate( feature );
156                radiusVal = Double.parseDouble( stringValue );
157            } catch ( NumberFormatException e ) {
158                throw new FilterEvaluationException( "Given value ('" + stringValue
159                                                     + "') for radius of Halo does not denote a number." );
160            }
161    
162            return radiusVal;
163        }
164    
165        /**
166         * @see org.deegree.graphics.sld.Halo#getRadius(Feature)
167         *      <p>
168         * @param radius
169         *            radius to be set for the halo
170         */
171        public void setRadius( double radius ) {
172            ParameterValueType pvt = null;
173            if ( radius > 0 ) {
174                pvt = StyleFactory.createParameterValueType( "" + radius );
175                this.radius = pvt;
176            }
177        }
178    
179        /**
180         * Returns the underlying <tt>Stroke</tt> -instance.
181         * <p>
182         *
183         * @return the underlying <tt>Stroke</tt> -object or null
184         *
185         */
186        public Stroke getStroke() {
187            return stroke;
188        }
189    
190        /**
191         * Sets the underlying <tt>Stroke</tt> -instance.
192         * <p>
193         *
194         * @param stroke
195         *            defines the stroke color and pattern
196         */
197        public void setStroke( Stroke stroke ) {
198            this.stroke = stroke;
199        }
200    
201        /**
202         * exports the content of the Halo as XML formated String
203         *
204         * @return xml representation of the Halo
205         */
206        public String exportAsXML() {
207    
208            StringBuffer sb = new StringBuffer( 1000 );
209            sb.append( "<Halo>" );
210            if ( radius != null ) {
211                sb.append( "<Radius>" );
212                sb.append( ( (Marshallable) radius ).exportAsXML() );
213                sb.append( "</Radius>" );
214            }
215            if ( fill != null ) {
216                sb.append( ( (Marshallable) fill ).exportAsXML() );
217            }
218            if ( stroke != null ) {
219                sb.append( ( (Marshallable) stroke ).exportAsXML() );
220            }
221            sb.append( "</Halo>" );
222    
223            return sb.toString();
224        }
225    
226    }