001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui.charts;
042:
043: import java.awt.Color;
044: import java.util.Iterator;
045: import java.util.Vector;
046:
047: /**
048: *
049: * @author Jiri Sedlacek
050: */
051: public class DynamicSynchronousXYChartModel extends
052: AbstractSynchronousXYChartModel {
053: //~ Instance fields ----------------------------------------------------------------------------------------------------------
054:
055: protected long[] maxYValues;
056: protected long[] minYValues;
057: protected Color[] seriesColors;
058: protected String[] seriesNames;
059: protected long[] xValues;
060: protected long[][] yValues;
061: protected int itemCount = 0;
062: protected int seriesCount = 0;
063: protected long maxXValue = 0;
064: protected long minXValue = 0;
065: private int arrayBufferSize = 100;
066: private int currentArraySize;
067:
068: //~ Methods ------------------------------------------------------------------------------------------------------------------
069:
070: public void setArrayBufferSize(int arrayBufferSize) {
071: this .arrayBufferSize = arrayBufferSize;
072: checkArraySize();
073: }
074:
075: public int getArrayBufferSize() {
076: return arrayBufferSize;
077: }
078:
079: public int getItemCount() {
080: return itemCount;
081: }
082:
083: // TODO: will be moved to chart axis definition
084: public long getMaxDisplayYValue(int seriesIndex) {
085: return getMaxYValue(seriesIndex);
086: }
087:
088: public long getMaxXValue() {
089: return maxXValue;
090: }
091:
092: public long getMaxYValue(int seriesIndex) {
093: return maxYValues[seriesIndex];
094: }
095:
096: // TODO: will be moved to chart axis definition
097: public long getMinDisplayYValue(int seriesIndex) {
098: return 0;
099: }
100:
101: public long getMinXValue() {
102: return minXValue;
103: }
104:
105: public long getMinYValue(int seriesIndex) {
106: return minYValues[seriesIndex];
107: }
108:
109: public Color getSeriesColor(int seriesIndex) {
110: return seriesColors[seriesIndex];
111: }
112:
113: // --- Abstract SynchronousXYChartModel --------------------------------------
114: public int getSeriesCount() {
115: return seriesCount;
116: }
117:
118: public String getSeriesName(int seriesIndex) {
119: return seriesNames[seriesIndex];
120: }
121:
122: public long getXValue(int itemIndex) {
123: return xValues[itemIndex];
124: }
125:
126: public long getYValue(int itemIndex, int seriesIndex) {
127: return yValues[itemIndex][seriesIndex];
128: }
129:
130: public void addItemValues(long xValue, long[] yValues) {
131: // is array extension needed?
132: checkArraySize();
133:
134: // first data arrived, initialize min/max values
135: if (itemCount == 0) {
136: minXValue = xValue;
137: maxXValue = xValue;
138:
139: for (int i = 0; i < seriesCount; i++) {
140: minYValues[i] = yValues[i];
141: maxYValues[i] = yValues[i];
142: }
143: } else {
144: // check "timeline" consistency
145: if (xValues[itemCount - 1] >= xValue) {
146: throw new RuntimeException(
147: "New x-value not greater than previous x-value."); // NOI18N
148: }
149:
150: // check min/max for x value
151: maxXValue = xValue; // new values are always greater
152:
153: // check min/max for y values
154: for (int i = 0; i < seriesCount; i++) {
155: minYValues[i] = Math.min(minYValues[i], yValues[i]);
156: maxYValues[i] = Math.max(maxYValues[i], yValues[i]);
157: }
158: }
159:
160: // add new x value
161: xValues[itemCount] = xValue;
162:
163: // add new y values
164: this .yValues[itemCount] = yValues;
165:
166: // increment item counter
167: itemCount++;
168:
169: fireChartDataChanged();
170: }
171:
172: public void setupModel(String[] seriesNames, Color[] seriesColors) {
173: this .seriesNames = seriesNames;
174: this .seriesColors = seriesColors;
175:
176: if (seriesNames.length != seriesColors.length) {
177: seriesCount = 0;
178: throw new RuntimeException(
179: "Counts of series names and series colors don't match."); // NOI18N
180: } else {
181: seriesCount = seriesNames.length;
182: }
183:
184: itemCount = 0;
185: currentArraySize = arrayBufferSize;
186:
187: xValues = new long[arrayBufferSize];
188: yValues = new long[arrayBufferSize][];
189:
190: minXValue = 0;
191: maxXValue = 0;
192:
193: minYValues = new long[seriesCount];
194: maxYValues = new long[seriesCount];
195: }
196:
197: // --- Private Implementation ------------------------------------------------
198: private void checkArraySize() {
199: // array extension is needed
200: if (currentArraySize == itemCount) {
201: // extend array for xValues (1-dimensional)
202: xValues = extendArray(xValues, arrayBufferSize);
203:
204: // extend array for yValues (2-dimensional)
205: yValues = extendArray(yValues, arrayBufferSize);
206:
207: // extend array for min/max yValues (1-dimensional)
208: minYValues = extendArray(minYValues, arrayBufferSize);
209: maxYValues = extendArray(maxYValues, arrayBufferSize);
210:
211: // update current array size
212: currentArraySize += arrayBufferSize;
213: }
214: }
215:
216: // extends 1-dimensional array
217: private static long[] extendArray(long[] array, int extraLength) {
218: int originalLength = array.length;
219: long[] newArray = new long[originalLength + extraLength];
220: System.arraycopy(array, 0, newArray, 0, originalLength);
221:
222: return newArray;
223: }
224:
225: // extends 2-dimensional array
226: private static long[][] extendArray(long[][] array, int extraLength) {
227: int originalLength = array.length;
228: long[][] newArray = new long[originalLength + extraLength][];
229: System.arraycopy(array, 0, newArray, 0, originalLength);
230:
231: return newArray;
232: }
233: }
|