Last active
July 26, 2017 05:54
-
-
Save wduminy/6199987 to your computer and use it in GitHub Desktop.
Shows how to make a cell in a JFace TableViewer look and act like a hyperlink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.eclipse.jface.viewers.*; | |
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.custom.StyleRange; | |
import org.eclipse.swt.events.*; | |
import org.eclipse.swt.graphics.Point; | |
import org.eclipse.swt.graphics.Rectangle; | |
import org.eclipse.swt.layout.FillLayout; | |
import org.eclipse.swt.widgets.Display; | |
import org.eclipse.swt.widgets.Event; | |
import org.eclipse.swt.widgets.Shell; | |
interface LinkOpener { | |
void openLink(Object rowObject); | |
} | |
class LinkLabelProvider extends StyledCellLabelProvider { | |
private final CellLabelProvider shin; | |
private int columnIndex = -1; | |
private final LinkOpener click; | |
private int charWidth; | |
public LinkLabelProvider(CellLabelProvider aShin, LinkOpener onClick) { | |
shin = aShin; | |
click = onClick; | |
} | |
@Override | |
public void initialize(ColumnViewer viewer, ViewerColumn column) { | |
super.initialize(viewer, column); | |
Listener mouseListener = new Listener(viewer); | |
viewer.getControl().addMouseListener(mouseListener); | |
} | |
@Override | |
protected void paint(Event event, Object element) { | |
super.paint(event, element); | |
charWidth = event.gc.getFontMetrics().getAverageCharWidth() ; | |
} | |
@Override | |
public void update(ViewerCell cell) { | |
shin.update(cell); | |
StyleRange s = new StyleRange(); | |
s.foreground = cell.getItem().getDisplay().getSystemColor(SWT.COLOR_BLUE); | |
s.underline = true; | |
s.start = 0; | |
s.length = cell.getText().length(); | |
cell.setStyleRanges(new StyleRange[] {s}); | |
columnIndex = cell.getColumnIndex(); | |
} | |
private final class Listener extends MouseAdapter { | |
private final ColumnViewer column; | |
public Listener(ColumnViewer viewer) {column = viewer;} | |
@Override public void mouseDown(MouseEvent e) { | |
Point point = new Point(e.x,e.y); | |
ViewerCell cell = column.getCell(point); | |
if (cell != null && cell.getColumnIndex() == columnIndex) { | |
Rectangle rect = cell.getTextBounds(); | |
rect.width = cell.getText().length() * charWidth; | |
if (rect.contains(point)) | |
click.openLink(cell.getElement()); | |
} | |
} | |
} | |
} | |
public class TableCellClick { | |
public class DataItem { | |
public int counter; | |
public DataItem(int counter) {this.counter = counter;} | |
public String toString() {return "Item " + this.counter;} | |
} | |
public TableCellClick(Shell shell) { | |
final TableViewer v = new TableViewer(shell, SWT.BORDER | SWT.FULL_SELECTION); | |
v.setContentProvider(new ArrayContentProvider()); | |
TableViewerColumn column = new TableViewerColumn(v, SWT.NONE); | |
column.getColumn().setWidth(200); | |
column.getColumn().setMoveable(true); | |
column.getColumn().setText("Column With Link"); | |
LinkOpener linkHandler = new LinkOpener() { | |
@Override | |
public void openLink(Object rowObject) { | |
System.out.println("You clicked on " + rowObject); | |
} | |
}; | |
column.setLabelProvider(new LinkLabelProvider(new ColumnLabelProvider(), linkHandler)); | |
column = new TableViewerColumn(v, SWT.NONE); | |
column.getColumn().setWidth(200); | |
column.getColumn().setMoveable(true); | |
column.getColumn().setText("Normal column"); | |
column.setLabelProvider(new ColumnLabelProvider()); | |
DataItem[] data = createdData(); | |
v.setInput(data); | |
v.getTable().setHeaderVisible(true); | |
} | |
private DataItem[] createdData() { | |
DataItem[] result = new DataItem[10]; | |
for (int i = 0; i < 10; i++) | |
result[i] = new DataItem(i); | |
return result; | |
} | |
public static void main(String[] args) { | |
Display display = new Display(); | |
Shell shell = new Shell(display); | |
shell.setLayout(new FillLayout()); | |
new TableCellClick(shell); | |
shell.open(); | |
while (!shell.isDisposed()) { | |
if (!display.readAndDispatch()) | |
display.sleep(); | |
} | |
display.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, thank you!