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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * -------------------------
  25. * TimeSeriesTableModel.java
  26. * -------------------------
  27. * (C) Copyright 2001-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: TimeSeriesTableModel.java,v 1.2 2005/01/14 17:29:48 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 14-Nov-2001 : Version 1 (DG);
  37. * 05-Apr-2002 : Removed redundant first column (DG);
  38. * 24-Jun-2002 : Removed unnecessary local variable (DG);
  39. * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  40. *
  41. */
  42. package org.jfree.data.time;
  43. import javax.swing.table.AbstractTableModel;
  44. import org.jfree.data.general.SeriesChangeEvent;
  45. import org.jfree.data.general.SeriesChangeListener;
  46. /**
  47. * Wrapper around a time series to convert it to a table model for use in
  48. * a <code>JTable</code>.
  49. */
  50. public class TimeSeriesTableModel extends AbstractTableModel implements SeriesChangeListener {
  51. /** The series. */
  52. private TimeSeries series;
  53. /** A flag that controls whether the series is editable. */
  54. private boolean editable;
  55. /** The new time period. */
  56. private RegularTimePeriod newTimePeriod;
  57. /** The new value. */
  58. private Number newValue;
  59. /**
  60. * Default constructor.
  61. */
  62. public TimeSeriesTableModel() {
  63. this(new TimeSeries("Untitled"));
  64. }
  65. /**
  66. * Constructs a table model for a time series.
  67. *
  68. * @param series the time series.
  69. */
  70. public TimeSeriesTableModel(TimeSeries series) {
  71. this(series, false);
  72. }
  73. /**
  74. * Creates a table model based on a time series.
  75. *
  76. * @param series the time series.
  77. * @param editable if <ocde>true</code>, the table is editable.
  78. */
  79. public TimeSeriesTableModel(TimeSeries series, boolean editable) {
  80. this.series = series;
  81. this.series.addChangeListener(this);
  82. this.editable = editable;
  83. }
  84. /**
  85. * Returns the number of columns in the table model. For this particular
  86. * model, the column count is fixed at 2.
  87. *
  88. * @return The column count.
  89. */
  90. public int getColumnCount() {
  91. return 2;
  92. }
  93. /**
  94. * Returns the column class in the table model.
  95. *
  96. * @param column The column index.
  97. *
  98. * @return The column class in the table model.
  99. */
  100. public Class getColumnClass(int column) {
  101. if (column == 0) {
  102. return String.class;
  103. }
  104. else {
  105. if (column == 1) {
  106. return Double.class;
  107. }
  108. else {
  109. return null;
  110. }
  111. }
  112. }
  113. /**
  114. * Returns the name of a column
  115. *
  116. * @param column the column index.
  117. *
  118. * @return The name of a column.
  119. */
  120. public String getColumnName(int column) {
  121. if (column == 0) {
  122. return "Period:";
  123. }
  124. else {
  125. if (column == 1) {
  126. return "Value:";
  127. }
  128. else {
  129. return null;
  130. }
  131. }
  132. }
  133. /**
  134. * Returns the number of rows in the table model.
  135. *
  136. * @return The row count.
  137. */
  138. public int getRowCount() {
  139. return this.series.getItemCount();
  140. }
  141. /**
  142. * Returns the data value for a cell in the table model.
  143. *
  144. * @param row the row number.
  145. * @param column the column number.
  146. *
  147. * @return The data value for a cell in the table model.
  148. */
  149. public Object getValueAt(int row, int column) {
  150. if (row < this.series.getItemCount()) {
  151. if (column == 0) {
  152. return this.series.getTimePeriod(row);
  153. }
  154. else {
  155. if (column == 1) {
  156. return this.series.getValue(row);
  157. }
  158. else {
  159. return null;
  160. }
  161. }
  162. }
  163. else {
  164. if (column == 0) {
  165. return this.newTimePeriod;
  166. }
  167. else {
  168. if (column == 1) {
  169. return this.newValue;
  170. }
  171. else {
  172. return null;
  173. }
  174. }
  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. if (this.editable) {
  187. if ((column == 0) || (column == 1)) {
  188. return true;
  189. }
  190. else {
  191. return false;
  192. }
  193. }
  194. else {
  195. return false;
  196. }
  197. }
  198. /**
  199. * Updates the time series.
  200. *
  201. * @param value the new value.
  202. * @param row the row.
  203. * @param column the column.
  204. */
  205. public void setValueAt(Object value, int row, int column) {
  206. if (row < this.series.getItemCount()) {
  207. // update the time series appropriately
  208. if (column == 1) {
  209. try {
  210. Double v = Double.valueOf(value.toString());
  211. this.series.update(row, v);
  212. }
  213. catch (NumberFormatException nfe) {
  214. System.err.println("Number format exception");
  215. }
  216. }
  217. }
  218. else {
  219. if (column == 0) {
  220. // this.series.getClass().valueOf(value.toString());
  221. this.newTimePeriod = null;
  222. }
  223. else if (column == 1) {
  224. this.newValue = Double.valueOf(value.toString());
  225. }
  226. }
  227. }
  228. /**
  229. * Receives notification that the time series has been changed. Responds
  230. * by firing a table data change event.
  231. *
  232. * @param event the event.
  233. */
  234. public void seriesChanged(SeriesChangeEvent event) {
  235. fireTableDataChanged();
  236. }
  237. }