KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > UIUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss.ui;
21
22 import javax.swing.*;
23 import javax.swing.plaf.basic.BasicHTML JavaDoc;
24 import javax.swing.text.View JavaDoc;
25 import java.awt.*;
26
27 /**
28  * Swing toolkit utility methods.
29  *
30  * @author Petr Kuzel
31  */

32 public class UIUtils {
33
34     /**
35      * Computes and sets label preferred size based
36      * on <b>preferred width</b>. Compare to standard preferred
37      * size algorithm that takes width from longest line
38      * (it does not count with fact that HTML label wraps).
39      *
40      * <p>You can rollback by <code>label.setPreferredSize(null)</code>;
41      *
42      * @param label HTML enabled imageless label to be statically patched
43      * @param ex preferred width in number of <tt>x</tt> characters in current label font
44      */

45     public static void computePreferredSize(JLabel label, int ex) {
46         assert ex > 0;
47         FontMetrics fm = label.getFontMetrics(label.getFont());
48         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(ex);
49         for (int i = 0; i<ex; i++) {
50             sb.append("x"); // NOI18N
51
}
52         int exWidth = SwingUtilities.computeStringWidth(fm, sb.toString());
53         View JavaDoc v = BasicHTML.createHTMLView(label, label.getText());
54         v.setSize(exWidth, 0);
55         int width = (int) v.getPreferredSpan(View.X_AXIS);
56         int height = (int) v.getPreferredSpan(View.Y_AXIS);
57
58         Insets insets = label.getInsets();
59         int dx = insets.left + insets.right;
60         int dy = insets.top + insets.bottom;
61
62         label.setPreferredSize(new Dimension(width+dx, height+dy));
63     }
64 }
65
Popular Tags