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

先看一下效果预览:

为了添加一个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的图片。

如何在RCP程序中添加一个banner栏的更多相关文章

  1. 对类HelloWorld程序中添加一个MessageBox弹窗

    对类HelloWorld程序中添加一个MessageBox弹窗 分析: 任一程序运行的时候都会加载kernel32.dll的,但MessageBoxA()这个API却是在user32.dll中的.所以 ...

  2. 如何在form组件中添加一个单选或者多选的字段

    解决办法: 需要在增加的类里面加入choices   具体操作如下:

  3. 如何在WPF程序中使用ArcGIS Engine的控件

    原文 http://www.gisall.com/html/47/122747-4038.html WPF(Windows Presentation Foundation)是美国微软公司推出.NET ...

  4. 如何在VS2010的VC++ 基于对话框的MFC程序中添加菜单

    方法1:亲测 成功  转载自https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/48338f6b-e5d9-4c0c-8b17-05ca3ef ...

  5. [保姆级教程] 如何在 Linux Kernel (V5.17.7) 中添加一个系统调用(System call)

    最近在学习 <linux Kernel Development>,本书用的linux kernel 是v2.6 版本的.看完"系统调用"一节后,想尝试添加一个系统调用, ...

  6. 如何在Android Studio中添加注释模板信息?

    如何在Android Studio中添加注释模板信息? 在开发程序的时候,我们一般都会给文件自动添加上一些关于文件的注释信息,比如开发者的名字,开发的时间,开发者的联系方式等等.那么在android ...

  7. 如何在Word表格中的某一栏添加背景颜色

     如何在Word表格中的某一栏添加背景颜色 编写人:CC阿爸 2014-3-14 用鼠标选中某一个单元格然后右键单击 下拉菜单选择.<边框和低纹>然后点<低纹>选项卡 选中色卡 ...

  8. Entity Framework 的小实例:在项目中添加一个实体类,并做插入操作

    Entity Framework 的小实例:在项目中添加一个实体类,并做插入操作 1>. 创建一个控制台程序2>. 添加一个 ADO.NET实体数据模型,选择对应的数据库与表(Studen ...

  9. 【转】如何在 Android 程序中禁止屏幕旋转和重启Activity

    原文网址:http://www.cnblogs.com/bluestorm/p/3665890.html 禁止屏幕随手机旋转变化 有时候我们希望让一个程序的界面始终保持在一个方向,不随手机方向旋转而变 ...

随机推荐

  1. mongoperf用法

    mongoperf是mongoDB自带工具,用于评估磁盘随机IO性能. 官方文档 使用方法 作用:用于部署前,评估mongodb所在存储的IO性能 用法:mongoperf <conffile, ...

  2. Python学习【第四篇】用户输入及判断

    用户输入: 例1.写一个用户输入密码的小程序,流程如下: 1.用户输入自己的用户名 2.打印"Hello+用户名" #!/usr/bin/env python #name = ra ...

  3. k8s入门系列之guestbook快速部署

    k8s集群以及一些扩展插件已经安装完毕,本篇文章介绍一下如何在k8s集群上快速部署guestbook应用. •实验环境为集群:master(1)+node(4),详细内容参考<k8s入门系列之集 ...

  4. swift language

    API reference Swift UIKit Swift 菜鸟教程 Great Installed Visual Studio Code, I found I cannot open it fr ...

  5. delphi 判断一个数组的长度用 Length 还是 SizeOf ?

    判断一个数组的长度用 Length 还是 SizeOf ?最近发现一些代码, 甚至有一些专家代码, 在遍历数组时所用的数组长度竟然是 SizeOf(arr); 这不合适! 如果是一维数组.且元素大小是 ...

  6. SqlServer try catch 捕获不到的一些错误及解决方法(转载)

    测试注意 :①假如系统能捕获异常 ,并且我们自己开启了事务.系统会自动 回滚事务的,但是 我们还是要在catch里面加上 rollback tran的习惯,这样也不会提示重复rollback的错误,这 ...

  7. APP开发:一个APP开发需要哪些技术人员?

          亿合科技小编了解到:国民老公王思聪曾经在一个访谈中谈到过,如果他是一个普通人,他会选择移动互联网去创业,因为做个网站或者App开发门槛较低,做大做强的机会也比较多.小编觉得创业就是投资,重 ...

  8. 化繁为简,最简易的SqlDataSource配合Cache使用~

    最近一个小项目用了比较方便傻瓜的DevExpress控件,实践中DevExpress的控件确实很方便. 在用ASPxGridView控件的时候,为了偷懒结合了SqlDataSource使用,但一直在大 ...

  9. 解决从jenkins打开robot framework报告会提示‘Opening Robot Framework log failed ’的问题

    最新的jenkins打开jenkins robot framework报告会提示如下 Verify that you have JavaScript enabled in your browser.  ...

  10. Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...