如何在RCP程序中添加一个banner栏
前言:這段時(shí)間還算比較空閑,我準(zhǔn)備把過去做過的有些形形色色,甚至有些奇怪的研究總結(jié)一下,也許剛好有人用的著也不一定,不枉為之抓耳撓腮的時(shí)光和浪費(fèi)的電力。以前有個(gè)客戶提出要在RCP程序中添加一個(gè)banner欄,研究了很久才搞定。代碼是基于eclipse4.3.2的。
先看一下效果預(yù)覽:
?
為了添加一個(gè)banner欄,我們必須重寫RCP程序最外層的layout類,即TrimmedPartLayout.java。這個(gè)layout類是用來控制menu,toolbar等最基本的layout布局的。我們寫一個(gè)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 areaif (top != null && top.isVisible()) {Point topSize = top.computeSize(caRect.width, SWT.DEFAULT, true);// Don't layout unless we've changedRectangle 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 areaif (bottom != null && bottom.isVisible()) {Point bottomSize = bottom.computeSize(caRect.width, SWT.DEFAULT,true);caRect.height -= bottomSize.y;// Don't layout unless we've changedRectangle 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 changedRectangle 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 changedRectangle 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 changedif (!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 areaif (top != null && top.isVisible()) {Point topSize = top.computeSize(caRect.width, SWT.DEFAULT, true);// Don't layout unless we've changedRectangle 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 areaif (bottom != null && bottom.isVisible()) {Point bottomSize = bottom.computeSize(caRect.width, SWT.DEFAULT,true);caRect.height -= bottomSize.y;// Don't layout unless we've changedRectangle 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 changedRectangle 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 changedRectangle 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 changedif (!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);} .... }?
其中 ---
標(biāo)記為綠色的代碼是用來替換Layout類的
標(biāo)記為藍(lán)色的代碼是用來在banner中創(chuàng)建一個(gè)Label,并加載一張作為banner的圖片。
轉(zhuǎn)載于:https://www.cnblogs.com/Binhua-Liu/p/5090226.html
總結(jié)
以上是生活随笔為你收集整理的如何在RCP程序中添加一个banner栏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 漫话:如何给女朋友解释什么是2PC(二阶
- 下一篇: webx学习(三)——Webx Turb