001: /*
002: * ScrollLayout.java
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2004 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.textarea;
024:
025: //{{{ Imports
026: import java.awt.*;
027: import javax.swing.border.Border;
028: import javax.swing.JComponent;
029:
030: //}}}
031:
032: public class ScrollLayout implements LayoutManager {
033: public static final String CENTER = "center";
034: public static final String RIGHT = "right";
035: public static final String LEFT = "left";
036: public static final String BOTTOM = "bottom";
037: public static final String TOP = "top";
038:
039: //{{{ addLayoutComponent() method
040: public void addLayoutComponent(String name, Component comp) {
041: if (name.equals(CENTER))
042: center = comp;
043: else if (name.equals(RIGHT))
044: right = comp;
045: else if (name.equals(LEFT))
046: left = comp;
047: else if (name.equals(BOTTOM))
048: bottom = comp;
049: else if (name.equals(TOP))
050: top = comp;
051: } //}}}
052:
053: //{{{ removeLayoutComponent() method
054: public void removeLayoutComponent(Component comp) {
055: if (center == comp)
056: center = null;
057: else if (right == comp)
058: right = null;
059: else if (left == comp)
060: left = null;
061: else if (bottom == comp)
062: bottom = null;
063: else if (top == comp)
064: top = null;
065: } //}}}
066:
067: //{{{ preferredLayoutSize() method
068: public Dimension preferredLayoutSize(Container parent) {
069: Dimension dim = new Dimension();
070: Insets insets = getInsets(parent);
071:
072: dim.width = insets.left + insets.right;
073: dim.height = insets.top + insets.bottom;
074:
075: Dimension leftPref = left.getPreferredSize();
076: dim.width += leftPref.width;
077: Dimension centerPref = center.getPreferredSize();
078: dim.width += centerPref.width;
079: dim.height += centerPref.height;
080: Dimension rightPref = right.getPreferredSize();
081: dim.width += rightPref.width;
082: Dimension bottomPref = bottom.getPreferredSize();
083: dim.height += bottomPref.height;
084: if (top != null) {
085: Dimension topPref = top.getPreferredSize();
086: dim.height += topPref.height;
087: }
088:
089: return dim;
090: } //}}}
091:
092: //{{{ minimumLayoutSize() method
093: public Dimension minimumLayoutSize(Container parent) {
094: Dimension dim = new Dimension();
095: Insets insets = getInsets(parent);
096:
097: dim.width = insets.left + insets.right;
098: dim.height = insets.top + insets.bottom;
099:
100: Dimension leftPref = left.getMinimumSize();
101: dim.width += leftPref.width;
102: Dimension centerPref = center.getMinimumSize();
103: dim.width += centerPref.width;
104: dim.height += centerPref.height;
105: Dimension rightPref = right.getMinimumSize();
106: dim.width += rightPref.width;
107: Dimension bottomPref = bottom.getMinimumSize();
108: dim.height += bottomPref.height;
109: if (top != null) {
110: Dimension topPref = top.getMinimumSize();
111: dim.height += topPref.height;
112: }
113:
114: return dim;
115: } //}}}
116:
117: //{{{ layoutContainer() method
118: public void layoutContainer(Container parent) {
119: Dimension size = parent.getSize();
120: Insets insets = getInsets(parent);
121:
122: int itop = insets.top;
123: int ileft = insets.left;
124: int ibottom = insets.bottom;
125: int iright = insets.right;
126:
127: int rightWidth = right.getPreferredSize().width;
128: int leftWidth = left.getPreferredSize().width;
129: int topHeight;
130: if (top != null) {
131: topHeight = top.getPreferredSize().height;
132: } else {
133: topHeight = 0;
134: }
135: int bottomHeight = bottom.getPreferredSize().height;
136: int centerWidth = Math.max(0, size.width - leftWidth
137: - rightWidth - ileft - iright);
138: int centerHeight = Math.max(0, size.height - topHeight
139: - bottomHeight - itop - ibottom);
140:
141: left
142: .setBounds(ileft, itop + topHeight, leftWidth,
143: centerHeight);
144:
145: center.setBounds(ileft + leftWidth, itop + topHeight,
146: centerWidth, centerHeight);
147:
148: right.setBounds(ileft + leftWidth + centerWidth, itop
149: + topHeight, rightWidth, centerHeight);
150:
151: bottom.setBounds(ileft, itop + topHeight + centerHeight, Math
152: .max(0, size.width - bottom.getHeight() - ileft
153: - iright), bottomHeight);
154: if (top != null) {
155: top.setBounds(ileft, itop, leftWidth + centerWidth
156: + rightWidth, topHeight);
157: }
158: } //}}}
159:
160: //{{{ Private members
161: private Component center;
162: private Component left;
163: private Component right;
164: private Component bottom;
165: private Component top;
166:
167: //{{{ getInsets() method
168: private Insets getInsets(Component parent) {
169: Border border = ((JComponent) parent).getBorder();
170: if (border == null)
171: return new Insets(0, 0, 0, 0);
172: else
173: return border.getBorderInsets(parent);
174: } //}}}
175:
176: //}}}
177: }
|