001 /* ====================================================== 002 * Orson : a free chart beans library based on JFreeChart 003 * ====================================================== 004 * 005 * (C) Copyright 2007, by Object Refinery Limited. 006 * 007 * Project Info: not-yet-released 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 package org.jfree.beans.editors; 029 030 import javax.swing.table.AbstractTableModel; 031 import javax.swing.table.TableModel; 032 033 import org.jfree.data.general.DefaultPieDataset; 034 035 /** 036 * A TableModel based on a DefaultPieDataset. Using this to experiment with 037 * editing. 038 */ 039 public class DefaultPieDatasetTableModel extends AbstractTableModel 040 implements TableModel { 041 042 private DefaultPieDataset dataset; 043 044 private int insertionRow = -1; 045 046 /** 047 * If this is true, the model has a column zero containing a boolean flag 048 * indicating whether or not this is the current row for editing. 049 */ 050 private boolean hasEditingColumn; 051 052 /** 053 * Creates a new instance. 054 * 055 * @param dataset the underlying dataset. 056 */ 057 public DefaultPieDatasetTableModel(DefaultPieDataset dataset) { 058 if (dataset == null) { 059 throw new IllegalArgumentException("Null 'dataset' argument."); 060 } 061 this.dataset = dataset; 062 } 063 064 public int getColumnCount() { 065 int result = 2; 066 if (this.hasEditingColumn) { 067 result++; 068 } 069 return result; 070 } 071 072 /** 073 * Returns the number of rows in the table, which is equal to the number 074 * of items in the dataset, plus an editing row if it is being used. 075 * 076 * @return The row count. 077 */ 078 public int getRowCount() { 079 int result = this.dataset.getItemCount(); 080 if (this.insertionRow >= 0) { 081 result++; 082 } 083 return result; 084 } 085 086 /** 087 * Returns the item at the specified row and column. We return a value 088 * from the underlying dataset. 089 * 090 * @param rowIndex the row index. 091 * @param columnIndex the column index. 092 * 093 * @return The value. 094 */ 095 public Object getValueAt(int rowIndex, int columnIndex) { 096 if (columnIndex == 0) { 097 return this.dataset.getKey(rowIndex); 098 } 099 else if (columnIndex == 1) { 100 return this.dataset.getValue(rowIndex); 101 } 102 else if (columnIndex == 2) { 103 // if (this.hasEditingColumn) { 104 // return this.dataset.getValue(rowIndex;) 105 // } 106 } 107 return null; 108 109 } 110 111 public boolean isCellEditable(int rowIndex, int columnIndex) { 112 return true; 113 } 114 115 public void setValueAt(Object value, int rowIndex, int columnIndex) { 116 if (columnIndex == 0) { 117 Comparable key; 118 if (!(value instanceof Comparable)) { 119 key = value.toString(); 120 } 121 else { 122 key = (Comparable) value; 123 } 124 Comparable oldKey = this.dataset.getKey(rowIndex); 125 Number n = this.dataset.getValue(oldKey); 126 this.dataset.remove(oldKey); 127 this.dataset.setValue(key, n); 128 fireTableCellUpdated(rowIndex, columnIndex); 129 } 130 // else if (columnIndex == 1) { 131 // Comparable key = this.dataset.getKey(rowIndex); 132 // Number n = null; 133 // if (value instanceof Number) { 134 // n = (Number) value; 135 // } 136 // this.dataset.setValue(key, n); 137 // fireTableCellUpdated(rowIndex, columnIndex); 138 // } 139 } 140 141 public String getColumnName(int columnIndex) { 142 if (columnIndex == 0) { 143 return "Key"; 144 } 145 else if (columnIndex == 1) { 146 return "Value"; 147 } 148 else { 149 return null; 150 } 151 } 152 }