    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     * XYDatasetTableModel.java
    029     * ------------------------
    030     * (C)opyright 2003-2005, by Bryan Scott and Contributors.
    031     *
    032     * Original Author:  Bryan Scott ;
    033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034     *
    035     * Changes
    036     * -------
    037     * 01-Jul-2003 : Version 1 contributed by Bryan Scott (DG);
    038     * 27-Apr-2005 : Change XYDataset --> TableXYDataset because the table model
    039     *               assumes all series share the same x-values, and this is not
    040     *               enforced by XYDataset.  Also fixed bug 1191046, a problem
    041     *               in the getValueAt() method (DG);
    042     *
    043     */
    044
    045    package org.jfree.data.xy;
    046
    047    import javax.swing.table.AbstractTableModel;
    048    import javax.swing.table.TableModel;
    049
    050    import org.jfree.data.general.DatasetChangeEvent;
    051    import org.jfree.data.general.DatasetChangeListener;
    052
    053    /**
    054     * A READ-ONLY wrapper around a {@link TableXYDataset} to convert it to a
    055     * table model for use in a JTable.  The first column of the table shows the
    056     * x-values, the remaining columns show the y-values for each series (series 0
    057     * appears in column 1, series 1 appears in column 2, etc).
    058     * <P>
    059     * TO DO:
    060     * <ul>
    061     * <li>implement proper naming for x axis (getColumnName)</li>
    062     * <li>implement setValueAt to remove READ-ONLY constraint (not sure how)</li>
    063     * </ul>
    064     *
    065     * @author           Bryan Scott
    066     */
    067    public class XYDatasetTableModel extends AbstractTableModel
    068                                     implements TableModel, DatasetChangeListener  {
    069
    070        /** The dataset. */
    071        TableXYDataset model = null;
    072
    073        /**
    074         * Default constructor.
    075         */
    076        public XYDatasetTableModel() {
    077            super();
    078        }
    079
    080        /**
    081         * Creates a new table model based on the specified dataset.
    082         *
    083         * @param dataset  the dataset.
    084         */
    085        public XYDatasetTableModel(TableXYDataset dataset) {
    086            this();
    087            this.model = dataset;
    088            this.model.addChangeListener(this);
    089        }
    090
    091        /**
    092         * Sets the model (dataset).
    093         *
    094         * @param dataset  the dataset.
    095         */
    096        public void setModel(TableXYDataset dataset) {
    097            this.model = dataset;
    098            this.model.addChangeListener(this);
    099            fireTableDataChanged();
    100        }
    101
    102        /**
    103         * Returns the number of rows.
    104         *
    105         * @return The row count.
    106         */
    107        public int getRowCount() {
    108            if (this.model == null) {
    109                return 0;
    110            }
    111            return this.model.getItemCount();
    112        }
    113
    114        /**
    115         * Gets the number of columns in the model.
    116         *
    117         * @return The number of columns in the model.
    118         */
    119        public int getColumnCount() {
    120            if (this.model == null) {
    121                return 0;
    122            }
    123            return this.model.getSeriesCount() + 1;
    124        }
    125
    126        /**
    127         * Returns the column name.
    128         *
    129         * @param column  the column index.
    130         *
    131         * @return The column name.
    132         */
    133        public String getColumnName(int column) {
    134            if (this.model == null) {
    135                return super.getColumnName(column);
    136            }
    137            if (column < 1) {
    138                return "X Value";
    139            }
    140            else {
    141                return this.model.getSeriesKey(column - 1).toString();
    142            }
    143        }
    144
    145        /**
    146         * Returns a value of the specified cell.
    147         * Column 0 is the X axis, Columns 1 and over are the Y axis
    148         *
    149         * @param row  the row number.
    150         * @param column  the column number.
    151         *
    152         * @return The value of the specified cell.
    153         */
    154        public Object getValueAt(int row, int column) {
    155            if (this.model == null) {
    156                return null;
    157            }
    158            if (column < 1) {
    159                return this.model.getX(0, row);
    160            }
    161            else {
    162                return this.model.getY(column - 1, row);
    163            }
    164        }
    165
    166        /**
    167         * Receives notification that the underlying dataset has changed.
    168        *
    169         * @param event  the event
    170         *
    171         * @see DatasetChangeListener
    172         */
    173        public void datasetChanged(DatasetChangeEvent event) {
    174            fireTableDataChanged();
    175        }
    176
    177        /**
    178         * Returns a flag indicating whether or not the specified cell is editable.
    179         *
    180         * @param row  the row number.
    181         * @param column  the column number.
    182         *
    183         * @return <code>true</code> if the specified cell is editable.
    184         */
    185        public boolean isCellEditable(int row, int column) {
    186            return false;
    187       }
    188
    189        /**
    190         * Updates the {@link XYDataset} if allowed.
    191         *
    192         * @param value  the new value.
    193         * @param row  the row.
    194         * @param column  the column.
    195         */
    196        public void setValueAt(Object value, int row, int column) {
    197            if (isCellEditable(row, column)) {
    198                // XYDataset only provides methods for reading a dataset...
    199            }
    200        }
    201
    202    //    /**
    203    //     * Run a demonstration of the table model interface.
    204    //     *
    205    //     * @param args  ignored.
    206    //     *
    207    //     * @throws Exception when an error occurs.
    208    //     */
    209    //    public static void main(String args[]) throws Exception {
    210    //        JFrame frame = new JFrame();
    211    //        JPanel panel = new JPanel();
    212    //        panel.setLayout(new BorderLayout());
    213    //
    214    //        XYSeries s1 = new XYSeries("Series 1", true, false);
    215    //        for (int i = 0; i < 10; i++) {
    216    //            s1.add(i, Math.random());
    217    //        }
    218    //        XYSeries s2 = new XYSeries("Series 2", true, false);
    219    //        for (int i = 0; i < 15; i++) {
    220    //            s2.add(i, Math.random());
    221    //        }
    222    //        DefaultTableXYDataset dataset = new DefaultTableXYDataset();
    223    //        dataset.addSeries(s1);
    224    //        dataset.addSeries(s2);
    225    //        XYDatasetTableModel tablemodel = new XYDatasetTableModel();
    226    //
    227    //        tablemodel.setModel(dataset);
    228    //
    229    //        JTable dataTable = new JTable(tablemodel);
    230    //        JScrollPane scroll = new JScrollPane(dataTable);
    231    //        scroll.setPreferredSize(new Dimension(600, 150));
    232    //
    233    //        JFreeChart chart = ChartFactory.createXYLineChart(
    234    //            "XY Series Demo",
    235    //            "X", "Y", dataset, PlotOrientation.VERTICAL,
    236    //            true,
    237    //            true,
    238    //            false
    239    //        );
    240    //
    241    //        ChartPanel chartPanel = new ChartPanel(chart);
    242    //
    243    //        panel.add(chartPanel, BorderLayout.CENTER);
    244    //        panel.add(scroll, BorderLayout.SOUTH);
    245    //
    246    //        frame.setContentPane(panel);
    247    //        frame.setSize(600, 500);
    248    //        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    249    //        frame.show();
    250    //        RefineryUtilities.centerFrameOnScreen(frame);
    251    //    }
    252
    253    }