如何自定义RCP外观
来源:互联网 发布:微信淘宝推广转链接 编辑:程序博客网 时间:2024/05/16 10:22
转自:http://www.fengfly.com/plus/view-168171-1.html
如果说只需要改变RCP的皮肤可以参照IBM上的Eclipse程序界面美化技术
http://www.fengfly.com/plus/view-168476-1.html
如果想自定义RCP的外观就得用到presentationFactories,presentationFactories是eclipse为editor以及view提供的一个外观工厂,在eclipse官网上推荐的书中就提到过这么个工厂,今天再看MP3MANAGER源代码的时候发现实现起来也挺简单的,不过我还是习惯eclipse风格,所以没将应用的外观改变,之前也一直在寻找改变RCP外观的方法,昨天网友告诉我IBM上有我就去看了下,就是简单的变换了图片和背景颜色,也就是个SWT的皮肤而已,下面我就直接贴代码演示presentationFactories的实现方法
首先就是Presentation 类:
- /*******************************************************************************
- * Copyright (c) 2009 Siemens AG
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Kai T枚dter - initial API and implementation
- *******************************************************************************/
- package com.siemens.ct.mp3m.ui.presentation;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.DisposeEvent;
- import org.eclipse.swt.events.DisposeListener;
- import org.eclipse.swt.events.PaintEvent;
- import org.eclipse.swt.events.PaintListener;
- import org.eclipse.swt.graphics.Color;
- import org.eclipse.swt.graphics.GC;
- import org.eclipse.swt.graphics.Point;
- import org.eclipse.swt.graphics.Rectangle;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Control;
- import org.eclipse.ui.IPropertyListener;
- import org.eclipse.ui.presentations.IPresentablePart;
- import org.eclipse.ui.presentations.IStackPresentationSite;
- import org.eclipse.ui.presentations.StackDropResult;
- import org.eclipse.ui.presentations.StackPresentation;
- public class Presentation extends StackPresentation {
- private Color borderColor;
- private IPresentablePart current;
- private final PaintListener paintListener = new PaintListener() {
- public void paintControl(final PaintEvent e) {
- final Rectangle clientArea = presentationControl.getClientArea();
- // Image img = new Image(e.display, clientArea.width,
- // clientArea.height);
- // GC gc = new GC(img);
- final GC gc = e.gc;
- final int border = 1;
- gc.setLineWidth(border);
- gc.setForeground(borderColor);
- gc.drawRectangle(clientArea.x, clientArea.y, clientArea.width - border,
- clientArea.height - border);
- // e.gc.drawImage(img, 0, 0);
- // gc.dispose();
- // img.dispose();
- }
- };
- /**
- * Listener attached to all child parts. It responds to changes in part
- * properties
- */
- private final IPropertyListener partPropertyChangeListener = new IPropertyListener() {
- public void propertyChanged(final Object source, final int property) {
- if (source instanceof IPresentablePart) {
- redraw();
- }
- }
- };
- private Composite presentationControl;
- private TabContainer tabContainer;
- private Title titleBar;
- private Color toolBarColor;
- public Presentation(final Composite parent, final IStackPresentationSite site) {
- super(site);
- // Create a top-level control for the presentation.
- presentationControl = new Composite(parent, SWT.NONE);
- borderColor = new Color(presentationControl.getDisplay(), 50, 50, 50);
- toolBarColor = new Color(presentationControl.getDisplay(), 203, 220, 235);
- presentationControl.addPaintListener(paintListener);
- titleBar = new Title(presentationControl, SWT.NONE, getSite());
- tabContainer = new TabContainer(presentationControl, SWT.NONE);
- /*
- * Add a dispose listener. Important because dispose() may // not always
- * be called.
- */
- presentationControl.addDisposeListener(new DisposeListener() {
- public void widgetDisposed(final DisposeEvent e) {
- presentationDisposed();
- }
- });
- }
- protected Presentation(final IStackPresentationSite stackSite) {
- super(stackSite);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void addPart(final IPresentablePart newPart, final Object cookie) {
- newPart.addPropertyListener(partPropertyChangeListener);
- tabContainer.addPart(newPart, this, getSite());
- }
- @Override
- public int computePreferredSize(final boolean width, final int availableParallel,
- final int availablePerpendicular, final int preferredResult) {
- if (width) {
- return Math.max(preferredResult, 100);
- } else {
- return tabContainer.getHeight() + titleBar.getHeight();
- }
- }
- @Override
- public void dispose() {
- presentationDisposed();
- }
- @Override
- public StackDropResult dragOver(final Control currentControl, final Point location) {
- // TODO Auto-generated method stub
- return null;
- }
- /**
- * Gets the colorBorder.
- *
- * @return Returns the colorBorder.
- */
- public Color getColorBorder() {
- return borderColor;
- }
- @Override
- public Control getControl() {
- return presentationControl;
- }
- @Override
- public Control[] getTabList(final IPresentablePart part) {
- return new Control[] { part.getControl() };
- }
- @Override
- public void removePart(final IPresentablePart oldPart) {
- oldPart.removePropertyListener(partPropertyChangeListener);
- tabContainer.removePart(oldPart);
- titleBar.removePart(oldPart);
- if (current == oldPart) {
- current = null;
- }
- redraw();
- }
- @Override
- public void selectPart(final IPresentablePart toSelect) {
- // Ignore redundant selections
- if (toSelect == current) {
- return;
- }
- // If there was an existing part selected, make it invisible
- if (current != null) {
- current.setVisible(false);
- }
- // Select the new part
- current = toSelect;
- // Make the part visible before setBounds, or the call to setBounds
- // may be ignored.
- if (current != null) {
- current.setVisible(true);
- setBounds(presentationControl.getBounds());
- titleBar.setPresentablePart(current);
- tabContainer.setPresentablePart(current);
- }
- }
- @Override
- public void setActive(final int newState) {
- // TODO Auto-generated method stub
- }
- @Override
- public void setBounds(final Rectangle bounds) {
- // Set the bounds of the presentation widget
- presentationControl.setBounds(bounds);
- final int titlebarHeight = titleBar.getHeight();
- final Rectangle clientArea = presentationControl.getClientArea();
- titleBar
- .setBounds(clientArea.x + 1, clientArea.y + 1, clientArea.width - 2, titlebarHeight);
- final int tabPaneHeight = tabContainer.getHeight();
- tabContainer.setBounds(clientArea.x, clientArea.y + clientArea.height - tabPaneHeight,
- clientArea.width, tabPaneHeight);
- if (current != null) {
- final Rectangle contentArea = presentationControl.getBounds();
- int toolBarHeight = 0;
- final Control toolBar = current.getToolBar();
- if (toolBar != null) {
- toolBar.setBackground(toolBarColor);
- toolBarHeight = toolBar.getBounds().height;
- toolBar.setBounds(clientArea.x + 1, clientArea.y + 1 + titlebarHeight,
- clientArea.width - 2, toolBarHeight);
- }
- contentArea.x += 1;
- contentArea.y += titlebarHeight + toolBarHeight;
- contentArea.width -= 2;
- contentArea.height -= titlebarHeight + tabPaneHeight + toolBarHeight;
- if (tabPaneHeight == 0) {
- contentArea.height -= 1;
- }
- current.setBounds(contentArea);
- }
- }
- @Override
- public void setState(final int state) {
- // TODO Auto-generated method stub
- }
- @Override
- public void setVisible(final boolean isVisible) {
- // Make the presentation widget visible
- presentationControl.setVisible(isVisible);
- titleBar.setVisible(isVisible);
- // Make the currently visible part visible
- if (current != null) {
- current.setVisible(isVisible);
- if (isVisible) {
- // Restore the bounds of the currently visible part.
- // IPartPresentations can be used by multiple
- // StackPresentations,
- // although only one such presentation is ever visible at a
- // time.
- // It is possible that some other presentation has changed the
- // bounds of the part since it was last visible, so we need to
- // update the part's bounds when the presentation becomes
- // visible.
- setBounds(presentationControl.getBounds());
- }
- }
- }
- @Override
- public void showPaneMenu() {
- // TODO Auto-generated method stub
- }
- @Override
- public void showSystemMenu() {
- // TODO Auto-generated method stub
- }
- protected void presentationDisposed() {
- // Remove any listeners that were attached to any
- // global Eclipse resources. This is necessary in order to prevent
- // memory leaks.
- borderColor.dispose();
- toolBarColor.dispose();
- presentationControl.removePaintListener(paintListener);
- presentationControl.dispose();
- presentationControl = null;
- }
- protected void redraw() {
- if (presentationControl != null) {
- presentationControl.redraw();
- titleBar.redraw();
- tabContainer.redraw();
- }
- }
- }
然后就是生产Presentation 的工厂:
- /*******************************************************************************
- * Copyright (c) 2009 Siemens AG
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Kai T枚dter - initial API and implementation
- *******************************************************************************/
- package com.siemens.ct.mp3m.ui.presentation;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.ui.presentations.AbstractPresentationFactory;
- import org.eclipse.ui.presentations.IStackPresentationSite;
- import org.eclipse.ui.presentations.StackPresentation;
- public class PresentationFactory extends AbstractPresentationFactory {
- public StackPresentation createEditorPresentation(Composite parent, IStackPresentationSite site) {
- return new Presentation(parent, site);
- }
- public StackPresentation createViewPresentation(Composite parent, IStackPresentationSite site) {
- return new Presentation(parent, site);
- }
- public StackPresentation createStandaloneViewPresentation(Composite parent,
- IStackPresentationSite site, boolean showTitle) {
- return new Presentation(parent, site);
- }
- }
辅助类:
- /*******************************************************************************
- * Copyright (c) 2009 Siemens AG
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Kai T枚dter - initial API and implementation
- *******************************************************************************/
- package com.siemens.ct.mp3m.ui.presentation;
- import java.util.ArrayList;
- import java.util.List;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.ui.presentations.IPresentablePart;
- import org.eclipse.ui.presentations.IStackPresentationSite;
- public class TabContainer extends Composite {
- protected List<Tab> tabItems = new ArrayList<Tab>();
- protected IPresentablePart part;
- protected int style;
- public TabContainer(Composite parent, int style) {
- super(parent, style);
- this.style = style;
- }
- public void addPart(IPresentablePart part, Presentation presentation,
- IStackPresentationSite site) {
- Tab tab = new Tab(this, style, presentation, site);
- tab.setPresentablePart(part);
- tabItems.add(tab);
- redraw();
- }
- public void setPresentablePart(IPresentablePart part) {
- this.part = part;
- for (Tab b : tabItems) {
- b.setSected(b.checkPart(part));
- }
- redraw();
- }
- public int getHeight() {
- if (tabItems.size() < 2) {
- return 0;
- }
- return tabItems.size() * tabItems.get(0).getHeight() - tabItems.size() + 1;
- }
- @Override
- public void setBounds(int x, int y, int width, int height) {
- int y2 = 0;
- int h = 21;
- for (Tab b : tabItems) {
- b.setBounds(x, y2, width, h);
- y2 += 20;
- }
- super.setBounds(x, y, width, height);
- }
- public void redraw() {
- if (tabItems.size() < 2) {
- return;
- }
- for (Tab b : tabItems) {
- b.redraw();
- }
- }
- public void removePart(IPresentablePart oldPart) {
- Tab foundTab = null;
- for (Tab b : tabItems) {
- if (b.getPart() == oldPart) {
- foundTab = b;
- break;
- }
- }
- if (foundTab != null) {
- tabItems.remove(foundTab);
- foundTab.dispose();
- }
- }
- }
- /*******************************************************************************
- * Copyright (c) 2009 Siemens AG
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Kai T枚dter - initial API and implementation
- *******************************************************************************/
- package com.siemens.ct.mp3m.ui.presentation;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.MouseAdapter;
- import org.eclipse.swt.events.MouseEvent;
- import org.eclipse.swt.events.MouseListener;
- import org.eclipse.swt.events.PaintEvent;
- import org.eclipse.swt.events.PaintListener;
- import org.eclipse.swt.graphics.Color;
- import org.eclipse.swt.graphics.Font;
- import org.eclipse.swt.graphics.GC;
- import org.eclipse.swt.graphics.Image;
- import org.eclipse.swt.graphics.Rectangle;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.ui.presentations.IPresentablePart;
- import org.eclipse.ui.presentations.IStackPresentationSite;
- public class Tab extends AbstractClosable implements PaintListener {
- private final Presentation presentation;
- private final IStackPresentationSite site;
- protected final Color unselectedTop = new Color(getDisplay(), 250, 250, 250);
- protected final Color unselectedBottom = new Color(getDisplay(), 200, 225, 255);
- protected final Color selectedTop = new Color(getDisplay(), 250, 230, 150);
- protected final Color selectedBottom = new Color(getDisplay(), 240, 155, 30);
- protected final Color mouseOverTop = new Color(getDisplay(), 255, 248, 223);
- protected final Color mouseOverBottom = new Color(getDisplay(), 255, 225, 120);
- protected Font font = new Font(getDisplay(), "Default", 10, SWT.NORMAL);
- private boolean isSelected;
- /**
- * This listener responds to selection events in all tabs.
- */
- private final MouseListener mouseListener = new MouseAdapter() {
- @Override
- public void mouseDown(MouseEvent e) {
- if (part != null) {
- part.setFocus();
- if (isCloseSelected()) {
- site.close(new IPresentablePart[] { part });
- } else {
- site.selectPart(part);
- presentation.selectPart(part);
- }
- }
- }
- };
- public Tab(Composite parent, int style, Presentation presentation, IStackPresentationSite site) {
- super(parent, style | SWT.NO_BACKGROUND);
- this.presentation = presentation;
- this.site = site;
- setSected(false);
- addPaintListener(this);
- addMouseListener(mouseListener);
- }
- @Override
- public void setPresentablePart(IPresentablePart part) {
- this.part = part;
- setToolTipText(part.getTitleToolTip());
- layout();
- redraw();
- }
- public boolean checkPart(IPresentablePart part) {
- return (this.part == part);
- }
- public void setSected(boolean selected) {
- isSelected = selected;
- }
- /**
- * Paint the title bar
- */
- public void paintControl(PaintEvent e) {
- Rectangle clientArea = getBounds();
- Image img = new Image(e.display, clientArea.width, clientArea.height);
- GC gc = new GC(img);
- gc.setForeground(presentation.getColorBorder());
- gc.drawRectangle(0, 0, clientArea.width - 1, clientArea.height - 1);
- Color colorTop;
- Color colorBottom;
- if (isMouseOver) {
- if (isSelected) {
- colorTop = selectedBottom;
- colorBottom = selectedTop;
- } else {
- colorTop = mouseOverTop;
- colorBottom = mouseOverBottom;
- }
- } else {
- if (isSelected) {
- colorTop = selectedTop;
- colorBottom = selectedBottom;
- } else {
- colorTop = unselectedTop;
- colorBottom = unselectedBottom;
- }
- }
- gc.setForeground(colorTop);
- gc.setBackground(colorBottom);
- gc.fillGradientRectangle(1, 1, clientArea.width - 2, clientArea.height - 2, true);
- if (part != null) {
- Image partImage = part.getTitleImage();
- gc.drawImage(partImage, 2, 2);
- Color colorText = new Color(getDisplay(), 0, 0, 0);
- // gc.setFont(font);
- gc.setForeground(colorText);
- String dirty = "";
- if (part.isDirty()) {
- dirty = "*";
- }
- int closeImageOffset = 0;
- if (part.isCloseable()) {
- closeImageOffset = 20;
- }
- String text = shortenText(gc, dirty + part.getTitle(), clientArea.width - 25
- - closeImageOffset);
- gc.drawText(text, partImage.getBounds().width + 7, 3, true);
- }
- if (part.isCloseable()) {
- if (isCloseSelected) {
- gc.drawImage(closeSelectedImage, clientArea.width - 20, 3);
- } else {
- gc.drawImage(closeUnselectedImage, clientArea.width - 20, 3);
- }
- }
- e.gc.drawImage(img, 0, 0);
- gc.dispose();
- img.dispose();
- }
- public IPresentablePart getPart() {
- return part;
- }
- public boolean isCloseSelected() {
- return isCloseSelected;
- }
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.swt.widgets.Widget#dispose()
- */
- @Override
- public void dispose() {
- unselectedTop.dispose();
- unselectedBottom.dispose();
- selectedTop.dispose();
- selectedBottom.dispose();
- mouseOverTop.dispose();
- mouseOverBottom.dispose();
- font.dispose();
- super.dispose();
- }
- }
- /*******************************************************************************
- * Copyright (c) 2009 Siemens AG
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Kai T枚dter - initial API and implementation
- *******************************************************************************/
- package com.siemens.ct.mp3m.ui.presentation;
- import org.eclipse.jface.resource.JFaceResources;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.MouseAdapter;
- import org.eclipse.swt.events.MouseEvent;
- import org.eclipse.swt.events.MouseListener;
- import org.eclipse.swt.events.PaintEvent;
- import org.eclipse.swt.graphics.Color;
- import org.eclipse.swt.graphics.Font;
- import org.eclipse.swt.graphics.GC;
- import org.eclipse.swt.graphics.Image;
- import org.eclipse.swt.graphics.Rectangle;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.ui.presentations.IPresentablePart;
- import org.eclipse.ui.presentations.IStackPresentationSite;
- public class Title extends AbstractClosable {
- private IStackPresentationSite site;
- Color colorTop = new Color(getDisplay(), 135, 135, 135);
- Color colorBottom = new Color(getDisplay(), 50, 50, 50);
- Color colorText = new Color(getDisplay(), 255, 255, 255);
- protected Font font = new Font(getDisplay(), "Default", 10, SWT.BOLD);
- /**
- * This listener responds to selection events in all tab buttons.
- */
- private MouseListener mouseListener = new MouseAdapter() {
- public void mouseDown(MouseEvent e) {
- if (part != null) {
- part.setFocus();
- if (isCloseSelected) {
- site.close(new IPresentablePart[] { part });
- }
- }
- }
- };
- public Title(Composite parent, int style, IStackPresentationSite site) {
- super(parent, style | SWT.NO_BACKGROUND);
- this.site = site;
- addPaintListener(this);
- addMouseListener(mouseListener);
- }
- /**
- * Paint the title
- */
- public void paintControl(PaintEvent e) {
- Rectangle clientArea = getBounds();
- Image img = new Image(e.display, clientArea.width, clientArea.height);
- GC gc = new GC(img);
- gc.setForeground(colorTop);
- gc.setBackground(colorBottom);
- gc.fillGradientRectangle(0, 0, clientArea.width, clientArea.height, true);
- if (part != null) {
- gc.setFont(JFaceResources.getBannerFont());
- gc.setForeground(colorText);
- String dirty = "";
- if (part.isDirty()) {
- dirty = "*";
- }
- String text = shortenText(gc,dirty + part.getTitle(), clientArea.width - 30);
- gc.drawText(text, 5, 2, true);
- if (part.isCloseable()) {
- if (isCloseSelected) {
- gc.drawImage(closeSelectedImage, clientArea.width - 20, 2);
- } else {
- gc.drawImage(closeUnselectedImage, clientArea.width - 20, 2);
- }
- }
- }
- e.gc.drawImage(img, 0, 0);
- gc.dispose();
- img.dispose();
- }
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.swt.widgets.Widget#dispose()
- */
- @Override
- public void dispose() {
- colorTop.dispose();
- colorBottom.dispose();
- colorText.dispose();
- font.dispose();
- super.dispose();
- }
- }
我这里在提供两种调用的方法
方法一:
在plugin.xml中加入
- <extension
- point="org.eclipse.ui.presentationFactories">
- <factory
- class="com.siemens.ct.mp3m.ui.presentation.PresentationFactory"
- id="com.siemens.ct.mp3m.ui.presentation.PresentationFactory"
- name="Siemens CT Presentation"/>
- </extension>
第二种方法就是在ini文件中加入
- org.eclipse.ui/presentationFactoryId=com.siemens.ct.mp3m.ui.presentation.PresentationFactory
代码不难摘自MP3M,相信大家能理解
- 如何自定义RCP外观
- 如何自定义RCP外观
- 自定义RCP外观
- 自定义RCP外观
- org.eclipse.ui.presentationFactories自定义RCP外观
- 如何自定义RCP界面
- Android下如何自定义ProgressBar的外观
- Delphi berlin 如何自定义Listview外观样式
- ASP.NET - 如何:自定义 SiteMapPath Web 服务器控件的外观
- 在vc中如何自定义AfxMessgeBox的系统窗口外观
- RCP 自定义启动界面
- RCP 自定义启动界面
- RCP 自定义启动界面
- rcp 自定义工具栏
- rcp如何连接数据库
- 自定义DataGridView外观
- 自定义FileDialog的外观
- 自定义外观的Button
- some urll
- windos 下安装bugzilla
- 怎么将dump 出来的 bitmap index 中的rowid 还原回 file_id,block_id
- PKU2421 -一道不错的题(用到了并查集,优先队列)
- Printf系列函数及va_系列宏(stdarg.h)
- 如何自定义RCP外观
- 这个异常 困扰 我很久了, 一直没有查到是什么原因 大虾们 帮帮忙 !!!
- 80后需要牢记的十大人生底线
- 中国80后美女漫画家风靡日本(组图)
- 如何自定义RCP外观
- asp.net的excel怎么导入啊?
- 如何如何扩展CEGUI库
- 修复mysql表
- 揭开正则表达式的神秘面纱(转)