KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > XYDatasetTableModel


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ------------------------
23  * XYDatasetTableModel.java
24  * ------------------------
25  * (C)opyright 2003, by Bryan Scott and Contributors.
26  *
27  * Original Author: Bryan Scott ;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * Changes
31  * -------
32  * 01-Jul-2003 : Version 1 contributed by Bryan Scott (DG);
33  */

34
35 package org.jfree.data;
36
37 import javax.swing.table.AbstractTableModel JavaDoc;
38 import javax.swing.table.TableModel JavaDoc;
39
40 /**
41  * A READ-ONLY wrapper around an {@link XYDataset} to convert it to a
42  * table model for use in a JTable.
43  * <P>
44  * TO DO:
45  * <ul>
46  * <li>implement proper naming for x axis (getColumnName)</li>
47  * <li>implement setValueAt to remove READ-ONLY constraint (not sure how)</li>
48  * </ul>
49  *
50  * @author Bryan Scott
51  */

52
53 public class XYDatasetTableModel extends AbstractTableModel JavaDoc
54                                  implements TableModel JavaDoc, DatasetChangeListener {
55
56     /** The dataset. */
57     XYDataset model = null;
58
59     /**
60      * Default constructor.
61      */

62     public XYDatasetTableModel() {
63         super();
64     }
65
66     /**
67      * Creates a new table model based on the specified dataset.
68      *
69      * @param dataset the dataset.
70      */

71     public XYDatasetTableModel(XYDataset dataset) {
72         this();
73         setModel(dataset);
74     }
75
76     /**
77      * Sets the model (dataset).
78      *
79      * @param dataset the dataset.
80      */

81     public void setModel(XYDataset dataset) {
82         this.model = dataset;
83         this.model.addChangeListener(this);
84         fireTableDataChanged();
85     }
86
87     /**
88      * Returns the number of rows.
89      *
90      * @return The row count.
91      */

92     public int getRowCount() {
93         if (this.model == null) {
94             return 0;
95         }
96         return this.model.getItemCount(0);
97     }
98
99     /**
100      * Gets the number of columns in the model.
101      *
102      * @return The number of columns in the model.
103      */

104     public int getColumnCount() {
105         if (this.model == null) {
106             return 0;
107         }
108         return this.model.getSeriesCount() + 1;
109     }
110
111     /**
112      * Returns the column name.
113      *
114      * @param column the column index.
115      *
116      * @return The column name.
117      */

118     public String JavaDoc getColumnName(int column) {
119         if (this.model == null) {
120             return super.getColumnName(column);
121         }
122         if (column < 1) {
123             return "X Value";
124         }
125         else {
126             return this.model.getSeriesName(column - 1);
127         }
128     }
129
130     /**
131      * Returns a value of the specified cell.
132      * Column 0 is the X axis, Columns 1 and over are the Y axis
133      *
134      * @param row the row number.
135      * @param column the column number.
136      *
137      * @return the value of the specified cell.
138      */

139     public Object JavaDoc getValueAt(int row, int column) {
140         if (this.model == null) {
141             return null;
142         }
143         if (column < 1) {
144             return this.model.getXValue(0, row);
145         }
146         else {
147             return model.getYValue(row, column - 1);
148         }
149     }
150
151     /**
152      * Notify listeners that the underlying dataset has changed.
153     *
154      * @param datasetChangeEvent the event
155      *
156      * @see DatasetChangeListener
157      */

158     public void datasetChanged(DatasetChangeEvent datasetChangeEvent) {
159         fireTableDataChanged();
160     }
161
162     /**
163      * Returns a flag indicating whether or not the specified cell is editable.
164      *
165      * @param row the row number.
166      * @param column the column number.
167      *
168      * @return <code>true</code> if the specified cell is editable.
169      */

170     public boolean isCellEditable(int row, int column) {
171         return false;
172    }
173
174     /**
175      * Updates the {@link XYDataset} if allowed.
176      *
177      * @param value the new value.
178      * @param row the row.
179      * @param column the column.
180      */

181     public void setValueAt(Object JavaDoc value, int row, int column) {
182         if (this.isCellEditable(row, column)) {
183             // XYDataset only provides methods for reading a dataset...
184
}
185     }
186
187 // /**
188
// * Run a demonstration of the table model interface.
189
// *
190
// * @param args ignored.
191
// *
192
// * @throws exception when an error occurs.
193
// */
194
// public static void main(String args[]) throws Exception {
195
// JFrame frame = new JFrame();
196
// JPanel panel = new JPanel();
197
// JScrollPane scroll = new JScrollPane();
198
// panel.setLayout(new BorderLayout());
199
//
200
// XYDataset xyDataset = DemoDatasetFactory.createSampleXYDataset();
201
// XYDatasetTableModel tablemodel = new XYDatasetTableModel();
202
//
203
// tablemodel.setModel(xyDataset);
204
//
205
// JTable dataTable = new JTable(tablemodel);
206
// scroll.getViewport().add(dataTable, null);
207
//
208
// JFreeChart chart = ChartFactory.createXYLineChart(
209
// "XY Series Demo",
210
// "X", "Y", xyDataset, PlotOrientation.VERTICAL,
211
// true,
212
// true,
213
// false
214
// );
215
//
216
// ChartPanel chartPanel = new ChartPanel(chart);
217
//
218
// panel.add(chartPanel, BorderLayout.CENTER);
219
// panel.add(scroll, BorderLayout.SOUTH);
220
//
221
// frame.setContentPane(panel);
222
// frame.setSize(300, 500);
223
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
224
// frame.show();
225
// RefineryUtilities.centerFrameOnScreen(frame);
226
// }
227

228 }
229
Popular Tags