001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: AbstractChartDataModel.java
021: Created on 28. Juni 2001, 18:58
022: */
023:
024: package de.progra.charting.model;
025:
026: import de.progra.charting.event.ChartDataModelListener;
027: import de.progra.charting.event.ChartDataModelEvent;
028: import javax.swing.event.EventListenerList;
029: import de.progra.charting.CoordSystem;
030: import java.util.ArrayList;
031: import java.util.Arrays;
032: import java.util.TreeSet;
033: import java.util.Set;
034: import java.util.HashMap;
035:
036: /**
037: * This class implements the event-handling methods for a chart model.
038: * @author mueller
039: * @version 1.0
040: */
041: public abstract class AbstractChartDataModel implements ChartDataModel {
042:
043: /** The listener list. */
044: protected EventListenerList listener = new EventListenerList();
045:
046: /** Flag defining the automatic scaling of max and min values. */
047: protected boolean autoscale = false;
048:
049: /** Flag defining the manual scaling of max and min values. */
050: protected boolean manualscale = false;
051:
052: /** Maximum and minimum column values to be displayed. */
053: protected double maxcolumn, mincolumn;
054:
055: /** Maximum and minimum values to be displayed. */
056: protected Number maxvalue, minvalue;
057:
058: /** Creates new AbstractChartDataModel */
059: public AbstractChartDataModel() {
060: }
061:
062: /** Removes a ChartDataModelListener.
063: * @param l the ChartDataListener
064: */
065: public void removeChartDataModelListener(ChartDataModelListener l) {
066: listener.remove(ChartDataModelListener.class, l);
067: }
068:
069: /** Adds a ChartDataModelListener.
070: * @param l the ChartDataModelListener
071: */
072: public void addChartDataModelListener(ChartDataModelListener l) {
073: listener.add(ChartDataModelListener.class, l);
074: }
075:
076: /** Determines if the column values are numeric.
077: * @return <CODE>false</CODE> per default
078: */
079: public boolean isColumnNumeric() {
080: return false;
081: }
082:
083: /** Provides an empty implementation for not-editable DataModels.
084: * @param set the DataSet in which the value should be set
085: * @param index the index in the DataSet where the value should be stored
086: * @param value the value object
087: */
088: public void setValueAt(int set, int index, Object value) {
089: }
090:
091: /** Returns the class of the column values.
092: * @return <CODE>Object.class</CODE> per default
093: */
094: public Class getColumnClass() {
095: return Object.class;
096: }
097:
098: /** Promotes a new ChartDataModelEvent.
099: * @param src the source object of the event.
100: */
101: public void fireChartDataModelChangedEvent(Object src) {
102: ChartDataModelEvent e = new ChartDataModelEvent(src);
103: Object[] 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])
107: .chartDataChanged(e);
108: }
109: }
110: }
111:
112: /** Returns the Axis Binding for a specific DataSet, ie the Axis on
113: * which the DataSet should be plotted
114: * @param set the DataSet whose Axis binding should be determined
115: * @return <code>DataSet.FIRST_YAXIS</code> by default.
116: */
117: public int getAxisBinding(int set) {
118: return CoordSystem.FIRST_YAXIS;
119: }
120:
121: /** Provides a default empty implementation.
122: * @param set the DataSet
123: * @param axis the Axis binding
124: */
125: public void setAxisBinding(int set, int axis) {
126: }
127:
128: public void setAutoScale(boolean b) {
129: autoscale = b;
130: }
131:
132: public boolean isAutoScale() {
133: return autoscale;
134: }
135:
136: /** Enables the manual axis scaling. Set the desired
137: * maximum and minimum values using the setMaximum...Value
138: * functions.
139: */
140: public void setManualScale(boolean b) {
141: manualscale = b;
142: }
143:
144: /** Returns true if the manual axis scaling is enabled. This overrides
145: * the enabled automatic axis scaling.
146: */
147: public boolean isManualScale() {
148: return manualscale;
149: }
150:
151: /** Sets the maximum x-axis value. */
152: public void setMaximumColumnValue(double d) {
153: maxcolumn = d;
154: }
155:
156: /** Sets the minimum x-axis value. */
157: public void setMinimumColumnValue(double d) {
158: mincolumn = d;
159: }
160:
161: /** Sets the maximum y-axis value. */
162: public void setMaximumValue(Number n) {
163: maxvalue = n;
164: }
165:
166: /** Sets the minimum y-axis value. */
167: public void setMinimumValue(Number n) {
168: minvalue = n;
169: }
170:
171: public double getManualMaximumColumnValue() {
172: return maxcolumn;
173: }
174:
175: public double getManualMinimumColumnValue() {
176: return mincolumn;
177: }
178:
179: public Number getManualMaximumValue() {
180: return maxvalue;
181: }
182:
183: public Number getManualMinimumValue() {
184: return minvalue;
185: }
186:
187: /** Returns the title of the DataSet.
188: * @param set the DataSet identifier
189: * @return the the number of
190: * the DataSet per default.
191: */
192: public String getDataSetName(int set) {
193: return "Dataset " + set;
194: }
195:
196: /** Compares this ChartDataModel with another object.
197: * @param o the object to compare with
198: * @return true, if o is an AbstractChartDataModel, the number of
199: * DataSets is equal and all DataSet names and column values are equal.
200: */
201: public boolean equals(Object o) {
202: if (o == null)
203: return false;
204: try {
205: AbstractChartDataModel model = (AbstractChartDataModel) o;
206:
207: if (getDataSetNumber() != model.getDataSetNumber()) {
208: return false;
209: }
210:
211: for (int i = 0; i < getDataSetNumber(); i++) {
212: if (!getDataSetName(i).equals(model.getDataSetName(i))) {
213: return false;
214: }
215:
216: for (int j = 0; j < getDataSetLength(j); j++) {
217: if (!getColumnValueAt(j).equals(
218: model.getColumnValueAt(j))) {
219: return false;
220: }
221: }
222: }
223: } catch (Exception e) {
224: return false;
225: }
226:
227: return true;
228: }
229:
230: protected abstract TreeSet getOrderedValues(int axis);
231:
232: protected abstract double getFirstColumnValue();
233:
234: protected abstract double getLastColumnValue();
235: }
|