001 //$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/src/org/deegree/graphics/legend/LegendElementCollection.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.legend; 037 038 import static org.deegree.framework.log.LoggerFactory.getLogger; 039 040 import java.awt.Graphics; 041 import java.awt.image.BufferedImage; 042 import java.util.ArrayList; 043 044 import org.deegree.framework.log.ILogger; 045 046 /** 047 * <tt>LegendElementCollection</tt> is a collection of <tt>LegendElement</tt>s and is a <tt>LegendElement</tt> 048 * too. It can be used to group elements or to create more complex elements. 049 * <p> 050 * 051 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 052 * @version $Revision: 7363 $ $Date: 2007-05-29 20:47:55 +0200 (Di, 29 Mai 2007) $ 053 */ 054 public class LegendElementCollection extends LegendElement { 055 056 private static final ILogger LOG = getLogger( LegendElementCollection.class ); 057 058 ArrayList<LegendElement> collection; 059 060 /** 061 * 062 */ 063 String title = ""; 064 065 /** 066 * empty constructor 067 * 068 */ 069 public LegendElementCollection() { 070 this.collection = new ArrayList<LegendElement>(); 071 } 072 073 /** 074 * @param title 075 */ 076 public LegendElementCollection( String title ) { 077 super(); 078 this.collection = new ArrayList<LegendElement>(); 079 this.title = title; 080 } 081 082 /** 083 * adds a <tt>LegendElement</tt> to the collection. 084 * 085 * @param legendElement 086 * to add 087 */ 088 public void addLegendElement( LegendElement legendElement ) { 089 this.collection.add( legendElement ); 090 } 091 092 /** 093 * 094 * @return all legend elements 095 */ 096 public LegendElement[] getLegendElements() { 097 return collection.toArray( new LegendElement[collection.size()] ); 098 } 099 100 /** 101 * sets the title of the <tt>LegendElement</tt>. The title will be displayed on top of the 102 * <tt>LegendElementCollection</tt> 103 * 104 * @param title 105 * title of the <tt>LegendElement</tt> 106 * 107 */ 108 public void setTitle( String title ) { 109 this.title = title; 110 } 111 112 /** 113 * returns the title of the <tt>LegendElement</tt> 114 * 115 * @return the title of the <tt>LegendElement</tt> 116 * 117 */ 118 public String getTitle() { 119 return title; 120 } 121 122 /** 123 * returns the number of legend elements as an int 124 * 125 * @return number of legend elements 126 */ 127 public int getSize() { 128 return this.collection.size(); 129 } 130 131 /** 132 * @param mime 133 * the mime type of the requested legend (like image/png, etc) 134 * 135 * @return the legend as image 136 */ 137 @Override 138 public BufferedImage exportAsImage( String mime ) 139 throws LegendException { 140 141 int[] titleFontMetrics; 142 int titleheight = 0; // height of the title (default: 0, none) 143 144 int maxheight = 0; // maximum width of resulting Image 145 int maxwidth = 0; // maximum height of resulting Image 146 int buffer = 10; // bufferspace between LegendElements and Title (eventually) 147 148 LegendElement[] le = getLegendElements(); 149 BufferedImage[] imagearray = new BufferedImage[le.length]; 150 BufferedImage bi = null; 151 int imageType = 0; 152 if ( mime.equalsIgnoreCase( "image/gif" ) || mime.equalsIgnoreCase( "image/png" ) ) { 153 imageType = BufferedImage.TYPE_INT_ARGB; 154 } else { 155 imageType = BufferedImage.TYPE_INT_RGB; 156 } 157 158 for ( int i = 0; i < le.length; i++ ) { 159 imagearray[i] = le[i].exportAsImage( mime ); 160 maxheight += ( imagearray[i].getHeight() + buffer ); 161 if ( maxwidth < imagearray[i].getWidth() ) { 162 maxwidth = imagearray[i].getWidth(); 163 } 164 } 165 166 // printing the title (or not) 167 Graphics g = null; 168 if ( getTitle() != null && getTitle().length() > 0 ) { 169 titleFontMetrics = calculateFontMetrics( getTitle() ); 170 titleheight = titleFontMetrics[1] + titleFontMetrics[2]; 171 maxheight += titleheight; 172 173 // is title wider than the maxwidth? 174 if ( maxwidth <= titleFontMetrics[0] ) { 175 maxwidth = titleFontMetrics[0]; 176 } 177 178 bi = new BufferedImage( maxwidth, maxheight, imageType ); 179 g = bi.getGraphics(); 180 if ( imageType == BufferedImage.TYPE_INT_RGB ) { 181 g.setColor( java.awt.Color.WHITE ); 182 g.fillRect( 0, 0, bi.getWidth(), bi.getHeight() ); 183 } 184 g.setColor( java.awt.Color.BLACK ); 185 g.drawString( getTitle(), 0, 0 + titleheight ); 186 } else { 187 bi = new BufferedImage( maxwidth, maxheight, imageType ); 188 g = bi.getGraphics(); 189 if ( imageType == BufferedImage.TYPE_INT_RGB ) { 190 g.setColor( java.awt.Color.WHITE ); 191 g.fillRect( 0, 0, bi.getWidth(), bi.getHeight() ); 192 } 193 } 194 195 int offsety = titleheight + buffer; 196 for ( int j = 0; j < imagearray.length; j++ ) { 197 g.drawImage( imagearray[j], 0, offsety, null ); 198 offsety += imagearray[j].getHeight() + buffer; 199 if ( LOG.isDebug() ) { 200 LOG.logDebug( "LegendElementCollection.exportAsImage: j=" + j + ", imagearray[j].getHeight()=" 201 + imagearray[j].getHeight() ); 202 } 203 } 204 205 return bi; 206 } 207 }