Source for org.jfree.data.xy.XYDatasetTableModel

   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:  * XYDatasetTableModel.java
  29:  * ------------------------
  30:  * (C)opyright 2003-2007, by Bryan Scott and Contributors.
  31:  *
  32:  * Original Author:  Bryan Scott ;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 01-Jul-2003 : Version 1 contributed by Bryan Scott (DG);
  38:  * 27-Apr-2005 : Change XYDataset --> TableXYDataset because the table model
  39:  *               assumes all series share the same x-values, and this is not
  40:  *               enforced by XYDataset.  Also fixed bug 1191046, a problem
  41:  *               in the getValueAt() method (DG);
  42:  * 
  43:  */
  44: 
  45: package org.jfree.data.xy;
  46: 
  47: import javax.swing.table.AbstractTableModel;
  48: import javax.swing.table.TableModel;
  49: 
  50: import org.jfree.data.general.DatasetChangeEvent;
  51: import org.jfree.data.general.DatasetChangeListener;
  52: 
  53: /**
  54:  * A READ-ONLY wrapper around a {@link TableXYDataset} to convert it to a
  55:  * table model for use in a JTable.  The first column of the table shows the
  56:  * x-values, the remaining columns show the y-values for each series (series 0
  57:  * appears in column 1, series 1 appears in column 2, etc).
  58:  * <P>
  59:  * TO DO:
  60:  * <ul>
  61:  * <li>implement proper naming for x axis (getColumnName)</li>
  62:  * <li>implement setValueAt to remove READ-ONLY constraint (not sure how)</li>
  63:  * </ul>
  64:  */
  65: public class XYDatasetTableModel extends AbstractTableModel
  66:                                  implements TableModel, DatasetChangeListener  {
  67: 
  68:     /** The dataset. */
  69:     TableXYDataset model = null;
  70: 
  71:     /**
  72:      * Default constructor.
  73:      */
  74:     public XYDatasetTableModel() {
  75:         super();
  76:     }
  77: 
  78:     /**
  79:      * Creates a new table model based on the specified dataset.
  80:      *
  81:      * @param dataset  the dataset.
  82:      */
  83:     public XYDatasetTableModel(TableXYDataset dataset) {
  84:         this();
  85:         this.model = dataset;
  86:         this.model.addChangeListener(this);
  87:     }
  88: 
  89:     /**
  90:      * Sets the model (dataset).
  91:      *
  92:      * @param dataset  the dataset.
  93:      */
  94:     public void setModel(TableXYDataset dataset) {
  95:         this.model = dataset;
  96:         this.model.addChangeListener(this);
  97:         fireTableDataChanged();
  98:     }
  99: 
 100:     /**
 101:      * Returns the number of rows.
 102:      *
 103:      * @return The row count.
 104:      */
 105:     public int getRowCount() {
 106:         if (this.model == null) {
 107:             return 0;
 108:         }
 109:         return this.model.getItemCount();
 110:     }
 111: 
 112:     /**
 113:      * Gets the number of columns in the model.
 114:      *
 115:      * @return The number of columns in the model.
 116:      */
 117:     public int getColumnCount() {
 118:         if (this.model == null) {
 119:             return 0;
 120:         }
 121:         return this.model.getSeriesCount() + 1;
 122:     }
 123: 
 124:     /**
 125:      * Returns the column name.
 126:      *
 127:      * @param column  the column index.
 128:      *
 129:      * @return The column name.
 130:      */
 131:     public String getColumnName(int column) {
 132:         if (this.model == null) {
 133:             return super.getColumnName(column);
 134:         }
 135:         if (column < 1) {
 136:             return "X Value";
 137:         }
 138:         else {
 139:             return this.model.getSeriesKey(column - 1).toString();
 140:         }
 141:     }
 142: 
 143:     /**
 144:      * Returns a value of the specified cell.
 145:      * Column 0 is the X axis, Columns 1 and over are the Y axis
 146:      *
 147:      * @param row  the row number.
 148:      * @param column  the column number.
 149:      *
 150:      * @return The value of the specified cell.
 151:      */
 152:     public Object getValueAt(int row, int column) {
 153:         if (this.model == null) {
 154:             return null;
 155:         }
 156:         if (column < 1) {
 157:             return this.model.getX(0, row);
 158:         }
 159:         else {
 160:             return this.model.getY(column - 1, row);
 161:         }
 162:     }
 163: 
 164:     /**
 165:      * Receives notification that the underlying dataset has changed.
 166:     *
 167:      * @param event  the event
 168:      *
 169:      * @see DatasetChangeListener
 170:      */
 171:     public void datasetChanged(DatasetChangeEvent event) {
 172:         fireTableDataChanged();
 173:     }
 174: 
 175:     /**
 176:      * Returns a flag indicating whether or not the specified cell is editable.
 177:      *
 178:      * @param row  the row number.
 179:      * @param column  the column number.
 180:      *
 181:      * @return <code>true</code> if the specified cell is editable.
 182:      */
 183:     public boolean isCellEditable(int row, int column) {
 184:         return false;
 185:    }
 186: 
 187:     /**
 188:      * Updates the {@link XYDataset} if allowed.
 189:      *
 190:      * @param value  the new value.
 191:      * @param row  the row.
 192:      * @param column  the column.
 193:      */
 194:     public void setValueAt(Object value, int row, int column) {
 195:         if (isCellEditable(row, column)) {
 196:             // XYDataset only provides methods for reading a dataset...
 197:         }
 198:     }
 199: 
 200: //    /**
 201: //     * Run a demonstration of the table model interface.
 202: //     *
 203: //     * @param args  ignored.
 204: //     *
 205: //     * @throws Exception when an error occurs.
 206: //     */
 207: //    public static void main(String args[]) throws Exception {
 208: //        JFrame frame = new JFrame();
 209: //        JPanel panel = new JPanel();
 210: //        panel.setLayout(new BorderLayout());
 211: //
 212: //        XYSeries s1 = new XYSeries("Series 1", true, false);
 213: //        for (int i = 0; i < 10; i++) {
 214: //            s1.add(i, Math.random());   
 215: //        }
 216: //        XYSeries s2 = new XYSeries("Series 2", true, false);
 217: //        for (int i = 0; i < 15; i++) {
 218: //            s2.add(i, Math.random());   
 219: //        }
 220: //        DefaultTableXYDataset dataset = new DefaultTableXYDataset();
 221: //        dataset.addSeries(s1);
 222: //        dataset.addSeries(s2);
 223: //        XYDatasetTableModel tablemodel = new XYDatasetTableModel();
 224: //
 225: //        tablemodel.setModel(dataset);
 226: //
 227: //        JTable dataTable = new JTable(tablemodel);
 228: //        JScrollPane scroll = new JScrollPane(dataTable);
 229: //        scroll.setPreferredSize(new Dimension(600, 150));
 230: //
 231: //        JFreeChart chart = ChartFactory.createXYLineChart(
 232: //            "XY Series Demo",
 233: //            "X", "Y", dataset, PlotOrientation.VERTICAL,
 234: //            true,
 235: //            true,
 236: //            false
 237: //        );
 238: //
 239: //        ChartPanel chartPanel = new ChartPanel(chart);
 240: //
 241: //        panel.add(chartPanel, BorderLayout.CENTER);
 242: //        panel.add(scroll, BorderLayout.SOUTH);
 243: //
 244: //        frame.setContentPane(panel);
 245: //        frame.setSize(600, 500);
 246: //        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 247: //        frame.show();
 248: //        RefineryUtilities.centerFrameOnScreen(frame);
 249: //    }
 250: 
 251: }