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