001: /*
002: * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jconsole;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030:
031: import javax.swing.*;
032:
033: @SuppressWarnings("serial")
034: public class LabeledComponent extends JPanel {
035: JPanel rightPanel;
036: String labelStr, valueLabelStr, compoundStr;
037: JLabel label;
038: JComponent comp;
039:
040: public LabeledComponent(String text, JComponent comp) {
041: this (text, 0, comp);
042: }
043:
044: public LabeledComponent(String text, int mnemonic, JComponent comp) {
045: super (new BorderLayout(6, 6));
046:
047: this .labelStr = text;
048: this .label = new JLabel(text, JLabel.RIGHT);
049: this .comp = comp;
050:
051: label.setLabelFor(comp);
052: if (mnemonic > 0) {
053: label.setDisplayedMnemonic(mnemonic);
054: }
055:
056: add(label, BorderLayout.WEST);
057: add(comp, BorderLayout.CENTER);
058: }
059:
060: public void setLabel(String str) {
061: this .labelStr = str;
062: updateLabel();
063: }
064:
065: public void setValueLabel(String str) {
066: this .valueLabelStr = str;
067: updateLabel();
068: }
069:
070: private void updateLabel() {
071: String str = labelStr;
072: label.setText(str);
073: this .compoundStr = str;
074: JComponent container = (JComponent) getParent();
075: LabeledComponent.layout(container);
076: }
077:
078: public static void layout(Container container) {
079: int wMax = 0;
080:
081: for (Component c : container.getComponents()) {
082: if (c instanceof LabeledComponent) {
083: LabeledComponent lc = (LabeledComponent) c;
084: lc.label.setPreferredSize(null);
085: // int w = lc.label.getMinimumSize().width;
086: int w = lc.label.getPreferredSize().width;
087: if (w > wMax) {
088: wMax = w;
089: }
090: }
091: }
092:
093: for (Component c : container.getComponents()) {
094: if (c instanceof LabeledComponent) {
095: LabeledComponent lc = (LabeledComponent) c;
096: JLabel label = lc.label;
097: int h = label.getPreferredSize().height;
098:
099: label.setPreferredSize(new Dimension(wMax, h));
100: label.setHorizontalAlignment(JLabel.RIGHT);
101: }
102: }
103: }
104: }
|