Preserving cell selection and focus in GridTableViewer [message #589243] |
Sat, 12 April 2008 22:58 |
Jacek Kolodziejczyk Messages: 37 Registered: July 2009 |
Member |
|
|
Hi there,
It may seem to be a lower priority to some of you, but I could not stand
the cell selection and focus get messed up after for example sorting the
grid.
Don't you think it'd be good to have in the official implementation?
Anyway I attach a quick work around I currently employ. May be someone
would like to use it as it is for the time being or include it into main
branch eventually.
Cheers
Jacek
final GridTableViewer v = new GridTableViewer(grid) {
/**
* Represents the cell in the model terms.
* Translates back and forth between model cell and
* current grid coordinates where it's prsented.
*/
class ModelCell {
Object element;
String property;
public ModelCell(Point p) {
property = getColumnProperties()[p.x].toString();
element = getElementAt(p.y);
}
public Point getPoint() {
return new Point(
Arrays.asList(getColumnProperties()).indexOf(property),
grid.indexOf((GridItem) findItem(element)));
}
}
@Override
protected void preservingSelection(Runnable updateCode) {
if (!getGrid().getCellSelectionEnabled()) {
super.preservingSelection(updateCode);
return;
}
ArrayList<ModelCell> oldCellSelection = new ArrayList<ModelCell>();
ModelCell oldFocus = null;
boolean restoreSelection = true;
try {
// preserve cell selection
for (Point p: getGrid().getCellSelection()) {
oldCellSelection.add(new ModelCell(p));
}
// preserve cell focus
if (grid.getFocusItem() != null)
oldFocus = new ModelCell(grid.getFocusCell());
// perform the update
super.preservingSelection(updateCode);
} finally {
// restore selection
if (restoreSelection) {
grid.deselectAll();
// set cell selection
for (ModelCell sel: oldCellSelection) {
grid.selectCell(sel.getPoint());
}
// set focus
if (oldFocus != null) {
Point p = oldFocus.getPoint();
grid.setFocusItem(grid.getItem(p.y));
grid.setFocusColumn(grid.getColumn(p.x));
}
}
}
}
};
|
|
|
Powered by
FUDForum. Page generated in 0.02612 seconds