Skip to main content


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 #51485] Sat, 12 April 2008 22:58 Go to next message
Jacek Kolodziejczyk is currently offline Jacek KolodziejczykFriend
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));
}
}
}
}
};
Re: Preserving cell selection and focus in GridTableViewer [message #51515 is a reply to message #51485] Mon, 14 April 2008 11:58 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Did you filed a bug report for this?

Tom
Jacek Kolodziejczyk schrieb:
> 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));
> }
> }
> }
> }
> };


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: Preserving cell selection and focus in GridTableViewer [message #51541 is a reply to message #51515] Mon, 14 April 2008 14:27 Go to previous messageGo to next message
Jacek Kolodziejczyk is currently offline Jacek KolodziejczykFriend
Messages: 37
Registered: July 2009
Member
https://bugs.eclipse.org/bugs/show_bug.cgi?id=226936
J.

Tom Schindl wrote:
> Did you filed a bug report for this?
>
> Tom
> Jacek Kolodziejczyk schrieb:
>> 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));
>> }
>> }
>> }
>> }
>> };
>
>
Re: Preserving cell selection and focus in GridTableViewer [message #52449 is a reply to message #51485] Fri, 09 May 2008 18:36 Go to previous message
Jacek Kolodziejczyk is currently offline Jacek KolodziejczykFriend
Messages: 37
Registered: July 2009
Member
Hello,

My previous snippet did not consider some edge conditions like no items,
column removal or item removal. Below is an improved version.

Cheers
Jacek

/**
* Assumption: model property must identified by a String value in
the column.getData("property").
* This removes dependency on deprecated
viewer.getColumnProperties() method.
*/
@Override
protected void preservingSelection(Runnable updateCode) {
if (!getGrid().getCellSelectionEnabled()) {
super.preservingSelection(updateCode);
return;
}

ArrayList<ModelCell> oldCellSelection = new ArrayList<ModelCell>();
ModelCell oldFocusModel = null;
Point oldFocusCell = null;

boolean restoreSelection = true;
try {
// preserve cell selection
for (Point p: getGrid().getCellSelection()) {
oldCellSelection.add(new ModelCell(p));
}

// preserve cell focus
if (getGrid().getFocusItem() != null &&
getGrid().getFocusColumn() != null) {
oldFocusCell = getGrid().getFocusCell();
oldFocusModel = new ModelCell(oldFocusCell);
}

// perform the update
super.preservingSelection(updateCode);

} finally {
// restore selection
if (restoreSelection) {
getGrid().deselectAll();

// set cell selection
for (ModelCell sel: oldCellSelection) {
Point p = sel.getPoint();
if (p != null)
getGrid().selectCell(p);
}

// set focus
if (oldFocusCell != null && getGrid().getItemCount() > 0) {
Point p = null;
if (oldFocusModel != null) p =
oldFocusModel.getPoint();
if (p == null) {
// It means old focus element property has been
removed
oldFocusCell.y = Math.min(oldFocusCell.y,
getGrid().getItemCount()-1);
oldFocusCell.x = Math.min(oldFocusCell.x,
getGrid().getColumnCount()-1);
p = oldFocusCell;
}
p.y = Math.max(p.y, 0);
p.x = Math.max(p.x, 0);
getGrid().setFocusItem(getGrid().getItem(p.y));
getGrid().setFocusColumn(getGrid().getColumn(p.x));
}
}
}
}
/**
* Represents the cell in the model terms.
* Translates back and forth between model cell and current
getGrid() coordinates.
*/
class ModelCell {
Object element;
String property;
public ModelCell(Point p) {
property =
getGrid().getColumn(p.x).getData("property").toString();
element = getElementAt(p.y);
}
public Point getPoint() {
GridItem item = (GridItem) findItem(element);
int columnIndex = -1;
for (GridColumn column: getGrid().getColumns())
if (column.getData("property").equals(property)) {
columnIndex = getGrid().indexOf(column);
break;
}
if (item == null || columnIndex == -1) return null;
return new Point(columnIndex, getGrid().indexOf(item));
}
}
Re: Preserving cell selection and focus in GridTableViewer [message #589257 is a reply to message #51485] Mon, 14 April 2008 11:58 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Did you filed a bug report for this?

Tom
Jacek Kolodziejczyk schrieb:
> 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));
> }
> }
> }
> }
> };


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: Preserving cell selection and focus in GridTableViewer [message #589270 is a reply to message #51515] Mon, 14 April 2008 14:27 Go to previous message
Jacek Kolodziejczyk is currently offline Jacek KolodziejczykFriend
Messages: 37
Registered: July 2009
Member
https://bugs.eclipse.org/bugs/show_bug.cgi?id=226936
J.

Tom Schindl wrote:
> Did you filed a bug report for this?
>
> Tom
> Jacek Kolodziejczyk schrieb:
>> 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));
>> }
>> }
>> }
>> }
>> };
>
>
Re: Preserving cell selection and focus in GridTableViewer [message #589630 is a reply to message #51485] Fri, 09 May 2008 18:36 Go to previous message
Jacek Kolodziejczyk is currently offline Jacek KolodziejczykFriend
Messages: 37
Registered: July 2009
Member
Hello,

My previous snippet did not consider some edge conditions like no items,
column removal or item removal. Below is an improved version.

Cheers
Jacek

/**
* Assumption: model property must identified by a String value in
the column.getData("property").
* This removes dependency on deprecated
viewer.getColumnProperties() method.
*/
@Override
protected void preservingSelection(Runnable updateCode) {
if (!getGrid().getCellSelectionEnabled()) {
super.preservingSelection(updateCode);
return;
}

ArrayList<ModelCell> oldCellSelection = new ArrayList<ModelCell>();
ModelCell oldFocusModel = null;
Point oldFocusCell = null;

boolean restoreSelection = true;
try {
// preserve cell selection
for (Point p: getGrid().getCellSelection()) {
oldCellSelection.add(new ModelCell(p));
}

// preserve cell focus
if (getGrid().getFocusItem() != null &&
getGrid().getFocusColumn() != null) {
oldFocusCell = getGrid().getFocusCell();
oldFocusModel = new ModelCell(oldFocusCell);
}

// perform the update
super.preservingSelection(updateCode);

} finally {
// restore selection
if (restoreSelection) {
getGrid().deselectAll();

// set cell selection
for (ModelCell sel: oldCellSelection) {
Point p = sel.getPoint();
if (p != null)
getGrid().selectCell(p);
}

// set focus
if (oldFocusCell != null && getGrid().getItemCount() > 0) {
Point p = null;
if (oldFocusModel != null) p =
oldFocusModel.getPoint();
if (p == null) {
// It means old focus element property has been
removed
oldFocusCell.y = Math.min(oldFocusCell.y,
getGrid().getItemCount()-1);
oldFocusCell.x = Math.min(oldFocusCell.x,
getGrid().getColumnCount()-1);
p = oldFocusCell;
}
p.y = Math.max(p.y, 0);
p.x = Math.max(p.x, 0);
getGrid().setFocusItem(getGrid().getItem(p.y));
getGrid().setFocusColumn(getGrid().getColumn(p.x));
}
}
}
}
/**
* Represents the cell in the model terms.
* Translates back and forth between model cell and current
getGrid() coordinates.
*/
class ModelCell {
Object element;
String property;
public ModelCell(Point p) {
property =
getGrid().getColumn(p.x).getData("property").toString();
element = getElementAt(p.y);
}
public Point getPoint() {
GridItem item = (GridItem) findItem(element);
int columnIndex = -1;
for (GridColumn column: getGrid().getColumns())
if (column.getData("property").equals(property)) {
columnIndex = getGrid().indexOf(column);
break;
}
if (item == null || columnIndex == -1) return null;
return new Point(columnIndex, getGrid().indexOf(item));
}
}
Previous Topic:Calender with highlighted dates
Next Topic:Grid SetColumnOrder
Goto Forum:
  


Current Time: Fri Apr 19 12:22:06 GMT 2024

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

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

Back to the top