如何自定义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 类:

Java代码
  1. /*******************************************************************************  
  2.  * Copyright (c) 2009 Siemens AG  
  3.  *   
  4.  * All rights reserved. This program and the accompanying materials  
  5.  * are made available under the terms of the Eclipse Public License v1.0  
  6.  * which accompanies this distribution, and is available at  
  7.  * http://www.eclipse.org/legal/epl-v10.html  
  8.  *  
  9.  * Contributors:  
  10.  *    Kai T枚dter - initial API and implementation  
  11.  *******************************************************************************/  
  12.   
  13. package com.siemens.ct.mp3m.ui.presentation;   
  14.   
  15. import org.eclipse.swt.SWT;   
  16. import org.eclipse.swt.events.DisposeEvent;   
  17. import org.eclipse.swt.events.DisposeListener;   
  18. import org.eclipse.swt.events.PaintEvent;   
  19. import org.eclipse.swt.events.PaintListener;   
  20. import org.eclipse.swt.graphics.Color;   
  21. import org.eclipse.swt.graphics.GC;   
  22. import org.eclipse.swt.graphics.Point;   
  23. import org.eclipse.swt.graphics.Rectangle;   
  24. import org.eclipse.swt.widgets.Composite;   
  25. import org.eclipse.swt.widgets.Control;   
  26. import org.eclipse.ui.IPropertyListener;   
  27. import org.eclipse.ui.presentations.IPresentablePart;   
  28. import org.eclipse.ui.presentations.IStackPresentationSite;   
  29. import org.eclipse.ui.presentations.StackDropResult;   
  30. import org.eclipse.ui.presentations.StackPresentation;   
  31.   
  32. public class Presentation extends StackPresentation {   
  33.   
  34.     private Color borderColor;   
  35.   
  36.     private IPresentablePart current;   
  37.   
  38.     private final PaintListener paintListener = new PaintListener() {   
  39.   
  40.         public void paintControl(final PaintEvent e) {   
  41.             final Rectangle clientArea = presentationControl.getClientArea();   
  42.             // Image img = new Image(e.display, clientArea.width,   
  43.             // clientArea.height);   
  44.             // GC gc = new GC(img);   
  45.             final GC gc = e.gc;   
  46.   
  47.             final int border = 1;   
  48.             gc.setLineWidth(border);   
  49.             gc.setForeground(borderColor);   
  50.             gc.drawRectangle(clientArea.x, clientArea.y, clientArea.width - border,   
  51.                     clientArea.height - border);   
  52.   
  53.             // e.gc.drawImage(img, 0, 0);   
  54.             // gc.dispose();   
  55.             // img.dispose();   
  56.   
  57.         }   
  58.     };   
  59.   
  60.     /**  
  61.      * Listener attached to all child parts. It responds to changes in part  
  62.      * properties  
  63.      */  
  64.     private final IPropertyListener partPropertyChangeListener = new IPropertyListener() {   
  65.   
  66.         public void propertyChanged(final Object source, final int property) {   
  67.   
  68.             if (source instanceof IPresentablePart) {   
  69.                 redraw();   
  70.             }   
  71.         }   
  72.     };   
  73.   
  74.     private Composite presentationControl;   
  75.   
  76.     private TabContainer tabContainer;   
  77.   
  78.     private Title titleBar;   
  79.   
  80.     private Color toolBarColor;   
  81.   
  82.     public Presentation(final Composite parent, final IStackPresentationSite site) {   
  83.         super(site);   
  84.         // Create a top-level control for the presentation.   
  85.         presentationControl = new Composite(parent, SWT.NONE);   
  86.         borderColor = new Color(presentationControl.getDisplay(), 505050);   
  87.         toolBarColor = new Color(presentationControl.getDisplay(), 203220235);   
  88.   
  89.         presentationControl.addPaintListener(paintListener);   
  90.   
  91.         titleBar = new Title(presentationControl, SWT.NONE, getSite());   
  92.         tabContainer = new TabContainer(presentationControl, SWT.NONE);   
  93.   
  94.         /*  
  95.          * Add a dispose listener. Important because dispose() may // not always  
  96.          * be called.  
  97.          */  
  98.         presentationControl.addDisposeListener(new DisposeListener() {   
  99.   
  100.             public void widgetDisposed(final DisposeEvent e) {   
  101.                 presentationDisposed();   
  102.             }   
  103.         });   
  104.   
  105.     }   
  106.   
  107.     protected Presentation(final IStackPresentationSite stackSite) {   
  108.         super(stackSite);   
  109.         // TODO Auto-generated constructor stub   
  110.     }   
  111.   
  112.     @Override  
  113.     public void addPart(final IPresentablePart newPart, final Object cookie) {   
  114.         newPart.addPropertyListener(partPropertyChangeListener);   
  115.         tabContainer.addPart(newPart, this, getSite());   
  116.     }   
  117.   
  118.     @Override  
  119.     public int computePreferredSize(final boolean width, final int availableParallel,   
  120.             final int availablePerpendicular, final int preferredResult) {   
  121.         if (width) {   
  122.             return Math.max(preferredResult, 100);   
  123.         } else {   
  124.             return tabContainer.getHeight() + titleBar.getHeight();   
  125.         }   
  126.     }   
  127.   
  128.     @Override  
  129.     public void dispose() {   
  130.         presentationDisposed();   
  131.     }   
  132.   
  133.     @Override  
  134.     public StackDropResult dragOver(final Control currentControl, final Point location) {   
  135.         // TODO Auto-generated method stub   
  136.         return null;   
  137.     }   
  138.   
  139.     /**  
  140.      * Gets the colorBorder.  
  141.      *   
  142.      * @return Returns the colorBorder.  
  143.      */  
  144.     public Color getColorBorder() {   
  145.         return borderColor;   
  146.     }   
  147.   
  148.     @Override  
  149.     public Control getControl() {   
  150.         return presentationControl;   
  151.     }   
  152.   
  153.     @Override  
  154.     public Control[] getTabList(final IPresentablePart part) {   
  155.         return new Control[] { part.getControl() };   
  156.     }   
  157.   
  158.     @Override  
  159.     public void removePart(final IPresentablePart oldPart) {   
  160.         oldPart.removePropertyListener(partPropertyChangeListener);   
  161.         tabContainer.removePart(oldPart);   
  162.         titleBar.removePart(oldPart);   
  163.         if (current == oldPart) {   
  164.             current = null;   
  165.         }   
  166.         redraw();   
  167.     }   
  168.   
  169.     @Override  
  170.     public void selectPart(final IPresentablePart toSelect) {   
  171.         // Ignore redundant selections   
  172.         if (toSelect == current) {   
  173.             return;   
  174.         }   
  175.   
  176.         // If there was an existing part selected, make it invisible   
  177.         if (current != null) {   
  178.             current.setVisible(false);   
  179.         }   
  180.         // Select the new part   
  181.         current = toSelect;   
  182.   
  183.         // Make the part visible before setBounds, or the call to setBounds   
  184.         // may be ignored.   
  185.         if (current != null) {   
  186.             current.setVisible(true);   
  187.             setBounds(presentationControl.getBounds());   
  188.             titleBar.setPresentablePart(current);   
  189.             tabContainer.setPresentablePart(current);   
  190.         }   
  191.     }   
  192.   
  193.     @Override  
  194.     public void setActive(final int newState) {   
  195.         // TODO Auto-generated method stub   
  196.   
  197.     }   
  198.   
  199.     @Override  
  200.     public void setBounds(final Rectangle bounds) {   
  201.         // Set the bounds of the presentation widget   
  202.         presentationControl.setBounds(bounds);   
  203.   
  204.         final int titlebarHeight = titleBar.getHeight();   
  205.         final Rectangle clientArea = presentationControl.getClientArea();   
  206.         titleBar   
  207.                 .setBounds(clientArea.x + 1, clientArea.y + 1, clientArea.width - 2, titlebarHeight);   
  208.         final int tabPaneHeight = tabContainer.getHeight();   
  209.         tabContainer.setBounds(clientArea.x, clientArea.y + clientArea.height - tabPaneHeight,   
  210.                 clientArea.width, tabPaneHeight);   
  211.         if (current != null) {   
  212.             final Rectangle contentArea = presentationControl.getBounds();   
  213.             int toolBarHeight = 0;   
  214.             final Control toolBar = current.getToolBar();   
  215.             if (toolBar != null) {   
  216.                 toolBar.setBackground(toolBarColor);   
  217.                 toolBarHeight = toolBar.getBounds().height;   
  218.                 toolBar.setBounds(clientArea.x + 1, clientArea.y + 1 + titlebarHeight,   
  219.                         clientArea.width - 2, toolBarHeight);   
  220.             }   
  221.             contentArea.x += 1;   
  222.             contentArea.y += titlebarHeight + toolBarHeight;   
  223.             contentArea.width -= 2;   
  224.             contentArea.height -= titlebarHeight + tabPaneHeight + toolBarHeight;   
  225.             if (tabPaneHeight == 0) {   
  226.                 contentArea.height -= 1;   
  227.             }   
  228.             current.setBounds(contentArea);   
  229.         }   
  230.     }   
  231.   
  232.     @Override  
  233.     public void setState(final int state) {   
  234.         // TODO Auto-generated method stub   
  235.   
  236.     }   
  237.   
  238.     @Override  
  239.     public void setVisible(final boolean isVisible) {   
  240.         // Make the presentation widget visible   
  241.         presentationControl.setVisible(isVisible);   
  242.         titleBar.setVisible(isVisible);   
  243.   
  244.         // Make the currently visible part visible   
  245.         if (current != null) {   
  246.             current.setVisible(isVisible);   
  247.   
  248.             if (isVisible) {   
  249.                 // Restore the bounds of the currently visible part.   
  250.                 // IPartPresentations can be used by multiple   
  251.                 // StackPresentations,   
  252.                 // although only one such presentation is ever visible at a   
  253.                 // time.   
  254.                 // It is possible that some other presentation has changed the   
  255.                 // bounds of the part since it was last visible, so we need to   
  256.                 // update the part's bounds when the presentation becomes   
  257.                 // visible.   
  258.   
  259.                 setBounds(presentationControl.getBounds());   
  260.             }   
  261.         }   
  262.     }   
  263.   
  264.     @Override  
  265.     public void showPaneMenu() {   
  266.         // TODO Auto-generated method stub   
  267.     }   
  268.   
  269.     @Override  
  270.     public void showSystemMenu() {   
  271.         // TODO Auto-generated method stub   
  272.     }   
  273.   
  274.     protected void presentationDisposed() {   
  275.         // Remove any listeners that were attached to any   
  276.         // global Eclipse resources. This is necessary in order to prevent   
  277.         // memory leaks.   
  278.         borderColor.dispose();   
  279.         toolBarColor.dispose();   
  280.         presentationControl.removePaintListener(paintListener);   
  281.         presentationControl.dispose();   
  282.         presentationControl = null;   
  283.     }   
  284.   
  285.     protected void redraw() {   
  286.         if (presentationControl != null) {   
  287.             presentationControl.redraw();   
  288.             titleBar.redraw();   
  289.             tabContainer.redraw();   
  290.         }   
  291.     }   
  292. }  


