在swt中,Hyperlink可以实现 '点击跳转' 的效果。例如,在PDE的MANIFEST.MF的 Activator 点击该标签可以跳转到对应的Activator的类。
org.eclipse.pde.internal.ui.editor.plugin.rows.ReferenceAttributeRow.createLabel(Composite, FormToolkit)
protected void createLabel(Composite parent, FormToolkit toolkit) { if (!part.isEditable()) { super.createLabel(parent, toolkit); return; } Hyperlink link = toolkit.createHyperlink(parent, getPropertyLabel(), SWT.NULL); link.addHyperlinkListener(new HyperlinkAdapter() { public void linkActivated(HyperlinkEvent e) { if (!isReferenceModel()) { openReference(); // !!!Action } else { Display.getCurrent().beep(); } } }); PDETextHover.addHoverListenerToControl(fIC, link, this); }
如果需要在TreeItem上实现这样的效果需要怎么做呢?需要实现下面的UI显示:
1、 TreeItem的显示的Label需要有下划线
2、 文字颜色
在印象中,swt有一个paint的事件:SWT.Paint可以在发生paint事件,对元素UI进行重绘:
org.eclipse.swt.widgets.Control.addPaintListener(PaintListener)
public void addPaintListener (PaintListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener (listener); addListener (SWT.Paint,typedListener); //!!! }
但是TreeItem不是继承自Control。同时也没有重绘的事件发生。
Object
---Widget
------Item
---------TreeItem
在google后,找到了 http://www.iteye.com/topic/1113343 的文章,TreeItem可以通过Tree的SWT.PaintItem时来重绘。实现代码如下:
// 1> 修改字体颜色 this.tree.setForeground(toolkit.getColors().getColor(IFormColors.TITLE)); tree.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (e.detail == SWT.CHECK) { TreeItem item = (TreeItem) e.item; handleChanged((EObject) item.getData(), item.getChecked()); } else { // 3> 点击open openReference(e.item.getData()); } } }); /** * 2> 加下划线 */ tree.addListener(SWT.PaintItem, new Listener() { public void handleEvent(Event event) { TreeItem item = (TreeItem) event.item; Rectangle bounds = item.getBounds(); int textHeight = bounds.height; int textWidth = bounds.width; // 默认paint已经绘制了Text //event.gc.drawText(item.getText(), bounds.x, bounds.y, true); int descent = event.gc.getFontMetrics().getDescent(); int lineY = bounds.y + textHeight - descent + 1; event.gc.drawLine(bounds.x, lineY, bounds.x + textWidth, lineY); } });
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; public class Snippet300 { private static int IMAGE_MARGIN = 2; private static Image xImage; private static Display display; public static void main(String[] args) { display = new Display(); Shell shell = new Shell(display); shell.setBounds(10, 10, 350, 200); createImage(); final Tree tree = new Tree(shell, SWT.CHECK); tree.setBounds(10, 10, 300, 150); TreeItem item = new TreeItem(tree, SWT.NONE); item.setImage(xImage); item.setText("root item"); for (int i = 0; i < 4; i++) { TreeItem newItem = new TreeItem(item, SWT.NONE); newItem.setText("descendent " + i); // if (i % 2 == 0) { // newItem.setData(xImage); // } newItem.setImage(xImage); item.setExpanded(true); item = newItem; } treeAddListener(tree); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } xImage.dispose(); display.dispose(); } private static void createImage() { xImage = new Image(display, 16, 16); GC gc = new GC(xImage); gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); gc.drawLine(1, 1, 14, 14); gc.drawLine(1, 14, 14, 1); gc.drawOval(2, 2, 11, 11); gc.dispose(); } private static void treeAddListener(final Tree tree) { Listener listener = new Listener() { public void handleEvent(Event event) { TreeItem item = (TreeItem) event.item; Image image = (Image) item.getImage(); switch (event.type) { case SWT.MeasureItem: { if (image == null) break; event.width += 2 * (image.getBounds().width + IMAGE_MARGIN); break; } case SWT.EraseItem: { event.detail &= ~SWT.FOREGROUND; break; } case SWT.PaintItem: { if (image == null) break; int x, y; int itemH = tree.getItemHeight(); int imageH = image.getBounds().height; int imageW = image.getBounds().width; // draw the 1 image x = event.x; y = event.y + (itemH - imageH) / 2; event.gc.drawImage(image, x, y); // draw the 2 image x += IMAGE_MARGIN + imageW; event.gc.drawImage(image, x, y); // draw the string String text = item.getText(); x += IMAGE_MARGIN + imageW; y = event.y + 2; event.gc.drawText(text, x, y); // draw the 3 image x += event.width - imageW; y = event.y + (itemH - imageH) / 2; event.gc.drawImage(image, x, y); break; } } } }; tree.addListener(SWT.MeasureItem, listener); tree.addListener(SWT.EraseItem, listener); tree.addListener(SWT.PaintItem, listener); } }