KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > textarea > ScrollLayout


1 /*
2  * ScrollLayout.java
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2004 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.textarea;
24
25 //{{{ Imports
26
import java.awt.*;
27 import javax.swing.border.Border JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 //}}}
30

31 public class ScrollLayout implements LayoutManager
32 {
33     public static final String JavaDoc CENTER = "center";
34     public static final String JavaDoc RIGHT = "right";
35     public static final String JavaDoc LEFT = "left";
36     public static final String JavaDoc BOTTOM = "bottom";
37     public static final String JavaDoc TOP = "top";
38
39     //{{{ addLayoutComponent() method
40
public void addLayoutComponent(String JavaDoc name, Component JavaDoc comp)
41     {
42         if(name.equals(CENTER))
43             center = comp;
44         else if(name.equals(RIGHT))
45             right = comp;
46         else if(name.equals(LEFT))
47             left = comp;
48         else if(name.equals(BOTTOM))
49             bottom = comp;
50         else if(name.equals(TOP))
51             top = comp;
52     } //}}}
53

54     //{{{ removeLayoutComponent() method
55
public void removeLayoutComponent(Component JavaDoc comp)
56     {
57         if(center == comp)
58             center = null;
59         else if(right == comp)
60             right = null;
61         else if(left == comp)
62             left = null;
63         else if(bottom == comp)
64             bottom = null;
65         else if(top == comp)
66             top = null;
67     } //}}}
68

69     //{{{ preferredLayoutSize() method
70
public Dimension preferredLayoutSize(Container parent)
71     {
72         Dimension dim = new Dimension();
73         Insets insets = getInsets(parent);
74
75         dim.width = insets.left + insets.right;
76         dim.height = insets.top + insets.bottom;
77
78         Dimension leftPref = left.getPreferredSize();
79         dim.width += leftPref.width;
80         Dimension centerPref = center.getPreferredSize();
81         dim.width += centerPref.width;
82         dim.height += centerPref.height;
83         Dimension rightPref = right.getPreferredSize();
84         dim.width += rightPref.width;
85         Dimension bottomPref = bottom.getPreferredSize();
86         dim.height += bottomPref.height;
87         if(top != null)
88         {
89             Dimension topPref = top.getPreferredSize();
90             dim.height += topPref.height;
91         }
92
93         return dim;
94     } //}}}
95

96     //{{{ minimumLayoutSize() method
97
public Dimension minimumLayoutSize(Container parent)
98     {
99         Dimension dim = new Dimension();
100         Insets insets = getInsets(parent);
101
102         dim.width = insets.left + insets.right;
103         dim.height = insets.top + insets.bottom;
104
105         Dimension leftPref = left.getMinimumSize();
106         dim.width += leftPref.width;
107         Dimension centerPref = center.getMinimumSize();
108         dim.width += centerPref.width;
109         dim.height += centerPref.height;
110         Dimension rightPref = right.getMinimumSize();
111         dim.width += rightPref.width;
112         Dimension bottomPref = bottom.getMinimumSize();
113         dim.height += bottomPref.height;
114         if(top != null)
115         {
116             Dimension topPref = top.getMinimumSize();
117             dim.height += topPref.height;
118         }
119         
120         return dim;
121     } //}}}
122

123     //{{{ layoutContainer() method
124
public void layoutContainer(Container parent)
125     {
126         Dimension size = parent.getSize();
127         Insets insets = getInsets(parent);
128
129         int itop = insets.top;
130         int ileft = insets.left;
131         int ibottom = insets.bottom;
132         int iright = insets.right;
133
134         int rightWidth = right.getPreferredSize().width;
135         int leftWidth = left.getPreferredSize().width;
136         int topHeight;
137         if(top != null)
138         {
139             topHeight = top.getPreferredSize().height;
140         }
141         else
142         {
143             topHeight = 0;
144         }
145         int bottomHeight = bottom.getPreferredSize().height;
146         int centerWidth = Math.max(0,size.width - leftWidth
147             - rightWidth - ileft - iright);
148         int centerHeight = Math.max(0,size.height - topHeight
149             - bottomHeight - itop - ibottom);
150             
151         left.setBounds(
152             ileft,
153             itop+topHeight,
154             leftWidth,
155             centerHeight);
156
157         center.setBounds(
158             ileft + leftWidth,
159             itop+topHeight,
160             centerWidth,
161             centerHeight);
162
163         right.setBounds(
164             ileft + leftWidth + centerWidth,
165             itop+topHeight,
166             rightWidth,
167             centerHeight);
168
169         bottom.setBounds(
170             ileft,
171             itop + topHeight + centerHeight,
172             Math.max(0,size.width - bottom.getHeight()
173                 - ileft - iright),
174             bottomHeight);
175         if(top != null)
176         {
177             top.setBounds(
178                 ileft,
179                 itop,
180                 leftWidth+centerWidth+rightWidth,
181                 topHeight);
182         }
183     } //}}}
184

185     //{{{ Private members
186
private Component JavaDoc center;
187     private Component JavaDoc left;
188     private Component JavaDoc right;
189     private Component JavaDoc bottom;
190     private Component JavaDoc top;
191
192     //{{{ getInsets() method
193
private Insets getInsets(Component JavaDoc parent)
194     {
195         Border JavaDoc border = ((JComponent JavaDoc)parent).getBorder();
196         if(border == null)
197             return new Insets(0,0,0,0);
198         else
199             return border.getBorderInsets(parent);
200     } //}}}
201

202     //}}}
203
}
204
Popular Tags