Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/ClassTab.java
40948 views
/*1* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jconsole;2627import java.awt.*;28import java.awt.event.*;29import java.io.*;30import java.lang.management.*;31import java.lang.reflect.*;3233import javax.swing.*;34import javax.swing.border.*;353637import java.util.concurrent.*;3839import static sun.tools.jconsole.Formatter.*;40import static sun.tools.jconsole.Utilities.*;414243@SuppressWarnings("serial")44class ClassTab extends Tab implements ActionListener {45PlotterPanel loadedClassesMeter;46TimeComboBox timeComboBox;47private JCheckBox verboseCheckBox;48private HTMLPane details;49private ClassOverviewPanel overviewPanel;50private boolean plotterListening = false;5152private static final String loadedPlotterKey = "loaded";53private static final String totalLoadedPlotterKey = "totalLoaded";54private static final Color loadedPlotterColor = Plotter.defaultColor;55private static final Color totalLoadedPlotterColor = Color.red;5657/*58Hierarchy of panels and layouts for this tab:5960ClassTab (BorderLayout)6162North: topPanel (BorderLayout)6364Center: controlPanel (FlowLayout)65timeComboBox6667East: topRightPanel (FlowLayout)68verboseCheckBox6970Center: plotterPanel (BorderLayout)7172Center: plotter7374South: bottomPanel (BorderLayout)7576Center: details77*/7879public static String getTabName() {80return Messages.CLASSES;81}8283public ClassTab(VMPanel vmPanel) {84super(vmPanel, getTabName());8586setLayout(new BorderLayout(0, 0));87setBorder(new EmptyBorder(4, 4, 3, 4));8889JPanel topPanel = new JPanel(new BorderLayout());90JPanel plotterPanel = new JPanel(new BorderLayout());91JPanel bottomPanel = new JPanel(new BorderLayout());9293add(topPanel, BorderLayout.NORTH);94add(plotterPanel, BorderLayout.CENTER);95add(bottomPanel, BorderLayout.SOUTH);9697JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5));98topPanel.add(controlPanel, BorderLayout.CENTER);99100verboseCheckBox = new JCheckBox(Messages.VERBOSE_OUTPUT);101verboseCheckBox.addActionListener(this);102verboseCheckBox.setToolTipText(Messages.VERBOSE_OUTPUT_TOOLTIP);103JPanel topRightPanel = new JPanel();104topRightPanel.setBorder(new EmptyBorder(0, 65-8, 0, 70));105topRightPanel.add(verboseCheckBox);106topPanel.add(topRightPanel, BorderLayout.AFTER_LINE_ENDS);107108loadedClassesMeter = new PlotterPanel(Messages.NUMBER_OF_LOADED_CLASSES,109Plotter.Unit.NONE, false);110loadedClassesMeter.plotter.createSequence(loadedPlotterKey,111Messages.LOADED,112loadedPlotterColor,113true);114loadedClassesMeter.plotter.createSequence(totalLoadedPlotterKey,115Messages.TOTAL_LOADED,116totalLoadedPlotterColor,117true);118setAccessibleName(loadedClassesMeter.plotter,119Messages.CLASS_TAB_LOADED_CLASSES_PLOTTER_ACCESSIBLE_NAME);120plotterPanel.add(loadedClassesMeter);121122timeComboBox = new TimeComboBox(loadedClassesMeter.plotter);123controlPanel.add(new LabeledComponent(Messages.TIME_RANGE_COLON,124Resources.getMnemonicInt(Messages.TIME_RANGE_COLON),125timeComboBox));126127LabeledComponent.layout(plotterPanel);128129bottomPanel.setBorder(new CompoundBorder(new TitledBorder(Messages.DETAILS),130new EmptyBorder(10, 10, 10, 10)));131132details = new HTMLPane();133setAccessibleName(details, Messages.DETAILS);134JScrollPane scrollPane = new JScrollPane(details);135scrollPane.setPreferredSize(new Dimension(0, 150));136bottomPanel.add(scrollPane, BorderLayout.SOUTH);137138}139140public void actionPerformed(ActionEvent ev) {141final boolean b = verboseCheckBox.isSelected();142workerAdd(new Runnable() {143public void run() {144ProxyClient proxyClient = vmPanel.getProxyClient();145try {146proxyClient.getClassLoadingMXBean().setVerbose(b);147} catch (UndeclaredThrowableException e) {148proxyClient.markAsDead();149} catch (IOException ex) {150// Ignore151}152}153});154}155156157public SwingWorker<?, ?> newSwingWorker() {158final ProxyClient proxyClient = vmPanel.getProxyClient();159160if (!plotterListening) {161proxyClient.addWeakPropertyChangeListener(loadedClassesMeter.plotter);162plotterListening = true;163}164165return new SwingWorker<Boolean, Object>() {166private long clCount, cuCount, ctCount;167private boolean isVerbose;168private String detailsStr;169private long timeStamp;170171public Boolean doInBackground() {172try {173ClassLoadingMXBean classLoadingMBean = proxyClient.getClassLoadingMXBean();174175clCount = classLoadingMBean.getLoadedClassCount();176cuCount = classLoadingMBean.getUnloadedClassCount();177ctCount = classLoadingMBean.getTotalLoadedClassCount();178isVerbose = classLoadingMBean.isVerbose();179detailsStr = formatDetails();180timeStamp = System.currentTimeMillis();181182return true;183} catch (UndeclaredThrowableException e) {184proxyClient.markAsDead();185return false;186} catch (IOException e) {187return false;188}189}190191protected void done() {192try {193if (get()) {194loadedClassesMeter.plotter.addValues(timeStamp, clCount, ctCount);195196if (overviewPanel != null) {197overviewPanel.updateClassInfo(ctCount, clCount);198overviewPanel.getPlotter().addValues(timeStamp, clCount);199}200201loadedClassesMeter.setValueLabel(clCount + "");202verboseCheckBox.setSelected(isVerbose);203details.setText(detailsStr);204}205} catch (InterruptedException ex) {206} catch (ExecutionException ex) {207if (JConsole.isDebug()) {208ex.printStackTrace();209}210}211}212213private String formatDetails() {214String text = "<table cellspacing=0 cellpadding=0>";215216long time = System.currentTimeMillis();217String timeStamp = formatDateTime(time);218text += newRow(Messages.TIME, timeStamp);219text += newRow(Messages.CURRENT_CLASSES_LOADED, justify(clCount, 5));220text += newRow(Messages.TOTAL_CLASSES_LOADED, justify(ctCount, 5));221text += newRow(Messages.TOTAL_CLASSES_UNLOADED, justify(cuCount, 5));222223return text;224}225};226}227228229OverviewPanel[] getOverviewPanels() {230if (overviewPanel == null) {231overviewPanel = new ClassOverviewPanel();232}233return new OverviewPanel[] { overviewPanel };234}235236private static class ClassOverviewPanel extends OverviewPanel {237ClassOverviewPanel() {238super(Messages.CLASSES, loadedPlotterKey, Messages.LOADED, null);239}240241private void updateClassInfo(long total, long loaded) {242long unloaded = (total - loaded);243getInfoLabel().setText(Resources.format(Messages.CLASS_TAB_INFO_LABEL_FORMAT,244loaded, unloaded, total));245}246}247}248249250