然后就是生产Presentation 的工厂:

Java代码
  1. /*******************************************************************************  
  2.  * Copyright (c) 2009 Siemens AG  
  3.  *   
  4.  * All rights reserved. This program and the accompanying materials  
  5.  * are made available under the terms of the Eclipse Public License v1.0  
  6.  * which accompanies this distribution, and is available at  
  7.  * http://www.eclipse.org/legal/epl-v10.html  
  8.  *  
  9.  * Contributors:  
  10.  *    Kai T枚dter - initial API and implementation  
  11.  *******************************************************************************/  
  12.   
  13. package com.siemens.ct.mp3m.ui.presentation;   
  14.   
  15. import org.eclipse.swt.widgets.Composite;   
  16. import org.eclipse.ui.presentations.AbstractPresentationFactory;   
  17. import org.eclipse.ui.presentations.IStackPresentationSite;   
  18. import org.eclipse.ui.presentations.StackPresentation;   
  19.   
  20. public class PresentationFactory extends AbstractPresentationFactory {   
  21.     public StackPresentation createEditorPresentation(Composite parent, IStackPresentationSite site) {   
  22.         return new Presentation(parent, site);   
  23.     }   
  24.   
  25.     public StackPresentation createViewPresentation(Composite parent, IStackPresentationSite site) {   
  26.         return new Presentation(parent, site);   
  27.     }   
  28.   
  29.     public StackPresentation createStandaloneViewPresentation(Composite parent,   
  30.             IStackPresentationSite site, boolean showTitle) {   
  31.         return new Presentation(parent, site);   
  32.     }   
  33. }  


