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  |
Eclipse User |
|
|
|
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   |
Eclipse User |
|
|
|
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
------------------------------------------------------------ --------
|
|
| | | | | | |
Goto Forum:
Current Time: Fri Jul 18 00:43:30 EDT 2025
Powered by FUDForum. Page generated in 0.06412 seconds
|