Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Why does the StyledCellLabelProvider.update() method repeat the 1st column text?
Why does the StyledCellLabelProvider.update() method repeat the 1st column text? [message #483948] Thu, 03 September 2009 17:42 Go to next message
Eclipse UserFriend
Originally posted by: slocumj.vmware.com

/**
* This class demonstrates a problem with Eclipse Gtk and SWT on Linux. By
contrast, the same exact code works fine on Windows.
* The Linux platform is: CentOS 5, GNOME 2.16.0, Eclipse RCP/Plug-in
Developers (Eclipse 3.5), Eclipse Build ID: 20090619-0625
* The Windows platform is: Windows XP SP3, Eclipse RCP/Plug-in Developers
(Eclipse 3.5), Eclipse Build ID: 20090619-0625
* A simple 3-row, 3-column table is displayed in a window on the screen.
* Each cell in the table has its text set to "Row # Column #".
* On Windows, this looks like:
* Row 0 Column 0 | Row 0 Column 1 | Row 0 Column 2
* Row 1 Column 0 | Row 1 Column 1 | Row 1 Column 2
* Row 2 Column 0 | Row 2 Column 1 | Row 2 Column 2
* This is correct.
*
* However, on Linux, this looks like:
* Row 0 Column 0 | Row 0 Column 0 | Row 0 Column 0
* Row 1 Column 0 | Row 1 Column 0 | Row 1 Column 0
* Row 2 Column 0 | Row 2 Column 0 | Row 2 Column 0
* This is NOT correct. The text values in the first column are being
repeated in the 2nd and 3rd column.
* Why is this happening?
*
*/

import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class ExampleProblem {

private static Display DISPLAY = Display.getDefault();

public static void main(String[] args) {

Shell shell = new Shell(DISPLAY, SWT.CLOSE | SWT.RESIZE);
shell.setSize(400, 400);
shell.setLayout(new GridLayout(1, false));

ExampleProblem example = new ExampleProblem();
Control composite = example.createPartControl(shell);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
true, 1, 1));

shell.open();

while (!shell.isDisposed()) {
if (!DISPLAY.readAndDispatch()) {
DISPLAY.sleep();
}
}
DISPLAY.dispose();
}

public ExampleProblem() {
}

public Composite createPartControl(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);

TableColumnLayout tcLayout = new TableColumnLayout();

composite.setLayout(tcLayout);

final TableViewer tableViewer = new TableViewer(composite,
SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);

Table table = tableViewer.getTable();

TableColumn firstColumn = new TableColumn(table, SWT.LEFT);
firstColumn.setText("First Column");
tcLayout.setColumnData(firstColumn, new ColumnWeightData(1));

TableColumn secondColumn = new TableColumn(table, SWT.LEFT);
secondColumn.setText("Second Column");
tcLayout.setColumnData(secondColumn, new ColumnWeightData(1));

TableColumn thirdColumn = new TableColumn(table, SWT.LEFT);
secondColumn.setText("Third Column");
tcLayout.setColumnData(thirdColumn, new ColumnWeightData(1));

ExampleLabelProvider labelProvider = new ExampleLabelProvider();
FileSystemContentProvider contentProvider= new
FileSystemContentProvider();

tableViewer.setContentProvider(contentProvider);
tableViewer.setLabelProvider(labelProvider);

GridData data = new GridData(GridData.FILL, GridData.FILL, true,
true);
tableViewer.getControl().setLayoutData(data);
tableViewer.setInput(new Object());

return composite;
}

private static class ExampleLabelProvider extends
StyledCellLabelProvider {

private static final Image IMAGE1 = new Image(DISPLAY,
DISPLAY.getSystemImage(SWT.ICON_WARNING).getImageData().scal edTo(16, 16));
private static final Image IMAGE2 = new Image(DISPLAY,
DISPLAY.getSystemImage(SWT.ICON_ERROR).getImageData().scaled To(16, 16));

public ExampleLabelProvider() {
}

public void update(ViewerCell cell) {
// On Linux, the second and third cells contain the text of
the first cell --> INCORRECT
// On Windows, the second and third cells contain the correct
text
// However, the image is NOT repeated on Linux or Windows
(correct behavior on both platforms)
cell.setText((String)cell.getElement() + " Column " +
cell.getColumnIndex());
if (cell.getColumnIndex() == 0) {
cell.setImage(IMAGE1);
} else if (cell.getColumnIndex() == 1) {
cell.setImage(IMAGE2);
} else {
cell.setImage(IMAGE2);
}
super.update(cell);
}
}