辅助类:

Java代码
  1. /*******************************************************************************  
  2.  * Copyright (c) 2009 Siemens AG  
  3.  *   
  4.  * All rights reserved. This program and the accompanying materials  
  5.  * are made available under the terms of the Eclipse Public License v1.0  
  6.  * which accompanies this distribution, and is available at  
  7.  * http://www.eclipse.org/legal/epl-v10.html  
  8.  *  
  9.  * Contributors:  
  10.  *    Kai T枚dter - initial API and implementation  
  11.  *******************************************************************************/  
  12.   
  13. package com.siemens.ct.mp3m.ui.presentation;   
  14.   
  15. import java.util.ArrayList;   
  16. import java.util.List;   
  17.   
  18. import org.eclipse.swt.widgets.Composite;   
  19. import org.eclipse.ui.presentations.IPresentablePart;   
  20. import org.eclipse.ui.presentations.IStackPresentationSite;   
  21.   
  22. public class TabContainer extends Composite {   
  23.   
  24.     protected List<Tab> tabItems = new ArrayList<Tab>();   
  25.   
  26.     protected IPresentablePart part;   
  27.   
  28.     protected int style;   
  29.   
  30.     public TabContainer(Composite parent, int style) {   
  31.         super(parent, style);   
  32.         this.style = style;   
  33.     }   
  34.   
  35.     public void addPart(IPresentablePart part, Presentation presentation,   
  36.             IStackPresentationSite site) {   
  37.         Tab tab = new Tab(this, style, presentation, site);   
  38.         tab.setPresentablePart(part);   
  39.         tabItems.add(tab);   
  40.         redraw();   
  41.     }   
  42.   
  43.     public void setPresentablePart(IPresentablePart part) {   
  44.         this.part = part;   
  45.         for (Tab b : tabItems) {   
  46.             b.setSected(b.checkPart(part));   
  47.         }   
  48.         redraw();   
  49.     }   
  50.   
  51.     public int getHeight() {   
  52.         if (tabItems.size() < 2) {   
  53.             return 0;   
  54.         }   
  55.         return tabItems.size() * tabItems.get(0).getHeight() - tabItems.size() + 1;   
  56.     }   
  57.   
  58.     @Override  
  59.     public void setBounds(int x, int y, int width, int height) {   
  60.         int y2 = 0;   
  61.         int h = 21;   
  62.         for (Tab b : tabItems) {   
  63.             b.setBounds(x, y2, width, h);   
  64.             y2 += 20;   
  65.         }   
  66.         super.setBounds(x, y, width, height);   
  67.     }   
  68.   
  69.     public void redraw() {   
  70.         if (tabItems.size() < 2) {   
  71.             return;   
  72.         }   
  73.         for (Tab b : tabItems) {   
  74.             b.redraw();   
  75.         }   
  76.     }   
  77.   
  78.     public void removePart(IPresentablePart oldPart) {   
  79.         Tab foundTab = null;   
  80.         for (Tab b : tabItems) {   
  81.             if (b.getPart() == oldPart) {   
  82.                 foundTab = b;   
  83.                 break;   
  84.             }   
  85.         }   
  86.         if (foundTab != null) {   
  87.             tabItems.remove(foundTab);   
  88.             foundTab.dispose();   
  89.         }   
  90.     }   
  91. }  

 

