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