1 /*
2  * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
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
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
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  *
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.
24  */
25 
26 package sun.tools.jconsole;
27 
28 import java.awt.*;
29 
30 import javax.swing.*;
31 
32 @SuppressWarnings("serial")
33 public class LabeledComponent extends JPanel {
34     JPanel rightPanel;
35     String labelStr, valueLabelStr, compoundStr;
36     JLabel label;
37     JComponent comp;
38 
LabeledComponent(String text, JComponent comp)39     public LabeledComponent(String text, JComponent comp) {
40         this(text, 0, comp);
41     }
42 
LabeledComponent(String text, int mnemonic, JComponent comp)43     public LabeledComponent(String text, int mnemonic, JComponent comp) {
44         super(new BorderLayout(6, 6));
45 
46         this.labelStr = text;
47         this.label = new JLabel(text, JLabel.RIGHT);
48         this.comp = comp;
49 
50         label.setLabelFor(comp);
51         if (mnemonic > 0) {
52             label.setDisplayedMnemonic(mnemonic);
53         }
54 
55         add(label, BorderLayout.WEST);
56         add(comp,  BorderLayout.CENTER);
57     }
58 
setLabel(String str)59     public void setLabel(String str) {
60         this.labelStr = str;
61         updateLabel();
62     }
63 
setValueLabel(String str)64     public void setValueLabel(String str) {
65         this.valueLabelStr = str;
66         updateLabel();
67     }
68 
updateLabel()69     private void updateLabel() {
70         String str = labelStr;
71         label.setText(str);
72         this.compoundStr = str;
73         JComponent container = (JComponent)getParent();
74         LabeledComponent.layout(container);
75     }
76 
layout(Container container)77     public static void layout(Container container) {
78         int wMax = 0;
79 
80         for (Component c : container.getComponents()) {
81             if (c instanceof LabeledComponent) {
82                 LabeledComponent lc = (LabeledComponent)c;
83 lc.label.setPreferredSize(null);
84 //              int w = lc.label.getMinimumSize().width;
85 int w = lc.label.getPreferredSize().width;
86                 if (w > wMax) {
87                     wMax = w;
88                 }
89             }
90         }
91 
92         for (Component c : container.getComponents()) {
93             if (c instanceof LabeledComponent) {
94                 LabeledComponent lc = (LabeledComponent)c;
95                 JLabel label = lc.label;
96                 int h = label.getPreferredSize().height;
97 
98                 label.setPreferredSize(new Dimension(wMax, h));
99                 label.setHorizontalAlignment(JLabel.RIGHT);
100             }
101         }
102     }
103 }
104