1 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 31 public class ColumnViewerToolTipSupport extends DefaultToolTip { 32 private ColumnViewer viewer; 33 34 private static final String LABEL_PROVIDER_KEY = Policy.JFACE 35 + "_LABEL_PROVIDER"; 37 private static final String ELEMENT_KEY = Policy.JFACE + "_ELEMENT_KEY"; 39 private static final int DEFAULT_SHIFT_X = 10; 40 41 private static final int DEFAULT_SHIFT_Y = 0; 42 43 56 protected ColumnViewerToolTipSupport(ColumnViewer viewer, int style, boolean manualActivation ) { 57 super(viewer.getControl(),style,manualActivation); 58 this.viewer = viewer; 59 } 60 61 69 public static void enableFor(ColumnViewer viewer) { 70 new ColumnViewerToolTipSupport(viewer,ToolTip.NO_RECREATE,false); 71 } 72 73 85 public static void enableFor(ColumnViewer viewer, int style) { 86 new ColumnViewerToolTipSupport(viewer,style,false); 87 } 88 89 protected Object 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(""); Point point = new Point(event.x, event.y); 104 105 if (row != null) { 106 Object 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 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 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 |