In Java, I have a TreeViewer and I am trying to detect a click on a specific cell (not any cell). For example, if I click on a the first cell in a row, I want to detect that the click was made on the first cell in particular.
The following code will fire an event as soon as I double click on a row and it will return all the information about that row. How can I ensure that it will only fire the double click event when I only double click on a particular cell (i.e. a particular row and column).
viewer = new TreeViewer(parent, SWT.BORDER | SWT.FULL_SELECTION);
viewer.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(DoubleClickEvent event) {
IStructuredSelection thisSelection = (IStructuredSelection) event.getSelection();
Object selectedNode = thisSelection.getFirstElement();
System.out.println(selectedNode.toString());
}
});
EDIT
greg-449 is right. The getFirstElement method doesn't have row or col attributes. Here is the answer to greg-449's question why I don't use the cell editing support. In fact, I am using the cell edition support already for one of the columns. Please have a look at the following tree structure:
October
|__(Report 1)(Forecast revenue)(Actual revenue <-- editable)
|__(Report 2)(Forecast revenue)(Actual revenue <-- editable)
November
|__(Report 1)(Forecast revenue)(Actual revenue <-- editable)
|__(Report 2)(Forecast revenue)(Actual revenue <-- editable)
December
|__(Report 1)(Forecast revenue)(Actual revenue <-- editable)
|__(Report 2)(Forecast revenue)(Actual revenue <-- editable)
|__(Report 3)(Forecast revenue)(Actual revenue <-- editable)
In the above TreeView, I have 3 main columns: Report, Forecast revenue and Actual revenue. The actual revenue is an editable column which allows the user to enter a value, and that is the column I use the cell editing support. Now when the user double click on the cell of "Report 3" in December, I want to open the corresponding pdf file related to the report 3 in December on a tab. Basically, I want to treat the column 1 (Report) as a double click button to do something corresponding to that particular cell. When the user clicks or double clicks on column 2 "Forecast revenue", it shouldn't do anything. When the user clicks on column 3 Actual revenue", he can enter the actual value. Using the viewer.addDoubleClickListener(new IDoubleClickListener() {...} as mentioned in my 1st thread, when the user double clicks on column 2 or column 3, it opens the pdf file, which is not what I want! In addition, I have tried to use the cell editing support for column one "Report". If I set the canEdit to true, the column one becomes editable. If I set the canEdit to false, nothing happens. I don't want to edit column 1. I just want to detect the double click event! I hope I've made myself clear this time. Thanks in advance...
To detect a double-click on a particular cell in a TreeView, here is a possible solution which is based on the idea of showing cell tips in a TreeView found on the Internet. The mistake I've made was using viewer.addDoubleClickListener(new IDoubleClickListener() {..}.
Listener treeListener = new Listener () {
@Override
public void handleEvent (Event event) {
switch (event.type) {
case SWT.MouseDoubleClick: {
Point coords = new Point(event.x, event.y);
TreeItem item = viewer.getTree().getItem(coords);
if (item != null) {
int columns = viewer.getTree().getColumnCount();
for (int i = 0; i < columns; i++) {
if (item.getBounds(i).contains(coords)) {
System.out.println("Double Clicked on: " + item.getData() + " : " + i);
}
}
}
}
}
}
};
viewer.getTree().addListener (SWT.MouseDoubleClick, treeListener);