001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ------------------------
028 * XYDatasetTableModel.java
029 * ------------------------
030 * (C)opyright 2003-present, by Bryan Scott and Contributors.
031 *
032 * Original Author:  Bryan Scott;
033 * Contributor(s):   David Gilbert;
034 *
035 */
036
037package org.jfree.data.xy;
038
039import javax.swing.table.AbstractTableModel;
040import javax.swing.table.TableModel;
041
042import org.jfree.data.general.DatasetChangeEvent;
043import org.jfree.data.general.DatasetChangeListener;
044
045/**
046 * A READ-ONLY wrapper around a {@link TableXYDataset} to convert it to a
047 * table model for use in a JTable.  The first column of the table shows the
048 * x-values, the remaining columns show the y-values for each series (series 0
049 * appears in column 1, series 1 appears in column 2, etc).
050 * <P>
051 * TO DO:
052 * <ul>
053 * <li>implement proper naming for x axis (getColumnName)</li>
054 * <li>implement setValueAt to remove READ-ONLY constraint (not sure how)</li>
055 * </ul>
056 */
057public class XYDatasetTableModel extends AbstractTableModel
058        implements TableModel, DatasetChangeListener  {
059
060    /** The dataset. */
061    TableXYDataset model = null;
062
063    /**
064     * Default constructor.
065     */
066    public XYDatasetTableModel() {
067        super();
068    }
069
070    /**
071     * Creates a new table model based on the specified dataset.
072     *
073     * @param dataset  the dataset.
074     */
075    public XYDatasetTableModel(TableXYDataset dataset) {
076        this();
077        this.model = dataset;
078        this.model.addChangeListener(this);
079    }
080
081    /**
082     * Sets the model (dataset).
083     *
084     * @param dataset  the dataset.
085     */
086    public void setModel(TableXYDataset dataset) {
087        this.model = dataset;
088        this.model.addChangeListener(this);
089        fireTableDataChanged();
090    }
091
092    /**
093     * Returns the number of rows.
094     *
095     * @return The row count.
096     */
097    @Override
098    public int getRowCount() {
099        if (this.model == null) {
100            return 0;
101        }
102        return this.model.getItemCount();
103    }
104
105    /**
106     * Gets the number of columns in the model.
107     *
108     * @return The number of columns in the model.
109     */
110    @Override
111    public int getColumnCount() {
112        if (this.model == null) {
113            return 0;
114        }
115        return this.model.getSeriesCount() + 1;
116    }
117
118    /**
119     * Returns the column name.
120     *
121     * @param column  the column index.
122     *
123     * @return The column name.
124     */
125    @Override
126    public String getColumnName(int column) {
127        if (this.model == null) {
128            return super.getColumnName(column);
129        }
130        if (column < 1) {
131            return "X Value";
132        }
133        else {
134            return this.model.getSeriesKey(column - 1).toString();
135        }
136    }
137
138    /**
139     * Returns a value of the specified cell.
140     * Column 0 is the X axis, Columns 1 and over are the Y axis
141     *
142     * @param row  the row number.
143     * @param column  the column number.
144     *
145     * @return The value of the specified cell.
146     */
147    @Override
148    public Object getValueAt(int row, int column) {
149        if (this.model == null) {
150            return null;
151        }
152        if (column < 1) {
153            return this.model.getX(0, row);
154        }
155        else {
156            return this.model.getY(column - 1, row);
157        }
158    }
159
160    /**
161     * Receives notification that the underlying dataset has changed.
162    *
163     * @param event  the event
164     *
165     * @see DatasetChangeListener
166     */
167    @Override
168    public void datasetChanged(DatasetChangeEvent event) {
169        fireTableDataChanged();
170    }
171
172    /**
173     * Returns a flag indicating whether or not the specified cell is editable.
174     *
175     * @param row  the row number.
176     * @param column  the column number.
177     *
178     * @return {@code true} if the specified cell is editable.
179     */
180    @Override
181    public boolean isCellEditable(int row, int column) {
182        return false;
183   }
184
185    /**
186     * Updates the {@link XYDataset} if allowed.
187     *
188     * @param value  the new value.
189     * @param row  the row.
190     * @param column  the column.
191     */
192    @Override
193    public void setValueAt(Object value, int row, int column) {
194        if (isCellEditable(row, column)) {
195            // XYDataset only provides methods for reading a dataset...
196        }
197    }
198
199}