Simulation of BorderLayout layout for SWT (JFace) Experience

Source: Internet
Author: User

SWT does not have the BorderLayout layout manager of AWT. The following is a custom implementation under SWT:
BorderLayout. java Copy codeThe Code is as follows: package swt_jface.demo2;
Import org. eclipse. swt. SWT;
Import org. eclipse. swt. graphics. Point;
Import org. eclipse. swt. graphics. Rectangle;
Import org. eclipse. swt. widgets. Composite;
Import org. eclipse. swt. widgets. Control;
Import org. eclipse. swt. widgets. Layout;
Public class BorderLayout extends Layout {
Public static final int NORTH = 0;
Public static final int SOUTH = 1;
Public static final int CENTER = 2;
Public static final int EAST = 3;
Public static final int WEST = 4;
Public static class BorderData {

Public int region = CENTER;
Public BorderData (){
}
Public BorderData (int region ){
This. region = region;
}
}
Public Control [] controls = new Control [5];
Point [] sizes;
Int width;
Int height;
Protected Point computeSize (Composite composite, int wHint, int hHint, boolean flushCache ){

If (sizes = null | flushCache = true)
RefreshSizes (composite. getChildren ());
Int w = wHint;
Int h = hHint;
If (w = SWT. DEFAULT) w = width;
If (h = SWT. DEFAULT) h = height;
Return new Point (w, h );
}
Protected void layout (Composite composite, boolean flushCache ){
If (flushCache | sizes = null)
RefreshSizes (composite. getChildren ());
Rectangle clientArea = composite. getClientArea ();
If (controls [NORTH]! = Null ){
Controls [NORTH]. setBounds (
ClientArea. x,
ClientArea. y,
ClientArea. width,
Sizes [NORTH]. y );
}
If (controls [SOUTH]! = Null ){
Controls [SOUTH]. setBounds (
ClientArea. x,
ClientArea. y + clientArea. height-sizes [SOUTH]. y,
ClientArea. width,
Sizes [SOUTH]. y );
}
If (controls [WEST]! = Null ){
Controls [WEST]. setBounds (
ClientArea. x,
ClientArea. y + sizes [NORTH]. y,
Sizes [WEST]. x,
ClientArea. height-sizes [NORTH]. y-sizes [SOUTH]. y );
}
If (controls [EAST]! = Null ){
Controls [EAST]. setBounds (
ClientArea. x + clientArea. width-sizes [EAST]. x,
ClientArea. y + sizes [NORTH]. y,
Sizes [EAST]. x,
ClientArea. height-sizes [NORTH]. y-sizes [SOUTH]. y );
}
If (controls [CENTER]! = Null ){
Controls [CENTER]. setBounds (
ClientArea. x + sizes [WEST]. x,
ClientArea. y + sizes [NORTH]. y,
ClientArea. width-sizes [WEST]. x-sizes [EAST]. x,
ClientArea. height-sizes [NORTH]. y-sizes [SOUTH]. y );
}
}
Private void refreshSizes (Control [] children ){

For (int I = 0; I <children. length; I ++ ){
Object layoutData = children [I]. getLayoutData ();
If (layoutData = null | (! (LayoutData instanceof BorderData )))
Continue;
BorderData borderData = (BorderData) layoutData;
If (borderData. region <0 | borderData. region> 4) // Invalid.
Continue;
Controls [borderData. region] = children [I];
}
Width = 0;
Height = 0;
If (sizes = null)
Sizes = new Point [5];
For (int I = 0; I <controls. length; I ++ ){
Control control = controls [I];
If (control = null ){
Sizes [I] = new Point (0, 0 );
} Else {
Sizes [I] = control. computeSize (SWT. DEFAULT, SWT. DEFAULT, true );
}
}
Width = Math. max (width, sizes [NORTH]. x );
Width = Math. max (width, sizes [WEST]. x + sizes [CENTER]. x + sizes [EAST]. x );
Width = Math. max (width, sizes [SOUTH]. x );
Height = Math. max (Math. max (sizes [WEST]. y, sizes [EAST]. y), sizes [CENTER]. y)
+ Sizes [NORTH]. y
+ Sizes [SOUTH]. y;
}
}

Test code:
BorderLayoutSample. javaCopy codeThe Code is as follows: package swt_jface.demo2;
Import org. eclipse. swt. SWT;
Import org. eclipse. swt. widgets. Button;
Import org. eclipse. swt. widgets. Display;
Import org. eclipse. swt. widgets. Shell;
Import org. eclipse. swt. widgets. Text;
Public class BorderLayoutSample {

Display display = new Display ();
Shell shell = new Shell (display );
Public BorderLayoutSample (){

Shell. setLayout (new BorderLayout ());

Button buttonWest = new Button (shell, SWT. PUSH );
ButtonWest. setText ("West ");
ButtonWest. setLayoutData (new BorderLayout. BorderData (BorderLayout. WEST ));

Button buttonEast = new Button (shell, SWT. PUSH );
ButtonEast. setText ("East ");
ButtonEast. setLayoutData (new BorderLayout. BorderData (BorderLayout. EAST ));
Button buttonNorth = new Button (shell, SWT. PUSH );
ButtonNorth. setText ("North ");
ButtonNorth. setLayoutData (new BorderLayout. BorderData (BorderLayout. NORTH ));

Button buttonSouth = new Button (shell, SWT. PUSH );
ButtonSouth. setText ("South ");
ButtonSouth. setLayoutData (new BorderLayout. BorderData (BorderLayout. SOUTH ));

Text text = new Text (shell, SWT. MULTI | SWT. BORDER | SWT. V_SCROLL | SWT. H_SCROLL );
Text. setText ("Center ");
Text. setLayoutData (new BorderLayout. BorderData (BorderLayout. CENTER ));

Shell. pack ();
Shell. open ();

While (! Shell. isDisposed ()){
If (! Display. readAndDispatch ()){
Display. sleep ();
}
}
Display. dispose ();
}
Public static void main (String [] args ){
New BorderLayoutSample ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.