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 License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ------------------------
  27. * XYDatasetTableModel.java
  28. * ------------------------
  29. * (C)opyright 2003, 2004, by Bryan Scott and Contributors.
  30. *
  31. * Original Author: Bryan Scott ;
  32. * Contributor(s): David Gilbert (for Object Refinery Limited);
  33. *
  34. * Changes
  35. * -------
  36. * 01-Jul-2003 : Version 1 contributed by Bryan Scott (DG);
  37. */
  38. package org.jfree.data.xy;
  39. import javax.swing.table.AbstractTableModel;
  40. import javax.swing.table.TableModel;
  41. import org.jfree.data.general.DatasetChangeEvent;
  42. import org.jfree.data.general.DatasetChangeListener;
  43. /**
  44. * A READ-ONLY wrapper around an {@link XYDataset} to convert it to a
  45. * table model for use in a JTable.
  46. * <P>
  47. * TO DO:
  48. * <ul>
  49. * <li>implement proper naming for x axis (getColumnName)</li>
  50. * <li>implement setValueAt to remove READ-ONLY constraint (not sure how)</li>
  51. * </ul>
  52. *
  53. * @author Bryan Scott
  54. */
  55. public class XYDatasetTableModel extends AbstractTableModel
  56. implements TableModel, DatasetChangeListener {
  57. /** The dataset. */
  58. XYDataset model = null;
  59. /**
  60. * Default constructor.
  61. */
  62. public XYDatasetTableModel() {
  63. super();
  64. }
  65. /**
  66. * Creates a new table model based on the specified dataset.
  67. *
  68. * @param dataset the dataset.
  69. */
  70. public XYDatasetTableModel(XYDataset dataset) {
  71. this();
  72. this.model = dataset;
  73. this.model.addChangeListener(this);
  74. }
  75. /**
  76. * Sets the model (dataset).
  77. *
  78. * @param dataset the dataset.
  79. */
  80. public void setModel(XYDataset dataset) {
  81. this.model = dataset;
  82. this.model.addChangeListener(this);
  83. fireTableDataChanged();
  84. }
  85. /**
  86. * Returns the number of rows.
  87. *
  88. * @return The row count.
  89. */
  90. public int getRowCount() {
  91. if (this.model == null) {
  92. return 0;
  93. }
  94. return this.model.getItemCount(0);
  95. }
  96. /**
  97. * Gets the number of columns in the model.
  98. *
  99. * @return The number of columns in the model.
  100. */
  101. public int getColumnCount() {
  102. if (this.model == null) {
  103. return 0;
  104. }
  105. return this.model.getSeriesCount() + 1;
  106. }
  107. /**
  108. * Returns the column name.
  109. *
  110. * @param column the column index.
  111. *
  112. * @return The column name.
  113. */
  114. public String getColumnName(int column) {
  115. if (this.model == null) {
  116. return super.getColumnName(column);
  117. }
  118. if (column < 1) {
  119. return "X Value";
  120. }
  121. else {
  122. return this.model.getSeriesName(column - 1);
  123. }
  124. }
  125. /**
  126. * Returns a value of the specified cell.
  127. * Column 0 is the X axis, Columns 1 and over are the Y axis
  128. *
  129. * @param row the row number.
  130. * @param column the column number.
  131. *
  132. * @return the value of the specified cell.
  133. */
  134. public Object getValueAt(int row, int column) {
  135. if (this.model == null) {
  136. return null;
  137. }
  138. if (column < 1) {
  139. return this.model.getX(0, row);
  140. }
  141. else {
  142. return this.model.getY(row, column - 1);
  143. }
  144. }
  145. /**
  146. * Notify listeners that the underlying dataset has changed.
  147. *
  148. * @param datasetChangeEvent the event
  149. *
  150. * @see DatasetChangeListener
  151. */
  152. public void datasetChanged(DatasetChangeEvent datasetChangeEvent) {
  153. fireTableDataChanged();
  154. }
  155. /**
  156. * Returns a flag indicating whether or not the specified cell is editable.
  157. *
  158. * @param row the row number.
  159. * @param column the column number.
  160. *
  161. * @return <code>true</code> if the specified cell is editable.
  162. */
  163. public boolean isCellEditable(int row, int column) {
  164. return false;
  165. }
  166. /**
  167. * Updates the {@link XYDataset} if allowed.
  168. *
  169. * @param value the new value.
  170. * @param row the row.
  171. * @param column the column.
  172. */
  173. public void setValueAt(Object value, int row, int column) {
  174. if (this.isCellEditable(row, column)) {
  175. // XYDataset only provides methods for reading a dataset...
  176. }
  177. }
  178. // /**
  179. // * Run a demonstration of the table model interface.
  180. // *
  181. // * @param args ignored.
  182. // *
  183. // * @throws exception when an error occurs.
  184. // */
  185. // public static void main(String args[]) throws Exception {
  186. // JFrame frame = new JFrame();
  187. // JPanel panel = new JPanel();
  188. // JScrollPane scroll = new JScrollPane();
  189. // panel.setLayout(new BorderLayout());
  190. //
  191. // XYDataset xyDataset = DemoDatasetFactory.createSampleXYDataset();
  192. // XYDatasetTableModel tablemodel = new XYDatasetTableModel();
  193. //
  194. // tablemodel.setModel(xyDataset);
  195. //
  196. // JTable dataTable = new JTable(tablemodel);
  197. // scroll.getViewport().add(dataTable, null);
  198. //
  199. // JFreeChart chart = ChartFactory.createXYLineChart(
  200. // "XY Series Demo",
  201. // "X", "Y", xyDataset, PlotOrientation.VERTICAL,
  202. // true,
  203. // true,
  204. // false
  205. // );
  206. //
  207. // ChartPanel chartPanel = new ChartPanel(chart);
  208. //
  209. // panel.add(chartPanel, BorderLayout.CENTER);
  210. // panel.add(scroll, BorderLayout.SOUTH);
  211. //
  212. // frame.setContentPane(panel);
  213. // frame.setSize(300, 500);
  214. // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  215. // frame.show();
  216. // RefineryUtilities.centerFrameOnScreen(frame);
  217. // }
  218. }