1 19 20 package org.netbeans.modules.xml.xam.ui.layout; 21 22 import java.awt.Component ; 23 import java.awt.Container ; 24 import java.awt.Dimension ; 25 import java.awt.Insets ; 26 import java.awt.LayoutManager2 ; 27 import java.awt.Rectangle ; 28 import javax.swing.JComponent ; 29 import javax.swing.JPanel ; 30 import javax.swing.JScrollBar ; 31 import javax.swing.JScrollPane ; 32 33 146 public class SplitterLayout implements LayoutManager2 , java.io.Serializable { 147 148 public static final int VERTICAL = 0; 149 150 public static final int HORIZONTAL = 1; 151 152 static JSplitterBar dragee; 153 154 private int lastW=-1, lastH=-1; 155 private boolean newComponentAdded; 156 157 private static final long serialVersionUID = -8658291919501921765L; 158 private boolean fill = true; private Dimension originalPreferredSize; 161 162 163 public SplitterLayout() { 164 } 165 166 170 public SplitterLayout( boolean fill) { 171 setFill(fill); 172 } 173 176 public final void addLayoutComponent(Component comp, Object constraints) { 177 if (constraints == null) constraints = "1"; 179 if (constraints instanceof Integer ) { 180 newComponentAdded = true; 181 } else 182 addLayoutComponent((String )constraints, comp); 183 } 184 187 public final void addLayoutComponent(String name, Component comp) { 188 newComponentAdded = true; 190 191 } 192 193 194 public final Dimension checkLayoutSize(Container target, boolean getPrefSize) { 196 Dimension dim = new Dimension (0, 0); 198 Component c[] = target.getComponents(); 199 200 Dimension d = null; 201 for(int i = 0; i < c.length; i++) { 202 if (c[i].isVisible()) { 203 if (getPrefSize || (c[i] instanceof JSplitterBar)) { 204 d = c[i].getPreferredSize(); 205 } 206 else { 207 d = c[i].getMinimumSize(); 208 } 209 dim.height = Math.max(d.height, dim.height); 210 dim.width += d.width; 211 } 214 } 215 216 Insets insets = target.getInsets(); 217 dim.width += insets.left + insets.right; 219 dim.height += insets.top + insets.bottom; 220 return dim; 222 } 223 224 public final float getLayoutAlignmentX(Container parent) {return 0.5f;} 225 226 public final float getLayoutAlignmentY(Container parent) {return 0.5f;} 227 228 public final void invalidateLayout(Container target) {} 229 230 231 232 235 public final void layoutContainer(Container target) { 236 if (originalPreferredSize == null){ originalPreferredSize = target.getPreferredSize(); 240 } 241 Component c[] = target.getComponents(); 243 Insets insets = target.getInsets(); 244 Dimension dim = target.getSize(); 245 int top = insets.top; 247 int bottom = dim.height - insets.bottom; 248 int left = insets.left; 249 int right = dim.width - insets.right; 250 251 boolean reScaleW = false, reScaleH=false; 252 254 if (lastW == -1) { lastW = dim.width; 259 lastH = dim.height; 260 } else { 261 if (lastW != dim.width) { 262 reScaleW = true; 263 lastW = dim.width; 266 } 267 if (lastH != dim.height) { 268 reScaleH = true; 269 lastH = dim.height; 272 } 273 } 274 277 dim.width = right - left; 278 dim.height = bottom - top; 279 280 281 int x = left; 282 int y = top; 283 for(int i = 0; i < c.length; i++) { 284 if (c[i].isVisible()) { 288 297 Dimension prefD = c[i].getPreferredSize(); 299 Dimension size = c[i].getSize(); 300 if (size.width == 0){ 301 c[i].setBounds(x, y, prefD.width, dim.height); 302 x += prefD.width; 303 } 304 else { 305 Container scrollpane = target.getParent().getParent(); 307 Container cv = scrollpane.getParent(); 308 int cvHeight = 0; 309 if (cv instanceof JPanel ){ 311 cvHeight = cv.getSize().height; 312 313 } 314 if (scrollpane instanceof JScrollPane ){ 315 JScrollBar hBar = ((JScrollPane )scrollpane).getHorizontalScrollBar(); 316 if (hBar.isVisible()){ 319 cvHeight -= hBar.getHeight(); 320 } 321 } 326 c[i].setBounds(x, y, size.width, cvHeight-2); 327 x += size.width; 329 } 330 331 } 333 } 334 335 Rectangle lastComp = c[c.length-1].getBounds(); 339 int totalCompsWidth = lastComp.x + lastComp.width; 340 Dimension targetSize = target.getSize(); 341 if (!(targetSize.width == totalCompsWidth && targetSize.height == lastComp.height)){ 346 target.setPreferredSize(new Dimension (totalCompsWidth, lastComp.height)); 347 if (target instanceof JComponent ){ 348 ((JComponent )target).revalidate(); 349 } 350 } 351 newComponentAdded = false; 353 } 354 358 public final Dimension maximumLayoutSize(Container target) { 359 return new Dimension (Integer.MAX_VALUE, Integer.MAX_VALUE); 361 } 362 366 public final Dimension minimumLayoutSize(Container target) { 368 return checkLayoutSize(target, true); 370 } 371 373 377 public final Dimension preferredLayoutSize(Container target) { 378 return checkLayoutSize(target, true); 380 } 381 384 public final void removeLayoutComponent(Component comp) { 385 newComponentAdded = true; } 387 388 392 public void setFill(boolean fill){ 393 this.fill = fill; 394 } 395 396 397 398 402 public boolean getFill( ){ 403 return this.fill; 404 } 405 406 407 408 public final String toString() { 409 return getClass().getName(); 410 } 411 } 412 | Popular Tags |