KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > ColumnViewerToolTipSupport


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
11  * Fredy Dobler <fredy@dobler.net> - bug 159600
12  * Brock Janiczak <brockj@tpg.com.au> - bug 182443
13  *******************************************************************************/

14
15 package org.eclipse.jface.viewers;
16
17 import org.eclipse.jface.util.Policy;
18 import org.eclipse.jface.window.DefaultToolTip;
19 import org.eclipse.jface.window.ToolTip;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.widgets.Event;
24
25 /**
26  * The ColumnViewerTooltipSupport is the class that provides tool tips for ColumnViewers.
27  *
28  * @since 3.3
29  *
30  */

31 public class ColumnViewerToolTipSupport extends DefaultToolTip {
32     private ColumnViewer viewer;
33
34     private static final String JavaDoc LABEL_PROVIDER_KEY = Policy.JFACE
35             + "_LABEL_PROVIDER"; //$NON-NLS-1$
36

37     private static final String JavaDoc ELEMENT_KEY = Policy.JFACE + "_ELEMENT_KEY"; //$NON-NLS-1$
38

39     private static final int DEFAULT_SHIFT_X = 10;
40
41     private static final int DEFAULT_SHIFT_Y = 0;
42
43     /**
44      * Enable ToolTip support for the viewer by creating an instance from this
45      * class. To get all necessary informations this support class consults the
46      * {@link CellLabelProvider}.
47      *
48      * @param viewer
49      * the viewer the support is attached to
50      * @param style style passed to control tool tip behavior
51      *
52      * @param manualActivation
53      * <code>true</code> if the activation is done manually using
54      * {@link #show(Point)}
55      */

56     protected ColumnViewerToolTipSupport(ColumnViewer viewer, int style, boolean manualActivation ) {
57         super(viewer.getControl(),style,manualActivation);
58         this.viewer = viewer;
59     }
60
61     /**
62      * Enable ToolTip support for the viewer by creating an instance from this
63      * class. To get all necessary informations this support class consults the
64      * {@link CellLabelProvider}.
65      *
66      * @param viewer
67      * the viewer the support is attached to
68      */

69     public static void enableFor(ColumnViewer viewer) {
70         new ColumnViewerToolTipSupport(viewer,ToolTip.NO_RECREATE,false);
71     }
72     
73     /**
74      * Enable ToolTip support for the viewer by creating an instance from this
75      * class. To get all necessary informations this support class consults the
76      * {@link CellLabelProvider}.
77      *
78      * @param viewer
79      * the viewer the support is attached to
80      * @param style style passed to control tool tip behavior
81      *
82      * @see ToolTip#RECREATE
83      * @see ToolTip#NO_RECREATE
84      */

85     public static void enableFor(ColumnViewer viewer, int style) {
86         new ColumnViewerToolTipSupport(viewer,style,false);
87     }
88     
89     protected Object JavaDoc getToolTipArea(Event event) {
90         return viewer.getCell(new Point(event.x,event.y));
91     }
92
93     protected final boolean shouldCreateToolTip(Event event) {
94         if( ! super.shouldCreateToolTip(event) ) {
95             return false;
96         }
97         
98         boolean rv = false;
99         
100         ViewerRow row = viewer.getViewerRow(new Point(event.x, event.y));
101
102         viewer.getControl().setToolTipText(""); //$NON-NLS-1$
103
Point point = new Point(event.x, event.y);
104
105         if (row != null) {
106             Object JavaDoc element = row.getItem().getData();
107
108             ViewerColumn viewPart = viewer.getViewerColumn(row
109                     .getColumnIndex(point));
110
111             if (viewPart == null) {
112                 return false;
113             }
114
115             CellLabelProvider labelProvider = viewPart.getLabelProvider();
116             boolean useNative = labelProvider.useNativeToolTip(element);
117             
118             String JavaDoc text = labelProvider.getToolTipText(element);
119             Image img = null;
120             
121             if( ! useNative ) {
122                 img = labelProvider.getToolTipImage(element);
123             }
124             
125             if( useNative || (text == null && img == null ) ) {
126                 viewer.getControl().setToolTipText(text);
127                 rv = false;
128             } else {
129                 setPopupDelay(labelProvider.getToolTipDisplayDelayTime(element));
130                 setHideDelay(labelProvider.getToolTipTimeDisplayed(element));
131
132                 Point shift = labelProvider.getToolTipShift(element);
133
134                 if (shift == null) {
135                     setShift(new Point(DEFAULT_SHIFT_X, DEFAULT_SHIFT_Y));
136                 } else {
137                     setShift(new Point(shift.x, shift.y));
138                 }
139                 
140                 setData(LABEL_PROVIDER_KEY, labelProvider);
141                 setData(ELEMENT_KEY, element);
142
143                 setText(text);
144                 setImage(img);
145                 setStyle(labelProvider.getToolTipStyle(element));
146                 setForegroundColor(labelProvider.getToolTipForegroundColor(element));
147                 setBackgroundColor(labelProvider.getToolTipBackgroundColor(element));
148                 setFont(labelProvider.getToolTipFont(element));
149                 
150                 // Check if at least one of the values is set
151
rv = getText(event) != null || getImage(event) != null;
152             }
153         }
154
155         return rv;
156     }
157
158     protected void afterHideToolTip(Event event) {
159         if (event != null && event.widget != viewer.getControl()) {
160             if (event.type == SWT.MouseDown) {
161                 viewer.setSelection(new StructuredSelection());
162             } else {
163                 viewer.getControl().setFocus();
164             }
165         }
166     }
167 }
168
Popular Tags