001 //$HeadURL: http://svn.wald.intevation.org/svn/deegree/base/trunk/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: 29808 $ $Date: 2011-02-28 13:22:06 +0100 (Mon, 28 Feb 2011) $ 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 if ( !s.equalsIgnoreCase( "random" ) ) { 106 try { 107 awtColor = Color.decode( s ); 108 } catch ( NumberFormatException e ) { 109 throw new FilterEvaluationException( "Given value ('" + s + "') for CSS-Parameter 'fill' " 110 + "does not denote a valid color!" ); 111 } 112 } else { 113 awtColor = ColorUtils.getRandomColor( false ); 114 } 115 } 116 117 return awtColor; 118 } 119 120 /** 121 * sets the value of the fill's CssParameter 'fill' as a simple color 122 * 123 * @param color 124 * color to be set 125 */ 126 public void setFill( Color color ) { 127 128 String hex = ColorUtils.toHexCode( "#", color ); 129 CssParameter fill = StyleFactory.createCssParameter( "fill", hex ); 130 131 cssParams.put( "fill", fill ); 132 } 133 134 /** 135 * Returns the (evaluated) value of the fill's CssParameter 'fill-opacity'. 136 * <p> 137 * 138 * @param feature 139 * specifies the <tt>Feature</tt> to be used for evaluation of the underlying 'sld:ParameterValueType' 140 * @return the (evaluated) value of the parameter 141 * @throws FilterEvaluationException 142 * if the evaluation fails or the value is invalid 143 */ 144 public double getOpacity( Feature feature ) 145 throws FilterEvaluationException { 146 double opacity = OPACITY_DEFAULT; 147 148 CssParameter cssParam = (CssParameter) cssParams.get( "fill-opacity" ); 149 150 if ( cssParam != null ) { 151 String value = cssParam.getValue( feature ); 152 if ( !value.equalsIgnoreCase( "random" ) ) { 153 try { 154 opacity = Double.parseDouble( value ); 155 } catch ( NumberFormatException e ) { 156 throw new FilterEvaluationException( "Given value for parameter 'fill-opacity' ('" + value 157 + "') has invalid format!" ); 158 } 159 } else { 160 opacity = 0.5 + Math.random() / 2d; 161 } 162 163 if ( ( opacity < 0.0 ) || ( opacity > 1.0 ) ) { 164 throw new FilterEvaluationException( "Value for parameter 'fill-opacity' (given: '" + value 165 + "') must be between 0.0 and 1.0!" ); 166 } 167 } 168 169 return opacity; 170 } 171 172 /** 173 * sets the value of the opacity's CssParameter 'opacity' as a value. Valid values ranges from 0 .. 1. If a value < 174 * 0 is passed it will be set to 0. If a value > 1 is passed it will be set to 1. 175 * 176 * @param opacity 177 * opacity to be set 178 */ 179 public void setOpacity( double opacity ) { 180 181 if ( opacity > 1 ) { 182 opacity = 1; 183 } else if ( opacity < 0 ) { 184 opacity = 0; 185 } 186 187 CssParameter fillOp = StyleFactory.createCssParameter( "fill-opacity", "" + opacity ); 188 cssParams.put( "fill-opacity", fillOp ); 189 } 190 191 public String getSymbol( Feature feature ) 192 throws FilterEvaluationException { 193 194 CssParameter cssParam = (CssParameter) cssParams.get( "symbol" ); 195 if ( cssParam != null ) { 196 return cssParam.getValue( feature ); 197 } 198 return null; 199 } 200 201 /** 202 * exports the content of the CssParameter as XML formated String 203 * 204 * @return xml representation of the CssParameter 205 */ 206 public String exportAsXML() { 207 208 StringBuffer sb = new StringBuffer( "<Fill>" ); 209 210 if ( graphicFill != null ) { 211 sb.append( ( (Marshallable) graphicFill ).exportAsXML() ); 212 } 213 for ( Object o : cssParams.values() ) { 214 sb.append( ( (Marshallable) o ).exportAsXML() ); 215 } 216 217 sb.append( "</Fill>" ); 218 219 return sb.toString(); 220 } 221 222 }