如何在RCP程序中添加一個banner欄

前言:這段時間還算比較空閒,我準備把過去作過的有些形形色色,甚至有些奇怪的研究總結一下,也許恰好有人用的着也不必定,不枉爲之抓耳撓腮的時光和浪費的電力。之前有個客戶提出要在RCP程序中添加一個banner欄,研究了好久才搞定。代碼是基於eclipse4.3.2的。java

先看一下效果預覽:shell

image

 

爲了添加一個banner欄,咱們必須重寫RCP程序最外層的layout類,即TrimmedPartLayout.java。這個layout類是用來控制menu,toolbar等最基本的layout佈局的。咱們寫一個CustomTrimmedPartLayout 類,繼承自TrimmedPartLayout:less

public class CustomTrimmedPartLayout extends TrimmedPartLayout {

    public Composite banner;

    /**
     * This layout is used to support parts that want trim for their containing
     * composites.
     * 
     * @param trimOwner
     */
    public CustomTrimmedPartLayout(Composite parent) {
        super(parent);
 banner 
= new Composite(parent, SWT.NONE); GridLayout gridLayout = new GridLayout(1, false ); gridLayout.horizontalSpacing=0 ; gridLayout.verticalSpacing = 0 ; gridLayout.marginHeight = 0 ; gridLayout.marginWidth = 0
; banner.setLayout(gridLayout);
        //banner.setLayoutData(new GridData(GridData.FILL_BOTH));
    }
    
    protected void layout(Composite composite, boolean flushCache) {
        Rectangle ca = composite.getClientArea();
        Rectangle caRect = new Rectangle(ca.x, ca.y, ca.width, ca.height);
        
// 'banner' spans the entire area if (banner != null && banner.isVisible() ) { Point bannerSize = banner.computeSize(caRect.width, SWT.DEFAULT, true ); // Don't layout unless we've changed Rectangle newBounds = new Rectangle(caRect.x, caRect.y, bannerSize.x, bannerSize.y); caRect.y += bannerSize.y; caRect.height -= bannerSize.y; if (!
newBounds.equals(banner.getBounds())) { banner.setBounds(newBounds); } }
        
        
        // 'Top' spans the entire area
        
         if (top != null && top.isVisible()) {
            Point topSize = top.computeSize(caRect.width, SWT.DEFAULT, true);

            // Don't layout unless we've changed
            Rectangle newBounds = new Rectangle(caRect.x, caRect.y, caRect.width,
                    topSize.y);
            if (!newBounds.equals(top.getBounds())) {
                top.setBounds(newBounds);
            }
            
            caRect.y += topSize.y;
            caRect.height -= topSize.y;
        }
        
        // Include the gutter whether there is a top area or not.
        caRect.y += gutterTop;
        caRect.height -= gutterTop;

        // 'Bottom' spans the entire area
        if (bottom != null && bottom.isVisible()) {
            Point bottomSize = bottom.computeSize(caRect.width, SWT.DEFAULT,
                    true);
            caRect.height -= bottomSize.y;

            // Don't layout unless we've changed
            Rectangle newBounds = new Rectangle(caRect.x, caRect.y
                    + caRect.height, caRect.width, bottomSize.y);
            if (!newBounds.equals(bottom.getBounds())) {
                bottom.setBounds(newBounds);
            }
        }
        caRect.height -= gutterBottom;

        // 'Left' spans between 'top' and 'bottom'
        if (left != null && left.isVisible()) {
            Point leftSize = left.computeSize(SWT.DEFAULT, caRect.height, true);
            caRect.x += leftSize.x;
            caRect.width -= leftSize.x;

            // Don't layout unless we've changed
            Rectangle newBounds = new Rectangle(caRect.x - leftSize.x,
                    caRect.y, leftSize.x, caRect.height);
            if (!newBounds.equals(left.getBounds())) {
                left.setBounds(newBounds);
            }
        }
        caRect.x += gutterLeft;
        caRect.width -= gutterLeft;

        // 'Right' spans between 'top' and 'bottom'
        if (right != null && right.isVisible()) {
            Point rightSize = right.computeSize(SWT.DEFAULT, caRect.height,
                    true);
            caRect.width -= rightSize.x;

            // Don't layout unless we've changed
            Rectangle newBounds = new Rectangle(caRect.x + caRect.width,
                    caRect.y, rightSize.x, caRect.height);
            if (!newBounds.equals(right.getBounds())) {
                right.setBounds(newBounds);
            }
        }
        caRect.width -= gutterRight;

        // Don't layout unless we've changed
        if (!caRect.equals(clientArea.getBounds())) {
            clientArea.setBounds(caRect);
        }
    }
}

 

若是咱們但願Banner放在工具欄下面,而不是上面,只要稍微修改一下代碼的位置eclipse

public class CustomTrimmedPartLayout extends TrimmedPartLayout {

public Composite banner;