Java代码
  1. /*******************************************************************************  
  2.  * Copyright (c) 2009 Siemens AG  
  3.  *   
  4.  * All rights reserved. This program and the accompanying materials  
  5.  * are made available under the terms of the Eclipse Public License v1.0  
  6.  * which accompanies this distribution, and is available at  
  7.  * http://www.eclipse.org/legal/epl-v10.html  
  8.  *  
  9.  * Contributors:  
  10.  *    Kai T枚dter - initial API and implementation  
  11.  *******************************************************************************/  
  12.   
  13. package com.siemens.ct.mp3m.ui.presentation;   
  14.   
  15. import org.eclipse.swt.SWT;   
  16. import org.eclipse.swt.events.MouseAdapter;   
  17. import org.eclipse.swt.events.MouseEvent;   
  18. import org.eclipse.swt.events.MouseListener;   
  19. import org.eclipse.swt.events.PaintEvent;   
  20. import org.eclipse.swt.events.PaintListener;   
  21. import org.eclipse.swt.graphics.Color;   
  22. import org.eclipse.swt.graphics.Font;   
  23. import org.eclipse.swt.graphics.GC;   
  24. import org.eclipse.swt.graphics.Image;   
  25. import org.eclipse.swt.graphics.Rectangle;   
  26. import org.eclipse.swt.widgets.Composite;   
  27. import org.eclipse.ui.presentations.IPresentablePart;   
  28. import org.eclipse.ui.presentations.IStackPresentationSite;   
  29.   
  30. public class Tab extends AbstractClosable implements PaintListener {   
  31.   
  32.     private final Presentation presentation;   
  33.   
  34.     private final IStackPresentationSite site;   
  35.   
  36.     protected final Color unselectedTop = new Color(getDisplay(), 250250250);   
  37.   
  38.     protected final Color unselectedBottom = new Color(getDisplay(), 200225255);   
  39.   
  40.     protected final Color selectedTop = new Color(getDisplay(), 250230150);   
  41.   
  42.     protected final Color selectedBottom = new Color(getDisplay(), 24015530);   
  43.   
  44.     protected final Color mouseOverTop = new Color(getDisplay(), 255248223);   
  45.   
  46.     protected final Color mouseOverBottom = new Color(getDisplay(), 255225120);   
  47.   
  48.     protected Font font = new Font(getDisplay(), "Default"10, SWT.NORMAL);   
  49.   
  50.     private boolean isSelected;   
  51.   
  52.     /**  
  53.      * This listener responds to selection events in all tabs.  
  54.      */  
  55.     private final MouseListener mouseListener = new MouseAdapter() {   
  56.         @Override  
  57.         public void mouseDown(MouseEvent e) {   
  58.             if (part != null) {   
  59.                 part.setFocus();   
  60.                 if (isCloseSelected()) {   
  61.                     site.close(new IPresentablePart[] { part });   
  62.                 } else {   
  63.                     site.selectPart(part);   
  64.                     presentation.selectPart(part);   
  65.                 }   
  66.             }   
  67.         }   
  68.     };   
  69.   
  70.     public Tab(Composite parent, int style, Presentation presentation, IStackPresentationSite site) {   
  71.         super(parent, style | SWT.NO_BACKGROUND);   
  72.         this.presentation = presentation;   
  73.         this.site = site;   
  74.   
  75.         setSected(false);   
  76.         addPaintListener(this);   
  77.         addMouseListener(mouseListener);   
  78.     }   
  79.   
  80.     @Override  
  81.     public void setPresentablePart(IPresentablePart part) {   
  82.         this.part = part;   
  83.         setToolTipText(part.getTitleToolTip());   
  84.         layout();   
  85.         redraw();   
  86.     }   
  87.   
  88.     public boolean checkPart(IPresentablePart part) {   
  89.         return (this.part == part);   
  90.     }   
  91.   
  92.     public void setSected(boolean selected) {   
  93.         isSelected = selected;   
  94.     }   
  95.   
  96.     /**  
  97.      * Paint the title bar  
  98.      */  
  99.     public void paintControl(PaintEvent e) {   
  100.         Rectangle clientArea = getBounds();   
  101.         Image img = new Image(e.display, clientArea.width, clientArea.height);   
  102.         GC gc = new GC(img);   
  103.   
  104.         gc.setForeground(presentation.getColorBorder());   
  105.         gc.drawRectangle(00, clientArea.width - 1, clientArea.height - 1);   
  106.   
  107.         Color colorTop;   
  108.         Color colorBottom;   
  109.   
  110.         if (isMouseOver) {   
  111.             if (isSelected) {   
  112.                 colorTop = selectedBottom;   
  113.                 colorBottom = selectedTop;   
  114.             } else {   
  115.                 colorTop = mouseOverTop;   
  116.                 colorBottom = mouseOverBottom;   
  117.             }   
  118.         } else {   
  119.             if (isSelected) {   
  120.                 colorTop = selectedTop;   
  121.                 colorBottom = selectedBottom;   
  122.             } else {   
  123.                 colorTop = unselectedTop;   
  124.                 colorBottom = unselectedBottom;   
  125.             }   
  126.         }   
  127.         gc.setForeground(colorTop);   
  128.         gc.setBackground(colorBottom);   
  129.         gc.fillGradientRectangle(11, clientArea.width - 2, clientArea.height - 2true);   
  130.   
  131.         if (part != null) {   
  132.             Image partImage = part.getTitleImage();   
  133.             gc.drawImage(partImage, 22);   
  134.   
  135.             Color colorText = new Color(getDisplay(), 000);   
  136.             // gc.setFont(font);   
  137.             gc.setForeground(colorText);   
  138.             String dirty = "";   
  139.             if (part.isDirty()) {   
  140.                 dirty = "*";   
  141.             }   
  142.             int closeImageOffset = 0;   
  143.             if (part.isCloseable()) {   
  144.                 closeImageOffset = 20;   
  145.             }   
  146.             String text = shortenText(gc, dirty + part.getTitle(), clientArea.width - 25  
  147.                     - closeImageOffset);   
  148.             gc.drawText(text, partImage.getBounds().width + 73true);   
  149.         }   
  150.   
  151.         if (part.isCloseable()) {   
  152.             if (isCloseSelected) {   
  153.                 gc.drawImage(closeSelectedImage, clientArea.width - 203);   
  154.             } else {   
  155.                 gc.drawImage(closeUnselectedImage, clientArea.width - 203);   
  156.             }   
  157.         }   
  158.   
  159.         e.gc.drawImage(img, 00);   
  160.         gc.dispose();   
  161.         img.dispose();   
  162.     }   
  163.   
  164.     public IPresentablePart getPart() {   
  165.         return part;   
  166.     }   
  167.   
  168.     public boolean isCloseSelected() {   
  169.         return isCloseSelected;   
  170.     }   
  171.   
  172.     /*  
  173.      * (non-Javadoc)  
  174.      *   
  175.      * @see org.eclipse.swt.widgets.Widget#dispose()  
  176.      */  
  177.     @Override  
  178.     public void dispose() {   
  179.         unselectedTop.dispose();   
  180.         unselectedBottom.dispose();   
  181.         selectedTop.dispose();   
  182.         selectedBottom.dispose();   
  183.         mouseOverTop.dispose();   
  184.         mouseOverBottom.dispose();   
  185.         font.dispose();   
  186.         super.dispose();   
  187.     }   
  188. }  

 

