KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, 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-2005, 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 JavaDoc;
48 import javax.swing.table.TableModel JavaDoc;
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  * @author Bryan Scott
66  */

67 public class XYDatasetTableModel extends AbstractTableModel JavaDoc
68                                  implements TableModel JavaDoc, DatasetChangeListener {
69
70     /** The dataset. */
71     TableXYDataset model = null;
72
73     /**
74      * Default constructor.
75      */

76     public XYDatasetTableModel() {
77         super();
78     }
79
80     /**
81      * Creates a new table model based on the specified dataset.
82      *
83      * @param dataset the dataset.
84      */

85     public XYDatasetTableModel(TableXYDataset dataset) {
86         this();
87         this.model = dataset;
88         this.model.addChangeListener(this);
89     }
90
91     /**
92      * Sets the model (dataset).
93      *
94      * @param dataset the dataset.
95      */

96     public void setModel(TableXYDataset dataset) {
97         this.model = dataset;
98         this.model.addChangeListener(this);
99         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 JavaDoc 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 JavaDoc 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 JavaDoc 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 }
254
Popular Tags