jdk/src/share/classes/sun/tools/jconsole/inspector/XPlottingViewer.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 699 afedf7cc9bfe
child 1233 3b08cf0cab53
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 699
diff changeset
     2
 * Copyright 2004-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
55
5ecee29e98d8 6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents: 2
diff changeset
    25
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole.inspector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.tools.jconsole.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
@SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class XPlottingViewer extends PlotterPanel implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // TODO: Make number of decimal places customizable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final int PLOTTER_DECIMALS = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private JButton plotButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // The plotter cache holds Plotter instances for the various attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static HashMap<String, XPlottingViewer> plotterCache =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        new HashMap<String, XPlottingViewer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     private static HashMap<String, Timer> timerCache =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
         new HashMap<String, Timer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private JPanel bordered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Number value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private MBeansTab tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private XMBean mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private String attributeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private JTable table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private XPlottingViewer(String key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                            XMBean mbean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                            String attributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                            Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                            JTable table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                            MBeansTab tab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.tab = tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.mbean = mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.table = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.attributeName = attributeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        Plotter plotter = createPlotter(mbean, attributeName, key, table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        setupDisplay(plotter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static void dispose(MBeansTab tab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        Iterator it = plotterCache.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        while(it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            String key = (String) it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if(key.startsWith(String.valueOf(tab.hashCode()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        //plotterCache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        it = timerCache.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        while(it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            String key = (String) it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if(key.startsWith(String.valueOf(tab.hashCode()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                Timer t = timerCache.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                t.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static boolean isViewableValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return (value instanceof Number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    //Fired by dbl click
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public  static Component loadPlotting(XMBean mbean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                          String attributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                          Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                          JTable table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                          MBeansTab tab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        Component comp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if(isViewableValue(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            String key = String.valueOf(tab.hashCode()) + " " + String.valueOf(mbean.hashCode()) + " " + mbean.getObjectName().getCanonicalName() + attributeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            XPlottingViewer plotter = plotterCache.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if(plotter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                plotter = new XPlottingViewer(key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                              mbean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                              attributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                              value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                              table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                              tab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                plotterCache.put(key, plotter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            comp = plotter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /*public void paintComponent(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        super.paintComponent(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        setBackground(g.getColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        plotter.paintComponent(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        plotterCache.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Timer t = timerCache.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        t.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        ((XMBeanAttributes) table).collapse(attributeName, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    //Create plotter instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public Plotter createPlotter(final XMBean xmbean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                 final String attributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                 String key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                 JTable table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        final Plotter plotter = new XPlotter(table, Plotter.Unit.NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                Dimension prefSize = new Dimension(400, 170);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    return prefSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    return prefSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        plotter.createSequence(attributeName, attributeName, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        TimerTask timerTask = new TimerTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    tab.workerAdd(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                    Number n =
699
afedf7cc9bfe 6714244: Plotters in MBeans tab should use SnapshotMBeanServerConnection too
lmalvent
parents: 55
diff changeset
   160
                                        (Number) xmbean.getSnapshotMBeanServerConnection().getAttribute(xmbean.getObjectName(), attributeName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                    long v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                    if (n instanceof Float || n instanceof Double) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                        plotter.setDecimals(PLOTTER_DECIMALS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                        double d = (n instanceof Float) ? (Float)n : (Double)n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                        v = Math.round(d * Math.pow(10.0, PLOTTER_DECIMALS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                        v = n.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                    plotter.addValues(System.currentTimeMillis(), v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                    // Should have a trace logged with proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                    // trace mecchanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        String timerName = "Timer-" + key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        Timer timer = new Timer(timerName, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        timer.schedule(timerTask, 0, tab.getUpdateInterval());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        timerCache.put(key, timer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return plotter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    //Create Plotter display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private void setupDisplay(Plotter plotter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        //setLayout(new GridLayout(2,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        GridBagLayout gbl = new GridBagLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        setLayout(gbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        plotButton = new JButton(Resources.getText("Discard chart"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        plotButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        plotButton.setEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Add the display to the top four cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        GridBagConstraints buttonConstraints = new GridBagConstraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        buttonConstraints.gridx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        buttonConstraints.gridy = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        buttonConstraints.fill = GridBagConstraints.VERTICAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        buttonConstraints.anchor = GridBagConstraints.CENTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        gbl.setConstraints(plotButton, buttonConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        add(plotButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        GridBagConstraints plotterConstraints = new GridBagConstraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        plotterConstraints.gridx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        plotterConstraints.gridy = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        plotterConstraints.weightx = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        //plotterConstraints.gridwidth = (int) plotter.getPreferredSize().getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        //plotterConstraints.gridheight =  (int) plotter.getPreferredSize().getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        plotterConstraints.fill = GridBagConstraints.VERTICAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        gbl.setConstraints(plotter, plotterConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        //bordered = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        //bordered.setPreferredSize(new Dimension(400, 250));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        //bordered.add(plotButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        //bordered.add(plotter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        //add(bordered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        setPlotter(plotter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
}