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