001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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-2007, 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 implements
066: 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: } else {
138: return this .model.getSeriesKey(column - 1).toString();
139: }
140: }
141:
142: /**
143: * Returns a value of the specified cell.
144: * Column 0 is the X axis, Columns 1 and over are the Y axis
145: *
146: * @param row the row number.
147: * @param column the column number.
148: *
149: * @return The value of the specified cell.
150: */
151: public Object getValueAt(int row, int column) {
152: if (this .model == null) {
153: return null;
154: }
155: if (column < 1) {
156: return this .model.getX(0, row);
157: } else {
158: return this .model.getY(column - 1, row);
159: }
160: }
161:
162: /**
163: * Receives notification that the underlying dataset has changed.
164: *
165: * @param event the event
166: *
167: * @see DatasetChangeListener
168: */
169: public void datasetChanged(DatasetChangeEvent event) {
170: fireTableDataChanged();
171: }
172:
173: /**
174: * Returns a flag indicating whether or not the specified cell is editable.
175: *
176: * @param row the row number.
177: * @param column the column number.
178: *
179: * @return <code>true</code> if the specified cell is editable.
180: */
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: public void setValueAt(Object value, int row, int column) {
193: if (isCellEditable(row, column)) {
194: // XYDataset only provides methods for reading a dataset...
195: }
196: }
197:
198: // /**
199: // * Run a demonstration of the table model interface.
200: // *
201: // * @param args ignored.
202: // *
203: // * @throws Exception when an error occurs.
204: // */
205: // public static void main(String args[]) throws Exception {
206: // JFrame frame = new JFrame();
207: // JPanel panel = new JPanel();
208: // panel.setLayout(new BorderLayout());
209: //
210: // XYSeries s1 = new XYSeries("Series 1", true, false);
211: // for (int i = 0; i < 10; i++) {
212: // s1.add(i, Math.random());
213: // }
214: // XYSeries s2 = new XYSeries("Series 2", true, false);
215: // for (int i = 0; i < 15; i++) {
216: // s2.add(i, Math.random());
217: // }
218: // DefaultTableXYDataset dataset = new DefaultTableXYDataset();
219: // dataset.addSeries(s1);
220: // dataset.addSeries(s2);
221: // XYDatasetTableModel tablemodel = new XYDatasetTableModel();
222: //
223: // tablemodel.setModel(dataset);
224: //
225: // JTable dataTable = new JTable(tablemodel);
226: // JScrollPane scroll = new JScrollPane(dataTable);
227: // scroll.setPreferredSize(new Dimension(600, 150));
228: //
229: // JFreeChart chart = ChartFactory.createXYLineChart(
230: // "XY Series Demo",
231: // "X", "Y", dataset, PlotOrientation.VERTICAL,
232: // true,
233: // true,
234: // false
235: // );
236: //
237: // ChartPanel chartPanel = new ChartPanel(chart);
238: //
239: // panel.add(chartPanel, BorderLayout.CENTER);
240: // panel.add(scroll, BorderLayout.SOUTH);
241: //
242: // frame.setContentPane(panel);
243: // frame.setSize(600, 500);
244: // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
245: // frame.show();
246: // RefineryUtilities.centerFrameOnScreen(frame);
247: // }
248:
249: }
|