private static class FileSystemContentProvider implements
IStructuredContentProvider {

public Object[] getElements(Object element) {
Object[] objArr = new Object[3];
for (int i = 0; i < objArr.length; i++) {
objArr[i] = "Row " + i + ": ";
}
return objArr;
}

public void dispose() {
}

public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {
}
}
}
Re: Why does the StyledCellLabelProvider.update() method repeat the 1st column text? [message #484007 is a reply to message #483948] Thu, 03 September 2009 20:42 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi,

It looks like a bug, so I've logged
https://bugs.eclipse.org/bugs/show_bug.cgi?id=288565 . I believe it's a
custom-draw problem, so if you didn't care about using custom draw in your
app then you could probably work around it by using a conventional
ITableLabelProvider implementation instead (I realize that you probably do
care about using custom draw though). I don't know of another workaround at
the moment since it's jface that ultimately draws the text.

Grant


"Justin Slocum" <slocumj@vmware.com> wrote in message
news:62b6bb33fcd11d43cbc51b6b59cb164b$1@www.eclipse.org...
> /**
> * This class demonstrates a problem with Eclipse Gtk and SWT on Linux. By
> contrast, the same exact code works fine on Windows.
> * The Linux platform is: CentOS 5, GNOME 2.16.0, Eclipse RCP/Plug-in
> Developers (Eclipse 3.5), Eclipse Build ID: 20090619-0625
> * The Windows platform is: Windows XP SP3, Eclipse RCP/Plug-in Developers
> (Eclipse 3.5), Eclipse Build ID: 20090619-0625
> * A simple 3-row, 3-column table is displayed in a window on the screen.
> * Each cell in the table has its text set to "Row # Column #".
> * On Windows, this looks like:
> * Row 0 Column 0 | Row 0 Column 1 | Row 0 Column 2
> * Row 1 Column 0 | Row 1 Column 1 | Row 1 Column 2
> * Row 2 Column 0 | Row 2 Column 1 | Row 2 Column 2
> * This is correct.
> *
> * However, on Linux, this looks like:
> * Row 0 Column 0 | Row 0 Column 0 | Row 0 Column 0
> * Row 1 Column 0 | Row 1 Column 0 | Row 1 Column 0
> * Row 2 Column 0 | Row 2 Column 0 | Row 2 Column 0
> * This is NOT correct. The text values in the first column are being
> repeated in the 2nd and 3rd column.
> * Why is this happening?
> *
> */
>
> import org.eclipse.core.runtime.ISafeRunnable;
> import org.eclipse.jface.layout.TableColumnLayout;
> import org.eclipse.jface.viewers.ColumnWeightData;
> import org.eclipse.jface.viewers.IStructuredContentProvider;
> import org.eclipse.jface.viewers.StyledCellLabelProvider;
> import org.eclipse.jface.viewers.TableViewer;
> import org.eclipse.jface.viewers.Viewer;
> import org.eclipse.jface.viewers.ViewerCell;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.graphics.Image;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Control;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Table;
> import org.eclipse.swt.widgets.TableColumn;
>
> public class ExampleProblem {
>
> private static Display DISPLAY = Display.getDefault();
>
> public static void main(String[] args) {
>
> Shell shell = new Shell(DISPLAY, SWT.CLOSE | SWT.RESIZE);
> shell.setSize(400, 400);
> shell.setLayout(new GridLayout(1, false));
>
> ExampleProblem example = new ExampleProblem();
> Control composite = example.createPartControl(shell);
> composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
> true, 1, 1));
>
> shell.open();
>
> while (!shell.isDisposed()) {
> if (!DISPLAY.readAndDispatch()) {
> DISPLAY.sleep();
> }
> }
> DISPLAY.dispose();
> }
>
> public ExampleProblem() {
> }
>
> public Composite createPartControl(Composite parent) {
> Composite composite = new Composite(parent, SWT.NONE);
>
> TableColumnLayout tcLayout = new TableColumnLayout();
>
> composite.setLayout(tcLayout);
>
> final TableViewer tableViewer = new TableViewer(composite,
> SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
>
> Table table = tableViewer.getTable();
>
> TableColumn firstColumn = new TableColumn(table, SWT.LEFT);
> firstColumn.setText("First Column");
> tcLayout.setColumnData(firstColumn, new ColumnWeightData(1));
>
> TableColumn secondColumn = new TableColumn(table, SWT.LEFT);
> secondColumn.setText("Second Column");
> tcLayout.setColumnData(secondColumn, new ColumnWeightData(1));
>
> TableColumn thirdColumn = new TableColumn(table, SWT.LEFT);
> secondColumn.setText("Third Column");
> tcLayout.setColumnData(thirdColumn, new ColumnWeightData(1));
>
> ExampleLabelProvider labelProvider = new ExampleLabelProvider();
> FileSystemContentProvider contentProvider= new
> FileSystemContentProvider();
>
> tableViewer.setContentProvider(contentProvider);
> tableViewer.setLabelProvider(labelProvider);
>
> GridData data = new GridData(GridData.FILL, GridData.FILL, true,
> true);
> tableViewer.getControl().setLayoutData(data);
> tableViewer.setInput(new Object());
>
> return composite;
> }
>
> private static class ExampleLabelProvider extends
> StyledCellLabelProvider {
>
> private static final Image IMAGE1 = new Image(DISPLAY,
> DISPLAY.getSystemImage(SWT.ICON_WARNING).getImageData().scal edTo(16, 16));
> private static final Image IMAGE2 = new Image(DISPLAY,
> DISPLAY.getSystemImage(SWT.ICON_ERROR).getImageData().scaled To(16, 16));
>
> public ExampleLabelProvider() {
> }
>
> public void update(ViewerCell cell) {
> // On Linux, the second and third cells contain the text of
> the first cell --> INCORRECT
> // On Windows, the second and third cells contain the correct
> text
> // However, the image is NOT repeated on Linux or Windows
> (correct behavior on both platforms)
> cell.setText((String)cell.getElement() + " Column " +
> cell.getColumnIndex());
> if (cell.getColumnIndex() == 0) {
> cell.setImage(IMAGE1);
> } else if (cell.getColumnIndex() == 1) {
> cell.setImage(IMAGE2);
> } else {
> cell.setImage(IMAGE2);
> }
> super.update(cell);
> }
> }
>
> private static class FileSystemContentProvider implements
> IStructuredContentProvider {
>
> public Object[] getElements(Object element) {
> Object[] objArr = new Object[3];
> for (int i = 0; i < objArr.length; i++) {
> objArr[i] = "Row " + i + ": ";
> }
> return objArr;
> }
>
> public void dispose() {
> }
>
> public void inputChanged(Viewer viewer, Object oldInput, Object
> newInput) {
> }
> }
> }
>
>
Re: Why does the StyledCellLabelProvider.update() method repeat the 1st column text? [message #484008 is a reply to message #484007] Thu, 03 September 2009 20:57 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: slocumj.vmware.com

Even more weirdness:
I tried this again on Ubuntu 9.04/GNOME 2.26.1, and now I get the same
problem as before, but I noticed that when I use the mouse to re-size the
window, the text labels in the last column (column 2) flicker back and
forth between the correct value: "row...column 2" and the wrong value
"row...column 0". When I let go of the left mouse button, the value
settles to "row ... column 0" --> the incorrect value. It's like there is
a last event that is repainting the cells with the incorrect value before
returning. The correct values are buried in there somewhere.

I need to highlight or color-code certain sections of text in each cell,
so I don't think I can just use ITableLabelProvider and getColumnText() /
getColumnImage(). That is why I'm using StyledCellLabelProvider with its
update() method. Is there another way for me to highlight or color
sections of text in the cell in a Table without using
StyledCellLabelProvider ?

Thanks very much for looking into this.
Re: Why does the StyledCellLabelProvider.update() method repeat the 1st column text? [message #484628 is a reply to message #484008] Tue, 08 September 2009 14:20 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
I think that switching from TableViewer to using a Table instead will make
this work for you. Though your current problem is likely rooted in Table, I
think you're seeing it as a result of how TableViewer reacts to custom draw
events, which is more complex than a typical case. If you were to use a
Table and just do the drawing in response to these events then I think it
would work. For more info on using Table/Tree custom draw at the swt level
see
http://www.eclipse.org/articles/article.php?file=Article-Cus tomDrawingTableAndTreeItems/index.html .

The only other way to do this, which I would not suggest, would be to embed
Controls in the cells that are capable of drawing your content (eg.-
StyledTexts). For an example of embedding Controls in Table/Tree cells see
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet149.java?view=co .

HTH,
Grant


"Justin Slocum" <slocumj@vmware.com> wrote in message
news:72d478195a5cff27cb2ae34c50cf4e7a$1@www.eclipse.org...
> Even more weirdness:
> I tried this again on Ubuntu 9.04/GNOME 2.26.1, and now I get the same
> problem as before, but I noticed that when I use the mouse to re-size the
> window, the text labels in the last column (column 2) flicker back and
> forth between the correct value: "row...column 2" and the wrong value
> "row...column 0". When I let go of the left mouse button, the value
> settles to "row ... column 0" --> the incorrect value. It's like there is
> a last event that is repainting the cells with the incorrect value before
> returning. The correct values are buried in there somewhere.
>
> I need to highlight or color-code certain sections of text in each cell,
> so I don't think I can just use ITableLabelProvider and getColumnText() /
> getColumnImage(). That is why I'm using StyledCellLabelProvider with its
> update() method. Is there another way for me to highlight or color
> sections of text in the cell in a Table without using
> StyledCellLabelProvider ?
>
> Thanks very much for looking into this.
>
Re: Why does the StyledCellLabelProvider.update() method repeat the 1st column text? [message #485523 is a reply to message #484628] Sat, 12 September 2009 14:05 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Grant Gayed schrieb:
> I think that switching from TableViewer to using a Table instead will make
> this work for you. Though your current problem is likely rooted in Table, I
> think you're seeing it as a result of how TableViewer reacts to custom draw
> events, which is more complex than a typical case. If you were to use a
> Table and just do the drawing in response to these events then I think it
> would work. For more info on using Table/Tree custom draw at the swt level
> see
> http://www.eclipse.org/articles/article.php?file=Article-Cus tomDrawingTableAndTreeItems/index.html .

Anything we (JFace) could do to help out here Grant?

Tom
Re: Why does the StyledCellLabelProvider.update() method repeat the 1st column text? [message #485902 is a reply to message #485523] Tue, 15 September 2009 13:55 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Comment #1 in the report indicates the probable cause, so I think swt just
needs to make the fix. I've added a comment in other bug
https://bugs.eclipse.org/bugs/show_bug.cgi?id=228695 which could be the root
cause of this.

Grant


"Tom Schindl" <tom.schindl@bestsolution.at> wrote in message
news:h8g9r4$vpc$2@build.eclipse.org...
>
> Grant Gayed schrieb:
> > I think that switching from TableViewer to using a Table instead will
make
> > this work for you. Though your current problem is likely rooted in
Table, I
> > think you're seeing it as a result of how TableViewer reacts to custom
draw
> > events, which is more complex than a typical case. If you were to use a
> > Table and just do the drawing in response to these events then I think
it
> > would work. For more info on using Table/Tree custom draw at the swt
level
> > see
> >
http://www.eclipse.org/articles/article.php?file=Article-Cus tomDrawingTableAndTreeItems/index.html .
>
> Anything we (JFace) could do to help out here Grant?
>
> Tom
Previous Topic:Frequent update of image
Next Topic:newbie: columns in combo selection?
Goto Forum:
  


Current Time: Thu Apr 18 11:45:28 GMT 2024

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

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

Back to the top