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.memory;
042:
043: import org.netbeans.lib.profiler.results.ResultsSnapshot;
044: import org.netbeans.lib.profiler.results.memory.AllocMemoryResultsSnapshot;
045: import org.netbeans.lib.profiler.ui.UIUtils;
046: import java.awt.*;
047: import java.awt.event.ActionEvent;
048: import java.awt.event.ActionListener;
049: import java.util.ResourceBundle;
050: import javax.swing.*;
051:
052: /**
053: * This class implements presentation frames for Object Allocation Profiling.
054: *
055: * @author Ian Formanek
056: */
057: public class SnapshotAllocResultsPanel extends AllocResultsPanel
058: implements ActionListener {
059: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
060:
061: // -----
062: // I18N String constants
063: private static final ResourceBundle messages = ResourceBundle
064: .getBundle("org.netbeans.lib.profiler.ui.memory.Bundle"); // NOI18N
065: private static final String GO_SOURCE_POPUP_ITEM_NAME = messages
066: .getString("AllocResultsPanel_GoSourcePopupItemName"); // NOI18N
067: private static final String SHOW_STACK_TRACES_POPUP_ITEM_NAME = messages
068: .getString("AllocResultsPanel_ShowStackTracesPopupItemName"); // NOI18N
069: // -----
070:
071: //~ Instance fields ----------------------------------------------------------------------------------------------------------
072:
073: private AllocMemoryResultsSnapshot snapshot;
074: private JMenuItem popupShowSource;
075: private JMenuItem popupShowStacks;
076: private JPopupMenu memoryResPopupMenu;
077:
078: //~ Constructors -------------------------------------------------------------------------------------------------------------
079:
080: public SnapshotAllocResultsPanel(
081: AllocMemoryResultsSnapshot snapshot,
082: MemoryResUserActionsHandler actionsHandler) {
083: super (actionsHandler);
084: this .snapshot = snapshot;
085:
086: fetchResultsFromSnapshot();
087:
088: //prepareResults();
089: }
090:
091: //~ Methods ------------------------------------------------------------------------------------------------------------------
092:
093: public ResultsSnapshot getSnapshot() {
094: return snapshot;
095: }
096:
097: public void actionPerformed(ActionEvent e) {
098: if (e.getSource() == popupShowStacks) {
099: actionsHandler.showStacksForClass(selectedClassId,
100: getSortingColumn(), getSortingOrder());
101: } else if (e.getSource() == popupShowSource) {
102: showSourceForClass(selectedClassId);
103: }
104: }
105:
106: protected String getClassName(int classId) {
107: return snapshot.getClassName(classId);
108: }
109:
110: protected String[] getClassNames() {
111: return snapshot.getClassNames();
112: }
113:
114: protected JPopupMenu getPopupMenu() {
115: if (memoryResPopupMenu == null) {
116: memoryResPopupMenu = new JPopupMenu();
117:
118: Font boldfont = memoryResPopupMenu.getFont().deriveFont(
119: Font.BOLD);
120:
121: popupShowSource = new JMenuItem();
122: popupShowSource.setFont(boldfont);
123: popupShowSource.setText(GO_SOURCE_POPUP_ITEM_NAME);
124: memoryResPopupMenu.add(popupShowSource);
125: popupShowSource.addActionListener(this );
126:
127: if (snapshot.containsStacks()) {
128: memoryResPopupMenu.addSeparator();
129: popupShowStacks = new JMenuItem();
130: popupShowStacks
131: .setText(SHOW_STACK_TRACES_POPUP_ITEM_NAME);
132: memoryResPopupMenu.add(popupShowStacks);
133: popupShowStacks.addActionListener(this );
134: }
135: }
136:
137: return memoryResPopupMenu;
138: }
139:
140: private void fetchResultsFromSnapshot() {
141: totalAllocObjectsSize = UIUtils.copyArray(snapshot
142: .getObjectsSizePerClass());
143: nTotalAllocObjects = UIUtils.copyArray(snapshot
144: .getObjectsCounts());
145:
146: // In some situations nInstrClasses can be already updated, but nTotalAllocObjects.length and/ort totalAllocObjectsSize - not yet.
147: // Take measures to avoid ArrayIndexOutOfBoundsException.
148: nTrackedItems = snapshot.getNProfiledClasses();
149:
150: if (nTrackedItems > nTotalAllocObjects.length) {
151: nTrackedItems = nTotalAllocObjects.length;
152: }
153:
154: if (nTrackedItems > totalAllocObjectsSize.length) {
155: nTrackedItems = totalAllocObjectsSize.length;
156: }
157:
158: // Operations necessary for correct bar representation of results
159: maxValue = 0;
160: nTotalBytes = 0;
161: nTotalClasses = 0;
162:
163: for (int i = 0; i < nTrackedItems; i++) {
164: if (maxValue < totalAllocObjectsSize[i]) {
165: maxValue = totalAllocObjectsSize[i];
166: }
167:
168: nTotalBytes += totalAllocObjectsSize[i];
169: nTotalClasses += nTotalAllocObjects[i];
170: }
171:
172: initDataUponResultsFetch();
173: }
174: }
|