001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/graphics/sld/Fill.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 java.awt.Color;
039    import java.util.HashMap;
040    import java.util.Map;
041    
042    import org.deegree.framework.util.ColorUtils;
043    import org.deegree.framework.xml.Marshallable;
044    import org.deegree.model.feature.Feature;
045    import org.deegree.model.filterencoding.FilterEvaluationException;
046    
047    /**
048     * A Fill allows area geometries to be filled. There are two types of fills: solid-color and repeated GraphicFill. In
049     * general, if a Fill element is omitted in its containing element, no fill will be rendered. The default is a solid
050     * 50%-gray (color "#808080") opaque fill.
051     * <p>
052     * 
053     * @author <a href="mailto:k.lupp@web.de">Katharina Lupp </a>
054     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
055     * @version $Revision: 27181 $ $Date: 2010-10-05 15:24:30 +0200 (Di, 05 Okt 2010) $
056     */
057    public class Fill extends Drawing implements Marshallable {
058    
059        /**
060         * default fill color is #808080
061         */
062        public static final Color FILL_DEFAULT = Color.decode( "#808080" );
063    
064        /**
065         * Opacity is 1
066         */
067        public static final double OPACITY_DEFAULT = 1.0;
068    
069        /**
070         * Constructs a new <tt>Fill</tt>.
071         */
072        protected Fill() {
073            super( new HashMap<String, Object>(), null );
074        }
075    
076        /**
077         * Constructs a new <tt>Fill</tt>.
078         * 
079         * @param cssParams
080         * @param graphicFill
081         */
082        public Fill( Map<String, Object> cssParams, GraphicFill graphicFill ) {
083            super( cssParams, graphicFill );
084        }
085    
086        /**
087         * Returns the (evaluated) value of the fill's CssParameter 'fill'.
088         * <p>
089         * 
090         * @param feature
091         *            specifies the <tt>Feature</tt> to be used for evaluation of the underlying 'sld:ParameterValueType'
092         * @return the (evaluated) value of the parameter
093         * @throws FilterEvaluationException
094         *             if the evaluation fails or the value is invalid
095         */
096        public Color getFill( Feature feature )
097                                throws FilterEvaluationException {
098            Color awtColor = FILL_DEFAULT;
099    
100            CssParameter cssParam = (CssParameter) cssParams.get( "fill" );
101    
102            if ( cssParam != null ) {
103                String s = cssParam.getValue( feature );
104    
105                try {
106                    awtColor = Color.decode( s );
107                } catch ( NumberFormatException e ) {
108                    throw new FilterEvaluationException( "Given value ('" + s + "') for CSS-Parameter 'fill' "
109                                                         + "does not denote a valid color!" );
110                }
111            }
112    
113            return awtColor;
114        }
115    
116        /**
117         * sets the value of the fill's CssParameter 'fill' as a simple color
118         * 
119         * @param color
120         *            color to be set
121         */
122        public void setFill( Color color ) {
123    
124            String hex = ColorUtils.toHexCode( "#", color );
125            CssParameter fill = StyleFactory.createCssParameter( "fill", hex );
126    
127            cssParams.put( "fill", fill );
128        }
129    
130        /**
131         * Returns the (evaluated) value of the fill's CssParameter 'fill-opacity'.
132         * <p>
133         * 
134         * @param feature
135         *            specifies the <tt>Feature</tt> to be used for evaluation of the underlying 'sld:ParameterValueType'
136         * @return the (evaluated) value of the parameter
137         * @throws FilterEvaluationException
138         *             if the evaluation fails or the value is invalid
139         */
140        public double getOpacity( Feature feature )
141                                throws FilterEvaluationException {
142            double opacity = OPACITY_DEFAULT;
143    
144            CssParameter cssParam = (CssParameter) cssParams.get( "fill-opacity" );
145    
146            if ( cssParam != null ) {
147                String value = cssParam.getValue( feature );
148    
149                try {
150                    opacity = Double.parseDouble( value );
151                } catch ( NumberFormatException e ) {
152                    throw new FilterEvaluationException( "Given value for parameter 'fill-opacity' ('" + value
153                                                         + "') has invalid format!" );
154                }
155    
156                if ( ( opacity < 0.0 ) || ( opacity > 1.0 ) ) {
157                    throw new FilterEvaluationException( "Value for parameter 'fill-opacity' (given: '" + value
158                                                         + "') must be between 0.0 and 1.0!" );
159                }
160            }
161    
162            return opacity;
163        }
164    
165        /**
166         * sets the value of the opacity's CssParameter 'opacity' as a value. Valid values ranges from 0 .. 1. If a value <
167         * 0 is passed it will be set to 0. If a value > 1 is passed it will be set to 1.
168         * 
169         * @param opacity
170         *            opacity to be set
171         */
172        public void setOpacity( double opacity ) {
173    
174            if ( opacity > 1 ) {
175                opacity = 1;
176            } else if ( opacity < 0 ) {
177                opacity = 0;
178            }
179    
180            CssParameter fillOp = StyleFactory.createCssParameter( "fill-opacity", "" + opacity );
181            cssParams.put( "fill-opacity", fillOp );
182        }
183    
184        public String getSymbol( Feature feature )
185                                throws FilterEvaluationException {
186    
187            CssParameter cssParam = (CssParameter) cssParams.get( "symbol" );
188            if ( cssParam != null ) {
189                return cssParam.getValue( feature );
190            }
191            return null;
192        }
193    
194        /**
195         * exports the content of the CssParameter as XML formated String
196         * 
197         * @return xml representation of the CssParameter
198         */
199        public String exportAsXML() {
200    
201            StringBuffer sb = new StringBuffer( "<Fill>" );
202    
203            if ( graphicFill != null ) {
204                sb.append( ( (Marshallable) graphicFill ).exportAsXML() );
205            }
206            for ( Object o : cssParams.values() ) {
207                sb.append( ( (Marshallable) o ).exportAsXML() );
208            }
209    
210            sb.append( "</Fill>" );
211    
212            return sb.toString();
213        }
214    
215    }