KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > model > AbstractChartDataModel


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     AbstractChartDataModel.java
21     Created on 28. Juni 2001, 18:58
22 */

23
24 package de.progra.charting.model;
25
26 import de.progra.charting.event.ChartDataModelListener;
27 import de.progra.charting.event.ChartDataModelEvent;
28 import javax.swing.event.EventListenerList JavaDoc;
29 import de.progra.charting.CoordSystem;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.TreeSet JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.HashMap JavaDoc;
35
36 /**
37  * This class implements the event-handling methods for a chart model.
38  * @author mueller
39  * @version 1.0
40  */

41 public abstract class AbstractChartDataModel implements ChartDataModel {
42     
43     /** The listener list. */
44     protected EventListenerList JavaDoc listener = new EventListenerList JavaDoc();
45
46     /** Flag defining the automatic scaling of max and min values. */
47     protected boolean autoscale = false;
48
49     /** Flag defining the manual scaling of max and min values. */
50     protected boolean manualscale = false;
51
52     /** Maximum and minimum column values to be displayed. */
53     protected double maxcolumn, mincolumn;
54
55     /** Maximum and minimum values to be displayed. */
56     protected Number JavaDoc maxvalue, minvalue;
57     
58     /** Creates new AbstractChartDataModel */
59     public AbstractChartDataModel() {
60     }
61
62     /** Removes a ChartDataModelListener.
63      * @param l the ChartDataListener
64      */

65     public void removeChartDataModelListener(ChartDataModelListener l) {
66         listener.remove(ChartDataModelListener.class, l);
67     }
68     
69     /** Adds a ChartDataModelListener.
70      * @param l the ChartDataModelListener
71      */

72     public void addChartDataModelListener(ChartDataModelListener l) {
73         listener.add(ChartDataModelListener.class, l);
74     }
75     
76     /** Determines if the column values are numeric.
77      * @return <CODE>false</CODE> per default
78      */

79     public boolean isColumnNumeric() {
80         return false;
81     }
82     
83     /** Provides an empty implementation for not-editable DataModels.
84      * @param set the DataSet in which the value should be set
85      * @param index the index in the DataSet where the value should be stored
86      * @param value the value object
87      */

88     public void setValueAt(int set, int index, Object JavaDoc value) {
89     }
90     
91     /** Returns the class of the column values.
92      * @return <CODE>Object.class</CODE> per default
93      */

94     public Class JavaDoc getColumnClass() {
95         return Object JavaDoc.class;
96     }
97     
98     /** Promotes a new ChartDataModelEvent.
99      * @param src the source object of the event.
100      */

101     public void fireChartDataModelChangedEvent(Object JavaDoc src) {
102         ChartDataModelEvent e = new ChartDataModelEvent(src);
103         Object JavaDoc[] ls = listener.getListenerList();
104         for (int i = (ls.length - 2); i >= 0; i-=2) {
105             if (ls[i] == ChartDataModelListener.class) {
106                 ((ChartDataModelListener)ls[i + 1]).chartDataChanged(e);
107             }
108         }
109     }
110     
111     /** Returns the Axis Binding for a specific DataSet, ie the Axis on
112      * which the DataSet should be plotted
113      * @param set the DataSet whose Axis binding should be determined
114      * @return <code>DataSet.FIRST_YAXIS</code> by default.
115      */

116     public int getAxisBinding(int set) {
117         return CoordSystem.FIRST_YAXIS;
118     }
119     
120     /** Provides a default empty implementation.
121      * @param set the DataSet
122      * @param axis the Axis binding
123      */

124     public void setAxisBinding(int set, int axis) {
125     }
126     
127     public void setAutoScale(boolean b) {
128         autoscale = b;
129     }
130     
131     public boolean isAutoScale() {
132         return autoscale;
133     }
134
135     /** Enables the manual axis scaling. Set the desired
136      * maximum and minimum values using the setMaximum...Value
137      * functions.
138      */

139     public void setManualScale(boolean b) {
140         manualscale = b;
141     }
142
143     /** Returns true if the manual axis scaling is enabled. This overrides
144      * the enabled automatic axis scaling.
145      */

146     public boolean isManualScale() {
147         return manualscale;
148     }
149
150     /** Sets the maximum x-axis value. */
151     public void setMaximumColumnValue(double d) {
152         maxcolumn = d;
153     }
154
155     /** Sets the minimum x-axis value. */
156     public void setMinimumColumnValue(double d) {
157         mincolumn = d;
158     }
159
160     /** Sets the maximum y-axis value. */
161     public void setMaximumValue(Number JavaDoc n) {
162         maxvalue = n;
163     }
164
165     /** Sets the minimum y-axis value. */
166     public void setMinimumValue(Number JavaDoc n) {
167         minvalue = n;
168     }
169
170     public double getManualMaximumColumnValue() {
171         return maxcolumn;
172     }
173
174     public double getManualMinimumColumnValue() {
175         return mincolumn;
176     }
177
178     public Number JavaDoc getManualMaximumValue() {
179         return maxvalue;
180     }
181
182     public Number JavaDoc getManualMinimumValue() {
183         return minvalue;
184     }
185     
186     /** Returns the title of the DataSet.
187      * @param set the DataSet identifier
188      * @return the the number of
189      * the DataSet per default.
190      */

191     public String JavaDoc getDataSetName(int set) {
192         return "Dataset "+set;
193     }
194     
195     /** Compares this ChartDataModel with another object.
196      * @param o the object to compare with
197      * @return true, if o is an AbstractChartDataModel, the number of
198      * DataSets is equal and all DataSet names and column values are equal.
199      */

200     public boolean equals(Object JavaDoc o) {
201         if(o == null)
202             return false;
203         try {
204             AbstractChartDataModel model = (AbstractChartDataModel)o;
205             
206             if(getDataSetNumber() != model.getDataSetNumber()) {
207                 return false;
208             }
209             
210             for(int i = 0; i < getDataSetNumber(); i++) {
211                 if(!getDataSetName(i).equals(model.getDataSetName(i))) {
212                     return false;
213                 }
214                 
215                 for(int j = 0; j < getDataSetLength(j); j++) {
216                     if(!getColumnValueAt(j).equals(model.getColumnValueAt(j))) {
217                         return false;
218                     }
219                 }
220             }
221         } catch(Exception JavaDoc e) { return false;}
222         
223         return true;
224     }
225     
226     protected abstract TreeSet JavaDoc getOrderedValues(int axis);
227     
228     protected abstract double getFirstColumnValue();
229     
230     protected abstract double getLastColumnValue();
231 }
232
Popular Tags