jdk/src/share/classes/sun/tools/jconsole/VMPanel.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 55 5ecee29e98d8
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Timer;
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.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.jconsole.JConsolePlugin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.jconsole.JConsoleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import static com.sun.tools.jconsole.JConsoleContext.ConnectionState.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import static sun.tools.jconsole.ProxyClient.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
@SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class VMPanel extends JTabbedPane implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private ProxyClient proxyClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private int updateInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private String hostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private String userName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private String password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private String url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private VMInternalFrame vmIF = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final String windowsLaF =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static ArrayList<TabInfo> tabInfos = new ArrayList<TabInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private boolean wasConnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // The everConnected flag keeps track of whether the window can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // closed if the user clicks Cancel after a failed connection attempt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private boolean everConnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // The initialUpdate flag is used to enable/disable tabs each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // a connect or reconnect takes place. This flag avoids having to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // enable/disable tabs on each update call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private boolean initialUpdate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // Each VMPanel has its own instance of the JConsolePlugin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // A map of JConsolePlugin to the previous SwingWorker
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private Map<JConsolePlugin, SwingWorker<?,?>> plugins = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean pluginTabsAdded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // Update these only on the EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private JOptionPane optionPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private JProgressBar progressBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private long time0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        tabInfos.add(new TabInfo(OverviewTab.class, OverviewTab.getTabName(), true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        tabInfos.add(new TabInfo(MemoryTab.class,  MemoryTab.getTabName(),  true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        tabInfos.add(new TabInfo(ThreadTab.class,  ThreadTab.getTabName(),  true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        tabInfos.add(new TabInfo(ClassTab.class,   ClassTab.getTabName(),   true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        tabInfos.add(new TabInfo(SummaryTab.class, SummaryTab.getTabName(), true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        tabInfos.add(new TabInfo(MBeansTab.class,  MBeansTab.getTabName(),  true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static TabInfo[] getTabInfos() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return tabInfos.toArray(new TabInfo[tabInfos.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    VMPanel(ProxyClient proxyClient, int updateInterval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.proxyClient = proxyClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.updateInterval = updateInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.hostName = proxyClient.getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.port     = proxyClient.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.vmid     = proxyClient.getVmid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.userName = proxyClient.getUserName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.password = proxyClient.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.url = proxyClient.getUrl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for (TabInfo tabInfo : tabInfos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (tabInfo.tabVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                addTab(tabInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        plugins = new LinkedHashMap<JConsolePlugin, SwingWorker<?,?>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        for (JConsolePlugin p : JConsole.getPlugins()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            p.setContext(proxyClient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            plugins.put(p, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Utilities.updateTransparency(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        ToolTipManager.sharedInstance().registerComponent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // Start listening to connection state events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        proxyClient.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        addMouseListener(new MouseAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (connectedIconBounds != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    && (e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    && connectedIconBounds.contains(e.getPoint())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    if (isConnected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        wasConnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private static Icon connectedIcon16 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        new ImageIcon(VMPanel.class.getResource("resources/connected16.png"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private static Icon connectedIcon24 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        new ImageIcon(VMPanel.class.getResource("resources/connected24.png"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static Icon disconnectedIcon16 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        new ImageIcon(VMPanel.class.getResource("resources/disconnected16.png"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static Icon disconnectedIcon24 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        new ImageIcon(VMPanel.class.getResource("resources/disconnected24.png"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private Rectangle connectedIconBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // Override to increase right inset for tab area,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    // in order to reserve space for the connect toggle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void setUI(TabbedPaneUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        Insets insets = (Insets)UIManager.getLookAndFeelDefaults().get("TabbedPane.tabAreaInsets");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        insets = (Insets)insets.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        insets.right += connectedIcon24.getIconWidth() + 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        UIManager.put("TabbedPane.tabAreaInsets", insets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        super.setUI(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // Override to paint the connect toggle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected void paintComponent(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        super.paintComponent(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        Icon icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Component c0 = getComponent(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (c0 != null && c0.getY() > 24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            icon = isConnected() ? connectedIcon24 : disconnectedIcon24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            icon = isConnected() ? connectedIcon16 : disconnectedIcon16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        Insets insets = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        int x = getWidth() - insets.right - icon.getIconWidth() - 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (c0 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            y = (c0.getY() - icon.getIconHeight()) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        icon.paintIcon(this, g, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        connectedIconBounds = new Rectangle(x, y, icon.getIconWidth(), icon.getIconHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public String getToolTipText(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (connectedIconBounds.contains(event.getPoint())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (isConnected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                return getText("Connected. Click to disconnect.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                return getText("Disconnected. Click to connect.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return super.getToolTipText(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private synchronized void addTab(TabInfo tabInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        Tab tab = instantiate(tabInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (tab != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            addTab(tabInfo.name, tab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            tabInfo.tabVisible = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private synchronized void insertTab(TabInfo tabInfo, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        Tab tab = instantiate(tabInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (tab != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            insertTab(tabInfo.name, null, tab, null, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            tabInfo.tabVisible = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public synchronized void removeTabAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        super.removeTabAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    private Tab instantiate(TabInfo tabInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            Constructor con = tabInfo.tabClass.getConstructor(VMPanel.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return (Tab)con.newInstance(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            System.err.println(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return proxyClient.isConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public int getUpdateInterval() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return updateInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * WARNING NEVER CALL THIS METHOD TO MAKE JMX REQUEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * IF  assertThread == false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * DISPATCHER THREAD IS NOT ASSERTED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * IT IS USED TO MAKE SOME LOCAL MANIPULATIONS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    ProxyClient getProxyClient(boolean assertThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if(assertThread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return getProxyClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return proxyClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public ProxyClient getProxyClient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        String threadClass = Thread.currentThread().getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (threadClass.equals("java.awt.EventDispatchThread")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            String msg = "Calling VMPanel.getProxyClient() from the Event Dispatch Thread!";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            new RuntimeException(msg).printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return proxyClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public void cleanUp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        //proxyClient.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        for (Tab tab : getTabs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            tab.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        for (JConsolePlugin p : plugins.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            p.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // Cancel pending update tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (timer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            timer.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // Stop listening to connection state events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        proxyClient.removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public void connect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (isConnected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // create plugin tabs if not done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            createPluginTabs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // Notify tabs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            fireConnectedChange(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // Enable/disable tabs on initial update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            initialUpdate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // Start/Restart update timer on connect/reconnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            startUpdateTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            new Thread("VMPanel.connect") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    proxyClient.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public void disconnect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        proxyClient.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        updateFrameTitle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    // Called on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public void propertyChange(PropertyChangeEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        String prop = ev.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (prop == CONNECTION_STATE_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            ConnectionState oldState = (ConnectionState)ev.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            ConnectionState newState = (ConnectionState)ev.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            switch (newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
              case CONNECTING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                onConnecting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
              case CONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                if (progressBar != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    progressBar.setIndeterminate(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    progressBar.setValue(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                closeOptionPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                updateFrameTitle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                // create tabs if not done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                createPluginTabs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                // Notify tabs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                fireConnectedChange(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                // Enable/disable tabs on initial update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                initialUpdate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                // Start/Restart update timer on connect/reconnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                startUpdateTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
              case DISCONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                if (progressBar != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    progressBar.setIndeterminate(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    progressBar.setValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    closeOptionPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                vmPanelDied();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (oldState == ConnectionState.CONNECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    // Notify tabs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    fireConnectedChange(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    // Called on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private void onConnecting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        time0 = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        final JConsole jc = (JConsole)SwingUtilities.getWindowAncestor(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        String connectionName = getConnectionName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        progressBar = new JProgressBar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        progressBar.setIndeterminate(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        JPanel progressPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        progressPanel.add(progressBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Object[] message = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            "<html><h3>" + getText("connectingTo1", connectionName) + "</h3></html>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            progressPanel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            "<html><b>"  + getText("connectingTo2", connectionName) + "</b></html>"
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
        optionPane =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            SheetDialog.showOptionDialog(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                         message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                         JOptionPane.DEFAULT_OPTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                         JOptionPane.INFORMATION_MESSAGE, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                         new String[] { getText("Cancel") },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                         0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    // Called on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private void closeOptionPane() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (optionPane != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            new Thread("VMPanel.sleeper") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    long elapsed = System.currentTimeMillis() - time0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    if (elapsed < 2000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                            sleep(2000 - elapsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                            // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                            optionPane.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                            progressBar = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    void updateFrameTitle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        VMInternalFrame vmIF = getFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (vmIF != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            String displayName = getDisplayName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if (!proxyClient.isConnected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                displayName = getText("ConnectionName (disconnected)", displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            vmIF.setTitle(displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    private VMInternalFrame getFrame() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (vmIF == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            vmIF = (VMInternalFrame)SwingUtilities.getAncestorOfClass(VMInternalFrame.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                                                                      this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return vmIF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    // TODO: this method is not needed when all JConsole tabs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    // are migrated to use the new JConsolePlugin API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    // A thread safe clone of all JConsole tabs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    synchronized List<Tab> getTabs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        ArrayList<Tab> list = new ArrayList<Tab>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        int n = getTabCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            Component c = getComponentAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            if (c instanceof Tab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                list.add((Tab) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    private void startUpdateTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (timer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            timer.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        TimerTask timerTask = new TimerTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                update();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        String timerName = "Timer-"+getConnectionName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        timer = new Timer(timerName, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        timer.schedule(timerTask, 0, updateInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    private void vmPanelDied() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        final JConsole jc = (JConsole)SwingUtilities.getWindowAncestor(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        JOptionPane optionPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        final String connectStr   = getText("Connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        final String reconnectStr = getText("Reconnect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        final String cancelStr    = getText("Cancel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        String msgTitle, msgExplanation, buttonStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (wasConnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            wasConnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            msgTitle = getText("connectionLost1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            msgExplanation = getText("connectionLost2", getConnectionName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            buttonStr = reconnectStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            msgTitle = getText("connectionFailed1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            msgExplanation = getText("connectionFailed2", getConnectionName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            buttonStr = connectStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        optionPane =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            SheetDialog.showOptionDialog(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                         "<html><h3>" + msgTitle + "</h3>" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                         "<b>" + msgExplanation + "</b>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                         JOptionPane.DEFAULT_OPTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                                         JOptionPane.WARNING_MESSAGE, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                         new String[] { buttonStr, cancelStr },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                         0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        optionPane.addPropertyChangeListener(new PropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            public void propertyChange(PropertyChangeEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                if (event.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    Object value = event.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    if (value == reconnectStr || value == connectStr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    } else if (!everConnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                            getFrame().setClosed(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                        } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                            // Should not happen, but can be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    // Note: This method is called on a TimerTask thread. Any GUI manipulation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    // must be performed with invokeLater() or invokeAndWait().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private Object lockObject = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    private void update() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        synchronized(lockObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            if (!isConnected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                if (wasConnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    EventQueue.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                            vmPanelDied();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                wasConnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                wasConnected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                everConnected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            proxyClient.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            List<Tab> tabs = getTabs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            final int n = tabs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                final int index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    if (!proxyClient.isDead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                        // Update tab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        tabs.get(index).update();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        // Enable tab on initial update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        if (initialUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                            EventQueue.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                    setEnabledAt(index, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    // Disable tab on initial update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    if (initialUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        EventQueue.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                                setEnabledAt(index, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            // plugin GUI update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            for (JConsolePlugin p : plugins.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                SwingWorker<?,?> sw = p.newSwingWorker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                SwingWorker<?,?> prevSW = plugins.get(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                // schedule SwingWorker to run only if the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                // SwingWorker has finished its task and it hasn't started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                if (prevSW == null || prevSW.isDone()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    if (sw == null || sw.getState() == SwingWorker.StateValue.PENDING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        plugins.put(p, sw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                        if (sw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                            sw.execute();
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            // Set the first enabled tab in the tabīs list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            // as the selected tab on initial update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            if (initialUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                EventQueue.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        // Select first enabled tab if current tab isn't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        int index = getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        if (index < 0 || !isEnabledAt(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                                if (isEnabledAt(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                                    setSelectedIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                initialUpdate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public String getHostName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return hostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public String getUserName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return userName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    public String getUrl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    public String getPassword() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        return password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    public String getConnectionName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return proxyClient.connectionName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    public String getDisplayName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        return proxyClient.getDisplayName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    static class TabInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        Class<? extends Tab> tabClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        boolean tabVisible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        TabInfo(Class<? extends Tab> tabClass, String name, boolean tabVisible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            this.tabClass = tabClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            this.tabVisible = tabVisible;
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
    // Convenience methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    private static String getText(String key, Object... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return Resources.getText(key, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    private void createPluginTabs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        // add plugin tabs if not done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        if (!pluginTabsAdded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            for (JConsolePlugin p : plugins.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                Map<String, JPanel> tabs = p.getTabs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                for (Map.Entry<String, JPanel> e : tabs.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    addTab(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            pluginTabsAdded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    private void fireConnectedChange(boolean connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        for (Tab tab : getTabs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            tab.firePropertyChange(JConsoleContext.CONNECTION_STATE_PROPERTY, !connected, connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
}