    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2008, 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-2008, 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    public class XYDatasetTableModel extends AbstractTableModel
    066            implements TableModel, DatasetChangeListener  {
    067
    068        /** The dataset. */
    069        TableXYDataset model = null;
    070
    071        /**
    072         * Default constructor.
    073         */
    074        public XYDatasetTableModel() {
    075            super();
    076        }
    077
    078        /**
    079         * Creates a new table model based on the specified dataset.
    080         *
    081         * @param dataset  the dataset.
    082         */
    083        public XYDatasetTableModel(TableXYDataset dataset) {
    084            this();
    085            this.model = dataset;
    086            this.model.addChangeListener(this);
    087        }
    088
    089        /**
    090         * Sets the model (dataset).
    091         *
    092         * @param dataset  the dataset.
    093         */
    094        public void setModel(TableXYDataset dataset) {
    095            this.model = dataset;
    096            this.model.addChangeListener(this);
    097            fireTableDataChanged();
    098        }
    099
    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    }