ToolTip

/**这个方法用来设置Table的ToolTip,其中tbv为外部的TableViewer
   * 可以类似地写出TreeViewer的ToolTip,这个自定义控件酷似Eclipse的编辑器的
   * 弹出Tip,可以定制Tip的内部控件,如果你愿意的话.
   * @param pop true弹出自定义ToolTip,否则取消弹出
   * @author jenkinv
   * */
private void setToolTiip(boolean pop) {
   // TODO 自动生成方法存根
   final Table table = tbv.getTable();
   table.setToolTipText("");
   final Display display = PlatformUI.getWorkbench().getDisplay();
   /**文本框监听器
    * MouseEnter()
    * MouseExit()*/
   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.setFocus();
                      break;

                     case SWT.MouseExit:
                         shell.dispose ();
                         break;
                 }
             }
         };
   /**表格监听事件*/
   Listener tableListener = new Listener(){
    Shell tip = null;
    Text text = null;
    TableItem oldItem = null;
    public void handleEvent(Event event) {
     // TODO 自动生成方法存根
     switch(event.type){
     case SWT.Dispose:
     case SWT.KeyDown:
     /*case SWT.MouseMove: */{
                     if (tip == null) break;
                     tip.dispose ();
                     tip = null;
                     text = null;
                     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 ();
                         tip = new Shell (sf.getShell(), SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
                         tip.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND));
                         GridLayout layout = new GridLayout ();
                         layout.marginWidth = 5;
                         tip.setLayout (layout);
                        
                         //Button b = new Button(tip,SWT.NONE);
                         //b.setText("hello!");
                         text = new Text (tip, SWT.MULTI |SWT.WRAP |SWT.V_SCROLL);
                         text.setLayoutData(new GridData(GridData.FILL_BOTH));
                         text.setEditable(false);
                        
                         text.setForeground (display.getSystemColor (SWT.COLOR_INFO_FOREGROUND));
                         text.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND));
                         text.setData ("_TABLEITEM", item);
                         text.setText ( "文件名:\n\t" + item.getText(0) + "\n" + 
                             "作者: \n\t" + item.getText(1) + "\n" +
                             "标题: \n\t" + item.getText(2) + "\n" + 
                             "单位: \n\t" + item.getText(3) + "\n" + 
                             "关键词:\n\t" + item.getText(4) + "\n" + 
                             "摘要: \n\t" + item.getText(5) + "\n" + 
                             "文件路径:\n\t" + item.getText(6) + "\n" +  
                             "文件大小:\n\t" + item.getText(7) + "\n"  
                         );
                         text.addListener (SWT.MouseExit, textListener);
                         text.addListener (SWT.MouseEnter, textListener);
                         //Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT);
                         Rectangle rect = item.getBounds (0);
                         /**计算机绝对坐标
                          * 很重要的处理方法*/
                         Point pt = table.toDisplay (rect.x, rect.y);
                         /*设置tip的位置还有大小*/
                         tip.setBounds (event.x + pt.x + 5, pt.y + 15, 300, 300);
                         tip.setVisible (true);
                        
                     }else{//item为空时,则销毁tip
                      if((tip != null) && !tip.isDisposed()) 
                       tip.dispose();
                     }
                 }
     }
    }
   
   };
   if (pop) {
    table.addListener(SWT.Dispose, tableListener);
    table.addListener(SWT.KeyDown, tableListener);
    table.addListener(SWT.MouseMove, tableListener);
    table.addListener(SWT.MouseHover, tableListener);
   }else{
    table.removeListener(SWT.Dispose, tableListener);
    table.removeListener(SWT.KeyDown, tableListener);
    table.removeListener(SWT.MouseMove, tableListener);
    table.removeListener(SWT.MouseHover, tableListener);
   }

你可能感兴趣的:(eclipse)