001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
011: * Fredy Dobler <fredy@dobler.net> - bug 159600
012: * Brock Janiczak <brockj@tpg.com.au> - bug 182443
013: *******************************************************************************/package org.eclipse.jface.viewers;
014:
015: import org.eclipse.jface.util.Policy;
016: import org.eclipse.jface.window.DefaultToolTip;
017: import org.eclipse.jface.window.ToolTip;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.graphics.Image;
020: import org.eclipse.swt.graphics.Point;
021: import org.eclipse.swt.widgets.Event;
022:
023: /**
024: * The ColumnViewerTooltipSupport is the class that provides tool tips for ColumnViewers.
025: *
026: * @since 3.3
027: *
028: */
029: public class ColumnViewerToolTipSupport extends DefaultToolTip {
030: private ColumnViewer viewer;
031:
032: private static final String LABEL_PROVIDER_KEY = Policy.JFACE
033: + "_LABEL_PROVIDER"; //$NON-NLS-1$
034:
035: private static final String ELEMENT_KEY = Policy.JFACE
036: + "_ELEMENT_KEY"; //$NON-NLS-1$
037:
038: private static final int DEFAULT_SHIFT_X = 10;
039:
040: private static final int DEFAULT_SHIFT_Y = 0;
041:
042: /**
043: * Enable ToolTip support for the viewer by creating an instance from this
044: * class. To get all necessary informations this support class consults the
045: * {@link CellLabelProvider}.
046: *
047: * @param viewer
048: * the viewer the support is attached to
049: * @param style style passed to control tool tip behavior
050: *
051: * @param manualActivation
052: * <code>true</code> if the activation is done manually using
053: * {@link #show(Point)}
054: */
055: protected ColumnViewerToolTipSupport(ColumnViewer viewer,
056: int style, boolean manualActivation) {
057: super (viewer.getControl(), style, manualActivation);
058: this .viewer = viewer;
059: }
060:
061: /**
062: * Enable ToolTip support for the viewer by creating an instance from this
063: * class. To get all necessary informations this support class consults the
064: * {@link CellLabelProvider}.
065: *
066: * @param viewer
067: * the viewer the support is attached to
068: */
069: public static void enableFor(ColumnViewer viewer) {
070: new ColumnViewerToolTipSupport(viewer, ToolTip.NO_RECREATE,
071: false);
072: }
073:
074: /**
075: * Enable ToolTip support for the viewer by creating an instance from this
076: * class. To get all necessary informations this support class consults the
077: * {@link CellLabelProvider}.
078: *
079: * @param viewer
080: * the viewer the support is attached to
081: * @param style style passed to control tool tip behavior
082: *
083: * @see ToolTip#RECREATE
084: * @see ToolTip#NO_RECREATE
085: */
086: public static void enableFor(ColumnViewer viewer, int style) {
087: new ColumnViewerToolTipSupport(viewer, style, false);
088: }
089:
090: protected Object getToolTipArea(Event event) {
091: return viewer.getCell(new Point(event.x, event.y));
092: }
093:
094: protected final boolean shouldCreateToolTip(Event event) {
095: if (!super .shouldCreateToolTip(event)) {
096: return false;
097: }
098:
099: boolean rv = false;
100:
101: ViewerRow row = viewer
102: .getViewerRow(new Point(event.x, event.y));
103:
104: viewer.getControl().setToolTipText(""); //$NON-NLS-1$
105: Point point = new Point(event.x, event.y);
106:
107: if (row != null) {
108: Object element = row.getItem().getData();
109:
110: ViewerColumn viewPart = viewer.getViewerColumn(row
111: .getColumnIndex(point));
112:
113: if (viewPart == null) {
114: return false;
115: }
116:
117: CellLabelProvider labelProvider = viewPart
118: .getLabelProvider();
119: boolean useNative = labelProvider.useNativeToolTip(element);
120:
121: String text = labelProvider.getToolTipText(element);
122: Image img = null;
123:
124: if (!useNative) {
125: img = labelProvider.getToolTipImage(element);
126: }
127:
128: if (useNative || (text == null && img == null)) {
129: viewer.getControl().setToolTipText(text);
130: rv = false;
131: } else {
132: setPopupDelay(labelProvider
133: .getToolTipDisplayDelayTime(element));
134: setHideDelay(labelProvider
135: .getToolTipTimeDisplayed(element));
136:
137: Point shift = labelProvider.getToolTipShift(element);
138:
139: if (shift == null) {
140: setShift(new Point(DEFAULT_SHIFT_X, DEFAULT_SHIFT_Y));
141: } else {
142: setShift(new Point(shift.x, shift.y));
143: }
144:
145: setData(LABEL_PROVIDER_KEY, labelProvider);
146: setData(ELEMENT_KEY, element);
147:
148: setText(text);
149: setImage(img);
150: setStyle(labelProvider.getToolTipStyle(element));
151: setForegroundColor(labelProvider
152: .getToolTipForegroundColor(element));
153: setBackgroundColor(labelProvider
154: .getToolTipBackgroundColor(element));
155: setFont(labelProvider.getToolTipFont(element));
156:
157: // Check if at least one of the values is set
158: rv = getText(event) != null || getImage(event) != null;
159: }
160: }
161:
162: return rv;
163: }
164:
165: protected void afterHideToolTip(Event event) {
166: if (event != null && event.widget != viewer.getControl()) {
167: if (event.type == SWT.MouseDown) {
168: viewer.setSelection(new StructuredSelection());
169: } else {
170: viewer.getControl().setFocus();
171: }
172: }
173: }
174: }
|