KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.LayoutManager JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25
26 /**
27  * Panel whose dimension can be restricted by its preferred size.
28  * It is supposed to be used in containers using <code>BoxLayout</code>.
29  *
30  * @see javax.swing.BoxLayout BoxLayout
31  * @author Marian Petras
32  */

33 public class SizeRestrictedPanel extends JPanel JavaDoc {
34
35     /** whether the panel's width is restricted */
36     private final boolean widthRestriction;
37     /** whether the panel's height is restricted */
38     private final boolean heightRestriction;
39     
40     /**
41      * Creates a panel with flow layout, restricted in both directions.
42      */

43     public SizeRestrictedPanel() {
44         this(true, true);
45     }
46     
47     /**
48      * Creates a panel with flow layout, with width and/or height restricted.
49      *
50      * @param widthRestriction whether the panel's width should be restricted
51      * @param heightRestriction whether the panel's height should be restricted
52      */

53     public SizeRestrictedPanel(boolean widthRestriction,
54                                boolean heightRestriction) {
55         super();
56         this.widthRestriction = widthRestriction;
57         this.heightRestriction = heightRestriction;
58     }
59     
60     /**
61      * Creates a panel with the specified layout manager and with size
62      * restricted in both directions.
63      *
64      * @param layoutMgr layout manager for this panel
65      */

66     public SizeRestrictedPanel(LayoutManager JavaDoc layoutMgr) {
67         this(layoutMgr, true, true);
68     }
69     
70     /**
71      * Creates a panel with the specified layout manager and with width and/or
72      * height restricted.
73      *
74      * @param layoutMgr layout manager for this panel
75      * @param widthRestriction whether the panel's width should be restricted
76      * @param heightRestriction whether the panel's height should be restricted
77      */

78     public SizeRestrictedPanel(LayoutManager JavaDoc layoutMgr,
79                                boolean widthRestriction,
80                                boolean heightRestriction) {
81         super(layoutMgr);
82         this.widthRestriction = widthRestriction;
83         this.heightRestriction = heightRestriction;
84     }
85     
86     /**
87      * Returns maximum size of this panel.
88      * The maximum size can be restricted in width, height or in both
89      * directions, depending on parameters passed to the constructor.
90      *
91      * @return dimension returned from original <code>getMaximumSize()</code>
92      * and then modified according to restrictions specified
93      * by the constructor's parameters
94      */

95     public Dimension JavaDoc getMaximumSize() {
96         if (widthRestriction && heightRestriction) { //both true
97
return getPreferredSize();
98         }
99         if (widthRestriction == heightRestriction) { //both false
100
return super.getMaximumSize();
101         }
102         
103         Dimension JavaDoc maximumSize = super.getMaximumSize();
104         if (widthRestriction) {
105             maximumSize.width = getPreferredSize().width;
106         } else {
107             maximumSize.height = getPreferredSize().height;
108         }
109         return maximumSize;
110     }
111     
112 }
113
Popular Tags