设置Table的ToolTip

    /**
     * 设置Table的ToolTip 
     * @param tbv
     */
    public static void setToolTiip(final TableViewer tbv) {
        final Table table = tbv.getTable();
        table.setToolTipText("");
//        final Display display = PlatformUI.getWorkbench().getDisplay();
        
        final Listener textListener = new Listener() {

            public void handleEvent(Event event) {
                Text text = (Text) event.widget;
                Shell shell = text.getShell();
                switch (event.type) {
                    case SWT.MouseEnter:
                        text.forceFocus();
                        break;

                    case SWT.MouseExit:
                        shell.dispose();
                        break;
                }
            }
        };
        
        final Listener tableListener = new Listener() {

            Shell     tip     = null;
            Text      text    = null;
            TableItem oldItem = null;

            public void handleEvent(Event event) {
                switch (event.type) {
                    case SWT.Dispose:
                    case SWT.KeyDown: {
                        if (tip == null) break;
                        tip.dispose();
                        tip = null;
                        break;
                    }
                    
                    case SWT.MouseDown: {
                        if (tip == null) break;
                        tip.dispose();
                        tip = null;
                        break;
                    }
                    
                    case SWT.MouseWheel: {
                        if (tip == null || tip.isDisposed()) break;
                        table.notifyListeners(SWT.MouseHover, event);
                        break;
                    }
                    
                    case SWT.MouseHover: {
                        TableItem item = table.getItem(new Point(event.x, event.y));
                        if (item == oldItem && tip != null && !tip.isDisposed()) 
                            return;
                        else 
                            oldItem = item;

                        if (item != null) {
//                            if (tip != null && !tip.isDisposed()) tip.dispose();
                            
                            if (tip == null || tip.isDisposed())
                            {
                                tip = new Shell(event.display, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
                                tip.setBackground(event.display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
                                GridLayout layout = new GridLayout();
                                layout.marginWidth = 5;
                                tip.setLayout(layout);

                                text = new Text(tip, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
                                text.setLayoutData(new GridData(GridData.FILL_BOTH));
                                text.setEditable(false);

                                text.setForeground(event.display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
                                text.setBackground(event.display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
                                
                                text.addListener(SWT.MouseExit, textListener);
                                text.addListener(SWT.MouseEnter, textListener);
                            }
                            
                            TableColumn[] columns = tbv.getTable().getColumns();
                            StringBuilder buff = new StringBuilder();
                            for(int i=0; i<columns.length; i++)
                            {
                                buff.append(columns[i].getText()).append(": \n\t").append(item.getText(i)).append("\n");
                            }
                            
                            text.setText(buff.toString());
                            
                            Point p = table.getLocation();
                            Point point = table.toDisplay(p.x, p.y);
                            Rectangle rect = item.getBounds();
                            tip.setBounds(event.x + point.x, point.y + rect.y + table.getHeaderHeight()/2, 300, 300);
                            tip.setVisible(true);
//                            tip.forceFocus();
//                            tip.setActive();
//                            tip.addListener(SWT.Deactivate, new Listener() {
//                                @Override
//                                public void handleEvent(Event event) {
//                                    tip.dispose();
//                                }
//                            });
                        } else {
                            if ((tip != null) && !tip.isDisposed()) tip.dispose();
                        }
                    }
                }
            }

        };
        
        table.addListener(SWT.Dispose, tableListener);
        table.addListener(SWT.KeyDown, tableListener);
        table.addListener(SWT.MouseHover, tableListener);
        table.addListener(SWT.MouseDown, tableListener);
        table.addListener(SWT.MouseWheel, tableListener);

    }

 

自定义控件酷似Eclipse的编辑器的弹出ToolTip,可以定制Tip的内部控件。

 

 

你可能感兴趣的:(eclipse,table,RCP,SWT)