001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_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
041 import org.deegree.framework.util.ColorUtils;
042 import org.deegree.framework.xml.Marshallable;
043 import org.deegree.model.feature.Feature;
044 import org.deegree.model.filterencoding.FilterEvaluationException;
045
046 /**
047 * A Fill allows area geometries to be filled. There are two types of fills: solid-color and
048 * repeated GraphicFill. In general, if a Fill element is omitted in its containing element, no fill
049 * will be rendered. The default is a solid 50%-gray (color "#808080") opaque fill.
050 * <p>
051 *
052 * @author <a href="mailto:k.lupp@web.de">Katharina Lupp </a>
053 * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
054 * @version $Revision: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
055 */
056 public class Fill extends Drawing implements Marshallable {
057
058 /**
059 * default fill color is #808080
060 */
061 public static final Color FILL_DEFAULT = Color.decode( "#808080" );
062
063 /**
064 * Opacity is 1
065 */
066 public static final double OPACITY_DEFAULT = 1.0;
067
068 /**
069 * Constructs a new <tt>Fill</tt>.
070 */
071 protected Fill() {
072 super( new HashMap<String, Object>(), null );
073 }
074
075 /**
076 * Constructs a new <tt>Fill</tt>.
077 * @param cssParams
078 * @param graphicFill
079 */
080 protected Fill( HashMap<String, Object> cssParams, GraphicFill graphicFill ) {
081 super( cssParams, graphicFill );
082 }
083
084 /**
085 * Returns the (evaluated) value of the fill's CssParameter 'fill'.
086 * <p>
087 *
088 * @param feature
089 * specifies the <tt>Feature</tt> to be used for evaluation of the underlying
090 * 'sld:ParameterValueType'
091 * @return the (evaluated) value of the parameter
092 * @throws FilterEvaluationException
093 * if the evaluation fails or the value is invalid
094 */
095 public Color getFill( Feature feature )
096 throws FilterEvaluationException {
097 Color awtColor = FILL_DEFAULT;
098
099 CssParameter cssParam = (CssParameter) cssParams.get( "fill" );
100
101 if ( cssParam != null ) {
102 String s = cssParam.getValue( feature );
103
104 try {
105 awtColor = Color.decode( s );
106 } catch ( NumberFormatException e ) {
107 throw new FilterEvaluationException( "Given value ('" + s + "') for CSS-Parameter 'fill' "
108 + "does not denote a valid color!" );
109 }
110 }
111
112 return awtColor;
113 }
114
115 /**
116 * sets the value of the fill's CssParameter 'fill' as a simple color
117 *
118 * @param color
119 * color to be set
120 */
121 public void setFill( Color color ) {
122
123 String hex = ColorUtils.toHexCode( "#", color );
124 CssParameter fill = StyleFactory.createCssParameter( "fill", hex );
125
126 cssParams.put( "fill", fill );
127 }
128
129 /**
130 * Returns the (evaluated) value of the fill's CssParameter 'fill-opacity'.
131 * <p>
132 *
133 * @param feature
134 * specifies the <tt>Feature</tt> to be used for evaluation of the underlying
135 * '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 ..
167 * 1. If a value < 0 is passed it will be set to 0. If a value > 1 is passed it will be set to
168 * 1.
169 *
170 * @param opacity
171 * opacity to be set
172 */
173 public void setOpacity( double opacity ) {
174
175 if ( opacity > 1 ) {
176 opacity = 1;
177 } else if ( opacity < 0 ) {
178 opacity = 0;
179 }
180
181 CssParameter fillOp = StyleFactory.createCssParameter( "fill-opacity", "" + opacity );
182 cssParams.put( "fill-opacity", fillOp );
183 }
184
185 /**
186 * exports the content of the CssParameter as XML formated String
187 *
188 * @return xml representation of the CssParameter
189 */
190 public String exportAsXML() {
191
192 StringBuffer sb = new StringBuffer( "<Fill>" );
193
194 if ( graphicFill != null ) {
195 sb.append( ( (Marshallable) graphicFill ).exportAsXML() );
196 }
197 for( Object o:cssParams.values() ){
198 sb.append( ( (Marshallable) o ).exportAsXML() );
199 }
200
201 sb.append( "</Fill>" );
202
203 return sb.toString();
204 }
205
206 }