Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » [TableViewer] How to block FocusCellHighlighter and/or FocusCellManager
[TableViewer] How to block FocusCellHighlighter and/or FocusCellManager [message #324045] Wed, 16 January 2008 22:49 Go to next message
Eclipse UserFriend
Originally posted by: automatic.javalobby.org

I was using the following snippet from Tom Schindl's Snippet page with my tableviewe with EditingSupport. That works pretty well for me.

But now, I needed to hide some columns simply set up their width to zero. Sorry, but I can't just delete them.

EditingSupport is working pretty well to because I use the canEdit() method.

However my FocusCellHighlighter and FocusCellManager are still navigating on top of the hidden cells.

Please, I need some tip how I could block the navigation on hidden (zero-width) columns.

Thanks in advance!
And thank you, Tom! I apreciate your snippets, samples, your blog and your answers here.

Happy New Year!


- cut here --------------------
class EditableFocusCellHighlighter extends FocusCellHighlighter {
private ViewerCell oldCell;

/**
* @param viewer
* the viewer
*/
public EditableFocusCellHighlighter(ColumnViewer viewer) {
super(viewer);
this.hookListener(viewer);
}

private void markFocusedCell(Event event, ViewerCell cell) {
final GC gc = event.gc;

gc.setBackground(Activator.getStandardDisplay().getSystemCol or(
SWT.COLOR_WHITE));
gc.setForeground(Activator.getStandardDisplay().getSystemCol or(
SWT.COLOR_BLACK));
gc.setLineWidth(3);
final int currentCol = event.index;
final Rectangle rect = ((TableItem) event.item).getBounds(currentCol);
gc.drawRectangle(rect);

event.detail &= ~SWT.FOCUSED;
event.detail &= SWT.FocusOut;
event.detail &= ~SWT.SELECTED;
}

private void removeSelectionInformation(Event event, ViewerCell cell) {

}

private void hookListener(final ColumnViewer viewer) {
final Listener listener = new Listener() {

public void handleEvent(Event event) {
if ((event.detail & SWT.SELECTED) > 0) {
ViewerCell focusCell = EditableFocusCellHighlighter.this.getFocusCell();
ViewerRow row = focusCell.getViewerRow();

Assert
.isNotNull(row,
"Internal structure invalid. Item without associated row is not possible."); //$NON-NLS-1$

ViewerCell cell = row.getCell(event.index);

if (focusCell == null || !cell.equals(focusCell)) {
EditableFocusCellHighlighter.this.removeSelectionInformation (event, cell);
} else {
EditableFocusCellHighlighter.this.markFocusedCell(event, cell);
}
}
}

};
viewer.getControl().addListener(SWT.EraseItem, listener);
}

/**
* @param cell
* the cell which is colored
* @return the color
*/
protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
return null;
}

/**
* @param cell
* the cell which is colored
* @return the color
*/
protected Color getSelectedCellForegroundColor(ViewerCell cell) {
return null;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.FocusCellHighlighter#focusCellChan ged(org.eclipse.jface.viewers.ViewerCell)
*/
@Override
protected void focusCellChanged(ViewerCell cell) {
super.focusCellChanged(cell);

// Redraw new area
if (cell != null) {
final Rectangle rect = cell.getBounds();
final int x = cell.getColumnIndex() == 0 ? 0 : rect.x;
final int width = cell.getColumnIndex() == 0 ? rect.x + rect.width
: rect.width;
// 1 is a fix for Linux-GTK
cell.getControl().redraw(x, rect.y - 1, width, rect.height + 1,
true);
}

if (oldCell != null) {
final Rectangle rect = oldCell.getBounds();
final int x = oldCell.getColumnIndex() == 0 ? 0 : rect.x;
final int width = oldCell.getColumnIndex() == 0 ? rect.x + rect.width
: rect.width;
// 1 is a fix for Linux-GTK
oldCell.getControl().redraw(x, rect.y - 1, width,
rect.height + 1, true);
}

oldCell = cell;
}
}

class MyTableViewerFocusCellManager extends TableViewerFocusCellManager {
private final TableViewer viewer;
private Method setFocusCellMethod;
private Method getViewerRowFromItemMethod;

public MyTableViewerFocusCellManager(TableViewer viewer,
FocusCellHighlighter focusDrawingDelegate) {
super(viewer, focusDrawingDelegate);
this.viewer = viewer;
try {
setFocusCellMethod = TableViewerFocusCellManager.class.getSuperclass().getDeclare dMethod( "setFocusCell", new Class[] {ViewerCell.class});
setFocusCellMethod.setAccessible(true);
getViewerRowFromItemMethod = TableViewer.class.getDeclaredMethod("getViewerRowFromItem", new Class[] {Widget.class});
getViewerRowFromItemMethod.setAccessible(true);
} catch (final SecurityException e) {
e.printStackTrace();
} catch (final NoSuchMethodException e) {
e.printStackTrace();
}

}

@Override
public ViewerCell getFocusCell() {
final ViewerCell cell = super.getFocusCell();
final Table t = viewer.getTable();

// It is possible that the selection has changed under the hood
if (cell != null) {
if (t.getSelection().length == 1
&& t.getSelection()[0] != cell.getItem()) {
try {
final ViewerRow row = (ViewerRow) getViewerRowFromItemMethod.invoke(viewer, new Object[] {t.getSelection()[0]});
final ViewerCell newCell = row.getCell(cell.getColumnIndex());
setFocusCellMethod.invoke(this, new Object[] {newCell});
} catch (final IllegalArgumentException e) {
e.printStackTrace();
} catch (final IllegalAccessException e) {
e.printStackTrace();
} catch (final InvocationTargetException e) {
e.printStackTrace();
}

}
}

return super.getFocusCell();
}
}
Re: [TableViewer] How to block FocusCellHighlighter and/or FocusCellManager [message #324057 is a reply to message #324045] Thu, 17 January 2008 03:36 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

I need to think about it more but the first an easiest solution that
comes to my mind is to use Nebula-Grid which supports this out of the
box with it's column grouping function.

To make it work you'll need to use the latest Grid and GridViewer sources.

I'm moreover afraid that even if you could dispose / recreate this won't
work in 3.3 because we had index problems we fixed in 3.4. I'll bang my
head against it an come back to you later (today in the evening CET), if
I forget about it just ping this thread.

Tom

celeraman+ schrieb:
> I was using the following snippet from Tom Schindl's Snippet page with my tableviewe with EditingSupport. That works pretty well for me.
>
> But now, I needed to hide some columns simply set up their width to zero. Sorry, but I can't just delete them.
>
> EditingSupport is working pretty well to because I use the canEdit() method.
>
> However my FocusCellHighlighter and FocusCellManager are still navigating on top of the hidden cells.
>
> Please, I need some tip how I could block the navigation on hidden (zero-width) columns.
>
> Thanks in advance!
> And thank you, Tom! I apreciate your snippets, samples, your blog and your answers here.
>
> Happy New Year!
>
>
> - cut here --------------------
> class EditableFocusCellHighlighter extends FocusCellHighlighter {
> private ViewerCell oldCell;
>
> /**
> * @param viewer
> * the viewer
> */
> public EditableFocusCellHighlighter(ColumnViewer viewer) {
> super(viewer);
> this.hookListener(viewer);
> }
>
> private void markFocusedCell(Event event, ViewerCell cell) {
> final GC gc = event.gc;
>
> gc.setBackground(Activator.getStandardDisplay().getSystemCol or(
> SWT.COLOR_WHITE));
> gc.setForeground(Activator.getStandardDisplay().getSystemCol or(
> SWT.COLOR_BLACK));
> gc.setLineWidth(3);
> final int currentCol = event.index;
> final Rectangle rect = ((TableItem) event.item).getBounds(currentCol);
> gc.drawRectangle(rect);
>
> event.detail &= ~SWT.FOCUSED;
> event.detail &= SWT.FocusOut;
> event.detail &= ~SWT.SELECTED;
> }
>
> private void removeSelectionInformation(Event event, ViewerCell cell) {
>
> }
>
> private void hookListener(final ColumnViewer viewer) {
> final Listener listener = new Listener() {
>
> public void handleEvent(Event event) {
> if ((event.detail & SWT.SELECTED) > 0) {
> ViewerCell focusCell = EditableFocusCellHighlighter.this.getFocusCell();
> ViewerRow row = focusCell.getViewerRow();
>
> Assert
> .isNotNull(row,
> "Internal structure invalid. Item without associated row is not possible."); //$NON-NLS-1$
>
> ViewerCell cell = row.getCell(event.index);
>
> if (focusCell == null || !cell.equals(focusCell)) {
> EditableFocusCellHighlighter.this.removeSelectionInformation (event, cell);
> } else {
> EditableFocusCellHighlighter.this.markFocusedCell(event, cell);
> }
> }
> }
>
> };
> viewer.getControl().addListener(SWT.EraseItem, listener);
> }
>
> /**
> * @param cell
> * the cell which is colored
> * @return the color
> */
> protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
> return null;
> }
>
> /**
> * @param cell
> * the cell which is colored
> * @return the color
> */
> protected Color getSelectedCellForegroundColor(ViewerCell cell) {
> return null;
> }
>
> /*
> * (non-Javadoc)
> *
> * @see org.eclipse.jface.viewers.FocusCellHighlighter#focusCellChan ged(org.eclipse.jface.viewers.ViewerCell)
> */
> @Override
> protected void focusCellChanged(ViewerCell cell) {
> super.focusCellChanged(cell);
>
> // Redraw new area
> if (cell != null) {
> final Rectangle rect = cell.getBounds();
> final int x = cell.getColumnIndex() == 0 ? 0 : rect.x;
> final int width = cell.getColumnIndex() == 0 ? rect.x + rect.width
> : rect.width;
> // 1 is a fix for Linux-GTK
> cell.getControl().redraw(x, rect.y - 1, width, rect.height + 1,
> true);
> }
>
> if (oldCell != null) {
> final Rectangle rect = oldCell.getBounds();
> final int x = oldCell.getColumnIndex() == 0 ? 0 : rect.x;
> final int width = oldCell.getColumnIndex() == 0 ? rect.x + rect.width
> : rect.width;
> // 1 is a fix for Linux-GTK
> oldCell.getControl().redraw(x, rect.y - 1, width,
> rect.height + 1, true);
> }
>
> oldCell = cell;
> }
> }
>
> class MyTableViewerFocusCellManager extends TableViewerFocusCellManager {
> private final TableViewer viewer;
> private Method setFocusCellMethod;
> private Method getViewerRowFromItemMethod;
>
> public MyTableViewerFocusCellManager(TableViewer viewer,
> FocusCellHighlighter focusDrawingDelegate) {
> super(viewer, focusDrawingDelegate);
> this.viewer = viewer;
> try {
> setFocusCellMethod = TableViewerFocusCellManager.class.getSuperclass().getDeclare dMethod( "setFocusCell", new Class[] {ViewerCell.class});
> setFocusCellMethod.setAccessible(true);
> getViewerRowFromItemMethod = TableViewer.class.getDeclaredMethod("getViewerRowFromItem", new Class[] {Widget.class});
> getViewerRowFromItemMethod.setAccessible(true);
> } catch (final SecurityException e) {
> e.printStackTrace();
> } catch (final NoSuchMethodException e) {
> e.printStackTrace();
> }
>
> }
>
> @Override
> public ViewerCell getFocusCell() {
> final ViewerCell cell = super.getFocusCell();
> final Table t = viewer.getTable();
>
> // It is possible that the selection has changed under the hood
> if (cell != null) {
> if (t.getSelection().length == 1
> && t.getSelection()[0] != cell.getItem()) {
> try {
> final ViewerRow row = (ViewerRow) getViewerRowFromItemMethod.invoke(viewer, new Object[] {t.getSelection()[0]});
> final ViewerCell newCell = row.getCell(cell.getColumnIndex());
> setFocusCellMethod.invoke(this, new Object[] {newCell});
> } catch (final IllegalArgumentException e) {
> e.printStackTrace();
> } catch (final IllegalAccessException e) {
> e.printStackTrace();
> } catch (final InvocationTargetException e) {
> e.printStackTrace();
> }
>
> }
> }
>
> return super.getFocusCell();
> }
> }


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: [TableViewer] How to block FocusCellHighlighter and/or FocusCellManager [message #324064 is a reply to message #324057] Thu, 17 January 2008 05:04 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

Ok. I'm afraid there's no API for this is 3.3 because we somehow missed
the possibility to pass a CellNavigation-Strategy to the
Table/TreeFocusCellManagers.

So you need to use reflection.

Field navigationStrategy =
TableViewerFocusCellManager.class.getField(navigationStrateg y);

navigationStrategy.set(mgr,new CellNavigationStrategy() {
public ViewerCell findSelectedCell(ColumnViewer viewer,
ViewerCell currentSelectedCell, Event event) {
ViewerCell cell;

if( event.keyCode == SWT.ARROW_LEFT || event.keyCode ==
SWT.ARROW_RIGHT ) {
cell = currentSelectedCell;
while( ( cell = super.findSelectedCell(viewer,cell,event) )
!= null ) {
if( cell.getBounds().x != 0 ) { // Maybe you need to use
// another algorithm here
return cell;
}
}
} else {
cell = super.findSelectedCell(viewer,cell,event);
}

return cell;
}
});


This is untested code I is just a dump from my head.

There's already a bug filed for a custom Navigation-Strategy [1] which
I'm going to address hopefully for M5.

If you use the Tabbing support you have to set the columns in none-edit
else you'll tab into them [2].

I'd also like you to file a bug [3] asking for the possibility to have
something like a visibility flag on the ViewerColumn we could use to
identicate that a column is not visible.

You can reference the nebula-bug [2] in there showing for a use case
beside that don't forget to tell us about your use case :-)

Tom

[1]https://bugs.eclipse.org/bugs/show_bug.cgi?id=210752
[2]https://bugs.eclipse.org/bugs/show_bug.cgi?id=211466
[3] https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Platform &component=UI&short_desc=[Viewers]

--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: [TableViewer] How to block FocusCellHighlighter and/or FocusCellManager [message #324147 is a reply to message #324064] Thu, 17 January 2008 18:20 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: automatic.javalobby.org

Hi, Tom!

I will file a bug that you suggest, maybe I could post a tiny use case snippet. And I will follow your kind tip "dumped from your head" to try to fix my code.

One more time, thank you for your everywhere posts. I learned very very much from them.

Best regards,
celeraman+
Re: [TableViewer] How to block FocusCellHighlighter and/or FocusCellManager [message #324151 is a reply to message #324147] Thu, 17 January 2008 18:37 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

You are welcome :-) You might be interest that I started to fix the
whole thing in a greater extend today but haven't finished it completely
(too many things I need to take care of :-).

But I hope that there's not custom code needed in 3.4 to make your use
case possible.

Thanks for your kind words, I really appreciate them.

Tom

celeraman+ schrieb:
> Hi, Tom!
>
> I will file a bug that you suggest, maybe I could post a tiny use case snippet. And I will follow your kind tip "dumped from your head" to try to fix my code.
>
> One more time, thank you for your everywhere posts. I learned very very much from them.
>
> Best regards,
> celeraman+


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: [TableViewer] How to block FocusCellHighlighter and/or FocusCellManager [message #324157 is a reply to message #324151] Thu, 17 January 2008 20:53 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: automatic.javalobby.org

Just for information, the link to bugzilla is:

Bug: 77001
[Viewers] Visibility flag on the Viewer Column
https://bugs.eclipse.org/bugs/show_bug.cgi?id=77001

See you there,

celeraman+
Re: [TableViewer] How to block FocusCellHighlighter and/or FocusCellManager [message #324188 is a reply to message #324157] Fri, 18 January 2008 11:56 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: eclipse-news.rizzoweb.com

celeraman+ wrote:
> Just for information, the link to bugzilla is:
>
> Bug: 77001
> [Viewers] Visibility flag on the Viewer Column
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=77001

That appears to be the wrong link and bug number.
Re: [TableViewer] How to block FocusCellHighlighter and/or FocusCellManager [message #324190 is a reply to message #324188] Fri, 18 January 2008 12:28 Go to previous message
Eclipse UserFriend
https://bugs.eclipse.org/bugs/show_bug.cgi?id=215735

Tom

Eric Rizzo schrieb:
> celeraman+ wrote:
>> Just for information, the link to bugzilla is:
>>
>> Bug: 77001 [Viewers] Visibility flag on the Viewer Column
>> https://bugs.eclipse.org/bugs/show_bug.cgi?id=77001
>
> That appears to be the wrong link and bug number.
>


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Previous Topic:Dynamic popup menu only shows up when I add a static item first
Next Topic:Multi page editor
Goto Forum:
  


Current Time: Fri Jul 18 00:43:30 EDT 2025

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

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

Back to the top