How to add a banner bar in the RCP program, rcp

Source: Internet
Author: User

How to add a banner bar in the RCP program, rcp

This period of time is relatively idle. I am going to summarize some of the various and even some strange studies I have done in the past. Maybe some people may not use it, don't worry about the time and waste of power. In the past, a customer proposed to add a banner bar to the RCP program, which took a long time to complete. The Code is based on eclipse4.3.2.

Let's take a look at the effect preview:

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);        }    }}

 

If we want the Banner to be placed under the toolbar, instead of above, we only need to slightly modify the position of the Code.

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);        }    }}

 

Finally, we need to replace layout loaded by shell with trimtrimmedpartlayout from TrimmedPartLayout.

Add the following code to preWindowOpen of ApplicationWorkbenchWindowAdvisor:

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 partition (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);    }....}

 

---

The Code marked as green is used to replace the Layout class.

The Code marked as blue is used to create a Label in the banner and load an image as the banner.

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.