Files
Brainstorm/Foundation SWT/src/com/foundation/view/swt/layout/CenterLayout.java

83 lines
2.4 KiB
Java
Raw Normal View History

2014-05-30 10:31:51 -07:00
package com.foundation.view.swt.layout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
/**
* A very simple layout that centers the first child within the parent.
* All other children are not visible.
* There is no data attached to the children that is used by this layout.
*/
public class CenterLayout extends Layout {
private Point size = new Point(0, 0);
private Point firstChildSize = new Point(0, 0);
/**
* CenterLayout constructor.
*/
public CenterLayout() {
}//CenterLayout()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite, int, int, boolean)
*/
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
if(flushCache) {
Control[] children = composite.getChildren();
size.x = 0;
size.y = 0;
firstChildSize.x = 0;
firstChildSize.y = 0;
if(children != null) {
for(int index = 0; index < children.length; index++) {
Point childSize = children[index].computeSize(wHint, hHint, flushCache);
size.x = Math.max(childSize.x, size.x);
size.y = Math.max(childSize.y, size.y);
if(index == 0) {
firstChildSize.x = childSize.x;
firstChildSize.y = childSize.y;
}//if//
}//for//
}//if//
}//if//
return new Point(size.x, size.y);
}//computeSize()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite, boolean)
*/
protected void layout(Composite composite, boolean flushCache) {
Rectangle space = composite.getClientArea();
Control[] children = composite.getChildren();
if(flushCache || firstChildSize.x == 0 || firstChildSize.y == 0) {
if(children != null && children.length > 0) {
Point childSize = children[0].computeSize(-1, -1, true);
firstChildSize.x = childSize.x;
firstChildSize.y = childSize.y;
}//if//
else {
firstChildSize.x = 0;
firstChildSize.y = 0;
}//else//
}//if//
if(children != null) {
for(int index = 0; index < children.length; index++) {
if(index == 0) {
children[index].setSize(firstChildSize);
children[index].setLocation(space.x + ((space.width - firstChildSize.x) >> 1), space.y + ((space.height - firstChildSize.y) >> 1));
}//if//
else {
children[index].setSize(0, 0);
children[index].setLocation(0, 0);
}//else//
}//for//
}//if//
}//layout()//
}//CenterLayout//