    /**
     * This layout is used to support parts that want trim for their containing
     * composites.
     * 
     * @param trimOwner
     */
    public CustomTrimmedPartLayout(Composite parent) {
        super(parent);
        banner 
= new Composite(parent, SWT.NONE); GridLayout gridLayout = new GridLayout(1, false ); gridLayout.horizontalSpacing=0 ; gridLayout.verticalSpacing = 0 ; gridLayout.marginHeight = 0 ; gridLayout.marginWidth = 0
; banner.setLayout(gridLayout);
        //banner.setLayoutData(new GridData(GridData.FILL_BOTH));
    }
    
    protected void layout(Composite composite, boolean flushCache) {
        Rectangle ca = composite.getClientArea();
        Rectangle caRect = new Rectangle(ca.x, ca.y, ca.width, ca.height);
        
        
        // 'Top' spans the entire area
        
         if (top != null && top.isVisible()) {
            Point topSize = top.computeSize(caRect.width, SWT.DEFAULT, true);

            // Don't layout unless we've changed
            Rectangle newBounds = new Rectangle(caRect.x, caRect.y, caRect.width,
                    topSize.y);
            if (!newBounds.equals(top.getBounds())) {
                top.setBounds(newBounds);
            }
            
            caRect.y += topSize.y;
            caRect.height -= topSize.y;
        }
        
        
// 'banner' spans the entire area if (banner != null && banner.isVisible()) { Point bannerSize = banner.computeSize(caRect.width, SWT.DEFAULT, true ); // Don't layout unless we've changed Rectangle newBounds = new Rectangle(caRect.x, caRect.y, bannerSize.x, bannerSize.y); caRect.y += bannerSize.y; caRect.height -= bannerSize.y; if (!
newBounds.equals(banner.getBounds())) { banner.setBounds(newBounds); } }
        
        // Include the gutter whether there is a top area or not.
        caRect.y += gutterTop;
        caRect.height -= gutterTop;

        // 'Bottom' spans the entire area
        if (bottom != null && bottom.isVisible()) {
            Point bottomSize = bottom.computeSize(caRect.width, SWT.DEFAULT,
                    true);
            caRect.height -= bottomSize.y;

            // Don't layout unless we've changed
            Rectangle newBounds = new Rectangle(caRect.x, caRect.y
                    + caRect.height, caRect.width, bottomSize.y);
            if (!newBounds.equals(bottom.getBounds())) {
                bottom.setBounds(newBounds);
            }
        }
        caRect.height -= gutterBottom;

        // 'Left' spans between 'top' and 'bottom'
        if (left != null && left.isVisible()) {
            Point leftSize = left.computeSize(SWT.DEFAULT, caRect.height, true);
            caRect.x += leftSize.x;
            caRect.width -= leftSize.x;

            // Don't layout unless we've changed
            Rectangle newBounds = new Rectangle(caRect.x - leftSize.x,
                    caRect.y, leftSize.x, caRect.height);
            if (!newBounds.equals(left.getBounds())) {
                left.setBounds(newBounds);
            }
        }
        caRect.x += gutterLeft;
        caRect.width -= gutterLeft;

        // 'Right' spans between 'top' and 'bottom'
        if (right != null && right.isVisible()) {
            Point rightSize = right.computeSize(SWT.DEFAULT, caRect.height,
                    true);
            caRect.width -= rightSize.x;

            // Don't layout unless we've changed
            Rectangle newBounds = new Rectangle(caRect.x + caRect.width,
                    caRect.y, rightSize.x, caRect.height);
            if (!newBounds.equals(right.getBounds())) {
                right.setBounds(newBounds);
            }
        }
        caRect.width -= gutterRight;

        // Don't layout unless we've changed
        if (!caRect.equals(clientArea.getBounds())) {
            clientArea.setBounds(caRect);
        }
    }
}

 

最後,咱們須要把shell加載的layout從TrimmedPartLayout替換爲CustomTrimmedPartLayout。工具

在ApplicationWorkbenchWindowAdvisor 的preWindowOpen中添加以下代碼:佈局

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
...

    public void preWindowOpen() {
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        configurer.setInitialSize(new Point(600, 400));
        configurer.setShowCoolBar(true);
        configurer.setShowPerspectiveBar(true);
        configurer.setShowStatusLine(true);
        configurer.setShowProgressIndicator(false);
        configurer.setShowFastViewBars(false);
        
        IWorkbenchWindow workbenchWindow=  configurer.getWindow();
        workbenchWindow=  configurer.getWindow();
        
        Shell shell 
= workbenchWindow.getShell(); TrimmedPartLayout layout = (TrimmedPartLayout)shell.getLayout(); CustomTrimmedPartLayout customLayout = new CustomTrimmedPartLayout(layout.clientArea.getParent()); customLayout.clientArea =
 layout.clientArea; shell.setLayout(customLayout);

        Label bannerLabel 
= new Label(customLayout.banner, SWT.SIMPLE| SWT.CENTER); customLayout.banner.setBackground(…); GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true ); bannerLabel.setLayoutData(gridData); Image image =
 Activator.getImageDescriptor(bannerExtensionPoint.getImage()).createImage(); bannerLabel.setImage(image);
    }
....
}

 

其中 ---spa

標記爲綠色的代碼是用來替換Layout類的code

標記爲藍色的代碼是用來在banner中建立一個Label,並加載一張做爲banner的圖片。blog

相關文章
相關標籤/搜索