001: /*
002: * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
018: * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
019: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
020: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
021: * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
022: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
024: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
025: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
027: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: */
030:
031: package rcm.awt;
032:
033: import java.awt.*;
034:
035: public class BorderPanel extends Panel {
036:
037: int left, top, bottom, right;
038:
039: public BorderPanel(Insets insets) {
040: this .left = insets.left;
041: this .top = insets.top;
042: this .bottom = insets.bottom;
043: this .right = insets.right;
044: }
045:
046: public BorderPanel(int left, int top, int bottom, int right) {
047: this .left = left;
048: this .top = top;
049: this .bottom = bottom;
050: this .right = right;
051: }
052:
053: public void layout() {
054: Dimension d = getSize();
055:
056: int x = left;
057: int y = top;
058: int w = d.width - left - right;
059: int h = d.height - top - bottom;
060:
061: Component[] comps = getComponents();
062: for (int i = 0; i < comps.length; ++i)
063: comps[i].setBounds(x, y, w, h);
064: }
065:
066: public Dimension getPreferredSize() {
067: Dimension max = new Dimension(0, 0);
068:
069: Component[] comps = getComponents();
070: for (int i = 0; i < comps.length; ++i) {
071: Dimension d = comps[i].getPreferredSize();
072: max.width = Math.max(d.width, max.width);
073: max.height = Math.max(d.height, max.height);
074: }
075:
076: max.width += left + right;
077: max.height += top + bottom;
078: return max;
079: }
080:
081: public Dimension getMinimumSize() {
082: Dimension max = new Dimension(0, 0);
083:
084: Component[] comps = getComponents();
085: for (int i = 0; i < comps.length; ++i) {
086: Dimension d = comps[i].getMinimumSize();
087: max.width = Math.max(d.width, max.width);
088: max.height = Math.max(d.height, max.height);
089: }
090:
091: max.width += left + right;
092: max.height += top + bottom;
093: return max;
094: }
095:
096: public static Panel wrap(Component comp, Insets insets) {
097: Panel p = new BorderPanel(insets);
098: p.add(comp);
099: return p;
100: }
101:
102: public static Panel wrap(Component comp, int left, int top,
103: int bottom, int right) {
104: Panel p = new BorderPanel(left, top, bottom, right);
105: p.add(comp);
106: return p;
107: }
108:
109: public static Panel wrap(Component comp, int horiz, int vert) {
110: Panel p = new BorderPanel(horiz, vert, horiz, vert);
111: p.add(comp);
112: return p;
113: }
114:
115: public static Panel wrap(Component comp, int all) {
116: Panel p = new BorderPanel(all, all, all, all);
117: p.add(comp);
118: return p;
119: }
120: }
|