Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Nebula » Preserving cell selection and focus in GridTableViewer
Preserving cell selection and focus in GridTableViewer [message #589243] Sat, 12 April 2008 22:58
Jacek Kolodziejczyk is currently offline 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));
}
}
}
}
};
Previous Topic:[CompositeTable] a question of size
Next Topic:Date selection widgets and DataBinding
Goto Forum:
  


Current Time: Thu Oct 23 13:23:08 GMT 2014

Powered by FUDForum. Page generated in 0.13775 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software