前言:这段时间还算比较空闲,我准备把过去做过的有些形形色色,甚至有些奇怪的研究总结一下,也许刚好有人用的着也不一定,不枉为之抓耳挠腮的时光和浪费的电力。以前有个客户提出要在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. OC ---- 字符串 数组 iOS学习-----细碎知识点总结

    NSString *urlString = [NSString stringWithFormat:@"http://www.apple.com"];        // 获取字符串 ...

  2. windows系统IIS环境下如何部署MVC项目

    首先打开IIS:第一步:添加MVC程序映射 打开其中的:处理程序映射,如下图: 点击界面右边操作中的:添加脚本映射,弹出下图: 请求路径:*           可执行文件:c:/Windows/Mi ...

  3. C# 实现函数回调

    public class Lib { public delegate void UserFunctionCB(); private static UserFunctionCB m_userFnCB; ...

  4. js瀑布流(定位法)

    1.首先,自己写好图片路径,引入jquery <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

  5. 【转】PHP ob_start() 函数介绍

    php ob_start 与 ob_end_flush() 是 php 的缓冲输出函数. ob_start([string output_callback])- 打开输出缓冲区,所有的输出信息不在直接 ...

  6. 如何从投票的网站的管理后台导出已投票的邀请码数据至Excel,并且稍修改,再导入到现场抽奖软件中?

    第一步:进入投票网站的管理后台,导出 已投票 的 邀请码 相关信息至Excel中,下图所示: 并且 删除第一行表头汉字信息. 第二步:把第A列 数值 信息 转换 为 文本 信息(注:转换方法详细点击此 ...

  7. 【转】Deadlock的一些总结(死锁分析及处理)

    1.1.1 摘要 在系统设计过程中,系统的稳定性.响应速度和读写速度至关重要,就像12306.cn那样,当然我们可以通过提高系统并发能力来提高系统性能总体性能,但在并发作用下也会出现一些问题,例如死锁 ...

  8. linux rhel7 dock6.7安装

    1. 下载dock6.7 先申请license 在这个网址下载http://dock.compbio.ucsf.edu/Online_Licensing/index.htm 2. tar zxvf * ...

  9. >>> 主页推荐链接

    Apple专区 App Store 排行榜 App Store 排行榜 - 中国区 PC6苹果网 威锋网 第三方 环信 - 即时通讯云领导者 腾讯Bugly - Android Crash | iOS ...

  10. [原创]java WEB学习笔记104:Spring学习---AOP 前奏,通过一个问题引入动态代理

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...