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 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")
150: || mime.equalsIgnoreCase("image/png")) {
151: imageType = BufferedImage.TYPE_INT_ARGB;
152: } else {
153: imageType = BufferedImage.TYPE_INT_RGB;
154: }
155:
156: for (int i = 0; i < le.length; i++) {
157: imagearray[i] = le[i].exportAsImage(mime);
158: maxheight += (imagearray[i].getHeight() + buffer);
159: if (maxwidth < imagearray[i].getWidth()) {
160: maxwidth = imagearray[i].getWidth();
161: }
162: }
163:
164: // printing the title (or not)
165: Graphics g = null;
166: if (getTitle() != null && getTitle().length() > 0) {
167: titleFontMetrics = calculateFontMetrics(getTitle());
168: titleheight = titleFontMetrics[1] + titleFontMetrics[2];
169: maxheight += titleheight;
170:
171: // is title wider than the maxwidth?
172: if (maxwidth <= titleFontMetrics[0]) {
173: maxwidth = titleFontMetrics[0];
174: }
175:
176: bi = new BufferedImage(maxwidth, maxheight, imageType);
177: g = bi.getGraphics();
178: if (imageType == BufferedImage.TYPE_INT_RGB) {
179: g.setColor(java.awt.Color.WHITE);
180: g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
181: }
182: g.setColor(java.awt.Color.BLACK);
183: g.drawString(getTitle(), 0, 0 + titleheight);
184: } else {
185: bi = new BufferedImage(maxwidth, maxheight, imageType);
186: g = bi.getGraphics();
187: if (imageType == BufferedImage.TYPE_INT_RGB) {
188: g.setColor(java.awt.Color.WHITE);
189: g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
190: }
191: }
192:
193: for (int j = 0; j < imagearray.length; j++) {
194: g.drawImage(imagearray[j], 0,
195: (imagearray[j].getHeight() + buffer) * j
196: + titleheight + buffer, null);
197: }
198:
199: return bi;
200: }
201: }
|