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

先看一下效果预览:

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

  1. public class CustomTrimmedPartLayout extends TrimmedPartLayout {
  2.  
  3. public Composite banner;
  4.  
  5. /**
  6. * This layout is used to support parts that want trim for their containing
  7. * composites.
  8. *
  9. * @param trimOwner
  10. */
  11. public CustomTrimmedPartLayout(Composite parent) {
  12. super(parent);
  13. banner

= new Composite(parent, SWT.NONE); GridLayout gridLayout = new GridLayout(1, false); gridLayout.horizontalSpacing=0; gridLayout.verticalSpacing = 0; gridLayout.marginHeight = 0; gridLayout.marginWidth = 0

  1. ;
  2. banner.setLayout(gridLayout);
  3. //banner.setLayoutData(new GridData(GridData.FILL_BOTH));
  4. }
  5.  
  6. protected void layout(Composite composite, boolean flushCache) {
  7. Rectangle ca = composite.getClientArea();
  8. 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 (!

  1. newBounds.equals(banner.getBounds())) {
  2. banner.setBounds(newBounds);
  3. }
  4. }
  5.  
  6. // 'Top' spans the entire area
  7.  
  8. if (top != null && top.isVisible()) {
  9. Point topSize = top.computeSize(caRect.width, SWT.DEFAULT, true);
  10.  
  11. // Don't layout unless we've changed
  12. Rectangle newBounds = new Rectangle(caRect.x, caRect.y, caRect.width,
  13. topSize.y);
  14. if (!newBounds.equals(top.getBounds())) {
  15. top.setBounds(newBounds);
  16. }
  17.  
  18. caRect.y += topSize.y;
  19. caRect.height -= topSize.y;
  20. }
  21.  
  22. // Include the gutter whether there is a top area or not.
  23. caRect.y += gutterTop;
  24. caRect.height -= gutterTop;
  25.  
  26. // 'Bottom' spans the entire area
  27. if (bottom != null && bottom.isVisible()) {
  28. Point bottomSize = bottom.computeSize(caRect.width, SWT.DEFAULT,
  29. true);
  30. caRect.height -= bottomSize.y;
  31.  
  32. // Don't layout unless we've changed
  33. Rectangle newBounds = new Rectangle(caRect.x, caRect.y
  34. + caRect.height, caRect.width, bottomSize.y);
  35. if (!newBounds.equals(bottom.getBounds())) {
  36. bottom.setBounds(newBounds);
  37. }
  38. }
  39. caRect.height -= gutterBottom;
  40.  
  41. // 'Left' spans between 'top' and 'bottom'
  42. if (left != null && left.isVisible()) {
  43. Point leftSize = left.computeSize(SWT.DEFAULT, caRect.height, true);
  44. caRect.x += leftSize.x;
  45. caRect.width -= leftSize.x;
  46.  
  47. // Don't layout unless we've changed
  48. Rectangle newBounds = new Rectangle(caRect.x - leftSize.x,
  49. caRect.y, leftSize.x, caRect.height);
  50. if (!newBounds.equals(left.getBounds())) {
  51. left.setBounds(newBounds);
  52. }
  53. }
  54. caRect.x += gutterLeft;
  55. caRect.width -= gutterLeft;
  56.  
  57. // 'Right' spans between 'top' and 'bottom'
  58. if (right != null && right.isVisible()) {
  59. Point rightSize = right.computeSize(SWT.DEFAULT, caRect.height,
  60. true);
  61. caRect.width -= rightSize.x;
  62.  
  63. // Don't layout unless we've changed
  64. Rectangle newBounds = new Rectangle(caRect.x + caRect.width,
  65. caRect.y, rightSize.x, caRect.height);
  66. if (!newBounds.equals(right.getBounds())) {
  67. right.setBounds(newBounds);
  68. }
  69. }
  70. caRect.width -= gutterRight;
  71.  
  72. // Don't layout unless we've changed
  73. if (!caRect.equals(clientArea.getBounds())) {
  74. clientArea.setBounds(caRect);
  75. }
  76. }
  77. }

