001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ------------------------- 028 * CategoryToPieDataset.java 029 * ------------------------- 030 * (C) Copyright 2003-2005, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Christian W. Zuckschwerdt; 034 * 035 * $Id: CategoryToPieDataset.java,v 1.4.2.1 2005/10/25 21:29:58 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 23-Jan-2003 : Version 1 (DG); 040 * 30-Jul-2003 : Pass through DatasetChangeEvent (CZ); 041 * 29-Jan-2004 : Replaced 'extract' int with TableOrder (DG); 042 * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0 043 * release (DG); 044 * 045 */ 046 047 package org.jfree.data.category; 048 049 import java.util.List; 050 051 import org.jfree.data.general.AbstractDataset; 052 import org.jfree.data.general.DatasetChangeEvent; 053 import org.jfree.data.general.DatasetChangeListener; 054 import org.jfree.data.general.PieDataset; 055 import org.jfree.util.TableOrder; 056 057 /** 058 * A {@link PieDataset} implementation that obtains its data from one row or 059 * column of a {@link CategoryDataset}. 060 * 061 */ 062 public class CategoryToPieDataset extends AbstractDataset 063 implements PieDataset, DatasetChangeListener { 064 065 /** The source. */ 066 private CategoryDataset source; 067 068 /** The extract type. */ 069 private TableOrder extract; 070 071 /** The row or column index. */ 072 private int index; 073 074 /** 075 * An adaptor class that converts any {@link CategoryDataset} into a 076 * {@link PieDataset}, by taking the values from a single row or column. 077 * 078 * @param source the source dataset (<code>null</code> permitted). 079 * @param extract extract data from rows or columns? (<code>null</code> 080 * not permitted). 081 * @param index the row or column index. 082 */ 083 public CategoryToPieDataset(CategoryDataset source, 084 TableOrder extract, 085 int index) { 086 if (extract == null) { 087 throw new IllegalArgumentException("Null 'extract' argument."); 088 } 089 this.source = source; 090 this.source.addChangeListener(this); 091 this.extract = extract; 092 this.index = index; 093 } 094 095 /** 096 * Returns the number of items (values) in the collection. If the 097 * underlying dataset is <code>null</code>, this method returns zero. 098 * 099 * @return The item count. 100 */ 101 public int getItemCount() { 102 int result = 0; 103 if (this.source != null) { 104 if (this.extract == TableOrder.BY_ROW) { 105 result = this.source.getColumnCount(); 106 } 107 else if (this.extract == TableOrder.BY_COLUMN) { 108 result = this.source.getRowCount(); 109 } 110 } 111 return result; 112 } 113 114 /** 115 * Returns a value. 116 * 117 * @param item the item index (zero-based). 118 * 119 * @return The value (possibly <code>null</code>). 120 */ 121 public Number getValue(int item) { 122 Number result = null; 123 if (this.source != null) { 124 if (this.extract == TableOrder.BY_ROW) { 125 result = this.source.getValue(this.index, item); 126 } 127 else if (this.extract == TableOrder.BY_COLUMN) { 128 result = this.source.getValue(item, this.index); 129 } 130 } 131 return result; 132 } 133 134 /** 135 * Returns a key. 136 * 137 * @param index the item index (zero-based). 138 * 139 * @return The key. 140 */ 141 public Comparable getKey(int index) { 142 Comparable result = null; 143 if (this.extract == TableOrder.BY_ROW) { 144 result = this.source.getColumnKey(index); 145 } 146 else if (this.extract == TableOrder.BY_COLUMN) { 147 result = this.source.getRowKey(index); 148 } 149 return result; 150 } 151 152 /** 153 * Returns the index for a given key. 154 * 155 * @param key the key. 156 * 157 * @return The index. 158 */ 159 public int getIndex(Comparable key) { 160 int result = -1; 161 if (this.extract == TableOrder.BY_ROW) { 162 result = this.source.getColumnIndex(key); 163 } 164 else if (this.extract == TableOrder.BY_COLUMN) { 165 result = this.source.getRowIndex(key); 166 } 167 return result; 168 } 169 170 /** 171 * Returns the keys. 172 * 173 * @return The keys. 174 */ 175 public List getKeys() { 176 List result = null; 177 if (this.extract == TableOrder.BY_ROW) { 178 result = this.source.getColumnKeys(); 179 } 180 else if (this.extract == TableOrder.BY_COLUMN) { 181 result = this.source.getRowKeys(); 182 } 183 return result; 184 } 185 186 /** 187 * Returns the value for a given key. If the key is not recognised, the 188 * method should return <code>null</code> (but note that <code>null</code> 189 * can be associated with a valid key also). 190 * 191 * @param key the key. 192 * 193 * @return The value (possibly <code>null</code>). 194 */ 195 public Number getValue(Comparable key) { 196 Number result = null; 197 int keyIndex = getIndex(key); 198 if (this.extract == TableOrder.BY_ROW) { 199 result = this.source.getValue(this.index, keyIndex); 200 } 201 else if (this.extract == TableOrder.BY_COLUMN) { 202 result = this.source.getValue(keyIndex, this.index); 203 } 204 return result; 205 } 206 207 /** 208 * Passes the {@link DatasetChangeEvent} through. 209 * 210 * @param event the event. 211 */ 212 public void datasetChanged (DatasetChangeEvent event) { 213 fireDatasetChanged(); 214 } 215 216 }