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.ProfilerEngineSettings;
044: import org.netbeans.lib.profiler.global.ProfilingSessionStatus;
045: import org.netbeans.lib.profiler.results.memory.MemoryCCTManager;
046: import org.netbeans.lib.profiler.results.memory.PresoObjAllocCCTNode;
047: import org.netbeans.lib.profiler.results.memory.PresoObjLivenessCCTNode;
048: import org.netbeans.lib.profiler.ui.UIConstants;
049: import org.netbeans.lib.profiler.ui.UIUtils;
050: import org.netbeans.lib.profiler.ui.components.*;
051: import org.netbeans.lib.profiler.ui.components.JTreeTable;
052: import org.netbeans.lib.profiler.ui.components.table.CustomBarCellRenderer;
053: import org.netbeans.lib.profiler.ui.components.treetable.AbstractTreeTableModel;
054: import org.netbeans.lib.profiler.ui.components.treetable.ExtendedTreeTableModel;
055: import org.netbeans.lib.profiler.ui.components.treetable.JTreeTablePanel;
056: import org.netbeans.lib.profiler.ui.components.treetable.TreeTableModel;
057: import org.netbeans.lib.profiler.utils.StringUtils;
058: import java.awt.*;
059: import java.awt.event.InputEvent;
060: import java.awt.event.MouseAdapter;
061: import java.awt.event.MouseEvent;
062: import java.util.ResourceBundle;
063: import javax.swing.*;
064: import javax.swing.table.TableColumnModel;
065:
066: /**
067: * A panel containing a reverse call graph for all allocations of instances of a given class
068: *
069: * No used at the moment!!!
070: *
071: * @author Misha Dmitriev
072: * @author Jiri Sedlacek
073: */
074: public class LiveReverseMemCallGraphPanel extends
075: ReverseMemCallGraphPanel {
076: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
077:
078: // -----
079: // I18N String constants
080: private static final ResourceBundle messages = ResourceBundle
081: .getBundle("org.netbeans.lib.profiler.ui.memory.Bundle"); // NOI18N
082: private static final String NO_STACKS_MSG = messages
083: .getString("LiveReverseMemCallGraphPanel_NoStacksMsg"); // NOI18N
084: private static final String TREETABLE_ACCESS_NAME = messages
085: .getString("LiveReverseMemCallGraphPanel_TreeTableAccessName"); // NOI18N
086: // -----
087:
088: //~ Instance fields ----------------------------------------------------------------------------------------------------------
089:
090: protected MemoryCCTManager callGraphManager;
091: protected ProfilingSessionStatus status;
092: protected int classId;
093: private AbstractTreeTableModel abstractTreeTableModel;
094:
095: //~ Constructors -------------------------------------------------------------------------------------------------------------
096:
097: public LiveReverseMemCallGraphPanel(ProfilingSessionStatus status,
098: MemoryResUserActionsHandler actionsHandler) {
099: super (
100: actionsHandler,
101: status.currentInstrType == ProfilerEngineSettings.INSTR_OBJECT_LIVENESS);
102: this .status = status;
103: }
104:
105: //~ Methods ------------------------------------------------------------------------------------------------------------------
106:
107: public void setCallGraph(MemoryCCTManager callGraphManager,
108: int classId) {
109: this .callGraphManager = callGraphManager;
110: this .classId = classId;
111:
112: if (!callGraphManager.isEmpty()) {
113: customBarCellRenderer = new CustomBarCellRenderer(0,
114: ((PresoObjAllocCCTNode) callGraphManager
115: .getRootNode()).totalObjSize);
116: columnRenderers[1] = customBarCellRenderer;
117: }
118: }
119:
120: public void prepareResults() {
121: if (callGraphManager.isEmpty()) {
122: removeAll();
123: add(new JLabel(NO_STACKS_MSG), BorderLayout.CENTER);
124: } else {
125: abstractTreeTableModel = new AbstractTreeTableModel(
126: callGraphManager.getRootNode(), 1, false) {
127: public int getColumnCount() {
128: return columnNames.length;
129: }
130:
131: public String getColumnName(int column) {
132: return columnNames[column];
133: }
134:
135: public Class getColumnClass(int column) {
136: if (column == 0) {
137: return TreeTableModel.class;
138: } else {
139: return Object.class;
140: }
141: }
142:
143: public Object getValueAt(Object node, int column) {
144: long value;
145:
146: if (extendedResults) {
147: PresoObjLivenessCCTNode pNode = (PresoObjLivenessCCTNode) node;
148:
149: switch (column) {
150: case 0:
151: return pNode.toString();
152: case 1:
153: return new Long(pNode.totalObjSize);
154: case 2:
155: value = ((PresoObjLivenessCCTNode) root).totalObjSize;
156:
157: return intFormat.format(pNode.totalObjSize)
158: + " B ("
159: + ((value == 0) ? "-%"
160: : percentFormat
161: .format((float) pNode.totalObjSize
162: / (float) value))
163: + ")"; // NOI18N
164: case 3:
165: value = ((PresoObjLivenessCCTNode) root).nLiveObjects;
166:
167: return intFormat.format(pNode.nLiveObjects)
168: + " ("
169: + ((value == 0) ? "-%"
170: : percentFormat
171: .format((float) pNode.nLiveObjects
172: / (float) value))
173: + ")"; // NOI18N
174: case 4:
175: return intFormat.format(pNode.nCalls);
176: case 5:
177: return StringUtils
178: .floatPerCentToString(pNode.avgObjectAge);
179: case 6:
180: return intFormat.format(pNode.survGen);
181: }
182: } else {
183: PresoObjAllocCCTNode pNode = (PresoObjAllocCCTNode) node;
184:
185: switch (column) {
186: case 0:
187: return pNode.getNodeName();
188: case 1:
189: return new Long(pNode.totalObjSize);
190: case 2:
191: value = ((PresoObjAllocCCTNode) root).totalObjSize;
192:
193: return intFormat.format(pNode.totalObjSize)
194: + " B ("
195: + ((value == 0) ? "-%"
196: : percentFormat
197: .format((float) pNode.totalObjSize
198: / (float) value))
199: + ")"; // NOI18N
200: case 3:
201: value = ((PresoObjAllocCCTNode) root).nCalls;
202:
203: return intFormat.format(pNode.nCalls)
204: + " ("
205: + ((value == 0) ? "-%"
206: : percentFormat
207: .format((float) pNode.nCalls
208: / (float) value))
209: + ")"; // NOI18N
210: }
211: }
212:
213: return null;
214: }
215:
216: public String getColumnToolTipText(int col) {
217: return columnToolTips[col];
218: }
219:
220: public void sortByColumn(int column, boolean order) {
221: if (extendedResults) {
222: PresoObjLivenessCCTNode pRoot = (PresoObjLivenessCCTNode) root;
223:
224: switch (column) {
225: case 0:
226: pRoot
227: .sortChildren(
228: PresoObjLivenessCCTNode.SORT_BY_NAME,
229: order);
230:
231: break;
232: case 1:
233: case 2:
234: pRoot
235: .sortChildren(
236: PresoObjLivenessCCTNode.SORT_BY_LIVE_OBJ_SIZE,
237: order);
238:
239: break;
240: case 3:
241: pRoot
242: .sortChildren(
243: PresoObjLivenessCCTNode.SORT_BY_LIVE_OBJ_NUMBER,
244: order);
245:
246: break;
247: case 4:
248: pRoot
249: .sortChildren(
250: PresoObjLivenessCCTNode.SORT_BY_ALLOC_OBJ,
251: order);
252:
253: break;
254: case 5:
255: pRoot
256: .sortChildren(
257: PresoObjLivenessCCTNode.SORT_BY_AVG_AGE,
258: order);
259:
260: break;
261: case 6:
262: pRoot
263: .sortChildren(
264: PresoObjLivenessCCTNode.SORT_BY_SURV_GEN,
265: order);
266:
267: break;
268: }
269: } else {
270: PresoObjAllocCCTNode pRoot = (PresoObjAllocCCTNode) root;
271:
272: switch (column) {
273: case 0:
274: pRoot.sortChildren(
275: PresoObjAllocCCTNode.SORT_BY_NAME,
276: order);
277:
278: break;
279: case 1:
280: case 2:
281: pRoot
282: .sortChildren(
283: PresoObjAllocCCTNode.SORT_BY_ALLOC_OBJ_SIZE,
284: order);
285:
286: break;
287: case 3:
288: pRoot
289: .sortChildren(
290: PresoObjAllocCCTNode.SORT_BY_ALLOC_OBJ_NUMBER,
291: order);
292:
293: break;
294: }
295: }
296: };
297:
298: public boolean getInitialSorting(int column) {
299: switch (column) {
300: case 0:
301: return true;
302: default:
303: return false;
304: }
305: }
306: };
307:
308: treeTableModel = new ExtendedTreeTableModel(
309: abstractTreeTableModel);
310:
311: treeTable = new JTreeTable(treeTableModel) {
312: public void doLayout() {
313: int columnsWidthsSum = 0;
314: int realFirstColumn = -1;
315:
316: int index;
317: TableColumnModel colModel = getColumnModel();
318:
319: for (int i = 0; i < treeTableModel.getColumnCount(); i++) {
320: index = treeTableModel.getRealColumn(i);
321:
322: if (index == 0) {
323: realFirstColumn = i;
324: } else {
325: columnsWidthsSum += colModel.getColumn(i)
326: .getPreferredWidth();
327: }
328: }
329:
330: if (realFirstColumn != -1) {
331: colModel.getColumn(realFirstColumn)
332: .setPreferredWidth(
333: Math.max(getWidth()
334: - columnsWidthsSum,
335: minNamesColumnWidth));
336: }
337:
338: super .doLayout();
339: };
340: };
341: treeTable.getAccessibleContext().setAccessibleName(
342: TREETABLE_ACCESS_NAME);
343:
344: treeTable.setRowSelectionAllowed(true);
345: treeTable
346: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
347: treeTable
348: .setGridColor(UIConstants.TABLE_VERTICAL_GRID_COLOR);
349: treeTable
350: .setSelectionBackground(UIConstants.TABLE_SELECTION_BACKGROUND_COLOR);
351: treeTable
352: .setSelectionForeground(UIConstants.TABLE_SELECTION_FOREGROUND_COLOR);
353: treeTable
354: .setShowHorizontalLines(UIConstants.SHOW_TABLE_HORIZONTAL_GRID);
355: treeTable
356: .setShowVerticalLines(UIConstants.SHOW_TABLE_VERTICAL_GRID);
357: treeTable.setRowMargin(UIConstants.TABLE_ROW_MARGIN);
358: treeTable.setRowHeight(UIUtils.getDefaultRowHeight() + 2);
359:
360: setColumnsData();
361:
362: UIUtils.autoExpandRoot(treeTable.getTree());
363: UIUtils.makeTreeAutoExpandable(treeTable.getTree());
364:
365: treeTable.addMouseListener(new MouseAdapter() {
366: public void mousePressed(MouseEvent e) {
367: if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
368: treePath = treeTable.getTree().getPathForRow(
369: treeTable.rowAtPoint(e.getPoint()));
370:
371: if (treePath != null) {
372: treeTable.getTree().setSelectionPath(
373: treePath);
374: }
375: }
376: }
377:
378: public void mouseClicked(MouseEvent e) {
379: treePath = treeTable.getTree().getPathForRow(
380: treeTable.rowAtPoint(e.getPoint()));
381:
382: if (treePath == null) {
383: if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
384: treeTable.getTree().clearSelection();
385: }
386: } else {
387: if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
388: popupMenu.show(e.getComponent(), e.getX(),
389: e.getY());
390: } else if ((e.getModifiers() == InputEvent.BUTTON1_MASK)
391: && (e.getClickCount() == 2)) {
392: if (treeTableModel
393: .isLeaf(treePath.getPath()[treePath
394: .getPath().length - 1])) {
395: performDefaultAction(treePath);
396: }
397: }
398: }
399: }
400: });
401:
402: removeAll();
403: treeTablePanel = new JTreeTablePanel(treeTable);
404: treeTablePanel.setCorner(JScrollPane.UPPER_RIGHT_CORNER,
405: cornerButton);
406: add(treeTablePanel, BorderLayout.CENTER);
407: }
408: }
409: }
|