KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > awt > DefaultGraph3DModel


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 DefaultGraph3DModel class provides a default implementation
11 * of the Graph3DModel interface.
12 * @version 1.2
13 * @author Mark Hale
14 */

15 public final class DefaultGraph3DModel extends AbstractGraphModel implements Graph3DModel, TableModelListener JavaDoc {
16         private static final int X_AXIS_COLUMN = 0;
17         private static final int Y_AXIS_COLUMN = 1;
18         private static final int SERIES_COLUMN = 2;
19                 
20         private float xAxis[] = new float[0];
21         private float yAxis[] = new float[0];
22         private final List JavaDoc series=new ArrayList JavaDoc();
23         private int pos=0;
24         private DataSeries curSeries=null;
25
26         public DefaultGraph3DModel() {}
27         /**
28         * Sets the x-axis values.
29         * A copy of the values is made.
30         */

31         public void setXAxis(float x[]) {
32                 if(xAxis.length!=x.length)
33                         xAxis=new float[x.length];
34                 System.arraycopy(x,0,xAxis,0,x.length);
35                 fireGraphDataChanged();
36         }
37         /**
38         * Sets the y-axis values.
39         * A copy of the values is made.
40         */

41         public void setYAxis(float y[]) {
42                 if(yAxis.length!=y.length)
43                         yAxis=new float[y.length];
44                 System.arraycopy(y,0,yAxis,0,y.length);
45                 fireGraphDataChanged();
46         }
47         /**
48         * Adds a data series.
49         */

50         public void addSeries(DataSeries newSeries) {
51                 series.add(newSeries);
52                 fireGraphDataChanged();
53                 newSeries.addTableModelListener(this);
54         }
55         /**
56         * Adds a data series.
57         * Convenience method.
58         */

59         public void addSeries(float newSeries[]) {
60                 addSeries(new DataSeries(xAxis, yAxis, newSeries));
61         }
62         /**
63         * Changes a data series.
64         */

65         public void changeSeries(int i,DataSeries newSeries) {
66                 getSeries(i).removeTableModelListener(this);
67                 series.set(i, newSeries);
68                 fireGraphDataChanged();
69                 newSeries.addTableModelListener(this);
70         }
71         /**
72         * Changes a data series.
73         * Convenience method.
74         */

75         public void changeSeries(int i,float newSeries[]) {
76                 getSeries(i).setValues(newSeries);
77         }
78         /**
79         * Removes a data series.
80         */

81         public void removeSeries(int i) {
82                 getSeries(i).removeTableModelListener(this);
83                 series.remove(i);
84                 fireGraphDataChanged();
85         }
86         public DataSeries getSeries(int i) {
87                 return (DataSeries)series.get(i);
88         }
89         /**
90         * Convenience method.
91         */

92         public void setSeriesVisible(int i,boolean flag) {
93                 getSeries(i).setVisible(flag);
94         }
95         
96         /**
97         * Implementation of TabelModelListener.
98         * Application code will not use this method explicitly, it is used internally.
99         */

100         public void tableChanged(TableModelEvent JavaDoc evt) {
101                 if(evt.getColumn() == SERIES_COLUMN)
102                         fireGraphSeriesUpdated(series.indexOf(evt.getSource()));
103         }
104
105 // Graph3DModel interface
106

107         public float getXCoord(int i) {
108                 return xAxis[i];
109         }
110         public float getYCoord(int i) {
111                 return yAxis[i];
112         }
113         public float getZCoord(int i) {
114                 return curSeries.getValue(i);
115         }
116         public int seriesLength() {
117                 return curSeries.length();
118         }
119         public void firstSeries() {
120                 curSeries=getSeries(0);
121                 for(pos=0;!curSeries.isVisible() && pos<series.size()-1;)
122                         curSeries=getSeries(++pos);
123         }
124         public boolean nextSeries() {
125                 if(pos==series.size()-1)
126                         return false;
127                 do {
128                         curSeries=getSeries(++pos);
129                 } while(!curSeries.isVisible() && pos<series.size()-1);
130                 return curSeries.isVisible();
131         }
132
133         /**
134         * The DataSeries class encapsulates a data series for a graph.
135         */

136         public static class DataSeries extends AbstractTableModel JavaDoc {
137                 protected float xAxis[] = new float[0];
138                 protected float yAxis[] = new float[0];
139                 protected float series[] = new float[0];
140                 private boolean isVis = true;
141
142                 protected DataSeries() {}
143                 public DataSeries(float xValues[], float yValues[], float zValues[]) {
144                         setXAxis(xValues);
145                         setYAxis(yValues);
146                         setValues(zValues);
147                 }
148                 public DataSeries(float xValues[], float yValues[], double zValues[]) {
149                         setXAxis(xValues);
150                         setYAxis(yValues);
151                         setValues(zValues);
152                 }
153                 public DataSeries(double xValues[], double yValues[], double zValues[]) {
154                         setXAxis(xValues);
155                         setYAxis(yValues);
156                         setValues(zValues);
157                 }
158                 public void setXAxis(float xValues[]) {
159                         if(xAxis.length != xValues.length)
160                                 xAxis = new float[xValues.length];
161                         System.arraycopy(xValues,0,xAxis,0,xValues.length);
162                         fireTableColumnUpdated(X_AXIS_COLUMN);
163                 }
164                 public void setXAxis(double xValues[]) {
165                         if(xAxis.length != xValues.length)
166                                 xAxis = new float[xValues.length];
167                         for(int i=0;i<xValues.length;i++)
168                                 xAxis[i] = (float) xValues[i];
169                         fireTableColumnUpdated(X_AXIS_COLUMN);
170                 }
171                 public void setYAxis(float yValues[]) {
172                         if(yAxis.length != yValues.length)
173                                 yAxis = new float[yValues.length];
174                         System.arraycopy(yValues,0,yAxis,0,yValues.length);
175                         fireTableColumnUpdated(Y_AXIS_COLUMN);
176                 }
177                 public void setYAxis(double yValues[]) {
178                         if(yAxis.length != yValues.length)
179                                 yAxis = new float[yValues.length];
180                         for(int i=0;i<yValues.length;i++)
181                                 yAxis[i] = (float) yValues[i];
182                         fireTableColumnUpdated(Y_AXIS_COLUMN);
183                 }
184                 public void setValues(float zValues[]) {
185                         if(series.length != zValues.length)
186                                 series = new float[zValues.length];
187                         System.arraycopy(zValues,0,series,0,zValues.length);
188                         fireTableColumnUpdated(SERIES_COLUMN);
189                 }
190                 public void setValues(double zValues[]) {
191                         if(series.length != zValues.length)
192                                 series = new float[zValues.length];
193                         for(int i=0;i<zValues.length;i++)
194                                 series[i] = (float) zValues[i];
195                         fireTableColumnUpdated(SERIES_COLUMN);
196                 }
197                 public float getXCoord(int i) {
198                         return xAxis[i];
199                 }
200                 public void setXCoord(int i, float x) {
201                         xAxis[i] = x;
202                         fireTableCellUpdated(i, X_AXIS_COLUMN);
203                 }
204                 public float getYCoord(int i) {
205                         return yAxis[i];
206                 }
207                 public void setYCoord(int i, float y) {
208                         yAxis[i] = y;
209                         fireTableCellUpdated(i, Y_AXIS_COLUMN);
210                 }
211                 public float getValue(int i) {
212                         return series[i];
213                 }
214                 public void setValue(int i, float z) {
215                         series[i] = z;
216                         fireTableCellUpdated(i, SERIES_COLUMN);
217                 }
218                 public int length() {
219                         return Math.min(Math.min(xAxis.length, yAxis.length), series.length);
220                 }
221                 public final void setVisible(boolean flag) {
222                         if(flag != isVis) {
223                                 isVis = flag;
224                                 fireTableDataChanged();
225                         }
226                 }
227                 public final boolean isVisible() {
228                         return isVis;
229                 }
230                 private void fireTableColumnUpdated(int column) {
231                         if(column == X_AXIS_COLUMN)
232                                 fireTableChanged(new TableModelEvent JavaDoc(this, 0, xAxis.length-1, column));
233                         else if(column == Y_AXIS_COLUMN)
234                                 fireTableChanged(new TableModelEvent JavaDoc(this, 0, yAxis.length-1, column));
235                         else if(column == SERIES_COLUMN)
236                                 fireTableChanged(new TableModelEvent JavaDoc(this, 0, series.length-1, column));
237                 }
238                 
239 // TableModelInterface
240

241                 public String JavaDoc getColumnName(int col) {
242                         if(col == X_AXIS_COLUMN)
243                                 return "X";
244                         else if(col == Y_AXIS_COLUMN)
245                                 return "Y";
246                         else if(col == SERIES_COLUMN)
247                                 return "Z";
248                         else
249                                 return null;
250                 }
251                 public Class JavaDoc getColumnClass(int col) {
252                         return Float JavaDoc.class;
253                 }
254                 public final int getRowCount() {
255                         return length();
256                 }
257                 public final int getColumnCount() {
258                         return 3;
259                 }
260                 public Object JavaDoc getValueAt(int row, int col) {
261                         if(col == X_AXIS_COLUMN)
262                                 return new Float JavaDoc(getXCoord(row));
263                         else if(col == Y_AXIS_COLUMN)
264                                 return new Float JavaDoc(getYCoord(row));
265                         else if(col == SERIES_COLUMN)
266                                 return new Float JavaDoc(getValue(row));
267                         else
268                                 return null;
269                 }
270                 public void setValueAt(Object JavaDoc value, int row, int col) {
271                         if(col == X_AXIS_COLUMN)
272                                 setXCoord(row, ((Float JavaDoc)value).floatValue());
273                         else if(col == Y_AXIS_COLUMN)
274                                 setYCoord(row, ((Float JavaDoc)value).floatValue());
275                         else if(col == SERIES_COLUMN)
276                                 setValue(row, ((Float JavaDoc)value).floatValue());
277                 }
278                 public boolean isCellEditable(int row, int col) {
279                         return true;
280                 }
281         }
282 }
283
284
Popular Tags