|
Java example source code file (ConnectDialog.java)
This example Java source code file (ConnectDialog.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Learn more about this Java project at its project page.
The ConnectDialog.java Java example source code
/*
* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.tools.jconsole;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.BasicRadioButtonUI;
import javax.swing.table.*;
import static java.awt.BorderLayout.*;
import static javax.swing.ListSelectionModel.*;
import static sun.tools.jconsole.Utilities.*;
@SuppressWarnings("serial")
public class ConnectDialog extends InternalDialog
implements DocumentListener, FocusListener,
ItemListener, ListSelectionListener, KeyListener {
private static final int COL_NAME = 0;
private static final int COL_PID = 1;
JConsole jConsole;
JTextField userNameTF, passwordTF;
JRadioButton localRadioButton, remoteRadioButton;
JLabel localMessageLabel, remoteMessageLabel;
JTextField remoteTF;
JButton connectButton, cancelButton;
JPanel radioButtonPanel;
private Icon mastheadIcon =
new MastheadIcon(Messages.CONNECT_DIALOG_MASTHEAD_TITLE);
private Color hintTextColor, disabledTableCellColor;
// The table of managed VM (local process)
JTable vmTable;
ManagedVmTableModel vmModel = null;
JScrollPane localTableScrollPane = null;
private Action connectAction, cancelAction;
public ConnectDialog(JConsole jConsole) {
super(jConsole, Messages.CONNECT_DIALOG_TITLE, true);
this.jConsole = jConsole;
setAccessibleDescription(this,
Messages.CONNECT_DIALOG_ACCESSIBLE_DESCRIPTION);
setDefaultCloseOperation(HIDE_ON_CLOSE);
setResizable(false);
Container cp = (JComponent)getContentPane();
radioButtonPanel = new JPanel(new BorderLayout(0, 12));
radioButtonPanel.setBorder(new EmptyBorder(6, 12, 12, 12));
ButtonGroup radioButtonGroup = new ButtonGroup();
JPanel bottomPanel = new JPanel(new BorderLayout());
statusBar = new JLabel(" ", JLabel.CENTER);
setAccessibleName(statusBar,
Messages.CONNECT_DIALOG_STATUS_BAR_ACCESSIBLE_NAME);
Font normalLabelFont = statusBar.getFont();
Font boldLabelFont = normalLabelFont.deriveFont(Font.BOLD);
Font smallLabelFont = normalLabelFont.deriveFont(normalLabelFont.getSize2D() - 1);
JLabel mastheadLabel = new JLabel(mastheadIcon);
setAccessibleName(mastheadLabel,
Messages.CONNECT_DIALOG_MASTHEAD_ACCESSIBLE_NAME);
cp.add(mastheadLabel, NORTH);
cp.add(radioButtonPanel, CENTER);
cp.add(bottomPanel, SOUTH);
createActions();
remoteTF = new JTextField();
remoteTF.addActionListener(connectAction);
remoteTF.getDocument().addDocumentListener(this);
remoteTF.addFocusListener(this);
remoteTF.setPreferredSize(remoteTF.getPreferredSize());
setAccessibleName(remoteTF,
Messages.REMOTE_PROCESS_TEXT_FIELD_ACCESSIBLE_NAME);
//
// If the VM supports the local attach mechanism (is: Sun
// implementation) then the Local Process panel is created.
//
if (JConsole.isLocalAttachAvailable()) {
vmModel = new ManagedVmTableModel();
vmTable = new LocalTabJTable(vmModel);
vmTable.setSelectionMode(SINGLE_SELECTION);
vmTable.setPreferredScrollableViewportSize(new Dimension(400, 250));
vmTable.setColumnSelectionAllowed(false);
vmTable.addFocusListener(this);
vmTable.getSelectionModel().addListSelectionListener(this);
TableColumnModel columnModel = vmTable.getColumnModel();
TableColumn pidColumn = columnModel.getColumn(COL_PID);
pidColumn.setMaxWidth(getLabelWidth("9999999"));
pidColumn.setResizable(false);
TableColumn cmdLineColumn = columnModel.getColumn(COL_NAME);
cmdLineColumn.setResizable(false);
localRadioButton = new JRadioButton(Messages.LOCAL_PROCESS_COLON);
localRadioButton.setMnemonic(Resources.getMnemonicInt(Messages.LOCAL_PROCESS_COLON));
localRadioButton.setFont(boldLabelFont);
localRadioButton.addItemListener(this);
radioButtonGroup.add(localRadioButton);
JPanel localPanel = new JPanel(new BorderLayout());
JPanel localTablePanel = new JPanel(new BorderLayout());
radioButtonPanel.add(localPanel, NORTH);
localPanel.add(localRadioButton, NORTH);
localPanel.add(new Padder(localRadioButton), LINE_START);
localPanel.add(localTablePanel, CENTER);
localTableScrollPane = new JScrollPane(vmTable);
localTablePanel.add(localTableScrollPane, NORTH);
localMessageLabel = new JLabel(" ");
localMessageLabel.setFont(smallLabelFont);
localMessageLabel.setForeground(hintTextColor);
localTablePanel.add(localMessageLabel, SOUTH);
}
remoteRadioButton = new JRadioButton(Messages.REMOTE_PROCESS_COLON);
remoteRadioButton.setMnemonic(Resources.getMnemonicInt(Messages.REMOTE_PROCESS_COLON));
remoteRadioButton.setFont(boldLabelFont);
radioButtonGroup.add(remoteRadioButton);
JPanel remotePanel = new JPanel(new BorderLayout());
if (localRadioButton != null) {
remotePanel.add(remoteRadioButton, NORTH);
remotePanel.add(new Padder(remoteRadioButton), LINE_START);
Action nextRadioButtonAction =
new AbstractAction("nextRadioButton") {
public void actionPerformed(ActionEvent ev) {
JRadioButton rb =
(ev.getSource() == localRadioButton) ? remoteRadioButton
: localRadioButton;
rb.doClick();
rb.requestFocus();
}
};
localRadioButton.getActionMap().put("nextRadioButton", nextRadioButtonAction);
remoteRadioButton.getActionMap().put("nextRadioButton", nextRadioButtonAction);
localRadioButton.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
"nextRadioButton");
remoteRadioButton.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0),
"nextRadioButton");
} else {
JLabel remoteLabel = new JLabel(remoteRadioButton.getText());
remoteLabel.setFont(boldLabelFont);
remotePanel.add(remoteLabel, NORTH);
}
radioButtonPanel.add(remotePanel, SOUTH);
JPanel remoteTFPanel = new JPanel(new BorderLayout());
remotePanel.add(remoteTFPanel, CENTER);
remoteTFPanel.add(remoteTF, NORTH);
remoteMessageLabel = new JLabel("<html>" + Messages.REMOTE_TF_USAGE + " |