TableViewer tablesResultTable = new TableViewer(scrolledComposite, SWT.FULL_SELECTION );
Table table = tablesResultTable.getTable();
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
table.setHeaderVisible(true);
table.setLinesVisible(true);
tablesResultTable.setContentProvider(new FailedDDLContentProvider());
ColumnViewerToolTipSupport.enableFor(tablesResultTable,ToolTip.NO_RECREATE);
TableViewerColumn newColumnTableColumn1 = new TableViewerColumn(tablesResultTable, SWT.NONE);
newColumnTableColumn1.setLabelProvider(new FailedDDLLabelProvider(0));
newColumnTableColumn1.getColumn().setWidth(100);
newColumnTableColumn1.getColumn().setText(Messages.reportPage2TableName);
TableViewerColumn newColumnTableColumn2 = new TableViewerColumn(tablesResultTable, SWT.NONE);
newColumnTableColumn2.setLabelProvider(new FailedDDLLabelProvider(1));
newColumnTableColumn2.getColumn().setWidth(400);
newColumnTableColumn2.getColumn().setText(Messages.reportPage2FailedDDL);
TableViewerColumn newColumnTableColumn3 = new TableViewerColumn(tablesResultTable, SWT.NONE);
newColumnTableColumn3.setLabelProvider(new FailedDDLLabelProvider(2));
newColumnTableColumn3.getColumn().setWidth(400);
newColumnTableColumn3.getColumn().setText(Messages.reportPage2FailedReason);
class FailedDDLLabelProvider extends
CellLabelProvider {
private int index;
public FailedDDLLabelProvider(int index) {
this.index = index;
}
public String getColumnText(Object element, int columnIndex) {
if(element == null) return "";
if (element instanceof FailedDDL) {
FailedDDL failedDDL = (FailedDDL) element;
switch (columnIndex) {
case 0:
return failedDDL.getName();
case 1:
return failedDDL.getFailedDDL();
case 2:
return failedDDL.getFailedReson();
default:
return "";
}
}
return null;
}
public String getToolTipText(Object element) {
return getColumnText(element,index);
}
public Point getToolTipShift(Object object) {
return new Point(5, 5);
}
public int getToolTipDisplayDelayTime(Object object) {
return 300;
}
public int getToolTipTimeDisplayed(Object object) {
return 5000;
}
@Override
public void update(ViewerCell cell) {
cell.setText(getColumnText(cell.getElement(),index));
}
}
例子
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.window.ToolTip;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* Explore New API: JFace custom tooltips drawing.
*
* @author Tom Schindl <[email protected]>
* @since 3.3
*/
public class Snippet011CustomTooltips {
private static class MyContentProvider implements
IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
return new String[] { "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten" };
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
/**
* @param args
*/
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
TableViewer v = new TableViewer(shell, SWT.FULL_SELECTION);
v.getTable().setLinesVisible(true);
v.getTable().setHeaderVisible(true);
v.setContentProvider(new MyContentProvider());
ColumnViewerToolTipSupport.enableFor(v,ToolTip.NO_RECREATE);
TableViewerColumn column = new TableViewerColumn(v, SWT.NONE);
LabelProvider lp1 = new LabelProvider(0);
column.setLabelProvider(lp1);
column.getColumn().setText("Column 1");
column.getColumn().setWidth(100);
TableViewerColumn column1 = new TableViewerColumn(v, SWT.NONE);
LabelProvider lp2 = new LabelProvider(1);
column1.setLabelProvider(lp2);
column1.getColumn().setText("Column 2");
column1.getColumn().setWidth(100);
v.setInput("");
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
class LabelProvider extends CellLabelProvider {
private int index;
public LabelProvider(int index) {
this.index = index;
}
public String getToolTipText(Object element) {
return "Tooltip"+ index + " (" + element + ")";
}
public Point getToolTipShift(Object object) {
return new Point(5, 5);
}
public int getToolTipDisplayDelayTime(Object object) {
return 300;
}
public int getToolTipTimeDisplayed(Object object) {
return 2000;
}
public void update(ViewerCell cell) {
cell.setText(cell.getElement().toString());
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}