jdk/src/share/classes/sun/tools/jconsole/JConsole.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole;
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.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.security.auth.login.FailedLoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.net.ssl.SSLHandshakeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.jconsole.JConsolePlugin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.net.util.IPAddressUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import static sun.tools.jconsole.Resources.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import static sun.tools.jconsole.Utilities.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
@SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class JConsole extends JFrame
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    implements ActionListener, InternalFrameListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static /*final*/ boolean IS_GTK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static /*final*/ boolean IS_WIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // Apply the system L&F if it is GTK or Windows, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // the L&F is not specified using a system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (System.getProperty("swing.defaultlaf") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            String systemLaF = UIManager.getSystemLookAndFeelClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (systemLaF.equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                systemLaF.equals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    UIManager.setLookAndFeel(systemLaF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    System.err.println(Resources.getText("JConsole: ", e.getMessage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        updateLafValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static void updateLafValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        String lafName = UIManager.getLookAndFeel().getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        IS_GTK = lafName.equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        IS_WIN = lafName.equals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        //BorderedComponent.updateLafValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private final static String title =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Resources.getText("Java Monitoring & Management Console");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public final static String ROOT_URL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        "service:jmx:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static int updateInterval = 4000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static String pluginPath = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private JMenuBar menuBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private JMenuItem hotspotMI, connectMI, exitMI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private WindowMenu windowMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private JMenuItem tileMI, cascadeMI, minimizeAllMI, restoreAllMI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private JMenuItem userGuideMI, aboutMI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private JButton connectButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private JDesktopPane desktop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private ConnectDialog connectDialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private CreateMBeanDialog createDialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private ArrayList<VMInternalFrame> windows =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        new ArrayList<VMInternalFrame>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private int frameLoc = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static boolean debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public JConsole(boolean hotspot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        super(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        setRootPane(new FixedJRootPane());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        setAccessibleDescription(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                 getText("JConsole.accessibleDescription"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        menuBar = new JMenuBar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        setJMenuBar(menuBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // TODO: Use Actions !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        JMenu connectionMenu = new JMenu(getText("Connection"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        connectionMenu.setMnemonic(getMnemonicInt("Connection"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        menuBar.add(connectionMenu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if(hotspot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            hotspotMI = new JMenuItem(getText("Hotspot MBeans..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            hotspotMI.setMnemonic(getMnemonicInt("Hotspot MBeans..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            hotspotMI.setAccelerator(KeyStroke.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                     getKeyStroke(KeyEvent.VK_H,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                                  InputEvent.CTRL_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            hotspotMI.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            connectionMenu.add(hotspotMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            connectionMenu.addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        connectMI = new JMenuItem(Resources.getText("New Connection..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        connectMI.setMnemonic(getMnemonicInt("New Connection..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        connectMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                                        InputEvent.CTRL_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        connectMI.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        connectionMenu.add(connectMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        connectionMenu.addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        exitMI = new JMenuItem(Resources.getText("Exit"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        exitMI.setMnemonic(getMnemonicInt("Exit"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        exitMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                                     InputEvent.ALT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        exitMI.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        connectionMenu.add(exitMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        JMenu helpMenu = new JMenu(getText("HelpMenu.title"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        helpMenu.setMnemonic(getMnemonicInt("HelpMenu.title"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        menuBar.add(helpMenu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (AboutDialog.isBrowseSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            userGuideMI = new JMenuItem(getText("HelpMenu.UserGuide.title"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            userGuideMI.setMnemonic(getMnemonicInt("HelpMenu.UserGuide.title"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            userGuideMI.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            helpMenu.add(userGuideMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            helpMenu.addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        aboutMI = new JMenuItem(getText("HelpMenu.About.title"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        aboutMI.setMnemonic(getMnemonicInt("HelpMenu.About.title"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        aboutMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        aboutMI.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        helpMenu.add(aboutMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public JDesktopPane getDesktopPane() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return desktop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public List<VMInternalFrame> getInternalFrames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return windows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private void createMDI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // Restore title - we now show connection name on internal frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        setTitle(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        Container cp = getContentPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        Component oldCenter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            ((BorderLayout)cp.getLayout()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            getLayoutComponent(BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        windowMenu = new WindowMenu(Resources.getText("Window"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        windowMenu.setMnemonic(getMnemonicInt("Window"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // Add Window menu before Help menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        menuBar.add(windowMenu, menuBar.getComponentCount() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        desktop = new JDesktopPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        desktop.setBackground(new Color(235, 245, 255));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        cp.add(desktop, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (oldCenter instanceof VMPanel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            addFrame((VMPanel)oldCenter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private class WindowMenu extends JMenu {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        VMInternalFrame[] windowMenuWindows = new VMInternalFrame[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int separatorPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // The width value of viewR is used to truncate long menu items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // The rest are placeholders and are ignored for this purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        Rectangle viewR = new Rectangle(0, 0, 400, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        Rectangle textR = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        Rectangle iconR = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        WindowMenu(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            super(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            cascadeMI = new JMenuItem(Resources.getText("Cascade"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            cascadeMI.setMnemonic(getMnemonicInt("Cascade"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            cascadeMI.addActionListener(JConsole.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            add(cascadeMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            tileMI = new JMenuItem(Resources.getText("Tile"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            tileMI.setMnemonic(getMnemonicInt("Tile"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            tileMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                                         InputEvent.CTRL_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            tileMI.addActionListener(JConsole.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            add(tileMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            minimizeAllMI = new JMenuItem(Resources.getText("Minimize All"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            minimizeAllMI.setMnemonic(getMnemonicInt("Minimize All"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            minimizeAllMI.addActionListener(JConsole.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            add(minimizeAllMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            restoreAllMI = new JMenuItem(Resources.getText("Restore All"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            restoreAllMI.setMnemonic(getMnemonicInt("Restore All"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            restoreAllMI.addActionListener(JConsole.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            add(restoreAllMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            separatorPosition = getMenuComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        private void add(VMInternalFrame vmIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (separatorPosition == getMenuComponentCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            int index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            int position = separatorPosition + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            int n = windowMenuWindows.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                if (windowMenuWindows[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    // Slot is in use, try next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    position++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    // Found a free slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                // Create a slot at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                VMInternalFrame[] newArray = new VMInternalFrame[n + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                System.arraycopy(windowMenuWindows, 0, newArray, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                windowMenuWindows = newArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                index = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            windowMenuWindows[index] = vmIF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            String indexString = "" + (index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            String vmName = vmIF.getVMPanel().getDisplayName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // Maybe truncate menu item string and end with "..."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            String text =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                SwingUtilities.layoutCompoundLabel(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                        getGraphics().getFontMetrics(getFont()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                        indexString +  " " + vmName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                        null, 0, 0, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                        viewR, iconR, textR, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            JMenuItem mi = new JMenuItem(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (text.endsWith("...")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                mi.setToolTipText(vmName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // Set mnemonic using last digit of number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            int nDigits = indexString.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            mi.setMnemonic(indexString.charAt(nDigits-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            mi.setDisplayedMnemonicIndex(nDigits-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            mi.putClientProperty("JConsole.vmIF", vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            mi.addActionListener(JConsole.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            vmIF.putClientProperty("JConsole.menuItem", mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            add(mi, position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        private void remove(VMInternalFrame vmIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            for (int i = 0; i < windowMenuWindows.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                if (windowMenuWindows[i] == vmIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    windowMenuWindows[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            JMenuItem mi = (JMenuItem)vmIF.getClientProperty("JConsole.menuItem");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            remove(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            mi.putClientProperty("JConsole.vmIF", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            vmIF.putClientProperty("JConsole.menuItem", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (separatorPosition == getMenuComponentCount() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                remove(getMenuComponent(getMenuComponentCount() - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public void actionPerformed(ActionEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        Object src = ev.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (src == hotspotMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            showCreateMBeanDialog();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (src == connectButton || src == connectMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            VMPanel vmPanel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            JInternalFrame vmIF = desktop.getSelectedFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if (vmIF instanceof VMInternalFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                vmPanel = ((VMInternalFrame)vmIF).getVMPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                String hostName = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                String url = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (vmPanel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    hostName = vmPanel.getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    if(vmPanel.getUrl() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        url = vmPanel.getUrl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                showConnectDialog(url, hostName, 0, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } else if (src == tileMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            tileWindows();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } else if (src == cascadeMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            cascadeWindows();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } else if (src == minimizeAllMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    vmIF.setIcon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } else if (src == restoreAllMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    vmIF.setIcon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } else if (src == exitMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } else if (src == userGuideMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            AboutDialog.browseUserGuide(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } else if (src == aboutMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            AboutDialog.showAboutDialog(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } else if (src instanceof JMenuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            JMenuItem mi = (JMenuItem)src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            VMInternalFrame vmIF = (VMInternalFrame)mi.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                getClientProperty("JConsole.vmIF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (vmIF != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    vmIF.setIcon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    vmIF.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                vmIF.moveToFront();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public void tileWindows() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        int w = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        int h = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (!vmIF.isIcon()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                if (w == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        vmIF.setMaximum(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        w = vmIF.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        h = vmIF.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (n > 0 && w > 0 && h > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            int rows = (int)Math.ceil(Math.sqrt(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            int cols = n / rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (rows * cols < n) cols++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            w /= cols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            h /= rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            int col = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                if (!vmIF.isIcon()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        vmIF.setMaximum(n==1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    if (n > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        vmIF.setBounds(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    if (col < cols-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        col++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        x += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        col = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        y += h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public void cascadeWindows() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        int w = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        int h = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (!vmIF.isIcon()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    vmIF.setMaximum(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                vmIF.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                if (w == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        w = vmIF.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        h = vmIF.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        vmIF.setMaximum(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        w = vmIF.getWidth() - w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        h = vmIF.getHeight() - h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        vmIF.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        int dX = (n > 1) ? (w / (n - 1)) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        int dY = (n > 1) ? (h / (n - 1)) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (!vmIF.isIcon()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                vmIF.setLocation(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                vmIF.moveToFront();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                x += dX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                y += dY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    void addHost(String hostName, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                 String userName, String password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        addHost(hostName, port, userName, password, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    void addVmid(LocalVirtualMachine lvm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        addVmid(lvm, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    void addVmid(final LocalVirtualMachine lvm, final boolean tile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        new Thread("JConsole.addVmid") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    addProxyClient(ProxyClient.getProxyClient(lvm), tile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                } catch (final SecurityException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    failed(ex, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                } catch (final IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    failed(ex, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    void addUrl(final String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                final String userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                final String password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                final boolean tile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        new Thread("JConsole.addUrl") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    addProxyClient(ProxyClient.getProxyClient(url, userName, password),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                                   tile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                } catch (final MalformedURLException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    failed(ex, url, userName, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                } catch (final SecurityException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    failed(ex, url, userName, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                } catch (final IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    failed(ex, url, userName, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    void addHost(final String hostName, final int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                 final String userName, final String password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                 final boolean tile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        new Thread("JConsole.addHost") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    addProxyClient(ProxyClient.getProxyClient(hostName, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                                                              userName, password),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                                   tile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                } catch (final IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    dbgStackTrace(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                            showConnectDialog(null, hostName, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                                              userName, password, errorMessage(ex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    // Call on worker thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    void addProxyClient(final ProxyClient proxyClient, final boolean tile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                VMPanel vmPanel = new VMPanel(proxyClient, updateInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                addFrame(vmPanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                if (tile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                            tileWindows();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                vmPanel.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    // Call on worker thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    private void failed(final Exception ex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                        final String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        final String userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        final String password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                dbgStackTrace(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                showConnectDialog(url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                  null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                                  -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                                  userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                                  password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                  errorMessage(ex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    private VMInternalFrame addFrame(VMPanel vmPanel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        final VMInternalFrame vmIF = new VMInternalFrame(vmPanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        for (VMInternalFrame f : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                f.setMaximum(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        desktop.add(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        vmIF.setLocation(frameLoc, frameLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        frameLoc += 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        vmIF.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        windows.add(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (windows.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                vmIF.setMaximum(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        vmIF.addInternalFrameListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        windowMenu.add(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return vmIF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    private void showConnectDialog(String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                   String hostName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                                   int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                                   String userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                                   String password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                                   String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (connectDialog == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            connectDialog = new ConnectDialog(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        connectDialog.setConnectionParameters(url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                              hostName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                              port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                              userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                                              password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                                              msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        connectDialog.refresh();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        connectDialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            // Bring to front of other dialogs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            connectDialog.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        } catch (PropertyVetoException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    private void showCreateMBeanDialog() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if (createDialog == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            createDialog = new CreateMBeanDialog(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        createDialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            // Bring to front of other dialogs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            createDialog.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        } catch (PropertyVetoException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    private void removeVMInternalFrame(VMInternalFrame vmIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        windowMenu.remove(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        desktop.remove(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        desktop.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        vmIF.getVMPanel().cleanUp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        vmIF.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    private boolean isProxyClientUsed(ProxyClient client) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        for(VMInternalFrame frame : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            ProxyClient cli = frame.getVMPanel().getProxyClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            if(client == cli)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    static boolean isValidRemoteString(String txt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        boolean valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (txt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            txt = txt.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            if (txt.startsWith(ROOT_URL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                if (txt.length() > ROOT_URL.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                //---------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                // Supported host and port combinations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                //     hostname:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                //     IPv4Address:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                //     [IPv6Address]:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                //---------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                // Is literal IPv6 address?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                if (txt.startsWith("[")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    int index = txt.indexOf("]:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        // Extract literal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                        String address = txt.substring(1, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                        if (IPAddressUtil.isIPv6LiteralAddress(address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                            // Extract port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                                String portStr = txt.substring(index + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                                int port = Integer.parseInt(portStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                                if (port >= 0 && port <= 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                                    valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                            } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                                valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    String[] s = txt.split(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    if (s.length == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                            int port = Integer.parseInt(s[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                            if (port >= 0 && port <= 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                                valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                            valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        return valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    private String errorMessage(Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
       String msg = Resources.getText("Connection failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
       if (ex instanceof IOException || ex instanceof SecurityException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
           Throwable cause = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
           Throwable c = ex.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
           while (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
               cause = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
               c = c.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
           if (cause instanceof ConnectException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
               return msg + ": " + cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
           } else if (cause instanceof UnknownHostException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
               return Resources.getText("Unknown Host", cause.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
           } else if (cause instanceof NoRouteToHostException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
               return msg + ": " + cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
           } else if (cause instanceof FailedLoginException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
               return msg + ": " + cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
           } else if (cause instanceof SSLHandshakeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
               return msg + ": "+ cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        } else if (ex instanceof MalformedURLException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
           return Resources.getText("Invalid URL", ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        return msg + ": " + ex.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    // InternalFrameListener interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public void internalFrameClosing(InternalFrameEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        VMInternalFrame vmIF = (VMInternalFrame)e.getInternalFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        removeVMInternalFrame(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        windows.remove(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        ProxyClient client = vmIF.getVMPanel().getProxyClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if(!isProxyClientUsed(client))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            client.markAsDead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (windows.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            showConnectDialog("", "", 0, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    public void internalFrameOpened(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    public void internalFrameClosed(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    public void internalFrameIconified(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    public void internalFrameDeiconified(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    public void internalFrameActivated(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    public void internalFrameDeactivated(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    private static void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        System.err.println(Resources.getText("zz usage text", "jconsole"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    private static void mainInit(final List<String> urls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                 final List<String> hostNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                 final List<Integer> ports,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                                 final List<LocalVirtualMachine> vmids,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                                 final ProxyClient proxyClient,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                                 final boolean noTile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                                 final boolean hotspot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        // Always create Swing GUI on the Event Dispatching Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    JConsole jConsole = new JConsole(hotspot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    // Center the window on screen, taking into account screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    // size and insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    GraphicsConfiguration gc = jConsole.getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    Dimension scrSize = toolkit.getScreenSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    Insets scrInsets  = toolkit.getScreenInsets(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    Rectangle scrBounds =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                        new Rectangle(scrInsets.left, scrInsets.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                                      scrSize.width  - scrInsets.left - scrInsets.right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                                      scrSize.height - scrInsets.top  - scrInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    int w = Math.min(900, scrBounds.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    int h = Math.min(750, scrBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    jConsole.setBounds(scrBounds.x + (scrBounds.width  - w) / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                                       scrBounds.y + (scrBounds.height - h) / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                                       w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    jConsole.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    jConsole.createMDI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    for (int i = 0; i < hostNames.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                        jConsole.addHost(hostNames.get(i), ports.get(i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                                         null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                                         (i == hostNames.size() - 1) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                                         !noTile : false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    for (int i = 0; i < urls.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                        jConsole.addUrl(urls.get(i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                                        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                                        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                                        (i == urls.size() - 1) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                                        !noTile : false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    for (int i = 0; i < vmids.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                        jConsole.addVmid(vmids.get(i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                                        (i == vmids.size() - 1) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                                        !noTile : false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                    if (vmids.size() == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                        hostNames.size() == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                        urls.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                        jConsole.showConnectDialog(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                                                   null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                                                   0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                                                   null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                                                   null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                                                   null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        boolean noTile = false, hotspot = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        int argIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        ProxyClient proxyClient = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        if (System.getProperty("jconsole.showOutputViewer") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            OutputViewer.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        while (args.length - argIndex > 0 && args[argIndex].startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            String arg = args[argIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            if (arg.equals("-h") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                arg.equals("-help") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                arg.equals("-?")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            } else if (arg.startsWith("-interval=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                    updateInterval = Integer.parseInt(arg.substring(10)) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                        1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            } else if (arg.equals("-pluginpath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                if (argIndex < args.length && !args[argIndex].startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    pluginPath = args[argIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                    // Invalid argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            } else if (arg.equals("-notile")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                noTile = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            } else if (arg.equals("-version")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                Version.print(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            } else if (arg.equals("-debug")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            } else if (arg.equals("-fullversion")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                Version.printFullVersion(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                // Unknown switch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        if (System.getProperty("jconsole.showUnsupported") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            hotspot = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        List<String> urls = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        List<String> hostNames = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        List<Integer> ports = new ArrayList<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        List<LocalVirtualMachine> vms = new ArrayList<LocalVirtualMachine>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        for (int i = argIndex; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            String arg = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            if (isValidRemoteString(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                if (arg.startsWith(ROOT_URL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    urls.add(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                } else if (arg.matches(".*:[0-9]*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                    int p = arg.lastIndexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    hostNames.add(arg.substring(0, p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                        ports.add(Integer.parseInt(arg.substring(p+1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                        usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                if (!isLocalAttachAvailable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                    System.err.println("Local process monitoring is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    int vmid = Integer.parseInt(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                    LocalVirtualMachine lvm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                        LocalVirtualMachine.getLocalVirtualMachine(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    if (lvm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                        System.err.println("Invalid process id:" + vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    vms.add(lvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        mainInit(urls, hostNames, ports, vms, proxyClient, noTile, hotspot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    public static boolean isDebug() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        return debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    private static void dbgStackTrace(Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    private static final boolean localAttachmentSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        boolean supported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            Class.forName("com.sun.tools.attach.VirtualMachine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            Class.forName("sun.management.ConnectorAddressLink");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            supported = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        } catch (NoClassDefFoundError x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            supported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            supported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        localAttachmentSupported = supported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    public static boolean isLocalAttachAvailable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        return localAttachmentSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    private static ServiceLoader<JConsolePlugin> pluginService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    // Return a list of newly instantiated JConsolePlugin objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    static synchronized List<JConsolePlugin> getPlugins() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        if (pluginService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            // First time loading and initializing the plugins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            initPluginService(pluginPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            // reload the plugin so that new instances will be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            pluginService.reload();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        List<JConsolePlugin> plugins = new ArrayList<JConsolePlugin>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        for (JConsolePlugin p : pluginService) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            plugins.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        return plugins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    private static void initPluginService(String pluginPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        if (pluginPath.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                ClassLoader pluginCL = new URLClassLoader(pathToURLs(pluginPath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                ServiceLoader<JConsolePlugin> plugins =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    ServiceLoader.load(JConsolePlugin.class, pluginCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                // validate all plugins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            for (JConsolePlugin p : plugins) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    if (isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                        System.out.println("Plugin " + p.getClass() + " loaded.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                pluginService = plugins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            } catch (ServiceConfigurationError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                // Error occurs during initialization of plugin
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                System.out.println(Resources.getText("Fail to load plugin",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                                   e.getMessage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                if (JConsole.isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                System.out.println(Resources.getText("Invalid plugin path",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                                   e.getMessage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        if (pluginService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            initEmptyPlugin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    private static void initEmptyPlugin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        ClassLoader pluginCL = new URLClassLoader(new URL[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        pluginService = ServiceLoader.load(JConsolePlugin.class, pluginCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * Utility method for converting a search path string to an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * of directory and JAR file URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @param path the search path string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @return the resulting array of directory and JAR file URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    private static URL[] pathToURLs(String path) throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        String[] names = path.split(File.pathSeparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        URL[] urls = new URL[names.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        for (String f : names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            URL url = fileToURL(new File(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            urls[count++] = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        return urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * Returns the directory or JAR file URL corresponding to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * local file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * @param file the File object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @return the resulting directory or JAR file URL, or null if unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    private static URL fileToURL(File file) throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            name = file.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            name = file.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        name = name.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        if (!name.startsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            name = "/" + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        // If the file does not exist, then assume that it's a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        if (!file.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            name = name + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        return new URL("file", "", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    private static class FixedJRootPane extends JRootPane {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            updateLafValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            super.updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         * The revalidate method seems to be the only one that gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         * called whenever there is a change of L&F or change of theme
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
         * in Windows L&F and GTK L&F.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        public void revalidate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            // Workaround for Swing bug where the titledborder in both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            // GTK and Windows L&F's use calculated colors instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            // the highlight/shadow colors from the theme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            // Putting null removes any previous override and causes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            // fallback to the current L&F's value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            UIManager.put("TitledBorder.border", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            Border border = UIManager.getBorder("TitledBorder.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            if (border instanceof BorderUIResource.EtchedBorderUIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                Color highlight = UIManager.getColor("ToolBar.highlight");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                Color shadow    = UIManager.getColor("ToolBar.shadow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                border = new BorderUIResource.EtchedBorderUIResource(highlight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                                                                     shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                UIManager.put("TitledBorder.border", border);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            if (IS_GTK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                // Workaround for Swing bug where the titledborder in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                // GTK L&F use hardcoded color and font for the title
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                // instead of getting them from the theme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                UIManager.put("TitledBorder.titleColor",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                              UIManager.getColor("Label.foreground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                UIManager.put("TitledBorder.font",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                              UIManager.getFont("Label.font"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            super.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
}