KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > awt > DefaultGraph2DModel


1 package JSci.awt;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5 import javax.swing.event.TableModelEvent JavaDoc;
6 import javax.swing.event.TableModelListener JavaDoc;
7 import javax.swing.table.AbstractTableModel JavaDoc;
8
9 /**
10 * The DefaultGraph2DModel class provides a default implementation
11 * of the Graph2DModel interface.
12 * @version 1.2
13 * @author Mark Hale
14 */

15 public final class DefaultGraph2DModel extends AbstractGraphModel implements Graph2DModel, TableModelListener JavaDoc {
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 JavaDoc series=new ArrayList JavaDoc();
21         private int pos=0;
22         private DataSeries curSeries=null;
23
24         public DefaultGraph2DModel() {}
25         /**
26         * Sets the default x-axis values.
27         * A copy of the values is made.
28         * This method does not change the x-axis values of any data series
29         * and so does not fire any events.
30         */

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         /**
37         * Sets the default x-axis values.
38         * A copy of the values is made.
39         * This method does not change the x-axis values of any data series
40         * and so does not fire any events.
41         */

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         /**
49         * Sets the default x-axis values.
50         * This method does not change the x-axis values of any data series
51         * and so does not fire any events.
52         * @param a start of interval.
53         * @param b end of interval.
54         * @param n number of values.
55         */

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         /**
64         * Adds a data series using the default values for the x-axis.
65     * Be sure to call {@link #setXAxis(float[]) setXAxis} first.
66         */

67         public void addSeries(float newSeries[]) {
68                 addSeries(new DataSeries(defaultXAxis, newSeries));
69         }
70         /**
71         * Adds a data series using the default values for the x-axis.
72     * Be sure to call {@link #setXAxis(double[]) setXAxis} first.
73         */

74         public void addSeries(double newSeries[]) {
75                 addSeries(new DataSeries(defaultXAxis, newSeries));
76         }
77         /**
78         * Adds a data series.
79         */

80         public void addSeries(DataSeries newSeries) {
81                 series.add(newSeries);
82                 fireGraphDataChanged();
83                 newSeries.addTableModelListener(this);
84         }
85         /**
86         * Adds a data series.
87         * Convenience method.
88         */

89         public void addSeries(float newXAxis[], float newSeries[]) {
90                 addSeries(new DataSeries(newXAxis, newSeries));
91         }
92         /**
93         * Adds a data series.
94         * Convenience method.
95         */

96         public void addSeries(float newXAxis[], double newSeries[]) {
97                 addSeries(new DataSeries(newXAxis, newSeries));
98         }
99         /**
100         * Changes a data series.
101         */

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         /**
109         * Changes a data series.
110         * Convenience method.
111         */

112         public void changeSeries(int i,float newSeries[]) {
113                 getSeries(i).setValues(newSeries);
114         }
115         /**
116         * Changes a data series.
117         * Convenience method.
118         */

119         public void changeSeries(int i,double newSeries[]) {
120                 getSeries(i).setValues(newSeries);
121         }
122         /**
123         * Remove a data series.
124         */

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         /**
134         * Convenience method.
135         */

136         public void setSeriesVisible(int i,boolean flag) {
137                 getSeries(i).setVisible(flag);
138         }
139
140         /**
141         * Implementation of TabelModelListener.
142         * Application code will not use this method explicitly, it is used internally.
143         */

144         public void tableChanged(TableModelEvent JavaDoc evt) {
145                 if(evt.getColumn() == SERIES_COLUMN)
146                         fireGraphSeriesUpdated(series.indexOf(evt.getSource()));
147                 else
148                         fireGraphDataChanged();
149         }
150
151 // Graph2DModel interface
152

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         /**
177         * The DataSeries class encapsulates a data series for a graph.
178         */

179         public static class DataSeries extends AbstractTableModel JavaDoc {
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 JavaDoc(this, 0, xAxis.length-1, column));
252                         else if(column == SERIES_COLUMN)
253                                 fireTableChanged(new TableModelEvent JavaDoc(this, 0, series.length-1, column));
254                 }
255
256 // TableModelInterface
257

258                 public String JavaDoc 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 JavaDoc getColumnClass(int col) {
267                         return Float JavaDoc.class;
268                 }
269                 public final int getRowCount() {
270                         return length();
271                 }
272                 public final int getColumnCount() {
273                         return 2;
274                 }
275                 public Object JavaDoc getValueAt(int row, int col) {
276                         if(col == X_AXIS_COLUMN)
277                                 return new Float JavaDoc(getXCoord(row));
278                         else if(col == SERIES_COLUMN)
279                                 return new Float JavaDoc(getValue(row));
280                         else
281                                 return null;
282                 }
283                 public void setValueAt(Object JavaDoc value, int row, int col) {
284                         if(col == X_AXIS_COLUMN)
285                                 setXCoord(row, ((Float JavaDoc)value).floatValue());
286                         else if(col == SERIES_COLUMN)
287                                 setValue(row, ((Float JavaDoc)value).floatValue());
288                 }
289                 public boolean isCellEditable(int row, int col) {
290                         return true;
291                 }
292         }
293 }
294
295
Popular Tags