KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > awt > DefaultCategoryGraph2DModel


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

15 public final class DefaultCategoryGraph2DModel extends AbstractGraphModel implements CategoryGraph2DModel, TableModelListener JavaDoc {
16         private static final int X_AXIS_COLUMN = 0;
17         private static final int SERIES_COLUMN = 1;
18
19         private Object JavaDoc defaultCategories[] = new Object JavaDoc[0];
20         private final List JavaDoc series=new ArrayList JavaDoc();
21         private int pos=0;
22         private DataSeries curSeries=null;
23
24         public DefaultCategoryGraph2DModel() {}
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 setCategories(Object JavaDoc cat[]) {
32                 if(defaultCategories.length!=cat.length)
33                         defaultCategories=new Object JavaDoc[cat.length];
34                 System.arraycopy(cat,0,defaultCategories,0,cat.length);
35         }
36         /**
37         * Adds a data series using the default values for the x-axis.
38     * Be sure to call {@link #setCategories(Object[]) setCategories} first.
39         */

40         public void addSeries(float newSeries[]) {
41                 addSeries(new DataSeries(defaultCategories, newSeries));
42         }
43         /**
44         * Adds a data series using the default values for the x-axis.
45     * Be sure to call {@link #setCategories(Object[]) setCategories} first.
46         */

47         public void addSeries(double newSeries[]) {
48                 addSeries(new DataSeries(defaultCategories, newSeries));
49         }
50         /**
51         * Adds a data series.
52         */

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

62         public void addSeries(Object JavaDoc newXAxis[], float newSeries[]) {
63                 addSeries(new DataSeries(newXAxis, newSeries));
64         }
65         /**
66         * Adds a data series.
67         * Convenience method.
68         */

69         public void addSeries(Object JavaDoc newXAxis[], double newSeries[]) {
70                 addSeries(new DataSeries(newXAxis, newSeries));
71         }
72         /**
73         * Changes a data series.
74         */

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         /**
82         * Changes a data series.
83         * Convenience method.
84         */

85         public void changeSeries(int i,float newSeries[]) {
86                 getSeries(i).setValues(newSeries);
87         }
88         /**
89         * Changes a data series.
90         * Convenience method.
91         */

92         public void changeSeries(int i,double newSeries[]) {
93                 getSeries(i).setValues(newSeries);
94         }
95         /**
96         * Remove a data series.
97         */

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         /**
107         * Convenience method.
108         */

109         public void setSeriesVisible(int i,boolean flag) {
110                 getSeries(i).setVisible(flag);
111         }
112         
113         /**
114         * Implementation of TabelModelListener.
115         * Application code will not use this method explicitly, it is used internally.
116         */

117         public void tableChanged(TableModelEvent JavaDoc evt) {
118                 if(evt.getColumn() == SERIES_COLUMN)
119                         fireGraphSeriesUpdated(series.indexOf(evt.getSource()));
120         }
121
122 // CategoryGraph2DModel interface
123

124         public String JavaDoc 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         /**
148         * The DataSeries class encapsulates a data series for a graph.
149         */

150         public static class DataSeries extends AbstractTableModel JavaDoc {
151                 protected Object JavaDoc xAxis[] = new Object JavaDoc[0];
152                 protected float series[] = new float[0];
153                 private boolean isVis = true;
154
155                 protected DataSeries() {}
156                 public DataSeries(Object JavaDoc xValues[], float yValues[]) {
157                         setCategories(xValues);
158                         setValues(yValues);
159                 }
160                 public DataSeries(Object JavaDoc xValues[], double yValues[]) {
161                         setCategories(xValues);
162                         setValues(yValues);
163                 }
164                 public void setCategories(Object JavaDoc xValues[]) {
165                         if(xAxis.length != xValues.length)
166                                 xAxis = new Object JavaDoc[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 JavaDoc getCategory(int i) {
184                         return xAxis[i];
185                 }
186                 public void setCategory(int i, Object JavaDoc 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 JavaDoc(this, 0, xAxis.length-1, column));
212                         else if(column == SERIES_COLUMN)
213                                 fireTableChanged(new TableModelEvent JavaDoc(this, 0, series.length-1, column));
214                 }
215
216 // TableModelInterface
217

218                 public String JavaDoc 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 JavaDoc getColumnClass(int col) {
227                         if(col == X_AXIS_COLUMN)
228                                 return Object JavaDoc.class;
229                         else
230                                 return Float JavaDoc.class;
231                 }
232                 public final int getRowCount() {
233                         return length();
234                 }
235                 public final int getColumnCount() {
236                         return 2;
237                 }
238                 public Object JavaDoc 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 JavaDoc(getValue(row));
243                         else
244                                 return null;
245                 }
246                 public void setValueAt(Object JavaDoc 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 JavaDoc)value).floatValue());
251                 }
252                 public boolean isCellEditable(int row, int col) {
253                         return true;
254                 }
255         }
256 }
257
258
Popular Tags