如果我们希望Banner放在工具栏下面,而不是上面,只要稍微修改一下代码的位置

  1. public class CustomTrimmedPartLayout extends TrimmedPartLayout {
  2.  
  3. public Composite banner;
  4.  
  5. /**
  6. * This layout is used to support parts that want trim for their containing
  7. * composites.
  8. *
  9. * @param trimOwner
  10. */
  11. public CustomTrimmedPartLayout(Composite parent) {
  12. super(parent);
  13. banner

= new Composite(parent, SWT.NONE); GridLayout gridLayout = new GridLayout(1, false); gridLayout.horizontalSpacing=0; gridLayout.verticalSpacing = 0; gridLayout.marginHeight = 0; gridLayout.marginWidth = 0

  1. ;
  2. banner.setLayout(gridLayout);
  3. //banner.setLayoutData(new GridData(GridData.FILL_BOTH));
  4. }
  5.  
  6. protected void layout(Composite composite, boolean flushCache) {
  7. Rectangle ca = composite.getClientArea();
  8. Rectangle caRect = new Rectangle(ca.x, ca.y, ca.width, ca.height);
  9.  
  10. // 'Top' spans the entire area
  11.  
  12. if (top != null && top.isVisible()) {
  13. Point topSize = top.computeSize(caRect.width, SWT.DEFAULT, true);
  14.  
  15. // Don't layout unless we've changed
  16. Rectangle newBounds = new Rectangle(caRect.x, caRect.y, caRect.width,
  17. topSize.y);
  18. if (!newBounds.equals(top.getBounds())) {
  19. top.setBounds(newBounds);
  20. }
  21.  
  22. caRect.y += topSize.y;
  23. caRect.height -= topSize.y;
  24. }

// '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 (!

  1. newBounds.equals(banner.getBounds())) {
  2. banner.setBounds(newBounds);
  3. }
  4. }
  5.  
  6. // Include the gutter whether there is a top area or not.
  7. caRect.y += gutterTop;
  8. caRect.height -= gutterTop;
  9.  
  10. // 'Bottom' spans the entire area
  11. if (bottom != null && bottom.isVisible()) {
  12. Point bottomSize = bottom.computeSize(caRect.width, SWT.DEFAULT,
  13. true);
  14. caRect.height -= bottomSize.y;
  15.  
  16. // Don't layout unless we've changed
  17. Rectangle newBounds = new Rectangle(caRect.x, caRect.y
  18. + caRect.height, caRect.width, bottomSize.y);
  19. if (!newBounds.equals(bottom.getBounds())) {
  20. bottom.setBounds(newBounds);
  21. }
  22. }
  23. caRect.height -= gutterBottom;
  24.  
  25. // 'Left' spans between 'top' and 'bottom'
  26. if (left != null && left.isVisible()) {
  27. Point leftSize = left.computeSize(SWT.DEFAULT, caRect.height, true);
  28. caRect.x += leftSize.x;
  29. caRect.width -= leftSize.x;
  30.  
  31. // Don't layout unless we've changed
  32. Rectangle newBounds = new Rectangle(caRect.x - leftSize.x,
  33. caRect.y, leftSize.x, caRect.height);
  34. if (!newBounds.equals(left.getBounds())) {
  35. left.setBounds(newBounds);
  36. }
  37. }
  38. caRect.x += gutterLeft;
  39. caRect.width -= gutterLeft;
  40.  
  41. // 'Right' spans between 'top' and 'bottom'
  42. if (right != null && right.isVisible()) {
  43. Point rightSize = right.computeSize(SWT.DEFAULT, caRect.height,
  44. true);
  45. caRect.width -= rightSize.x;
  46.  
  47. // Don't layout unless we've changed
  48. Rectangle newBounds = new Rectangle(caRect.x + caRect.width,
  49. caRect.y, rightSize.x, caRect.height);
  50. if (!newBounds.equals(right.getBounds())) {
  51. right.setBounds(newBounds);
  52. }
  53. }
  54. caRect.width -= gutterRight;
  55.  
  56. // Don't layout unless we've changed
  57. if (!caRect.equals(clientArea.getBounds())) {
  58. clientArea.setBounds(caRect);
  59. }
  60. }
  61. }