Java代码
  1. /*******************************************************************************  
  2.  * Copyright (c) 2009 Siemens AG  
  3.  *   
  4.  * All rights reserved. This program and the accompanying materials  
  5.  * are made available under the terms of the Eclipse Public License v1.0  
  6.  * which accompanies this distribution, and is available at  
  7.  * http://www.eclipse.org/legal/epl-v10.html  
  8.  *  
  9.  * Contributors:  
  10.  *    Kai T枚dter - initial API and implementation  
  11.  *******************************************************************************/  
  12.   
  13. package com.siemens.ct.mp3m.ui.presentation;   
  14.   
  15. import org.eclipse.jface.resource.JFaceResources;   
  16. import org.eclipse.swt.SWT;   
  17. import org.eclipse.swt.events.MouseAdapter;   
  18. import org.eclipse.swt.events.MouseEvent;   
  19. import org.eclipse.swt.events.MouseListener;   
  20. import org.eclipse.swt.events.PaintEvent;   
  21. import org.eclipse.swt.graphics.Color;   
  22. import org.eclipse.swt.graphics.Font;   
  23. import org.eclipse.swt.graphics.GC;   
  24. import org.eclipse.swt.graphics.Image;   
  25. import org.eclipse.swt.graphics.Rectangle;   
  26. import org.eclipse.swt.widgets.Composite;   
  27. import org.eclipse.ui.presentations.IPresentablePart;   
  28. import org.eclipse.ui.presentations.IStackPresentationSite;   
  29.   
  30. public class Title extends AbstractClosable {   
  31.     private IStackPresentationSite site;   
  32.   
  33.     Color colorTop = new Color(getDisplay(), 135135135);   
  34.   
  35.     Color colorBottom = new Color(getDisplay(), 505050);   
  36.   
  37.     Color colorText = new Color(getDisplay(), 255255255);   
  38.   
  39.     protected Font font = new Font(getDisplay(), "Default"10, SWT.BOLD);   
  40.   
  41.     /**  
  42.      * This listener responds to selection events in all tab buttons.  
  43.      */  
  44.     private MouseListener mouseListener = new MouseAdapter() {   
  45.         public void mouseDown(MouseEvent e) {   
  46.             if (part != null) {   
  47.                 part.setFocus();   
  48.                 if (isCloseSelected) {   
  49.                     site.close(new IPresentablePart[] { part });   
  50.                 }   
  51.             }   
  52.         }   
  53.     };   
  54.   
  55.     public Title(Composite parent, int style, IStackPresentationSite site) {   
  56.         super(parent, style | SWT.NO_BACKGROUND);   
  57.         this.site = site;   
  58.         addPaintListener(this);   
  59.         addMouseListener(mouseListener);   
  60.     }   
  61.   
  62.     /**  
  63.      * Paint the title  
  64.      */  
  65.     public void paintControl(PaintEvent e) {   
  66.         Rectangle clientArea = getBounds();   
  67.         Image img = new Image(e.display, clientArea.width, clientArea.height);   
  68.         GC gc = new GC(img);   
  69.   
  70.         gc.setForeground(colorTop);   
  71.         gc.setBackground(colorBottom);   
  72.         gc.fillGradientRectangle(00, clientArea.width, clientArea.height, true);   
  73.   
  74.         if (part != null) {   
  75.             gc.setFont(JFaceResources.getBannerFont());   
  76.             gc.setForeground(colorText);   
  77.             String dirty = "";   
  78.             if (part.isDirty()) {   
  79.                 dirty = "*";   
  80.             }   
  81.             String text = shortenText(gc,dirty + part.getTitle(), clientArea.width - 30);   
  82.             gc.drawText(text, 52true);   
  83.   
  84.             if (part.isCloseable()) {   
  85.                 if (isCloseSelected) {   
  86.                     gc.drawImage(closeSelectedImage, clientArea.width - 202);   
  87.                 } else {   
  88.                     gc.drawImage(closeUnselectedImage, clientArea.width - 202);   
  89.                 }   
  90.             }   
  91.         }   
  92.   
  93.         e.gc.drawImage(img, 00);   
  94.         gc.dispose();   
  95.         img.dispose();   
  96.     }   
  97.   
  98.     /*  
  99.      * (non-Javadoc)  
  100.      *   
  101.      * @see org.eclipse.swt.widgets.Widget#dispose()  
  102.      */  
  103.     @Override  
  104.     public void dispose() {   
  105.         colorTop.dispose();   
  106.         colorBottom.dispose();   
  107.         colorText.dispose();   
  108.         font.dispose();   
  109.         super.dispose();   
  110.     }   
  111.   
  112. }  


我这里在提供两种调用的方法
方法一:
在plugin.xml中加入

Java代码
  1. <extension   
  2.         point="org.eclipse.ui.presentationFactories">   
  3.      <factory   
  4.            class="com.siemens.ct.mp3m.ui.presentation.PresentationFactory"  
  5.            id="com.siemens.ct.mp3m.ui.presentation.PresentationFactory"  
  6.            name="Siemens CT Presentation"/>   
  7.   </extension>  


第二种方法就是在ini文件中加入

Java代码
  1. org.eclipse.ui/presentationFactoryId=com.siemens.ct.mp3m.ui.presentation.PresentationFactory  


代码不难摘自MP3M,相信大家能理解

原创粉丝点击