author | egahlin |
Fri, 25 May 2012 12:24:57 +0200 | |
changeset 12851 | 3334e1c781d0 |
parent 5506 | 202f599c92aa |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.tools.jconsole; |
|
27 |
||
28 |
import java.util.List; |
|
29 |
import java.awt.*; |
|
30 |
import java.awt.event.*; |
|
31 |
import java.util.*; |
|
32 |
||
33 |
import javax.swing.*; |
|
34 |
import javax.swing.border.*; |
|
35 |
import javax.swing.event.*; |
|
36 |
import javax.swing.plaf.basic.BasicRadioButtonUI; |
|
37 |
import javax.swing.table.*; |
|
38 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
39 |
|
2 | 40 |
|
41 |
import static java.awt.BorderLayout.*; |
|
42 |
import static javax.swing.ListSelectionModel.*; |
|
43 |
import static sun.tools.jconsole.Utilities.*; |
|
44 |
||
45 |
@SuppressWarnings("serial") |
|
46 |
public class ConnectDialog extends InternalDialog |
|
47 |
implements DocumentListener, FocusListener, |
|
48 |
ItemListener, ListSelectionListener, KeyListener { |
|
49 |
||
50 |
private static final int COL_NAME = 0; |
|
51 |
private static final int COL_PID = 1; |
|
52 |
||
53 |
||
54 |
JConsole jConsole; |
|
55 |
JTextField userNameTF, passwordTF; |
|
56 |
JRadioButton localRadioButton, remoteRadioButton; |
|
57 |
JLabel localMessageLabel, remoteMessageLabel; |
|
58 |
JTextField remoteTF; |
|
59 |
JButton connectButton, cancelButton; |
|
60 |
JPanel radioButtonPanel; |
|
61 |
||
62 |
private Icon mastheadIcon = |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
63 |
new MastheadIcon(Messages.CONNECT_DIALOG_MASTHEAD_TITLE); |
2 | 64 |
private Color hintTextColor, disabledTableCellColor; |
65 |
||
66 |
// The table of managed VM (local process) |
|
67 |
JTable vmTable; |
|
68 |
ManagedVmTableModel vmModel = null; |
|
69 |
||
70 |
JScrollPane localTableScrollPane = null; |
|
71 |
||
72 |
private Action connectAction, cancelAction; |
|
73 |
||
74 |
||
75 |
public ConnectDialog(JConsole jConsole) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
76 |
super(jConsole, Messages.CONNECT_DIALOG_TITLE, true); |
2 | 77 |
|
78 |
this.jConsole = jConsole; |
|
79 |
setAccessibleDescription(this, |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
80 |
Messages.CONNECT_DIALOG_ACCESSIBLE_DESCRIPTION); |
2 | 81 |
setDefaultCloseOperation(HIDE_ON_CLOSE); |
82 |
setResizable(false); |
|
83 |
Container cp = (JComponent)getContentPane(); |
|
84 |
||
85 |
radioButtonPanel = new JPanel(new BorderLayout(0, 12)); |
|
86 |
radioButtonPanel.setBorder(new EmptyBorder(6, 12, 12, 12)); |
|
87 |
ButtonGroup radioButtonGroup = new ButtonGroup(); |
|
88 |
JPanel bottomPanel = new JPanel(new BorderLayout()); |
|
89 |
||
90 |
statusBar = new JLabel(" ", JLabel.CENTER); |
|
91 |
setAccessibleName(statusBar, |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
92 |
Messages.CONNECT_DIALOG_STATUS_BAR_ACCESSIBLE_NAME); |
2 | 93 |
|
94 |
Font normalLabelFont = statusBar.getFont(); |
|
95 |
Font boldLabelFont = normalLabelFont.deriveFont(Font.BOLD); |
|
96 |
Font smallLabelFont = normalLabelFont.deriveFont(normalLabelFont.getSize2D() - 1); |
|
97 |
||
98 |
JLabel mastheadLabel = new JLabel(mastheadIcon); |
|
99 |
setAccessibleName(mastheadLabel, |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
100 |
Messages.CONNECT_DIALOG_MASTHEAD_ACCESSIBLE_NAME); |
2 | 101 |
|
102 |
cp.add(mastheadLabel, NORTH); |
|
103 |
cp.add(radioButtonPanel, CENTER); |
|
104 |
cp.add(bottomPanel, SOUTH); |
|
105 |
||
106 |
createActions(); |
|
107 |
||
108 |
remoteTF = new JTextField(); |
|
109 |
remoteTF.addActionListener(connectAction); |
|
110 |
remoteTF.getDocument().addDocumentListener(this); |
|
111 |
remoteTF.addFocusListener(this); |
|
112 |
remoteTF.setPreferredSize(remoteTF.getPreferredSize()); |
|
113 |
setAccessibleName(remoteTF, |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
114 |
Messages.REMOTE_PROCESS_TEXT_FIELD_ACCESSIBLE_NAME); |
2 | 115 |
|
116 |
// |
|
117 |
// If the VM supports the local attach mechanism (is: Sun |
|
118 |
// implementation) then the Local Process panel is created. |
|
119 |
// |
|
120 |
if (JConsole.isLocalAttachAvailable()) { |
|
121 |
vmModel = new ManagedVmTableModel(); |
|
122 |
vmTable = new LocalTabJTable(vmModel); |
|
123 |
vmTable.setSelectionMode(SINGLE_SELECTION); |
|
124 |
vmTable.setPreferredScrollableViewportSize(new Dimension(400, 250)); |
|
125 |
vmTable.setColumnSelectionAllowed(false); |
|
126 |
vmTable.addFocusListener(this); |
|
127 |
vmTable.getSelectionModel().addListSelectionListener(this); |
|
128 |
||
129 |
TableColumnModel columnModel = vmTable.getColumnModel(); |
|
130 |
||
131 |
TableColumn pidColumn = columnModel.getColumn(COL_PID); |
|
132 |
pidColumn.setMaxWidth(getLabelWidth("9999999")); |
|
133 |
pidColumn.setResizable(false); |
|
134 |
||
135 |
TableColumn cmdLineColumn = columnModel.getColumn(COL_NAME); |
|
136 |
cmdLineColumn.setResizable(false); |
|
137 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
138 |
localRadioButton = new JRadioButton(Messages.LOCAL_PROCESS_COLON); |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
139 |
localRadioButton.setMnemonic(Resources.getMnemonicInt(Messages.LOCAL_PROCESS_COLON)); |
2 | 140 |
localRadioButton.setFont(boldLabelFont); |
141 |
localRadioButton.addItemListener(this); |
|
142 |
radioButtonGroup.add(localRadioButton); |
|
143 |
||
144 |
JPanel localPanel = new JPanel(new BorderLayout()); |
|
145 |
||
146 |
JPanel localTablePanel = new JPanel(new BorderLayout()); |
|
147 |
||
148 |
radioButtonPanel.add(localPanel, NORTH); |
|
149 |
||
150 |
localPanel.add(localRadioButton, NORTH); |
|
151 |
localPanel.add(new Padder(localRadioButton), LINE_START); |
|
152 |
localPanel.add(localTablePanel, CENTER); |
|
153 |
||
154 |
localTableScrollPane = new JScrollPane(vmTable); |
|
155 |
||
156 |
localTablePanel.add(localTableScrollPane, NORTH); |
|
157 |
||
158 |
localMessageLabel = new JLabel(" "); |
|
159 |
localMessageLabel.setFont(smallLabelFont); |
|
160 |
localMessageLabel.setForeground(hintTextColor); |
|
161 |
localTablePanel.add(localMessageLabel, SOUTH); |
|
162 |
} |
|
163 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
164 |
remoteRadioButton = new JRadioButton(Messages.REMOTE_PROCESS_COLON); |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
165 |
remoteRadioButton.setMnemonic(Resources.getMnemonicInt(Messages.REMOTE_PROCESS_COLON)); |
2 | 166 |
remoteRadioButton.setFont(boldLabelFont); |
167 |
radioButtonGroup.add(remoteRadioButton); |
|
168 |
||
169 |
JPanel remotePanel = new JPanel(new BorderLayout()); |
|
170 |
if (localRadioButton != null) { |
|
171 |
remotePanel.add(remoteRadioButton, NORTH); |
|
172 |
remotePanel.add(new Padder(remoteRadioButton), LINE_START); |
|
173 |
||
174 |
Action nextRadioButtonAction = |
|
175 |
new AbstractAction("nextRadioButton") { |
|
176 |
public void actionPerformed(ActionEvent ev) { |
|
177 |
JRadioButton rb = |
|
178 |
(ev.getSource() == localRadioButton) ? remoteRadioButton |
|
179 |
: localRadioButton; |
|
180 |
rb.doClick(); |
|
181 |
rb.requestFocus(); |
|
182 |
} |
|
183 |
}; |
|
184 |
||
185 |
localRadioButton.getActionMap().put("nextRadioButton", nextRadioButtonAction); |
|
186 |
remoteRadioButton.getActionMap().put("nextRadioButton", nextRadioButtonAction); |
|
187 |
||
188 |
localRadioButton.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), |
|
189 |
"nextRadioButton"); |
|
190 |
remoteRadioButton.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), |
|
191 |
"nextRadioButton"); |
|
192 |
} else { |
|
193 |
JLabel remoteLabel = new JLabel(remoteRadioButton.getText()); |
|
194 |
remoteLabel.setFont(boldLabelFont); |
|
195 |
remotePanel.add(remoteLabel, NORTH); |
|
196 |
} |
|
197 |
radioButtonPanel.add(remotePanel, SOUTH); |
|
198 |
||
199 |
JPanel remoteTFPanel = new JPanel(new BorderLayout()); |
|
200 |
remotePanel.add(remoteTFPanel, CENTER); |
|
201 |
||
202 |
remoteTFPanel.add(remoteTF, NORTH); |
|
203 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
204 |
remoteMessageLabel = new JLabel("<html>" + Messages.REMOTE_TF_USAGE + "</html>"); |
2 | 205 |
remoteMessageLabel.setFont(smallLabelFont); |
206 |
remoteMessageLabel.setForeground(hintTextColor); |
|
207 |
remoteTFPanel.add(remoteMessageLabel, CENTER); |
|
208 |
||
209 |
JPanel userPwdPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0)); |
|
210 |
userPwdPanel.setBorder(new EmptyBorder(12, 0, 0, 0)); // top padding |
|
211 |
||
212 |
int tfWidth = JConsole.IS_WIN ? 12 : 8; |
|
213 |
||
214 |
userNameTF = new JTextField(tfWidth); |
|
215 |
userNameTF.addActionListener(connectAction); |
|
216 |
userNameTF.getDocument().addDocumentListener(this); |
|
217 |
userNameTF.addFocusListener(this); |
|
218 |
setAccessibleName(userNameTF, |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
219 |
Messages.USERNAME_ACCESSIBLE_NAME); |
2 | 220 |
LabeledComponent lc; |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
221 |
lc = new LabeledComponent(Messages.USERNAME_COLON_, |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
222 |
Resources.getMnemonicInt(Messages.USERNAME_COLON_), |
2 | 223 |
userNameTF); |
224 |
lc.label.setFont(boldLabelFont); |
|
225 |
userPwdPanel.add(lc); |
|
226 |
||
227 |
passwordTF = new JPasswordField(tfWidth); |
|
228 |
// Heights differ, so fix here |
|
229 |
passwordTF.setPreferredSize(userNameTF.getPreferredSize()); |
|
230 |
passwordTF.addActionListener(connectAction); |
|
231 |
passwordTF.getDocument().addDocumentListener(this); |
|
232 |
passwordTF.addFocusListener(this); |
|
233 |
setAccessibleName(passwordTF, |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
234 |
Messages.PASSWORD_ACCESSIBLE_NAME); |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
235 |
|
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
236 |
lc = new LabeledComponent(Messages.PASSWORD_COLON_, |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
237 |
Resources.getMnemonicInt(Messages.PASSWORD_COLON_), |
2 | 238 |
passwordTF); |
239 |
lc.setBorder(new EmptyBorder(0, 12, 0, 0)); // Left padding |
|
240 |
lc.label.setFont(boldLabelFont); |
|
241 |
userPwdPanel.add(lc); |
|
242 |
||
243 |
remoteTFPanel.add(userPwdPanel, SOUTH); |
|
244 |
||
245 |
String connectButtonToolTipText = |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
246 |
Messages.CONNECT_DIALOG_CONNECT_BUTTON_TOOLTIP; |
2 | 247 |
connectButton = new JButton(connectAction); |
248 |
connectButton.setToolTipText(connectButtonToolTipText); |
|
249 |
||
250 |
cancelButton = new JButton(cancelAction); |
|
251 |
||
252 |
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING)); |
|
253 |
buttonPanel.setBorder(new EmptyBorder(12, 12, 2, 12)); |
|
254 |
if (JConsole.IS_GTK) { |
|
255 |
buttonPanel.add(cancelButton); |
|
256 |
buttonPanel.add(connectButton); |
|
257 |
} else { |
|
258 |
buttonPanel.add(connectButton); |
|
259 |
buttonPanel.add(cancelButton); |
|
260 |
} |
|
261 |
bottomPanel.add(buttonPanel, NORTH); |
|
262 |
||
263 |
bottomPanel.add(statusBar, SOUTH); |
|
264 |
||
265 |
updateButtonStates(); |
|
266 |
Utilities.updateTransparency(this); |
|
267 |
} |
|
268 |
||
269 |
public void revalidate() { |
|
270 |
// Adjust some colors |
|
271 |
hintTextColor = |
|
272 |
ensureContrast(UIManager.getColor("Label.disabledForeground"), |
|
273 |
UIManager.getColor("Panel.background")); |
|
274 |
disabledTableCellColor = |
|
275 |
ensureContrast(new Color(0x808080), |
|
276 |
UIManager.getColor("Table.background")); |
|
277 |
||
278 |
if (remoteMessageLabel != null) { |
|
279 |
remoteMessageLabel.setForeground(hintTextColor); |
|
280 |
// Update html color setting |
|
281 |
String colorStr = |
|
282 |
String.format("%06x", hintTextColor.getRGB() & 0xFFFFFF); |
|
283 |
remoteMessageLabel.setText("<html><font color=#" + colorStr + ">" + |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
284 |
Messages.REMOTE_TF_USAGE); |
2 | 285 |
} |
286 |
if (localMessageLabel != null) { |
|
287 |
localMessageLabel.setForeground(hintTextColor); |
|
288 |
// Update html color setting |
|
289 |
valueChanged(null); |
|
290 |
} |
|
291 |
||
292 |
super.revalidate(); |
|
293 |
} |
|
294 |
||
295 |
private void createActions() { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
296 |
connectAction = new AbstractAction(Messages.CONNECT) { |
2 | 297 |
/* init */ { |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
298 |
putValue(Action.MNEMONIC_KEY, Resources.getMnemonicInt(Messages.CONNECT)); |
2 | 299 |
} |
300 |
||
301 |
public void actionPerformed(ActionEvent ev) { |
|
302 |
if (!isEnabled() || !isVisible()) { |
|
303 |
return; |
|
304 |
} |
|
305 |
setVisible(false); |
|
306 |
statusBar.setText(""); |
|
307 |
||
308 |
if (remoteRadioButton.isSelected()) { |
|
309 |
String txt = remoteTF.getText().trim(); |
|
310 |
String userName = userNameTF.getText().trim(); |
|
311 |
userName = userName.equals("") ? null : userName; |
|
312 |
String password = passwordTF.getText(); |
|
313 |
password = password.equals("") ? null : password; |
|
314 |
try { |
|
315 |
if (txt.startsWith(JConsole.ROOT_URL)) { |
|
316 |
String url = txt; |
|
317 |
jConsole.addUrl(url, userName, password, false); |
|
318 |
remoteTF.setText(JConsole.ROOT_URL); |
|
319 |
return; |
|
320 |
} else { |
|
321 |
String host = remoteTF.getText().trim(); |
|
322 |
String port = "0"; |
|
323 |
int index = host.lastIndexOf(":"); |
|
324 |
if (index >= 0) { |
|
325 |
port = host.substring(index + 1); |
|
326 |
host = host.substring(0, index); |
|
327 |
} |
|
328 |
if (host.length() > 0 && port.length() > 0) { |
|
329 |
int p = Integer.parseInt(port.trim()); |
|
330 |
jConsole.addHost(host, p, userName, password); |
|
331 |
remoteTF.setText(""); |
|
332 |
userNameTF.setText(""); |
|
333 |
passwordTF.setText(""); |
|
334 |
return; |
|
335 |
} |
|
336 |
} |
|
337 |
} catch (Exception ex) { |
|
338 |
statusBar.setText(ex.toString()); |
|
339 |
} |
|
340 |
setVisible(true); |
|
341 |
} else if (localRadioButton != null && localRadioButton.isSelected()) { |
|
342 |
// Try to connect to selected VM. If a connection |
|
343 |
// cannot be established for some reason (the process has |
|
344 |
// terminated for example) then keep the dialog open showing |
|
345 |
// the connect error. |
|
346 |
// |
|
347 |
int row = vmTable.getSelectedRow(); |
|
348 |
if (row >= 0) { |
|
349 |
jConsole.addVmid(vmModel.vmAt(row)); |
|
350 |
} |
|
351 |
refresh(); |
|
352 |
} |
|
353 |
} |
|
354 |
}; |
|
355 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
356 |
cancelAction = new AbstractAction(Messages.CANCEL) { |
2 | 357 |
public void actionPerformed(ActionEvent ev) { |
358 |
setVisible(false); |
|
359 |
statusBar.setText(""); |
|
360 |
} |
|
361 |
}; |
|
362 |
} |
|
363 |
||
364 |
||
365 |
// a label used solely for calculating the width |
|
366 |
private static JLabel tmpLabel = new JLabel(); |
|
367 |
public static int getLabelWidth(String text) { |
|
368 |
tmpLabel.setText(text); |
|
369 |
return (int) tmpLabel.getPreferredSize().getWidth() + 1; |
|
370 |
} |
|
371 |
||
372 |
private class LocalTabJTable extends JTable { |
|
373 |
ManagedVmTableModel vmModel; |
|
374 |
Border rendererBorder = new EmptyBorder(0, 6, 0, 6); |
|
375 |
||
376 |
public LocalTabJTable(ManagedVmTableModel model) { |
|
377 |
super(model); |
|
378 |
this.vmModel = model; |
|
379 |
||
380 |
// Remove vertical lines, expect for GTK L&F. |
|
381 |
// (because GTK doesn't show header dividers) |
|
382 |
if (!JConsole.IS_GTK) { |
|
383 |
setShowVerticalLines(false); |
|
384 |
setIntercellSpacing(new Dimension(0, 1)); |
|
385 |
} |
|
386 |
||
387 |
// Double-click handler |
|
388 |
addMouseListener(new MouseAdapter() { |
|
389 |
public void mouseClicked(MouseEvent evt) { |
|
390 |
if (evt.getClickCount() == 2) { |
|
391 |
connectButton.doClick(); |
|
392 |
} |
|
393 |
} |
|
394 |
}); |
|
395 |
||
396 |
// Enter should call default action |
|
397 |
getActionMap().put("connect", connectAction); |
|
398 |
InputMap inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); |
|
399 |
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "connect"); |
|
400 |
} |
|
401 |
||
402 |
public String getToolTipText(MouseEvent e) { |
|
403 |
String tip = null; |
|
404 |
java.awt.Point p = e.getPoint(); |
|
405 |
int rowIndex = rowAtPoint(p); |
|
406 |
int colIndex = columnAtPoint(p); |
|
407 |
int realColumnIndex = convertColumnIndexToModel(colIndex); |
|
408 |
||
409 |
if (realColumnIndex == COL_NAME) { |
|
410 |
LocalVirtualMachine vmd = vmModel.vmAt(rowIndex); |
|
411 |
tip = vmd.toString(); |
|
412 |
} |
|
413 |
return tip; |
|
414 |
} |
|
415 |
||
416 |
public TableCellRenderer getCellRenderer(int row, int column) { |
|
417 |
return new DefaultTableCellRenderer() { |
|
418 |
public Component getTableCellRendererComponent(JTable table, |
|
419 |
Object value, |
|
420 |
boolean isSelected, |
|
421 |
boolean hasFocus, |
|
422 |
int row, |
|
423 |
int column) { |
|
424 |
Component comp = |
|
425 |
super.getTableCellRendererComponent(table, value, isSelected, |
|
426 |
hasFocus, row, column); |
|
427 |
||
428 |
if (!isSelected) { |
|
429 |
LocalVirtualMachine lvm = vmModel.vmAt(row); |
|
430 |
if (!lvm.isManageable() && !lvm.isAttachable()) { |
|
431 |
comp.setForeground(disabledTableCellColor); |
|
432 |
} |
|
433 |
} |
|
434 |
||
435 |
if (comp instanceof JLabel) { |
|
436 |
JLabel label = (JLabel)comp; |
|
437 |
label.setBorder(rendererBorder); |
|
438 |
||
439 |
if (value instanceof Integer) { |
|
440 |
label.setHorizontalAlignment(JLabel.RIGHT); |
|
441 |
} |
|
442 |
} |
|
443 |
||
444 |
return comp; |
|
445 |
} |
|
446 |
}; |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
public void setConnectionParameters(String url, |
|
451 |
String host, |
|
452 |
int port, |
|
453 |
String userName, |
|
454 |
String password, |
|
455 |
String msg) { |
|
456 |
if ((url != null && url.length() > 0) || |
|
457 |
(host != null && host.length() > 0 && port > 0)) { |
|
458 |
||
459 |
remoteRadioButton.setSelected(true); |
|
460 |
if (url != null && url.length() > 0) { |
|
461 |
remoteTF.setText(url); |
|
462 |
} else { |
|
463 |
remoteTF.setText(host+":"+port); |
|
464 |
} |
|
465 |
userNameTF.setText((userName != null) ? userName : ""); |
|
466 |
passwordTF.setText((password != null) ? password : ""); |
|
467 |
||
468 |
statusBar.setText((msg != null) ? msg : ""); |
|
469 |
if (getPreferredSize().width > getWidth()) { |
|
470 |
pack(); |
|
471 |
} |
|
472 |
remoteTF.requestFocus(); |
|
473 |
remoteTF.selectAll(); |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
||
478 |
public void itemStateChanged(ItemEvent ev) { |
|
479 |
if (!localRadioButton.isSelected()) { |
|
480 |
vmTable.getSelectionModel().clearSelection(); |
|
481 |
} |
|
482 |
updateButtonStates(); |
|
483 |
} |
|
484 |
||
485 |
private void updateButtonStates() { |
|
486 |
boolean connectEnabled = false; |
|
487 |
||
488 |
if (remoteRadioButton.isSelected()) { |
|
489 |
connectEnabled = JConsole.isValidRemoteString(remoteTF.getText()); |
|
490 |
} else if (localRadioButton != null && localRadioButton.isSelected()) { |
|
491 |
int row = vmTable.getSelectedRow(); |
|
492 |
if (row >= 0) { |
|
493 |
LocalVirtualMachine lvm = vmModel.vmAt(row); |
|
494 |
connectEnabled = (lvm.isManageable() || lvm.isAttachable()); |
|
495 |
} |
|
496 |
} |
|
497 |
||
498 |
connectAction.setEnabled(connectEnabled); |
|
499 |
} |
|
500 |
||
501 |
public void insertUpdate(DocumentEvent e) { |
|
502 |
updateButtonStates(); |
|
503 |
} |
|
504 |
||
505 |
public void removeUpdate(DocumentEvent e) { |
|
506 |
updateButtonStates(); |
|
507 |
} |
|
508 |
||
509 |
public void changedUpdate(DocumentEvent e) { |
|
510 |
updateButtonStates(); |
|
511 |
} |
|
512 |
||
513 |
public void focusGained(FocusEvent e) { |
|
514 |
Object source = e.getSource(); |
|
515 |
Component opposite = e.getOppositeComponent(); |
|
516 |
||
517 |
if (!e.isTemporary() && |
|
518 |
source instanceof JTextField && |
|
519 |
opposite instanceof JComponent && |
|
520 |
SwingUtilities.getRootPane(opposite) == getRootPane()) { |
|
521 |
||
522 |
((JTextField)source).selectAll(); |
|
523 |
} |
|
524 |
||
525 |
if (source == remoteTF) { |
|
526 |
remoteRadioButton.setSelected(true); |
|
527 |
} else if (source == vmTable) { |
|
528 |
localRadioButton.setSelected(true); |
|
529 |
if (vmModel.getRowCount() == 1) { |
|
530 |
// if there's only one process then select the row |
|
531 |
vmTable.setRowSelectionInterval(0, 0); |
|
532 |
} |
|
533 |
} |
|
534 |
updateButtonStates(); |
|
535 |
} |
|
536 |
||
537 |
public void focusLost(FocusEvent e) { |
|
538 |
} |
|
539 |
||
540 |
public void keyTyped(KeyEvent e) { |
|
541 |
char c = e.getKeyChar(); |
|
542 |
if (c == KeyEvent.VK_ESCAPE) { |
|
543 |
setVisible(false); |
|
544 |
} else if (!(Character.isDigit(c) || |
|
545 |
c == KeyEvent.VK_BACK_SPACE || |
|
546 |
c == KeyEvent.VK_DELETE)) { |
|
547 |
getToolkit().beep(); |
|
548 |
e.consume(); |
|
549 |
} |
|
550 |
} |
|
551 |
||
552 |
public void setVisible(boolean b) { |
|
553 |
boolean wasVisible = isVisible(); |
|
554 |
super.setVisible(b); |
|
555 |
if (b && !wasVisible) { |
|
556 |
SwingUtilities.invokeLater(new Runnable() { |
|
557 |
public void run() { |
|
558 |
if (remoteRadioButton.isSelected()) { |
|
559 |
remoteTF.requestFocus(); |
|
560 |
remoteTF.selectAll(); |
|
561 |
} |
|
562 |
} |
|
563 |
}); |
|
564 |
} |
|
565 |
} |
|
566 |
||
567 |
public void keyPressed(KeyEvent e) { |
|
568 |
} |
|
569 |
||
570 |
public void keyReleased(KeyEvent e) { |
|
571 |
} |
|
572 |
||
573 |
||
574 |
// ListSelectionListener interface |
|
575 |
public void valueChanged(ListSelectionEvent e) { |
|
576 |
updateButtonStates(); |
|
577 |
String labelText = " "; // Non-empty to reserve vertical space |
|
578 |
int row = vmTable.getSelectedRow(); |
|
579 |
if (row >= 0) { |
|
580 |
LocalVirtualMachine lvm = vmModel.vmAt(row); |
|
581 |
if (!lvm.isManageable()) { |
|
582 |
if (lvm.isAttachable()) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
583 |
labelText = Messages.MANAGEMENT_WILL_BE_ENABLED; |
2 | 584 |
} else { |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
585 |
labelText = Messages.MANAGEMENT_NOT_ENABLED; |
2 | 586 |
} |
587 |
} |
|
588 |
} |
|
589 |
String colorStr = |
|
590 |
String.format("%06x", hintTextColor.getRGB() & 0xFFFFFF); |
|
591 |
localMessageLabel.setText("<html><font color=#" + colorStr + ">" + labelText); |
|
592 |
} |
|
593 |
// ---- |
|
594 |
||
595 |
||
596 |
// Refresh the list of managed VMs |
|
597 |
public void refresh() { |
|
598 |
if (vmModel != null) { |
|
599 |
// Remember selection |
|
600 |
LocalVirtualMachine selected = null; |
|
601 |
int row = vmTable.getSelectedRow(); |
|
602 |
if (row >= 0) { |
|
603 |
selected = vmModel.vmAt(row); |
|
604 |
} |
|
605 |
||
606 |
vmModel.refresh(); |
|
607 |
||
608 |
int selectRow = -1; |
|
609 |
int n = vmModel.getRowCount(); |
|
610 |
if (selected != null) { |
|
611 |
for (int i = 0; i < n; i++) { |
|
612 |
LocalVirtualMachine lvm = vmModel.vmAt(i); |
|
613 |
if (selected.vmid() == lvm.vmid() && |
|
614 |
selected.toString().equals(lvm.toString())) { |
|
615 |
||
616 |
selectRow = i; |
|
617 |
break; |
|
618 |
} |
|
619 |
} |
|
620 |
} |
|
621 |
if (selectRow > -1) { |
|
622 |
vmTable.setRowSelectionInterval(selectRow, selectRow); |
|
623 |
} else { |
|
624 |
vmTable.getSelectionModel().clearSelection(); |
|
625 |
} |
|
626 |
||
627 |
Dimension dim = vmTable.getPreferredSize(); |
|
628 |
||
629 |
// Tricky. Reduce height by one to avoid double line at bottom, |
|
630 |
// but that causes a scroll bar to appear, so remove it. |
|
631 |
dim.height = Math.min(dim.height-1, 100); |
|
632 |
localTableScrollPane.setVerticalScrollBarPolicy((dim.height < 100) |
|
633 |
? JScrollPane.VERTICAL_SCROLLBAR_NEVER |
|
634 |
: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
|
635 |
localTableScrollPane.getViewport().setMinimumSize(dim); |
|
636 |
localTableScrollPane.getViewport().setPreferredSize(dim); |
|
637 |
} |
|
638 |
pack(); |
|
639 |
setLocationRelativeTo(jConsole); |
|
640 |
} |
|
641 |
||
642 |
// Represents the list of managed VMs as a tabular data model. |
|
643 |
private static class ManagedVmTableModel extends AbstractTableModel { |
|
644 |
private static String[] columnNames = { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
645 |
Messages.COLUMN_NAME, |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
646 |
Messages.COLUMN_PID, |
2 | 647 |
}; |
648 |
||
649 |
private List<LocalVirtualMachine> vmList; |
|
650 |
||
651 |
public int getColumnCount() { |
|
652 |
return columnNames.length; |
|
653 |
} |
|
654 |
||
655 |
public String getColumnName(int col) { |
|
656 |
return columnNames[col]; |
|
657 |
} |
|
658 |
||
659 |
public synchronized int getRowCount() { |
|
660 |
return vmList.size(); |
|
661 |
} |
|
662 |
||
663 |
public synchronized Object getValueAt(int row, int col) { |
|
664 |
assert col >= 0 && col <= columnNames.length; |
|
665 |
LocalVirtualMachine vm = vmList.get(row); |
|
666 |
switch (col) { |
|
667 |
case COL_NAME: return vm.displayName(); |
|
668 |
case COL_PID: return vm.vmid(); |
|
669 |
default: return null; |
|
670 |
} |
|
671 |
} |
|
672 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
673 |
public Class<?> getColumnClass(int column) { |
2 | 674 |
switch (column) { |
675 |
case COL_NAME: return String.class; |
|
676 |
case COL_PID: return Integer.class; |
|
677 |
default: return super.getColumnClass(column); |
|
678 |
} |
|
679 |
} |
|
680 |
||
681 |
public ManagedVmTableModel() { |
|
682 |
refresh(); |
|
683 |
} |
|
684 |
||
685 |
||
686 |
public synchronized LocalVirtualMachine vmAt(int pos) { |
|
687 |
return vmList.get(pos); |
|
688 |
} |
|
689 |
||
690 |
public synchronized void refresh() { |
|
691 |
Map<Integer, LocalVirtualMachine> map = |
|
692 |
LocalVirtualMachine.getAllVirtualMachines(); |
|
693 |
vmList = new ArrayList<LocalVirtualMachine>(); |
|
694 |
vmList.addAll(map.values()); |
|
695 |
||
696 |
// data has changed |
|
697 |
fireTableDataChanged(); |
|
698 |
} |
|
699 |
} |
|
700 |
||
701 |
// A blank component that takes up as much space as the |
|
702 |
// button part of a JRadioButton. |
|
703 |
private static class Padder extends JPanel { |
|
704 |
JRadioButton radioButton; |
|
705 |
||
706 |
Padder(JRadioButton radioButton) { |
|
707 |
this.radioButton = radioButton; |
|
708 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
709 |
setAccessibleName(this, Messages.BLANK); |
2 | 710 |
} |
711 |
||
712 |
public Dimension getPreferredSize() { |
|
713 |
Rectangle r = getTextRectangle(radioButton); |
|
714 |
int w = (r != null && r.x > 8) ? r.x : 22; |
|
715 |
||
716 |
return new Dimension(w, 0); |
|
717 |
} |
|
718 |
||
719 |
private static Rectangle getTextRectangle(AbstractButton button) { |
|
720 |
String text = button.getText(); |
|
721 |
Icon icon = (button.isEnabled()) ? button.getIcon() : button.getDisabledIcon(); |
|
722 |
||
723 |
if (icon == null && button.getUI() instanceof BasicRadioButtonUI) { |
|
724 |
icon = ((BasicRadioButtonUI)button.getUI()).getDefaultIcon(); |
|
725 |
} |
|
726 |
||
727 |
if ((icon == null) && (text == null)) { |
|
728 |
return null; |
|
729 |
} |
|
730 |
||
731 |
Rectangle paintIconR = new Rectangle(); |
|
732 |
Rectangle paintTextR = new Rectangle(); |
|
733 |
Rectangle paintViewR = new Rectangle(); |
|
734 |
Insets paintViewInsets = new Insets(0, 0, 0, 0); |
|
735 |
||
736 |
paintViewInsets = button.getInsets(paintViewInsets); |
|
737 |
paintViewR.x = paintViewInsets.left; |
|
738 |
paintViewR.y = paintViewInsets.top; |
|
739 |
paintViewR.width = button.getWidth() - (paintViewInsets.left + paintViewInsets.right); |
|
740 |
paintViewR.height = button.getHeight() - (paintViewInsets.top + paintViewInsets.bottom); |
|
741 |
||
742 |
Graphics g = button.getGraphics(); |
|
743 |
if (g == null) { |
|
744 |
return null; |
|
745 |
} |
|
746 |
SwingUtilities.layoutCompoundLabel(button, |
|
747 |
g.getFontMetrics(), |
|
748 |
text, |
|
749 |
icon, |
|
750 |
button.getVerticalAlignment(), |
|
751 |
button.getHorizontalAlignment(), |
|
752 |
button.getVerticalTextPosition(), |
|
753 |
button.getHorizontalTextPosition(), |
|
754 |
paintViewR, |
|
755 |
paintIconR, |
|
756 |
paintTextR, |
|
757 |
button.getIconTextGap()); |
|
758 |
||
759 |
return paintTextR; |
|
760 |
} |
|
761 |
} |
|
762 |
||
763 |
} |