KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > SelfResizingPanel


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 2004 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit;
21
22 import java.awt.Dimension JavaDoc;
23 import javax.swing.JPanel JavaDoc;
24 import javax.swing.SwingUtilities JavaDoc;
25
26 /**
27  * Panel that changes its hight automatically if the components inside
28  * it cannot fit in the current size. The panel checks and changes only its
29  * height, not width. The panel changes not only height of its own but also
30  * height of the toplevel <code>Window</code> it is embedded into. The size
31  * change occurs only after this panel's children are <em>painted</em>.
32  * <p>
33  * This panel is supposed to be used as a replacement for a normal
34  * <code>JPanel</code> if this panel contains a wrappable text and the panel
35  * needs to be high enough so that all lines of the possibly wrapped text
36  * can fit.
37  * <p>
38  * This class overrides method <code>paintChildren(Graphics)</code>.
39  * If overriding this method in this subclasses of this class,
40  * call <code>super.paintChildren(...)</code> so that the routine which performs
41  * the size change is not skipped.
42  *
43  * @author Marian Petras
44  */

45 public class SelfResizingPanel extends JPanel JavaDoc {
46     
47     /**
48      * <code>false</code> until this panel's children are painted
49      * for the first time
50      */

51     private boolean painted = false;
52     
53     /** Creates a new instance of SelfResizingPanel */
54     public SelfResizingPanel() {
55         super();
56     }
57     
58     /**
59      * Paints this panel's children and then displays the initial message
60      * (in the message area) if any.
61      * This method is overridden so that this panel receives a notification
62      * immediately after the children components are painted - it is necessary
63      * for computation of space needed by the message area for displaying
64      * the initial message.
65      * <p>
66      * The first time this method is called, method
67      * {@link #paintedFirstTime is called immediately after
68      * <code>super.paintChildren(..>)</code> finishes.
69      *
70      * @param g the <code>Graphics</code> context in which to paint
71      */

72     protected void paintChildren(java.awt.Graphics JavaDoc g) {
73         
74         /*
75          * This is a hack to make sure that window size adjustment
76          * is not done sooner than the text area is painted.
77          *
78          * The reason is that the window size adjustment routine
79          * needs the text area to compute height necessary for displaying
80          * the given message. But the text area does not return correct
81          * data (Dimension getPreferredSize()) until it is painted.
82          */

83         
84         super.paintChildren(g);
85         if (!painted) {
86             paintedFirstTime(g);
87             painted = true;
88         }
89     }
90     
91     /**
92      * This method is called the first time this panel's children are painted.
93      * By default, this method just calls {@link #adjustWindowSize()}.
94      *
95      * @param g <code>Graphics</code> used to paint this panel's children
96      */

97     protected void paintedFirstTime(java.awt.Graphics JavaDoc g) {
98         SwingUtilities.invokeLater(new Runnable JavaDoc() {
99             public void run() {
100                 adjustWindowSize();
101             }
102         });
103     }
104     
105     /**
106      * Checks whether the dialog is large enough for the message (if any)
107      * to be displayed and adjusts the dialogs size if it is too small.
108      * <p>
109      * Note: Resizing the dialog works only once this panel and its children
110      * are {@linkplain #paintChildren(java.awt.Graphics) painted}.
111      */

112     protected void adjustWindowSize() {
113         Dimension JavaDoc currSize = getSize();
114         int currHeight = currSize.height;
115         int prefHeight = getPreferredSize().height;
116         if (currHeight < prefHeight) {
117             int delta = prefHeight - currHeight;
118             java.awt.Window JavaDoc win = SwingUtilities.getWindowAncestor(this);
119             Dimension JavaDoc winSize = win.getSize();
120             win.setSize(winSize.width, winSize.height + delta);
121         }
122     }
123     
124     /**
125      * Has this panel's children been already painted?
126      *
127      * @return <code>true</code> if
128      * {@link #paintChildren #paintChildren(Graphics) has already been
129      * called; <code>false</code> otherwise
130      * @see #paintedFirstTime
131      */

132     protected boolean isPainted() {
133         return painted;
134     }
135     
136 }
137
Popular Tags