001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package view;
028:
029: import javax.swing.*;
030: import javax.swing.table.*;
031: import java.net.*;
032: import java.awt.*;
033: import java.awt.event.*;
034: import com.sun.cldchi.tools.memoryprofiler.data.JavaObject;
035: import com.sun.cldchi.tools.memoryprofiler.data.MPDataProvider;
036: import javax.swing.table.*;
037: import javax.swing.event.*;
038:
039: public class ViewObjectsPanel extends JPanel {
040: private JLabel address_label = new JLabel("address");
041: private JTextField address_field = new JTextField();
042: private JLabel type_label = new JLabel("type");
043: private JTextField type_field = new JTextField();
044: private JTable references = new ObjectListTable(
045: new ObjectListTableModel());
046: private JTable referees = new ObjectListTable(
047: new ObjectListTableModel());
048: private JavaObject _obj = null;
049: private MPDataProvider _provider;
050: private JButton _root_path;
051: private JList _object_list;
052: private JButton _stack_location;
053: private Object[] _objects;
054:
055: public ViewObjectsPanel(MPDataProvider provider) {
056: _provider = provider;
057: }
058:
059: public void initUI(boolean add_show_root_path_button) {
060: setLayout(new GridBagLayout());
061: JPanel top_panel = new JPanel();
062: top_panel.add(address_label);
063: top_panel.add(address_field);
064: address_field.setEditable(false);
065: type_field.setEditable(false);
066: address_field.setPreferredSize(new Dimension(80, 20));
067: type_field.setPreferredSize(new Dimension(120, 20));
068:
069: top_panel.add(type_label);
070: top_panel.add(type_field);
071: if (add_show_root_path_button) {
072: _root_path = new JButton("Show path from the root") {
073: public Dimension getPreferredSize() {
074: return new Dimension(160, 20);
075: }
076: };
077: _root_path.addActionListener(new ShowRootPathListener());
078:
079: _root_path.setFont(_root_path.getFont().deriveFont(9.0f));
080: _root_path.setEnabled(false);
081: top_panel.add(_root_path);
082: } else { //this is show path from the root object
083: _stack_location = new JButton("Show stack trace") {
084: public Dimension getPreferredSize() {
085: return new Dimension(160, 20);
086: }
087: };
088: _stack_location
089: .addActionListener(new ShowStackTraceListener());
090:
091: _stack_location.setFont(_stack_location.getFont()
092: .deriveFont(9.0f));
093: _stack_location.setEnabled(false);
094: top_panel.add(_stack_location);
095:
096: }
097: add(top_panel, new GridBagConstraints(0, 0, 1, 1, 1, 1,
098: GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
099: new Insets(2, 2, 2, 2), 0, 0));
100: JPanel bottom_panel = new JPanel();
101: JScrollPane pane = new JScrollPane() {
102: public Dimension getPreferredSize() {
103: return new Dimension(100, 220);
104: }
105: };
106: _object_list = new JList();
107: _object_list
108: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
109: _object_list
110: .addListSelectionListener(new ObjectListSelectionListener());
111: pane.getViewport().setView(_object_list);
112:
113: bottom_panel.setLayout(new GridBagLayout());
114: bottom_panel.add(pane, new GridBagConstraints(0, 0, 1, 2, 1, 1,
115: GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
116: new Insets(2, 2, 2, 2), 0, 0));
117: bottom_panel.add(new JLabel("Referees"),
118: new GridBagConstraints(1, 0, 1, 1, 1, 1,
119: GridBagConstraints.NORTHWEST,
120: GridBagConstraints.NONE,
121: new Insets(2, 2, 2, 2), 0, 0));
122: bottom_panel.add(new JLabel("References"),
123: new GridBagConstraints(2, 0, 1, 1, 1, 1,
124: GridBagConstraints.NORTHWEST,
125: GridBagConstraints.NONE,
126: new Insets(2, 2, 2, 2), 0, 0));
127: pane = new JScrollPane() {
128: public Dimension getPreferredSize() {
129: return new Dimension(220, 200);
130: }
131: };
132: pane.getViewport().setView(referees);
133: bottom_panel.add(pane, new GridBagConstraints(1, 1, 1, 1, 1,
134: 21, GridBagConstraints.NORTHWEST,
135: GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
136: pane = new JScrollPane() {
137: public Dimension getPreferredSize() {
138: return new Dimension(220, 200);
139: }
140: };
141: pane.getViewport().setView(references);
142: bottom_panel.add(pane, new GridBagConstraints(2, 1, 1, 1, 1,
143: 21, GridBagConstraints.NORTHWEST,
144: GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
145:
146: add(bottom_panel, new GridBagConstraints(0, 1, 1, 1, 1, 20,
147: GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
148: new Insets(2, 2, 2, 2), 0, 0));
149:
150: }
151:
152: private void setObject(JavaObject obj) {
153: _obj = obj;
154: ObjectListTableModel refs = (ObjectListTableModel) references
155: .getModel();
156: ObjectListTableModel refes = (ObjectListTableModel) referees
157: .getModel();
158: if (obj != null) {
159: address_field.setText(obj.toString());
160: type_field.setText(_provider.getObjectTypeName(obj));
161: refs.setData(obj.get_references());
162: refes.setData(obj.get_referees());
163: if (_root_path != null) {
164: _root_path.setEnabled(obj.getRootDistance() != -1);
165: }
166: if (_stack_location != null) {
167: _stack_location
168: .setEnabled(obj.object_type == MPDataProvider.STACK_OBJECT);
169: }
170: } else {
171: address_field.setText("");
172: type_field.setText("");
173: refs.setData(null);
174: refes.setData(null);
175: if (_root_path != null) {
176: _root_path.setEnabled(false);
177: }
178: }
179: refs.fireTableDataChanged();
180: refes.fireTableDataChanged();
181: }
182:
183: public void setObjects(Object[] objects) {
184: _object_list.setListData(objects);
185: _objects = objects;
186: repaint();
187: }
188:
189: class ObjectListTableModel extends AbstractTableModel {
190: private Object[] _data;
191:
192: public void setData(Object[] data) {
193: _data = data;
194: }
195:
196: public int getColumnCount() {
197: return 2;
198: }
199:
200: public Object getValueAt(int row, int col) {
201: if (_data == null)
202: return "y";
203: if (row >= _data.length)
204: return "z";
205: if (col == 0) {
206: return _data[row].toString();
207: } else if (col == 1) {
208: return _provider
209: .getObjectTypeName((JavaObject) _data[row]);
210: } else {
211: throw new RuntimeException();
212: }
213: }
214:
215: public int getRowCount() {
216: if (_data == null) {
217: return 0;
218: } else {
219: return _data.length;
220: }
221: }
222:
223: public String getColumnName(int i) {
224: if (i == 0) {
225: return "address";
226: } else if (i == 1) {
227: return "type";
228: } else {
229: throw new RuntimeException("Should not reach here!");
230: }
231: }
232: }
233:
234: class ObjectListTable extends JTable {
235: public ObjectListTable(TableModel model) {
236: super (model);
237: }
238:
239: public String getToolTipText(MouseEvent e) {
240: int row = rowAtPoint(e.getPoint());
241: int col = columnAtPoint(e.getPoint());
242: return (String) getModel().getValueAt(row, col);
243: }
244: }
245:
246: class ShowRootPathListener implements ActionListener {
247: public void actionPerformed(ActionEvent e) {
248: Object[] objs = _provider.pathFromTheRoot(_obj);
249: ViewRootPathDialog.showDialog(ViewObjectsPanel.this , objs,
250: "Path from the Root", _provider);
251: }
252: }
253:
254: class ObjectListSelectionListener implements ListSelectionListener {
255: public void valueChanged(ListSelectionEvent e) {
256: JavaObject item = (JavaObject) _object_list
257: .getSelectedValue();
258: setObject(item);
259: }
260: }
261:
262: class ShowStackTraceListener implements ActionListener {
263: public void actionPerformed(ActionEvent e) {
264: for (int i = 0; i < _objects.length - 1; i++) {
265: if (_objects[i] == _obj) {
266: int ptr = ((JavaObject) _objects[i + 1]).address;
267: String stackTrace = null;
268: try {
269: stackTrace = _provider.getStackTrace(
270: (JavaObject) _objects[i], ptr);
271: } catch (SocketException ex) {
272: //IMPL_NOTE: need to handle it
273: }
274: JOptionPane.showMessageDialog(null, stackTrace,
275: "Object location StackTrace",
276: JOptionPane.PLAIN_MESSAGE);
277: }
278: }
279: }
280: }
281: }
|