1 19 package org.openide.awt; 20 21 import java.awt.*; 22 23 24 34 public class EqualFlowLayout extends FlowLayout { 35 36 static final long serialVersionUID = -1996929627282401218L; 37 38 43 public EqualFlowLayout() { 44 super(); 45 } 46 47 56 public EqualFlowLayout(int align) { 57 super(align); 58 } 59 60 72 public EqualFlowLayout(int align, int hgap, int vgap) { 73 super(align, hgap, vgap); 74 } 75 76 private int getMaximumWidth(Container target) { 77 int maxWidth = 0; 78 79 synchronized (target.getTreeLock()) { 80 int nmembers = target.getComponentCount(); 81 82 for (int i = 0; i < nmembers; i++) { 83 Component m = target.getComponent(i); 84 85 if (m.isVisible()) { 86 Dimension d = m.getPreferredSize(); 87 maxWidth = Math.max(d.width, maxWidth); 88 } 89 } 90 } 91 92 return maxWidth; 93 } 94 95 106 public Dimension preferredLayoutSize(Container target) { 107 int maxWidth = getMaximumWidth(target); 108 109 synchronized (target.getTreeLock()) { 110 Dimension dim = new Dimension(0, 0); 111 int nmembers = target.getComponentCount(); 112 113 for (int i = 0; i < nmembers; i++) { 114 Component m = target.getComponent(i); 115 116 if (m.isVisible()) { 117 Dimension d = m.getPreferredSize(); 118 dim.height = Math.max(dim.height, d.height); 119 120 if (i > 0) { 121 dim.width += getHgap(); 122 } 123 124 dim.width += maxWidth; 125 } 126 } 127 128 Insets insets = target.getInsets(); 129 dim.width += (insets.left + insets.right + (getHgap() * 2)); 130 dim.height += (insets.top + insets.bottom + (getVgap() * 2)); 131 132 return dim; 133 } 134 } 135 136 147 public Dimension minimumLayoutSize(Container target) { 148 synchronized (target.getTreeLock()) { 149 Dimension dim = new Dimension(0, 0); 150 int nmembers = target.getComponentCount(); 151 152 for (int i = 0; i < nmembers; i++) { 153 Component m = target.getComponent(i); 154 155 if (m.isVisible()) { 156 Dimension d = m.getMinimumSize(); 157 dim.height = Math.max(dim.height, d.height); 158 159 if (i > 0) { 160 dim.width += getHgap(); 161 } 162 163 dim.width += d.width; 164 } 165 } 166 167 Insets insets = target.getInsets(); 168 dim.width += (insets.left + insets.right + (getHgap() * 2)); 169 dim.height += (insets.top + insets.bottom + (getVgap() * 2)); 170 171 return dim; 172 } 173 } 174 175 185 private void moveComponents2(Container target, int x, int y, int width, int height, int rowStart, int rowEnd) { 186 synchronized (target.getTreeLock()) { 187 switch (getAlignment()) { 188 case LEFT: 189 break; 190 191 case CENTER: 192 x += (width / 2); 193 194 break; 195 196 case RIGHT: 197 x += width; 198 199 break; 200 } 201 202 for (int i = rowStart; i < rowEnd; i++) { 203 Component m = target.getComponent(i); 204 205 if (m.isVisible()) { 206 m.setLocation(x, y + ((height - m.getSize().height) / 2)); 207 x += (getHgap() + m.getSize().width); 208 } 209 } 210 } 211 } 212 213 223 public void layoutContainer(Container target) { 224 int maxWidth = getMaximumWidth(target); 225 226 synchronized (target.getTreeLock()) { 227 Insets insets = target.getInsets(); 228 int maxwidth = target.getSize().width - (insets.left + insets.right + (getHgap() * 2)); 229 int nmembers = target.getComponentCount(); 230 int x = 0; 231 int y = insets.top + getVgap(); 232 int rowh = 0; 233 int start = 0; 234 235 for (int i = 0; i < nmembers; i++) { 236 Component m = target.getComponent(i); 237 238 if (m.isVisible()) { 239 Dimension d = m.getPreferredSize(); 240 d.width = maxWidth; 241 m.setSize(d.width, d.height); 242 243 if ((x == 0) || ((x + d.width) <= maxwidth)) { 244 if (x > 0) { 245 x += getHgap(); 246 } 247 248 x += d.width; 249 rowh = Math.max(rowh, d.height); 250 } else { 251 moveComponents2(target, insets.left + getHgap(), y, maxwidth - x, rowh, start, i); 252 x = d.width; 253 y += (getVgap() + rowh); 254 rowh = d.height; 255 start = i; 256 } 257 } 258 } 259 260 moveComponents2(target, insets.left + getHgap(), y, maxwidth - x, rowh, start, nmembers); 261 } 262 } 263 } 264 | Popular Tags |