最后,我们需要把shell加载的layout从TrimmedPartLayout替换为CustomTrimmedPartLayout。

在ApplicationWorkbenchWindowAdvisor 的preWindowOpen中添加如下代码:

  1. public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
  2. ...
  3.  
  4. public void preWindowOpen() {
  5. IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
  6. configurer.setInitialSize(new Point(600, 400));
  7. configurer.setShowCoolBar(true);
  8. configurer.setShowPerspectiveBar(true);
  9. configurer.setShowStatusLine(true);
  10. configurer.setShowProgressIndicator(false);
  11. configurer.setShowFastViewBars(false);
  12.  
  13. IWorkbenchWindow workbenchWindow= configurer.getWindow();
  14. workbenchWindow= configurer.getWindow();
  15.  
  16. Shell shell

= workbenchWindow.getShell(); TrimmedPartLayout layout = (TrimmedPartLayout)shell.getLayout(); CustomTrimmedPartLayout customLayout = new CustomTrimmedPartLayout(layout.clientArea.getParent()); customLayout.clientArea =

  1. layout.clientArea;
  2. shell.setLayout(customLayout);
  3.  
  4. 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 =

  1. Activator.getImageDescriptor(bannerExtensionPoint.getImage()).createImage();
  2. bannerLabel.setImage(image);
  3. }
  4. ....
  5. }

其中 ---

标记为绿色的代码是用来替换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. 启动Hive时出现的问题

    Caused by: org.apache.hadoop.hive.ql.metadata.HiveException: java.lang.RuntimeException: Unable to i ...

  2. 线性时间O(n)内求数组中第k大小的数

    --本文为博主原创,转载请注明出处 因为最近做的WSN(wireless sensor network)实验要求用3个传感器节点接受2000个包的数据并算出一些统计量,其中就有算出中位数这么一个要求, ...

  3. 数据库 定义 bit 类型 (true=1,false=0)

    当Sql Server数据库定义 数据 为 bit 类型时, 编写代码时 要用 true or false 赋值. 例如: OffTheShelf  定义类型为  bit 后台赋值时 OffTheSh ...

  4. 初识The Battle of Polytopia

    1.首先了解了一下<文明5-美丽新文明>视频介绍网址:http://list.youku.com/albumlist/show?id=19481409&ascending=1&am ...

  5. 在java中获取attr的值

    首先说如何获取已经在style里面定义好的attr的值,以colorPrimary为例: TypedValue value = new TypedValue(); mContext.getTheme( ...

  6. 使用 apache2 + `mod_proxy_uwsgi` + uwsgi + upstart 部署

    使用 apache2 + mod_proxy_uwsgi + uwsgi + upstart 部署 网上运行 python wsgi 的应用时,大部分的资料都是使用 nginx .uwsgi ,很少资 ...

  7. delphi中exit,abort,break,continue 的区别

    from:http://www.cnblogs.com/taofengli288/archive/2011/09/05/2167553.html delphi中表示跳出的有break,continue ...

  8. (地址)Spring Data Solr

    http://docs.spring.io/spring-data/solr/docs/1.3.0.RELEASE/reference/html/

  9. Apkplug 开发常见问题解答

    Android M (6.0) generatePackageInfo 偶现错误 出现这个现象之后会导致 BundleActivator.start() 方法不会被调用 6.0 方法签名 public ...

  10. input固定在底部

    // input固定在底部//isFocusing获取焦点:true 失去焦点:false _onTouchInput(isFocusing){ this.phone_width = screen.w ...