001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: DefaultChartModel.java,v $ 023 Revision 1.5 2004/05/05 22:40:17 markl 024 comment block updates 025 026 Revision 1.4 2003/01/19 09:33:06 markl 027 Javadoc & comment header updates. 028 029 Revision 1.3 2001/03/12 07:24:00 markl 030 Javadoc cleanup. 031 032 Revision 1.2 2001/03/12 04:11:41 markl 033 Source code cleanup. 034 035 Revision 1.1 2001/03/12 03:47:36 markl 036 Moved from kiwi.ui.graph package. 037 038 Revision 1.2 2000/10/15 09:40:29 markl 039 Added javadoc and final API polishing. 040 041 Revision 1.1 2000/10/13 08:05:15 markl 042 New class. 043 ---------------------------------------------------------------------------- 044*/ 045 046package kiwi.ui.model; 047 048import kiwi.event.*; 049import kiwi.ui.graph.*; 050 051import java.util.*; 052import javax.swing.event.*; 053import javax.swing.table.*; 054 055/** A default implementation of <code>ChartModel</code> that also implements 056 * the Swing <code>TableModel</code> interface. The labels of the chart values 057 * in this chart model provide the names of the columns for the table model. 058 * Therefore, each data sample in the chart model represents one row in the 059 * table model, and each chart value in the chart definition represents one 060 * column in the table model. This model can thus be used to simultaneously 061 * drive a <code>ChartView</code> and a <code>JTable</code>, effectively 062 * providing both a graphical and a spreadsheet view of the same underlying 063 * data. 064 * 065 * @author Mark Lindner 066 */ 067 068public class DefaultChartModel extends AbstractTableModel implements ChartModel 069 { 070 private EventListenerList listeners = new EventListenerList(); 071 private Vector data; 072 private Chart chart; 073 074 /** Construct a new <code>DefaultChartModel</code> for the specified chart 075 * definition. 076 * 077 * @param chart The chart definition. 078 */ 079 080 public DefaultChartModel(Chart chart) 081 { 082 this.chart = chart; 083 084 data = new Vector(); 085 } 086 087 /** Get the number of rows in the table model. 088 * 089 * @return The row count. 090 */ 091 092 public int getRowCount() 093 { 094 return(data.size()); 095 } 096 097 /** Get the number of columns in the table model. 098 * 099 * @return The column count. 100 */ 101 102 public int getColumnCount() 103 { 104 return(chart.getValueCount()); 105 } 106 107 /** Get the name of the specified column in the table model. 108 * 109 * @param col The column. 110 * @return The name of the specified column. 111 */ 112 113 public String getColumnName(int col) 114 { 115 return(chart.getValueAt(col).getLabel()); 116 } 117 118 /** Determine if the given cell is editable. 119 * 120 * @param row The row. 121 * @param col The Column. 122 * @return <code>true</code> if the cell is editable, <code>false</code> 123 * otherwise. This implementation always returns <code>false</code>. 124 */ 125 126 public boolean isCellEditable(int row, int col) 127 { 128 return(false); 129 } 130 131 /** Add a <code>ChartModelListener</code> to this model's list of listeners. 132 * 133 * @param listener The listener to add. 134 */ 135 136 public void addChartModelListener(ChartModelListener listener) 137 { 138 listeners.add(ChartModelListener.class, listener); 139 } 140 141 /** Remove a <code>ChartModelListener</code> from this model's list of 142 * listeners. 143 * 144 * @param listener The listener to remove. 145 */ 146 147 public void removeChartModelListener(ChartModelListener listener) 148 { 149 listeners.remove(ChartModelListener.class, listener); 150 } 151 152 /** Fire a <i>chart data changed</i> event. 153 */ 154 155 protected void fireChartDataChanged() 156 { 157 ChartModelEvent evt = null; 158 159 Object[] list = listeners.getListenerList(); 160 161 for(int i = list.length - 2; i >= 0; i -= 2) 162 { 163 if(list[i] == ChartModelListener.class) 164 { 165 // Lazily create the event: 166 if(evt == null) 167 evt = new ChartModelEvent(this); 168 ((ChartModelListener)list[i + 1]).chartDataChanged(evt); 169 } 170 } 171 } 172 173 /** Add a data sample to this model. 174 * 175 * @param ds The data sample to add. 176 */ 177 178 public void addDataSample(DataSample ds) 179 { 180 data.addElement(ds); 181 fireChartDataChanged(); 182 fireTableDataChanged(); 183 } 184 185 /** Get the number of data samples in this model. 186 * 187 * @return The number of data samples. 188 */ 189 190 public int getDataSampleCount() 191 { 192 return(data.size()); 193 } 194 195 /** Get all of the data samples in this model. 196 * 197 * @return An <code>Enumeration</code> of the <code>DataSample</code> 198 * objects in this model. 199 */ 200 201 public Enumeration getDataSamples() 202 { 203 return(data.elements()); 204 } 205 206 /** Get the data sample at the specified index. 207 * 208 * @param index The index of the desired data sample. 209 * @return The <code>DataSample</code> at the specified index, or 210 * <code>null</code> if there is no data sample at that index. 211 */ 212 213 public DataSample getDataSample(int index) 214 { 215 return((DataSample)data.elementAt(index)); 216 } 217 218 /** Remove the data sample at the specified index from this model. 219 * 220 * @param index The index of the data sample to remove. 221 */ 222 223 public void removeDataSample(int index) 224 { 225 data.removeElementAt(index); 226 } 227 228 /** Remove all data samples from this model. 229 */ 230 231 public void clear() 232 { 233 data.removeAllElements(); 234 fireChartDataChanged(); 235 fireTableDataChanged(); 236 } 237 238 /** Get the value at the specified row and column in the table model. 239 * 240 * @param row The row. 241 * @param col The column. 242 * @return The value at the specified row and column. 243 */ 244 245 public Object getValueAt(int row, int col) 246 { 247 DataSample ds = (DataSample)data.elementAt(row); 248 ChartValue val = chart.getValueAt(col); 249 return(ds.getValue(val.getName())); 250 } 251 252 } 253 254/* end of source file */