1 22 23 package org.gjt.sp.jedit.textarea; 24 25 import java.awt.*; 27 import javax.swing.border.Border ; 28 import javax.swing.JComponent ; 29 31 public class ScrollLayout implements LayoutManager 32 { 33 public static final String CENTER = "center"; 34 public static final String RIGHT = "right"; 35 public static final String LEFT = "left"; 36 public static final String BOTTOM = "bottom"; 37 public static final String TOP = "top"; 38 39 public void addLayoutComponent(String name, Component 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 } 54 public void removeLayoutComponent(Component 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 } 69 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 } 96 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 } 123 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 } 185 private Component center; 187 private Component left; 188 private Component right; 189 private Component bottom; 190 private Component top; 191 192 private Insets getInsets(Component parent) 194 { 195 Border border = ((JComponent )parent).getBorder(); 196 if(border == null) 197 return new Insets(0,0,0,0); 198 else 199 return border.getBorderInsets(parent); 200 } 202 } 204 | Popular Tags |