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.awt.*;
|
|
29 |
import java.awt.event.*;
|
|
30 |
|
|
31 |
import javax.swing.*;
|
|
32 |
|
|
33 |
@SuppressWarnings("serial")
|
|
34 |
public class LabeledComponent extends JPanel {
|
|
35 |
JPanel rightPanel;
|
|
36 |
String labelStr, valueLabelStr, compoundStr;
|
|
37 |
JLabel label;
|
|
38 |
JComponent comp;
|
|
39 |
|
|
40 |
public LabeledComponent(String text, JComponent comp) {
|
|
41 |
this(text, 0, comp);
|
|
42 |
}
|
|
43 |
|
|
44 |
public LabeledComponent(String text, int mnemonic, JComponent comp) {
|
|
45 |
super(new BorderLayout(6, 6));
|
|
46 |
|
|
47 |
this.labelStr = text;
|
|
48 |
this.label = new JLabel(text, JLabel.RIGHT);
|
|
49 |
this.comp = comp;
|
|
50 |
|
|
51 |
label.setLabelFor(comp);
|
|
52 |
if (mnemonic > 0) {
|
|
53 |
label.setDisplayedMnemonic(mnemonic);
|
|
54 |
}
|
|
55 |
|
|
56 |
add(label, BorderLayout.WEST);
|
|
57 |
add(comp, BorderLayout.CENTER);
|
|
58 |
}
|
|
59 |
|
|
60 |
public void setLabel(String str) {
|
|
61 |
this.labelStr = str;
|
|
62 |
updateLabel();
|
|
63 |
}
|
|
64 |
|
|
65 |
public void setValueLabel(String str) {
|
|
66 |
this.valueLabelStr = str;
|
|
67 |
updateLabel();
|
|
68 |
}
|
|
69 |
|
|
70 |
private void updateLabel() {
|
|
71 |
String str = labelStr;
|
|
72 |
label.setText(str);
|
|
73 |
this.compoundStr = str;
|
|
74 |
JComponent container = (JComponent)getParent();
|
|
75 |
LabeledComponent.layout(container);
|
|
76 |
}
|
|
77 |
|
|
78 |
public static void layout(Container container) {
|
|
79 |
int wMax = 0;
|
|
80 |
|
|
81 |
for (Component c : container.getComponents()) {
|
|
82 |
if (c instanceof LabeledComponent) {
|
|
83 |
LabeledComponent lc = (LabeledComponent)c;
|
|
84 |
lc.label.setPreferredSize(null);
|
|
85 |
// int w = lc.label.getMinimumSize().width;
|
|
86 |
int w = lc.label.getPreferredSize().width;
|
|
87 |
if (w > wMax) {
|
|
88 |
wMax = w;
|
|
89 |
}
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
for (Component c : container.getComponents()) {
|
|
94 |
if (c instanceof LabeledComponent) {
|
|
95 |
LabeledComponent lc = (LabeledComponent)c;
|
|
96 |
JLabel label = lc.label;
|
|
97 |
int h = label.getPreferredSize().height;
|
|
98 |
|
|
99 |
label.setPreferredSize(new Dimension(wMax, h));
|
|
100 |
label.setHorizontalAlignment(JLabel.RIGHT);
|
|
101 |
}
|
|
102 |
}
|
|
103 |
}
|
|
104 |
}
|