Source for org.jfree.chart.title.LegendItemBlockContainer

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * -----------------------------
  28:  * LegendItemBlockContainer.java
  29:  * -----------------------------
  30:  * (C) Copyright 2006, 2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 20-Jul-2006 : Version 1 (DG);
  38:  * 06-Oct-2006 : Added tooltip and URL text fields (DG);
  39:  * 18-May-2007 : Added seriesKey and dataset fields (DG);
  40:  * 
  41:  */
  42: 
  43: package org.jfree.chart.title;
  44: 
  45: import java.awt.Graphics2D;
  46: import java.awt.Shape;
  47: import java.awt.geom.Rectangle2D;
  48: 
  49: import org.jfree.chart.block.Arrangement;
  50: import org.jfree.chart.block.BlockContainer;
  51: import org.jfree.chart.block.BlockResult;
  52: import org.jfree.chart.block.EntityBlockParams;
  53: import org.jfree.chart.block.EntityBlockResult;
  54: import org.jfree.chart.entity.EntityCollection;
  55: import org.jfree.chart.entity.LegendItemEntity;
  56: import org.jfree.chart.entity.StandardEntityCollection;
  57: import org.jfree.data.general.Dataset;
  58: 
  59: /**
  60:  * A container that holds all the pieces of a single legend item.
  61:  *
  62:  * @since 1.0.2
  63:  */
  64: public class LegendItemBlockContainer extends BlockContainer {
  65: 
  66:     /** 
  67:      * The dataset. 
  68:      * 
  69:      * @since 1.0.6
  70:      */
  71:     private Dataset dataset;
  72:     
  73:     /**
  74:      * The series key.
  75:      * 
  76:      * @since 1.0.6
  77:      */
  78:     private Comparable seriesKey;
  79:     
  80:     /** The dataset index. */
  81:     private int datasetIndex;
  82:     
  83:     /** The series index. */
  84:     private int series;
  85:     
  86:     /** The tool tip text (can be <code>null</code>). */
  87:     private String toolTipText;
  88:     
  89:     /** The URL text (can be <code>null</code>). */
  90:     private String urlText;
  91:     
  92:     /**
  93:      * Creates a new legend item block.
  94:      * 
  95:      * @param arrangement  the arrangement.
  96:      * @param datasetIndex  the dataset index.
  97:      * @param series  the series index.
  98:      * 
  99:      * @deprecated As of 1.0.6, use the other constructor.
 100:      */
 101:     public LegendItemBlockContainer(Arrangement arrangement, int datasetIndex,
 102:             int series) {
 103:         super(arrangement);
 104:         this.datasetIndex = datasetIndex;
 105:         this.series = series;
 106:     }
 107:     
 108:     /**
 109:      * Creates a new legend item block.
 110:      * 
 111:      * @param arrangement  the arrangement.
 112:      * @param dataset  the dataset.
 113:      * @param seriesKey  the series key.
 114:      * 
 115:      * @since 1.0.6
 116:      */
 117:     public LegendItemBlockContainer(Arrangement arrangement, Dataset dataset,
 118:             Comparable seriesKey) {
 119:         super(arrangement);
 120:         this.dataset = dataset;
 121:         this.seriesKey = seriesKey;
 122:     }
 123:     
 124:     /**
 125:      * Returns a reference to the dataset for the associated legend item.
 126:      * 
 127:      * @return A dataset reference.
 128:      * 
 129:      * @since 1.0.6
 130:      */
 131:     public Dataset getDataset() {
 132:         return this.dataset;
 133:     }
 134:     
 135:     /**
 136:      * Returns the series key.
 137:      * 
 138:      * @return The series key.
 139:      * 
 140:      * @since 1.0.6
 141:      */
 142:     public Comparable getSeriesKey() {
 143:         return this.seriesKey;
 144:     }
 145:     
 146:     /**
 147:      * Returns the dataset index.
 148:      * 
 149:      * @return The dataset index.
 150:      * 
 151:      * @deprecated As of 1.0.6, use the {@link #getDataset()} method.
 152:      */
 153:     public int getDatasetIndex() {
 154:         return this.datasetIndex;
 155:     }
 156:    
 157:     /**
 158:      * Returns the series index.
 159:      * 
 160:      * @return The series index.
 161:      */
 162:     public int getSeriesIndex() {
 163:         return this.series;
 164:     }
 165:     
 166:     /**
 167:      * Returns the tool tip text.
 168:      * 
 169:      * @return The tool tip text (possibly <code>null</code>).
 170:      * 
 171:      * @since 1.0.3
 172:      */
 173:     public String getToolTipText() {
 174:         return this.toolTipText;
 175:     }
 176:     
 177:     /**
 178:      * Sets the tool tip text.
 179:      * 
 180:      * @param text  the text (<code>null</code> permitted).
 181:      * 
 182:      * @since 1.0.3
 183:      */
 184:     public void setToolTipText(String text) {
 185:         this.toolTipText = text;   
 186:     }
 187:     
 188:     /**
 189:      * Returns the URL text.
 190:      * 
 191:      * @return The URL text (possibly <code>null</code>).
 192:      * 
 193:      * @since 1.0.3
 194:      */
 195:     public String getURLText() {
 196:         return this.urlText;
 197:     }
 198:     
 199:     /**
 200:      * Sets the URL text.
 201:      * 
 202:      * @param text  the text (<code>null</code> permitted).
 203:      * 
 204:      * @since 1.0.3
 205:      */
 206:     public void setURLText(String text) {
 207:         this.urlText = text;   
 208:     }
 209:     
 210:     /**
 211:      * Draws the block within the specified area.
 212:      * 
 213:      * @param g2  the graphics device.
 214:      * @param area  the area.
 215:      * @param params  passed on to blocks within the container 
 216:      *                (<code>null</code> permitted).
 217:      * 
 218:      * @return An instance of {@link EntityBlockResult}, or <code>null</code>.
 219:      */
 220:     public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
 221:         // draw the block without collecting entities
 222:         super.draw(g2, area, null);
 223:         EntityBlockParams ebp = null;
 224:         BlockResult r = new BlockResult();
 225:         if (params instanceof EntityBlockParams) {
 226:             ebp = (EntityBlockParams) params;
 227:             if (ebp.getGenerateEntities()) {
 228:                 EntityCollection ec = new StandardEntityCollection();
 229:                 LegendItemEntity entity = new LegendItemEntity(
 230:                         (Shape) area.clone());
 231:                 entity.setSeriesIndex(this.series);
 232:                 entity.setSeriesKey(this.seriesKey);
 233:                 entity.setDataset(this.dataset);
 234:                 entity.setToolTipText(getToolTipText());
 235:                 entity.setURLText(getURLText());
 236:                 ec.add(entity);
 237:                 r.setEntityCollection(ec);
 238:             }
 239:         }
 240:         return r;
 241:     }
 242: }