Dengues Studio: Google Group:http://groups.google.com/group/dengues; QQ Group:24885404.
在自定义的一个Control上面显示一个ToolTip,它能显示节点的详细信息,如下图Dengues的实现:


它的实现代码在:
  1 /**
  2  * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
  3  * 
  4  * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-4-29 qiang.zhang $
  5  * 
  6  */
  7 public class WarehouseNodeToolTip extends ToolTip {
  8 
  9     private final static int X_SHIFT;
 10 
 11     static {
 12         if ("gtk".equals(SWT.getPlatform()) || "carbon".equals(SWT.getPlatform())) {
 13             X_SHIFT = -26;
 14         } else {
 15             X_SHIFT = -23;
 16         }
 17     }
 18 
 19     private final static int Y_SHIFT = 1;
 20 
 21     private IWarehouseNode node;
 22 
 23     private final Control control;
 24 
 25     /**
 26      * Qiang.Zhang.Adolf@gmail.com WarehouseNodeToolTip constructor comment.
 27      * 
 28      * @param control
 29      */
 30     public WarehouseNodeToolTip(Control control) {
 31         super(control);
 32         setShift(new Point(11));
 33         this.control = control;
 34     }
 35 
 36     /*
 37      * (non-Javadoc)
 38      * 
 39      * @see org.eclipse.jface.window.ToolTip#createToolTipContentArea(org.eclipse.swt.widgets.Event,
 40      * org.eclipse.swt.widgets.Composite)
 41      */
 42     @Override
 43     protected Composite createToolTipContentArea(Event event, Composite parent) {
 44         Composite composite = createToolTipContentAreaComposite(parent);
 45         addIconAndLabel(composite, WarehouseLabelUtil.getNodeIcon(node), node.getLabel());
 46         IWarehouseObject object = node.getObject();
 47         if (object != null) {
 48             Object data2 = object.getData();
 49             if (data2 instanceof Storage) {
 50                 addIconAndLabel(composite, null, ((Storage) data2).getComment());
 51             }
 52         }
 53         return composite;
 54     }
 55 
 56     /**
 57      * Qiang.Zhang.Adolf@gmail.com Comment method "addIconAndLabel".
 58      * 
 59      * @param parent
 60      * @param composite
 61      */
 62     private void addIconAndLabel(Composite composite, Image image, String label) {
 63         Label imageLabel = new Label(composite, SWT.NONE);
 64         imageLabel.setForeground(composite.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
 65         imageLabel.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
 66         imageLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
 67         imageLabel.setImage(image);
 68 
 69         Label textLabel = new Label(composite, SWT.NONE);
 70         textLabel.setForeground(composite.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
 71         textLabel.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
 72         textLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER));
 73         textLabel.setText(removeTrailingNewline(DenguesTextUtil.getNotNullString(label)));
 74     }
 75 
 76     /**
 77      * Qiang.Zhang.Adolf@gmail.com Comment method "removeTrailingNewline".
 78      * 
 79      * @param text
 80      * @return
 81      */
 82     private String removeTrailingNewline(String text) {
 83         if (text.endsWith("\n")) {
 84             return text.substring(0, text.length() - 1);
 85         }
 86         return text;
 87     }
 88 
 89     /*
 90      * (non-Javadoc)
 91      * 
 92      * @see org.eclipse.jface.window.ToolTip#getLocation(org.eclipse.swt.graphics.Point, org.eclipse.swt.widgets.Event)
 93      */
 94     @Override
 95     public Point getLocation(Point tipSize, Event event) {
 96         Widget widget = getTipWidget(event);
 97         if (widget != null) {
 98             Rectangle bounds = getBounds(widget);
 99             if (bounds != null) {
100                 return control.toDisplay(bounds.x + X_SHIFT, bounds.y + bounds.height + Y_SHIFT);
101             }
102         }
103         return super.getLocation(tipSize, event);
104     }
105 
106     /*
107      * (non-Javadoc)
108      * 
109      * @see org.eclipse.jface.window.ToolTip#shouldCreateToolTip(org.eclipse.swt.widgets.Event)
110      */
111     @Override
112     protected boolean shouldCreateToolTip(Event event) {
113         node = null;
114         if (super.shouldCreateToolTip(event)) {
115             Widget tipWidget = getTipWidget(event);
116             if (tipWidget != null) {
117                 Rectangle bounds = getBounds(tipWidget);
118                 if (bounds != null && control.getBounds().contains(bounds.x, bounds.y)) {
119                     node = getWarehouseNode(tipWidget);
120                 }
121             }
122         }
123         if (node == null) {
124             hide();
125             return false;
126         } else {
127             return true;
128         }
129     }
130 
131     /**
132      * Qiang.Zhang.Adolf@gmail.com Comment method "getWarehouseNode".
133      * 
134      * @param tipWidget
135      * @return
136      */
137     private IWarehouseNode getWarehouseNode(Widget hoverObject) {
138         if (hoverObject instanceof Widget) {
139             Object data = (hoverObject).getData();
140             if (data != null) {
141                 if (data instanceof IWarehouseNode) {
142                     return (IWarehouseNode) data;
143                 }
144             }
145         }
146 
147         return null;
148     }
149 
150     private Rectangle getBounds(Widget widget) {
151         if (widget instanceof ToolItem) {
152             ToolItem w = (ToolItem) widget;
153             return w.getBounds();
154         }
155         if (widget instanceof TableItem) {
156             TableItem w = (TableItem) widget;
157             return w.getBounds();
158         }
159         if (widget instanceof TreeItem) {
160             TreeItem w = (TreeItem) widget;
161             return w.getBounds();
162         }
163         return null;
164     }
165 
166     private Widget getTipWidget(Event event) {
167         Point widgetPosition = new Point(event.x, event.y);
168         Widget widget = event.widget;
169         if (widget instanceof ToolBar) {
170             ToolBar w = (ToolBar) widget;
171             return w.getItem(widgetPosition);
172         }
173         if (widget instanceof Table) {
174             Table w = (Table) widget;
175             return w.getItem(widgetPosition);
176         }
177         if (widget instanceof Tree) {
178             Tree w = (Tree) widget;
179             return w.getItem(widgetPosition);
180         }
181 
182         return widget;
183     }
184 
185     /**
186      * Qiang.Zhang.Adolf@gmail.com Comment method "createToolTipContentAreaComposite".
187      * 
188      * @param parent
189      * @return
190      */
191     private Composite createToolTipContentAreaComposite(Composite parent) {
192         Composite composite = new Composite(parent, SWT.NONE);
193         GridLayout gridLayout = new GridLayout();
194         gridLayout.numColumns = 2;
195         gridLayout.marginWidth = 5;
196         gridLayout.marginHeight = 2;
197         composite.setLayout(gridLayout);
198         composite.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
199         return composite;
200     }
201 }
202 
还有在org.dengues.warehouse.viewers.WarehouseView的初始化函数中实现:
 1 public void createPartControl(Composite parent) {
 2         viewer = new WarehouseTreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
 3         WarehouseViewProvider denguesViewProvider = new WarehouseViewProvider(this);
 4         viewer.setContentProvider(denguesViewProvider);
 5         viewer.setLabelProvider(denguesViewProvider);
 6         viewer.setSorter(new WarehouseViewNameSorter());
 7         IViewSite viewSite = getViewSite();
 8         viewer.setInput(viewSite);
 9         getSite().setSelectionProvider(viewer);
10 
11         makeActions();
12         hookContextMenu();
13         contributeToActionBars();
14         WarehouseViewFactory.initDragAndDrop(viewer);
15         hookDoubleClickAction();
16         nodeToolTip = new WarehouseNodeToolTip(viewer.getControl());//新建一个Tooltip
17         setPartName(Messages.getString("WarehouseView.Title.Name")); //$NON-NLS-1$
18     }
19 
并添加两个Listener:
 1 viewer.getTree().addFocusListener(new FocusAdapter() {
 2 
 3             @Override
 4             public void focusLost(FocusEvent e) {
 5                 nodeToolTip.hide();
 6             }
 7         });
 8 
 9         viewer.getTree().addKeyListener(new KeyListener() {
10 
11             public void keyPressed(KeyEvent e) {
12                 if (e.keyCode == SWT.ESC) {
13                     nodeToolTip.hide();
14                 }
15             }
16 
17             public void keyReleased(KeyEvent e) {
18             }
19 
20         });
这样就可以了。



Dengues论坛(http://groups.google.com/group/dengues/),一个很好的Eclipse开发者乐园.

Feedback

# re: [Dengues]自定义一个ToolTip,在你的View上显示提示信息。  回复  更多评论   

2008-04-30 10:31 by 王能
今天的打开速度真慢,还不如:http://www.flash2game.cn 小小游戏站 这样网站打开快.

# re: [Dengues]自定义一个ToolTip,在你的View上显示提示信息。  回复  更多评论   

2008-04-30 12:26 by BeanSoft
姓王的你就只会发广告!

只有注册用户登录后才能发表评论。


网站导航:
 
Dengues Studio: Google Group:http://groups.google.com/group/dengues; QQ Group:24885404.