1 package JSci.awt; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 import javax.swing.event.TableModelEvent ; 6 import javax.swing.event.TableModelListener ; 7 import javax.swing.table.AbstractTableModel ; 8 9 15 public final class DefaultCategoryGraph2DModel extends AbstractGraphModel implements CategoryGraph2DModel, TableModelListener { 16 private static final int X_AXIS_COLUMN = 0; 17 private static final int SERIES_COLUMN = 1; 18 19 private Object defaultCategories[] = new Object [0]; 20 private final List series=new ArrayList (); 21 private int pos=0; 22 private DataSeries curSeries=null; 23 24 public DefaultCategoryGraph2DModel() {} 25 31 public void setCategories(Object cat[]) { 32 if(defaultCategories.length!=cat.length) 33 defaultCategories=new Object [cat.length]; 34 System.arraycopy(cat,0,defaultCategories,0,cat.length); 35 } 36 40 public void addSeries(float newSeries[]) { 41 addSeries(new DataSeries(defaultCategories, newSeries)); 42 } 43 47 public void addSeries(double newSeries[]) { 48 addSeries(new DataSeries(defaultCategories, newSeries)); 49 } 50 53 public void addSeries(DataSeries newSeries) { 54 series.add(newSeries); 55 fireGraphDataChanged(); 56 newSeries.addTableModelListener(this); 57 } 58 62 public void addSeries(Object newXAxis[], float newSeries[]) { 63 addSeries(new DataSeries(newXAxis, newSeries)); 64 } 65 69 public void addSeries(Object newXAxis[], double newSeries[]) { 70 addSeries(new DataSeries(newXAxis, newSeries)); 71 } 72 75 public void changeSeries(int i,DataSeries newSeries) { 76 getSeries(i).removeTableModelListener(this); 77 series.set(i, newSeries); 78 fireGraphDataChanged(); 79 newSeries.addTableModelListener(this); 80 } 81 85 public void changeSeries(int i,float newSeries[]) { 86 getSeries(i).setValues(newSeries); 87 } 88 92 public void changeSeries(int i,double newSeries[]) { 93 getSeries(i).setValues(newSeries); 94 } 95 98 public void removeSeries(int i) { 99 getSeries(i).removeTableModelListener(this); 100 series.remove(i); 101 fireGraphDataChanged(); 102 } 103 public DataSeries getSeries(int i) { 104 return (DataSeries)series.get(i); 105 } 106 109 public void setSeriesVisible(int i,boolean flag) { 110 getSeries(i).setVisible(flag); 111 } 112 113 117 public void tableChanged(TableModelEvent evt) { 118 if(evt.getColumn() == SERIES_COLUMN) 119 fireGraphSeriesUpdated(series.indexOf(evt.getSource())); 120 } 121 122 124 public String getCategory(int i) { 125 return curSeries.getCategory(i).toString(); 126 } 127 public float getValue(int i) { 128 return curSeries.getValue(i); 129 } 130 public int seriesLength() { 131 return curSeries.length(); 132 } 133 public void firstSeries() { 134 curSeries=getSeries(0); 135 for(pos=0;!curSeries.isVisible() && pos<series.size()-1;) 136 curSeries=getSeries(++pos); 137 } 138 public boolean nextSeries() { 139 if(pos==series.size()-1) 140 return false; 141 do { 142 curSeries=getSeries(++pos); 143 } while(!curSeries.isVisible() && pos<series.size()-1); 144 return curSeries.isVisible(); 145 } 146 147 150 public static class DataSeries extends AbstractTableModel { 151 protected Object xAxis[] = new Object [0]; 152 protected float series[] = new float[0]; 153 private boolean isVis = true; 154 155 protected DataSeries() {} 156 public DataSeries(Object xValues[], float yValues[]) { 157 setCategories(xValues); 158 setValues(yValues); 159 } 160 public DataSeries(Object xValues[], double yValues[]) { 161 setCategories(xValues); 162 setValues(yValues); 163 } 164 public void setCategories(Object xValues[]) { 165 if(xAxis.length != xValues.length) 166 xAxis = new Object [xValues.length]; 167 System.arraycopy(xValues,0,xAxis,0,xValues.length); 168 fireTableColumnUpdated(X_AXIS_COLUMN); 169 } 170 public void setValues(float yValues[]) { 171 if(series.length != yValues.length) 172 series = new float[yValues.length]; 173 System.arraycopy(yValues,0,series,0,yValues.length); 174 fireTableColumnUpdated(SERIES_COLUMN); 175 } 176 public void setValues(double yValues[]) { 177 if(series.length != yValues.length) 178 series = new float[yValues.length]; 179 for(int i=0;i<yValues.length;i++) 180 series[i] = (float) yValues[i]; 181 fireTableColumnUpdated(SERIES_COLUMN); 182 } 183 public Object getCategory(int i) { 184 return xAxis[i]; 185 } 186 public void setCategory(int i, Object x) { 187 xAxis[i] = x; 188 fireTableCellUpdated(i, X_AXIS_COLUMN); 189 } 190 public float getValue(int i) { 191 return series[i]; 192 } 193 public void setValue(int i, float y) { 194 series[i] = y; 195 fireTableCellUpdated(i, SERIES_COLUMN); 196 } 197 public int length() { 198 return Math.min(xAxis.length, series.length); 199 } 200 public final void setVisible(boolean flag) { 201 if(flag != isVis) { 202 isVis = flag; 203 fireTableDataChanged(); 204 } 205 } 206 public final boolean isVisible() { 207 return isVis; 208 } 209 private void fireTableColumnUpdated(int column) { 210 if(column == X_AXIS_COLUMN) 211 fireTableChanged(new TableModelEvent (this, 0, xAxis.length-1, column)); 212 else if(column == SERIES_COLUMN) 213 fireTableChanged(new TableModelEvent (this, 0, series.length-1, column)); 214 } 215 216 218 public String getColumnName(int col) { 219 if(col == X_AXIS_COLUMN) 220 return "X"; 221 else if(col == SERIES_COLUMN) 222 return "Y"; 223 else 224 return null; 225 } 226 public Class getColumnClass(int col) { 227 if(col == X_AXIS_COLUMN) 228 return Object .class; 229 else 230 return Float .class; 231 } 232 public final int getRowCount() { 233 return length(); 234 } 235 public final int getColumnCount() { 236 return 2; 237 } 238 public Object getValueAt(int row, int col) { 239 if(col == X_AXIS_COLUMN) 240 return getCategory(row); 241 else if(col == SERIES_COLUMN) 242 return new Float (getValue(row)); 243 else 244 return null; 245 } 246 public void setValueAt(Object value, int row, int col) { 247 if(col == X_AXIS_COLUMN) 248 setCategory(row, value); 249 else if(col == SERIES_COLUMN) 250 setValue(row, ((Float )value).floatValue()); 251 } 252 public boolean isCellEditable(int row, int col) { 253 return true; 254 } 255 } 256 } 257 258 | Popular Tags |