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 DefaultGraph2DModel extends AbstractGraphModel implements Graph2DModel, TableModelListener { 16 private static final int X_AXIS_COLUMN = 0; 17 private static final int SERIES_COLUMN = 1; 18 19 private float defaultXAxis[]=new float[0]; 20 private final List series=new ArrayList (); 21 private int pos=0; 22 private DataSeries curSeries=null; 23 24 public DefaultGraph2DModel() {} 25 31 public void setXAxis(float x[]) { 32 if(defaultXAxis.length!=x.length) 33 defaultXAxis=new float[x.length]; 34 System.arraycopy(x,0,defaultXAxis,0,x.length); 35 } 36 42 public void setXAxis(double x[]) { 43 if(defaultXAxis.length!=x.length) 44 defaultXAxis=new float[x.length]; 45 for(int i=0;i<x.length;i++) 46 defaultXAxis[i]=(float)x[i]; 47 } 48 56 public void setXAxis(float a,float b,int n) { 57 if(defaultXAxis.length != n) 58 defaultXAxis = new float[n]; 59 final float scale = (b-a)/(n-1); 60 for(int i=0; i<n; i++) 61 defaultXAxis[i] = scale*i+a; 62 } 63 67 public void addSeries(float newSeries[]) { 68 addSeries(new DataSeries(defaultXAxis, newSeries)); 69 } 70 74 public void addSeries(double newSeries[]) { 75 addSeries(new DataSeries(defaultXAxis, newSeries)); 76 } 77 80 public void addSeries(DataSeries newSeries) { 81 series.add(newSeries); 82 fireGraphDataChanged(); 83 newSeries.addTableModelListener(this); 84 } 85 89 public void addSeries(float newXAxis[], float newSeries[]) { 90 addSeries(new DataSeries(newXAxis, newSeries)); 91 } 92 96 public void addSeries(float newXAxis[], double newSeries[]) { 97 addSeries(new DataSeries(newXAxis, newSeries)); 98 } 99 102 public void changeSeries(int i,DataSeries newSeries) { 103 getSeries(i).removeTableModelListener(this); 104 series.set(i, newSeries); 105 fireGraphDataChanged(); 106 newSeries.addTableModelListener(this); 107 } 108 112 public void changeSeries(int i,float newSeries[]) { 113 getSeries(i).setValues(newSeries); 114 } 115 119 public void changeSeries(int i,double newSeries[]) { 120 getSeries(i).setValues(newSeries); 121 } 122 125 public void removeSeries(int i) { 126 getSeries(i).removeTableModelListener(this); 127 series.remove(i); 128 fireGraphDataChanged(); 129 } 130 public DataSeries getSeries(int i) { 131 return (DataSeries)series.get(i); 132 } 133 136 public void setSeriesVisible(int i,boolean flag) { 137 getSeries(i).setVisible(flag); 138 } 139 140 144 public void tableChanged(TableModelEvent evt) { 145 if(evt.getColumn() == SERIES_COLUMN) 146 fireGraphSeriesUpdated(series.indexOf(evt.getSource())); 147 else 148 fireGraphDataChanged(); 149 } 150 151 153 public float getXCoord(int i) { 154 return curSeries.getXCoord(i); 155 } 156 public float getYCoord(int i) { 157 return curSeries.getValue(i); 158 } 159 public int seriesLength() { 160 return curSeries.length(); 161 } 162 public void firstSeries() { 163 curSeries=getSeries(0); 164 for(pos=0;!curSeries.isVisible() && pos<series.size()-1;) 165 curSeries=getSeries(++pos); 166 } 167 public boolean nextSeries() { 168 if(pos==series.size()-1) 169 return false; 170 do { 171 curSeries=getSeries(++pos); 172 } while(!curSeries.isVisible() && pos<series.size()-1); 173 return curSeries.isVisible(); 174 } 175 176 179 public static class DataSeries extends AbstractTableModel { 180 protected float xAxis[] = new float[0]; 181 protected float series[] = new float[0]; 182 private boolean isVis = true; 183 184 protected DataSeries() {} 185 public DataSeries(float xValues[], float yValues[]) { 186 setXAxis(xValues); 187 setValues(yValues); 188 } 189 public DataSeries(float xValues[], double yValues[]) { 190 setXAxis(xValues); 191 setValues(yValues); 192 } 193 public DataSeries(double xValues[], double yValues[]) { 194 setXAxis(xValues); 195 setValues(yValues); 196 } 197 public void setXAxis(float xValues[]) { 198 if(xAxis.length != xValues.length) 199 xAxis = new float[xValues.length]; 200 System.arraycopy(xValues,0,xAxis,0,xValues.length); 201 fireTableColumnUpdated(X_AXIS_COLUMN); 202 } 203 public void setXAxis(double xValues[]) { 204 if(xAxis.length != xValues.length) 205 xAxis = new float[xValues.length]; 206 for(int i=0;i<xValues.length;i++) 207 xAxis[i] = (float) xValues[i]; 208 fireTableColumnUpdated(X_AXIS_COLUMN); 209 } 210 public void setValues(float yValues[]) { 211 if(series.length != yValues.length) 212 series = new float[yValues.length]; 213 System.arraycopy(yValues,0,series,0,yValues.length); 214 fireTableColumnUpdated(SERIES_COLUMN); 215 } 216 public void setValues(double yValues[]) { 217 if(series.length != yValues.length) 218 series = new float[yValues.length]; 219 for(int i=0;i<yValues.length;i++) 220 series[i] = (float) yValues[i]; 221 fireTableColumnUpdated(SERIES_COLUMN); 222 } 223 public float getXCoord(int i) { 224 return xAxis[i]; 225 } 226 public void setXCoord(int i, float x) { 227 xAxis[i] = x; 228 fireTableCellUpdated(i, X_AXIS_COLUMN); 229 } 230 public float getValue(int i) { 231 return series[i]; 232 } 233 public void setValue(int i, float y) { 234 series[i] = y; 235 fireTableCellUpdated(i, SERIES_COLUMN); 236 } 237 public int length() { 238 return Math.min(xAxis.length, series.length); 239 } 240 public final void setVisible(boolean flag) { 241 if(flag != isVis) { 242 isVis = flag; 243 fireTableDataChanged(); 244 } 245 } 246 public final boolean isVisible() { 247 return isVis; 248 } 249 private void fireTableColumnUpdated(int column) { 250 if(column == X_AXIS_COLUMN) 251 fireTableChanged(new TableModelEvent (this, 0, xAxis.length-1, column)); 252 else if(column == SERIES_COLUMN) 253 fireTableChanged(new TableModelEvent (this, 0, series.length-1, column)); 254 } 255 256 258 public String getColumnName(int col) { 259 if(col == X_AXIS_COLUMN) 260 return "X"; 261 else if(col == SERIES_COLUMN) 262 return "Y"; 263 else 264 return null; 265 } 266 public Class getColumnClass(int col) { 267 return Float .class; 268 } 269 public final int getRowCount() { 270 return length(); 271 } 272 public final int getColumnCount() { 273 return 2; 274 } 275 public Object getValueAt(int row, int col) { 276 if(col == X_AXIS_COLUMN) 277 return new Float (getXCoord(row)); 278 else if(col == SERIES_COLUMN) 279 return new Float (getValue(row)); 280 else 281 return null; 282 } 283 public void setValueAt(Object value, int row, int col) { 284 if(col == X_AXIS_COLUMN) 285 setXCoord(row, ((Float )value).floatValue()); 286 else if(col == SERIES_COLUMN) 287 setValue(row, ((Float )value).floatValue()); 288 } 289 public boolean isCellEditable(int row, int col) { 290 return true; 291 } 292 } 293 } 294 295 | Popular Tags |