转载

如何在RCP程序中添加一个banner栏

前言:这段时间还算比较空闲,我准备把过去做过的有些形形色色,甚至有些奇怪的研究总结一下,也许刚好有人用的着也不一定,不枉为之抓耳挠腮的时光和浪费的电力。以前有个客户提出要在RCP程序中添加一个banner栏,研究了很久才搞定。代码是基于eclipse4.3.2的。

先看一下效果预览:

如何在RCP程序中添加一个banner栏

为了添加一个banner栏,我们必须重写RCP程序最外层的layout类,即TrimmedPartLayout.java。这个layout类是用来控制menu,toolbar等最基本的layout布局的。我们写一个CustomTrimmedPartLayout 类,继承自TrimmedPartLayout:

 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放在工具栏下面,而不是上面,只要稍微修改一下代码的位置

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

其中 ---

标记为绿色的代码是用来替换Layout类的

标记为蓝色的代码是用来在banner中创建一个Label,并加载一张作为banner的图片。

正文到此结束
Loading...