View Javadoc

1   /*
2   Tooltip component for GWT
3   Copyright (C) 2006 Alexei Sokolov http://gwt.components.googlepages.com/
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  
19  */
20  
21  package com.gwt.components.client;
22  
23  import com.google.gwt.user.client.Timer;
24  import com.google.gwt.user.client.ui.HTML;
25  import com.google.gwt.user.client.ui.MouseListenerAdapter;
26  import com.google.gwt.user.client.ui.PopupPanel;
27  import com.google.gwt.user.client.ui.Widget;
28  
29  public class TooltipListener extends MouseListenerAdapter {
30    private static final String DEFAULT_TOOLTIP_STYLE = "TooltipPopup";
31    private static final int DEFAULT_OFFSET_X = 10;
32    private static final int DEFAULT_OFFSET_Y = 35;
33  
34    private static class Tooltip extends PopupPanel {
35      private int delay;
36  
37      public Tooltip(Widget sender, int offsetX, int offsetY, 
38          final String text, final int delay, final String styleName) {
39        super(true);
40  
41        this.delay = delay;
42  
43        HTML contents = new HTML(text);
44        add(contents);
45  
46        int left = sender.getAbsoluteLeft() + offsetX;
47        int top = sender.getAbsoluteTop() + offsetY;
48  
49        setPopupPosition(left, top);
50        setStyleName(styleName);
51      }
52  
53      public void show() {
54        super.show();
55  
56        Timer t = new Timer() {
57  
58          public void run() {
59            Tooltip.this.hide();
60          }
61  
62        };
63        t.schedule(delay);
64      }
65    }
66  
67    private Tooltip tooltip;
68    private String text;
69    private String styleName;
70    private int delay;
71    private int offsetX = DEFAULT_OFFSET_X;
72    private int offsetY = DEFAULT_OFFSET_Y;
73  
74    public TooltipListener(String text, int delay) {
75      this(text, delay, DEFAULT_TOOLTIP_STYLE);
76    }
77  
78    public TooltipListener(String text, int delay, String styleName) {
79      this.text = text;
80      this.delay = delay;
81      this.styleName = styleName;
82    }
83  
84    public void onMouseEnter(Widget sender) {
85      if (tooltip != null) {
86        tooltip.hide();
87      }
88      tooltip = new Tooltip(sender, offsetX, offsetY, text, delay, styleName);
89      tooltip.show();
90    }
91  
92    public void onMouseLeave(Widget sender) {
93      if (tooltip != null) {
94        tooltip.hide();
95      }
96    }
97  
98    public String getStyleName() {
99      return styleName;
100   }
101 
102   public void setStyleName(String styleName) {
103     this.styleName = styleName;
104   }
105 
106   public int getOffsetX() {
107     return offsetX;
108   }
109 
110   public void setOffsetX(int offsetX) {
111     this.offsetX = offsetX;
112   }
113 
114   public int getOffsetY() {
115     return offsetY;
116   }
117 
118   public void setOffsetY(int offsetY) {
119     this.offsetY = offsetY;
120   }
121 }