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;
042:
043: import org.netbeans.lib.profiler.results.ResultsSnapshot;
044: import org.netbeans.lib.profiler.results.coderegion.CodeRegionResultsSnapshot;
045: import org.netbeans.lib.profiler.ui.components.HTMLTextArea;
046: import org.netbeans.lib.profiler.utils.StringUtils;
047: import java.awt.*;
048: import java.text.MessageFormat;
049: import java.util.Date;
050: import java.util.ResourceBundle;
051: import javax.swing.*;
052:
053: /**
054: * A display for snapshot of code region profiling results
055: *
056: * @author Ian Formanek
057: */
058: public class CodeRegionSnapshotPanel extends JPanel {
059: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
060:
061: // -----
062: // I18N String constants
063: private static final ResourceBundle messages = ResourceBundle
064: .getBundle("org.netbeans.lib.profiler.ui.cpu.Bundle"); // NOI18N
065: private static final String PANEL_NAME = messages
066: .getString("CodeRegionSnapshotPanel_PanelName"); // NOI18N
067: private static final String NO_RESULTS_REGION_MSG = messages
068: .getString("CodeRegionSnapshotPanel_NoResultsRegionMsg"); // NOI18N
069: private static final String INDIVIDUAL_TIMES_MSG = messages
070: .getString("CodeRegionSnapshotPanel_IndividualTimesMsg"); // NOI18N
071: private static final String SUMMARY_TIMES_MSG = messages
072: .getString("CodeRegionSnapshotPanel_SummaryTimesMsg"); // NOI18N
073: private static final String TOTAL_INVOCATIONS_MSG = messages
074: .getString("CodeRegionSnapshotPanel_TotalInvocationsMsg"); // NOI18N
075: private static final String ALL_REMEMBERED_MSG = messages
076: .getString("CodeRegionSnapshotPanel_AllRememberedMsg"); // NOI18N
077: private static final String LAST_REMEMBERED_MSG = messages
078: .getString("CodeRegionSnapshotPanel_LastRememberedMsg"); // NOI18N
079: private static final String INVOCATIONS_LISTED_MSG = messages
080: .getString("CodeRegionSnapshotPanel_InvocationsListedMsg"); // NOI18N
081: private static final String AREA_ACCESS_NAME = messages
082: .getString("CodeRegionSnapshotPanel_AreaAccessName"); // NOI18N
083: // -----
084:
085: //~ Instance fields ----------------------------------------------------------------------------------------------------------
086:
087: private CodeRegionResultsSnapshot snapshot;
088:
089: //~ Constructors -------------------------------------------------------------------------------------------------------------
090:
091: public CodeRegionSnapshotPanel(CodeRegionResultsSnapshot snapshot) {
092: this .snapshot = snapshot;
093: setLayout(new BorderLayout());
094: setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
095:
096: long[] results = snapshot.getTimes();
097: long timerCountsInSecond = snapshot.getTimerCountsInSecond();
098: StringBuffer resultText = new StringBuffer(results.length * 10);
099: StringBuffer summaryOfTimes = new StringBuffer();
100: long sum = 0;
101: long min;
102: long max;
103:
104: if (results.length < 2) {
105: resultText.append("<i>" + NO_RESULTS_REGION_MSG + "</i>"); // NOI18N
106: } else {
107: min = max = results[1];
108:
109: int nRes = results.length - 1;
110:
111: StringBuffer individualTimes = new StringBuffer();
112:
113: for (int i = 1; i < results.length; i++) {
114: long time = results[i];
115: sum += time;
116:
117: if (time > max) {
118: max = time;
119: } else if (time < min) {
120: min = time;
121: }
122:
123: individualTimes.append(MessageFormat.format(
124: INDIVIDUAL_TIMES_MSG,
125: new Object[] { StringUtils
126: .mcsTimeToString((time * 1000000)
127: / timerCountsInSecond) }));
128: individualTimes.append("<br>"); // NOI18N
129: }
130:
131: summaryOfTimes
132: .append(MessageFormat
133: .format(
134: SUMMARY_TIMES_MSG,
135: new Object[] {
136: StringUtils
137: .mcsTimeToString((sum * 1000000)
138: / timerCountsInSecond), // total
139: StringUtils
140: .mcsTimeToString((long) (((double) sum * 1000000)
141: / nRes / timerCountsInSecond)), // average
142: StringUtils
143: .mcsTimeToString((min * 1000000)
144: / timerCountsInSecond), // minimum
145: StringUtils
146: .mcsTimeToString((max * 1000000)
147: / timerCountsInSecond) // maximum
148: }));
149:
150: resultText.append(MessageFormat.format(
151: TOTAL_INVOCATIONS_MSG, new Object[] { ""
152: + results[0] })); // NOI18N
153: resultText.append(", "); // NOI18N
154:
155: if (results[0] <= nRes) {
156: resultText.append(ALL_REMEMBERED_MSG);
157: } else {
158: resultText.append(MessageFormat
159: .format(LAST_REMEMBERED_MSG, new Object[] { ""
160: + nRes })); // NOI18N
161: }
162:
163: resultText.append("<br>"); // NOI18N
164: resultText.append(summaryOfTimes);
165: resultText.append("<br><br><hr><br>"); // NOI18N
166: resultText.append(individualTimes);
167: resultText.append("<br><hr><br>"); // NOI18N
168: resultText
169: .append(MessageFormat.format(
170: INVOCATIONS_LISTED_MSG, new Object[] { ""
171: + nRes })); // NOI18N
172: resultText.append(", "); // NOI18N
173: resultText.append(summaryOfTimes);
174: }
175:
176: HTMLTextArea resArea = new HTMLTextArea(resultText.toString());
177: resArea.getAccessibleContext().setAccessibleName(
178: AREA_ACCESS_NAME);
179: add(new JScrollPane(resArea), BorderLayout.CENTER);
180: }
181:
182: //~ Methods ------------------------------------------------------------------------------------------------------------------
183:
184: public ResultsSnapshot getSnapshot() {
185: return snapshot;
186: }
187:
188: public String getTitle() {
189: return MessageFormat.format(PANEL_NAME,
190: new Object[] { StringUtils.formatUserDate(new Date(
191: snapshot.getTimeTaken())) });
192: }
193: }
|