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.cpu.statistics.drilldown;
042:
043: import org.netbeans.lib.profiler.results.cpu.marking.Mark;
044: import org.netbeans.lib.profiler.ui.charts.AbstractPieChartModel;
045: import java.awt.Color;
046: import java.util.Iterator;
047: import java.util.List;
048:
049: /**
050: *
051: * @author Jaroslav Bachorik
052: */
053: public abstract class DrillDownPieChartModel extends
054: AbstractPieChartModel implements DrillDownListener {
055: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
056:
057: private static Color[] COLORS = new Color[] { new Color(0x99ff99),
058: new Color(0x99cc99), new Color(0x666633),
059: new Color(0x336666), new Color(0x6699cc),
060: new Color(0x9999cc), new Color(0xffccff),
061: new Color(0xcc9999), new Color(0x660099),
062: new Color(0x006600) };
063:
064: //~ Instance fields ----------------------------------------------------------------------------------------------------------
065:
066: protected IDrillDown drillDown;
067: private Object itemMapLock = new Object();
068: private int[] itemMap = null;
069: private int itemCount = 0;
070:
071: //~ Constructors -------------------------------------------------------------------------------------------------------------
072:
073: /** Creates a new instance of DrillDownPieChartModel */
074: public DrillDownPieChartModel(IDrillDown model) {
075: setDrillDown(model);
076: }
077:
078: //~ Methods ------------------------------------------------------------------------------------------------------------------
079:
080: public void setDrillDown(IDrillDown model) {
081: if (drillDown != null) {
082: drillDown.removeListener(this );
083: }
084:
085: drillDown = model;
086: updateItemMap();
087: drillDown.addListener(this );
088: }
089:
090: @Override
091: public Color getItemColor(int index) {
092: if (index == -1) {
093: return Color.BLACK; // illegal index
094: }
095:
096: return COLORS[index % COLORS.length];
097: }
098:
099: @Override
100: public int getItemCount() {
101: synchronized (itemMapLock) {
102: return itemCount;
103: }
104: }
105:
106: public String getItemDescription(int index) {
107: if ((index == -1) || (getMappedIndex(index) == -1)) {
108: return ""; // illegal index // NOI18N
109: }
110:
111: return ((Mark) drillDown.getSubmarks().get(
112: getMappedIndex(index))).description;
113: }
114:
115: public void dataChanged() {
116: synchronized (itemMapLock) {
117: updateItemMap();
118: }
119:
120: fireChartDataChanged();
121: }
122:
123: public void drillDownPathChanged(List newDrillDownPath) {
124: synchronized (itemMapLock) {
125: updateItemMap();
126: }
127:
128: fireChartDataChanged();
129: }
130:
131: public void drilldown(int index) {
132: if ((index == -1) || (getMappedIndex(index) == -1)) {
133: return; // illegal index
134: }
135:
136: if (drillDown.getSubmarks().isEmpty()) {
137: return;
138: }
139:
140: drillDown.drilldown((Mark) drillDown.getSubmarks().get(
141: getMappedIndex(index)));
142: }
143:
144: public void drillup() {
145: drillDown.drillup();
146: }
147:
148: public void drillup(int index) {
149: if ((index == -1) || (getMappedIndex(index) == -1)) {
150: return; // illegal index
151: }
152:
153: drillDown.drillup((Mark) drillDown.getSubmarks().get(
154: getMappedIndex(index)));
155: }
156:
157: @Override
158: public boolean hasData() {
159: synchronized (itemMapLock) {
160: return (drillDown != null) && (itemCount > 0);
161: }
162: }
163:
164: protected int getMappedIndex(int index) {
165: synchronized (itemMapLock) {
166: if ((index < 0) || (index >= itemMap.length)) {
167: return -1; // check for boundaries
168: }
169:
170: return itemMap[index];
171: }
172: }
173:
174: private void updateItemMap() {
175: synchronized (itemMapLock) {
176: int counter = 0;
177: int mapCounter = 0;
178: int[] map = new int[drillDown.getSubmarks().size()];
179:
180: for (Iterator it = drillDown.getSubmarks().iterator(); it
181: .hasNext(); counter++) {
182: Mark mark = (Mark) it.next();
183:
184: if (drillDown.getMarkTime(mark, false) > 0) {
185: map[mapCounter++] = counter;
186: }
187: }
188:
189: itemCount = (mapCounter > 0) ? mapCounter : 0;
190: itemMap = map;
191:
192: // if (itemCount > 0) {
193: // int surplus = 0;
194: // if (drillDown.getCurrentTime(true) > 0) {
195: // surplus = 1;
196: // }
197: // itemMap = new int[itemCount + surplus];
198: // System.arraycopy(map, 0, itemMap, 0, itemCount);
199: // if (surplus > 0) {
200: // itemMap[itemCount + surplus - 1] = -1;
201: // }
202: // itemCount += surplus;
203: // } else {
204: // itemMap = new int[0];
205: // }
206: }
207: }
208: }
|