如何使自定义工具提示与Eclipse工具提示相同?

时间:2017-06-02 06:43:13

标签: java eclipse swt tooltip eclipse-rcp

Eclipse工具提示的基本功能: 当鼠标悬停在代码文本上然后工具提示打开然后当鼠标悬停工具提示焦点移动到打开工具提示滚动条可见。

我附加了工具提示屏幕。

在文本上使用Tooltipopen鼠标:

Tooltipopen mouse on the text

当鼠标悬停到工具提示时,

滚动条可见:

Scroll Bar is visible when mouse hover to tooltip

现在我想制作工具提示和Eclipse Tool-tip一样。

我希望这个工具提示工具进入SWT表单元工具提示。当鼠标光标悬停到表格单元格然后打开工具提示然后鼠标移动/悬停到该工具提示然后滚动条可见。 我已经做了很多谷歌搜索然后制作是我已经尝试制作工具提示但我不能像eclipse工具提示一样。

public static void generateTooltip(final TableCursor tableCursor) {         
    final Listener labelListener = new Listener() {
        public void handleEvent(Event event) {
            label = (Text) event.widget;
            tip = label.getShell();
            switch (event.type) 
            {
            case SWT.MouseDown:
                label.setFocus();
                break;
            case SWT.MouseExit:                     
                if(!tip.isFocusControl())
                {
                    tip.dispose();
                }
                break;
            case SWT.MouseHover:    
                label.setFocus();
                break;
            case SWT.FocusOut:  
                tip.dispose();
                break;
             case SWT.MouseEnter:
              label.setFocus();
              break;
            }

        }
    };
    Listener tableListener = new Listener() {
        public void handleEvent(Event event) {              
            switch (event.type) {               
            case SWT.Dispose: {
                if (tip == null)
                    break;
                tip.dispose();
                tip = null;
                label = null;
                break;
            }

            case SWT.MouseExit: {

                if(tip != null && !tip.isDisposed())
                {
                    if(!tip.isFocusControl())
                    {
                        tip.dispose();  
                    }
                }       
            }

            case SWT.MouseHover: {
                //for F1 pressed key focus.
                 TableItem item = tableCursor.getRow();
                if (item != null) 
                {

                if(tip != null && !tip.isDisposed()  )
                {
                    if(!tip.isFocusControl()){
                        tip.close();
                          tip.dispose();    
                    }                       
                }           
                if(tableCursor.getRow().getText(tableCursor.getColumn()).equals(""))
                {
                    return;
                }
                    tip = new Shell(tableCursor.getShell(), SWT.ON_TOP| SWT.TOOL);
                    tip.setLayout(new GridLayout(1, false));
                    tip.layout(true, true);
                    tip.setBackground(SWTResourceManager.getColor(255, 250, 205));
                    ScrolledComposite scroll = new ScrolledComposite(tip,SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
                    GridData gd_scrolledComposite = new GridData(SWT.FILL,SWT.FILL, true, true, 1, 1);
                    scroll.setLayoutData(gd_scrolledComposite);
                    scroll.setExpandHorizontal(true);
                    scroll.setExpandVertical(true);
                    scroll.setMinSize(item.getBounds().width,item.getBounds().height);
                    Composite compo = new Composite(scroll, SWT.RESIZE);
                    compo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,true, true, 1, 1));
                    compo.setLayout(new GridLayout(1, false));
                    compo.layout(true);
                    scroll.setContent(compo);                                               
                    label = new Text(compo, SWT.RESIZE | SWT.V_SCROLL | SWT.WRAP | SWT.BORDER | SWT.READ_ONLY);

                    GridData gd_lblNewLabel_2 = new GridData(SWT.FILL,SWT.FILL, true, true, 1, 1);
                    gd_lblNewLabel_2.widthHint = 251;
                    gd_lblNewLabel_2.heightHint = 75;
                    label.setLayoutData(gd_lblNewLabel_2);
                    label.setText(tableCursor.getRow().getText( tableCursor.getColumn()));

                    Label lblNewLabel_1 = new Label(tip, SWT.NONE);
                    GridData gd_lblNewLabel_1 = new GridData(SWT.LEFT,SWT.RIGHT, true, true, 1, 1);                 
                    lblNewLabel_1.setLayoutData(gd_lblNewLabel_1);  
                    lblNewLabel_1.setText("Press F1 to focus");
                    lblNewLabel_1.setBackground(SWTResourceManager.getColor(255, 250, 205));

                    Label saveTextLabel = new Label(tip, SWT.NONE);
                    GridData gd_savetext = new GridData(SWT.LEFT,SWT.RIGHT, true, true, 1, 1);
                    saveTextLabel.setLayoutData(gd_savetext);
                    saveTextLabel.setText("Press F3 to Save");
                    saveTextLabel.setVisible(false);
                    saveTextLabel.setBackground(SWTResourceManager.getColor(255, 250, 205));

                    label.addListener(SWT.MouseHover, labelListener);
                    label.addListener(SWT.MouseExit, labelListener);
                    label.addListener(SWT.MouseEnter, labelListener);

                    label.setBackground(SWTResourceManager.getColor(255,250, 205));
                    Rectangle rect = item.getBounds();
                    Point pt = tableCursor.toDisplay(rect.x, rect.y);
                    tip.setBounds(pt.x - rect.x, pt.y - rect.y  + tableCursor.getSize().y, rect.width,  rect.height);
                    tip.setVisible(true);                       
                    tip.pack(true);
                    setFocusTooltip = 0;

                }
                break;
             }      

            case SWT.Selection: {
                if (tip == null)
                    break;

                if(tip != null && !tip.isDisposed()){                   
                    tip.dispose();
                 }
                }
            case SWT.MouseMove: {
                if (tip == null)
                break;

                if(tip != null && !tip.isDisposed() && !tip.isFocusControl())
                {
                      tip.close();
                      tip.dispose();
                }

            }

            }
            if (event.keyCode == SWT.F1) 
            {               
                if (tip != null && !tip.isDisposed()) {
                    label.setFocus();
                     Listener resizelistener = new Listener (){  
                           @Override  
                           public void handleEvent(Event event) {  
                            Text t = (Text)event.widget; 
                            Rectangle r1 = t.getClientArea();  
                            Rectangle r2 = t.computeTrim(r1.x, r1.y, r1.width, r1.height);  
                            Point p = t.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);                               
                            if( t.getHorizontalBar() != null )
                            {
                                t.getHorizontalBar().setVisible(r2.width <= p.x);  
                            }
                            if(  t.getVerticalBar() != null)
                            {
                                t.getVerticalBar().setVisible(r2.height <= p.y);  
                            }

                            if (event.type == SWT.Modify){
                                   t.getParent().layout(true);  
                                   t.showSelection();  
                            }  
                            if(event.type == SWT.Verify){

                            }
                            if(event.type == SWT.MouseMove){
                                if(tip != null && !tip.isDisposed() && !tip.isFocusControl())
                                {
                                      tip.close();
                                      tip.dispose();
                                }
                              }                
                           }};  
                           label.addListener(SWT.Resize, resizelistener);  
                           label.addListener(SWT.Modify, resizelistener);
                           label.addListener(SWT.Verify, resizelistener);
                           label.addListener(SWT.KeyDown, resizelistener);
                           setFocusTooltip = 1;                           
                            }                       
            }}
        };      
        tableCursor.addListener(SWT.Dispose, tableListener);
        tableCursor.addListener(SWT.MouseExit, tableListener);
        tableCursor.addListener(SWT.KeyDown, tableListener);
        tableCursor.addListener(SWT.MouseHover, tableListener);
        tableCursor.addListener(SWT.FocusOut, tableListener);
        tableCursor.addListener(SWT.Traverse, tableListener);
        tableCursor.addListener(SWT.Selection, tableListener);
}

0 个答案:

没有答案