comparison org.eclipse.jface/src/org/eclipse/jface/viewers/ColumnViewerToolTipSupport.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2006, 2008 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 * bugfix in: 195137, 198089
12 * Fredy Dobler <fredy@dobler.net> - bug 159600
13 * Brock Janiczak <brockj@tpg.com.au> - bug 182443
14 * Port to the D programming language:
15 * Frank Benoit <benoit@tionex.de>
16 *******************************************************************************/
17
18 module org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
19
20 import org.eclipse.jface.viewers.ColumnViewer;
21 import org.eclipse.jface.viewers.ViewerRow;
22 import org.eclipse.jface.viewers.ViewerColumn;
23 import org.eclipse.jface.viewers.ViewerCell;
24 import org.eclipse.jface.viewers.CellLabelProvider;
25 import org.eclipse.jface.viewers.StructuredSelection;
26
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Event;
31 import org.eclipse.jface.util.Policy;
32 import org.eclipse.jface.window.DefaultToolTip;
33 import org.eclipse.jface.window.ToolTip;
34
35 import java.lang.all;
36 import java.util.Set;
37
38 /**
39 * The ColumnViewerTooltipSupport is the class that provides tool tips for
40 * ColumnViewers.
41 *
42 * @since 3.3
43 *
44 */
45 public class ColumnViewerToolTipSupport : DefaultToolTip {
46 private ColumnViewer viewer;
47
48 private static const String VIEWER_CELL_KEY = Policy.JFACE
49 ~ "_VIEWER_CELL_KEY"; //$NON-NLS-1$
50
51 private static const int DEFAULT_SHIFT_X = 10;
52
53 private static const int DEFAULT_SHIFT_Y = 0;
54
55 /**
56 * Enable ToolTip support for the viewer by creating an instance from this
57 * class. To get all necessary informations this support class consults the
58 * {@link CellLabelProvider}.
59 *
60 * @param viewer
61 * the viewer the support is attached to
62 * @param style
63 * style passed to control tool tip behavior
64 *
65 * @param manualActivation
66 * <code>true</code> if the activation is done manually using
67 * {@link #show(Point)}
68 */
69 protected this(ColumnViewer viewer, int style,
70 bool manualActivation) {
71 super(viewer.getControl(), style, manualActivation);
72 this.viewer = viewer;
73 }
74
75 /**
76 * Enable ToolTip support for the viewer by creating an instance from this
77 * class. To get all necessary informations this support class consults the
78 * {@link CellLabelProvider}.
79 *
80 * @param viewer
81 * the viewer the support is attached to
82 */
83 public static void enableFor(ColumnViewer viewer) {
84 new ColumnViewerToolTipSupport(viewer, ToolTip.NO_RECREATE, false);
85 }
86
87 /**
88 * Enable ToolTip support for the viewer by creating an instance from this
89 * class. To get all necessary informations this support class consults the
90 * {@link CellLabelProvider}.
91 *
92 * @param viewer
93 * the viewer the support is attached to
94 * @param style
95 * style passed to control tool tip behavior
96 *
97 * @see ToolTip#RECREATE
98 * @see ToolTip#NO_RECREATE
99 */
100 public static void enableFor(ColumnViewer viewer, int style) {
101 new ColumnViewerToolTipSupport(viewer, style, false);
102 }
103
104 protected override Object getToolTipArea(Event event) {
105 return viewer.getCell(new Point(event.x, event.y));
106 }
107
108 /**
109 * Instead of overwriting this method subclasses should overwrite
110 * {@link #createViewerToolTipContentArea(Event, ViewerCell, Composite)}
111 */
112 protected Composite createToolTipContentArea(Event event, Composite parent) {
113 ViewerCell cell = cast(ViewerCell) getData(VIEWER_CELL_KEY);
114 setData(VIEWER_CELL_KEY, null);
115
116 return createViewerToolTipContentArea(event, cell, parent);
117 }
118
119 /**
120 * Creates the content area of the tool tip giving access to the cell the
121 * tip is shown for. Subclasses can overload this method to implement their
122 * own tool tip design.
123 *
124 * <p>
125 * This method is called from
126 * {@link #createToolTipContentArea(Event, Composite)} and by default calls
127 * the {@link DefaultToolTip#createToolTipContentArea(Event, Composite)}.
128 * </p>
129 *
130 * @param event
131 * the event that which
132 * @param cell
133 * the cell the tool tip is shown for
134 * @param parent
135 * the parent of the control to create
136 * @return the control to be displayed in the tool tip area
137 * @since 3.4
138 */
139 protected Composite createViewerToolTipContentArea(Event event,
140 ViewerCell cell, Composite parent) {
141 return super.createToolTipContentArea(event, parent);
142 }
143
144 protected override bool shouldCreateToolTip(Event event) {
145 if (!super.shouldCreateToolTip(event)) {
146 return false;
147 }
148
149 bool rv = false;
150
151 ViewerRow row = viewer.getViewerRow_package(new Point(event.x, event.y));
152
153 viewer.getControl().setToolTipText(""); //$NON-NLS-1$
154 Point point = new Point(event.x, event.y);
155
156 if (row !is null) {
157 Object element = row.getItem().getData();
158
159 ViewerCell cell = row.getCell(point);
160 ViewerColumn viewPart = viewer.getViewerColumn(cell
161 .getColumnIndex());
162
163 if (viewPart is null) {
164 return false;
165 }
166
167 CellLabelProvider labelProvider = viewPart.getLabelProvider();
168 bool useNative = labelProvider.useNativeToolTip(element);
169
170 String text = labelProvider.getToolTipText(element);
171 Image img = null;
172
173 if (!useNative) {
174 img = labelProvider.getToolTipImage(element);
175 }
176
177 if (useNative || (text is null && img is null)) {
178 viewer.getControl().setToolTipText(text);
179 rv = false;
180 } else {
181 setPopupDelay(labelProvider.getToolTipDisplayDelayTime(element));
182 setHideDelay(labelProvider.getToolTipTimeDisplayed(element));
183
184 Point shift = labelProvider.getToolTipShift(element);
185
186 if (shift is null) {
187 setShift(new Point(DEFAULT_SHIFT_X, DEFAULT_SHIFT_Y));
188 } else {
189 setShift(new Point(shift.x, shift.y));
190 }
191
192 setData(VIEWER_CELL_KEY, cell);
193
194 setText(text);
195 setImage(img);
196 setStyle(labelProvider.getToolTipStyle(element));
197 setForegroundColor(labelProvider
198 .getToolTipForegroundColor(element));
199 setBackgroundColor(labelProvider
200 .getToolTipBackgroundColor(element));
201 setFont(labelProvider.getToolTipFont(element));
202
203 // Check if at least one of the values is set
204 rv = getText(event) !is null || getImage(event) !is null;
205 }
206 }
207
208 return rv;
209 }
210
211 protected override void afterHideToolTip(Event event) {
212 super.afterHideToolTip(event);
213 // Clear the restored value else this could be a source of a leak
214 setData(VIEWER_CELL_KEY, null);
215 if (event !is null && event.widget !is viewer.getControl()) {
216 viewer.getControl().setFocus();
217 }
218 }
219 }