如何在RCP程式中添加一個banner欄,rcp添加banner欄
前言:這段時間還算比較空閑,我準備把過去做過的有些形形色色,甚至有些奇怪的研究總結一下,也許剛好有人用的著也不一定,不枉為之抓耳撓腮的時光和浪費的電力。以前有個客戶提出要在RCP程式中添加一個banner欄,研究了很久才搞定。代碼是基於eclipse4.3.2的。
先看一下效果預覽:
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的圖片。