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