001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 2004 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.junit;
043:
044: import java.awt.Dimension;
045: import java.awt.LayoutManager;
046: import javax.swing.JPanel;
047:
048: /**
049: * Panel whose dimension can be restricted by its preferred size.
050: * It is supposed to be used in containers using <code>BoxLayout</code>.
051: *
052: * @see javax.swing.BoxLayout BoxLayout
053: * @author Marian Petras
054: */
055: public class SizeRestrictedPanel extends JPanel {
056:
057: /** whether the panel's width is restricted */
058: private final boolean widthRestriction;
059: /** whether the panel's height is restricted */
060: private final boolean heightRestriction;
061:
062: /**
063: * Creates a panel with flow layout, restricted in both directions.
064: */
065: public SizeRestrictedPanel() {
066: this (true, true);
067: }
068:
069: /**
070: * Creates a panel with flow layout, with width and/or height restricted.
071: *
072: * @param widthRestriction whether the panel's width should be restricted
073: * @param heightRestriction whether the panel's height should be restricted
074: */
075: public SizeRestrictedPanel(boolean widthRestriction,
076: boolean heightRestriction) {
077: super ();
078: this .widthRestriction = widthRestriction;
079: this .heightRestriction = heightRestriction;
080: }
081:
082: /**
083: * Creates a panel with the specified layout manager and with size
084: * restricted in both directions.
085: *
086: * @param layoutMgr layout manager for this panel
087: */
088: public SizeRestrictedPanel(LayoutManager layoutMgr) {
089: this (layoutMgr, true, true);
090: }
091:
092: /**
093: * Creates a panel with the specified layout manager and with width and/or
094: * height restricted.
095: *
096: * @param layoutMgr layout manager for this panel
097: * @param widthRestriction whether the panel's width should be restricted
098: * @param heightRestriction whether the panel's height should be restricted
099: */
100: public SizeRestrictedPanel(LayoutManager layoutMgr,
101: boolean widthRestriction, boolean heightRestriction) {
102: super (layoutMgr);
103: this .widthRestriction = widthRestriction;
104: this .heightRestriction = heightRestriction;
105: }
106:
107: /**
108: * Returns maximum size of this panel.
109: * The maximum size can be restricted in width, height or in both
110: * directions, depending on parameters passed to the constructor.
111: *
112: * @return dimension returned from original <code>getMaximumSize()</code>
113: * and then modified according to restrictions specified
114: * by the constructor's parameters
115: */
116: public Dimension getMaximumSize() {
117: if (widthRestriction && heightRestriction) { //both true
118: return getPreferredSize();
119: }
120: if (widthRestriction == heightRestriction) { //both false
121: return super .getMaximumSize();
122: }
123:
124: Dimension maximumSize = super.getMaximumSize();
125: if (widthRestriction) {
126: maximumSize.width = getPreferredSize().width;
127: } else {
128: maximumSize.height = getPreferredSize().height;
129: }
130: return maximumSize;
131: }
132:
133: }
|