Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » SWT.SetData in virtual table not working when SWT.MeasureItem is added
SWT.SetData in virtual table not working when SWT.MeasureItem is added [message #479624] Tue, 11 August 2009 18:10 Go to next message
PM  is currently offline PM Friend
Messages: 14
Registered: July 2009
Junior Member
Hi,

I have a TableViewer created with VIRTUAL flag. To this table i have added
some listeners of the following type, SWT.SetData, SWT.MeasureItem,
SWT.EraseItem, SWT.PaintItem (custom draw to display multi line in
columns. Without these listeners i am not able to display multi lines in
columns).
SWT.SetData has a logic to get more data from backend with scroll. Our
goal is to not get all the data from backend at once since this could
choke our backend servers. So we make this iterative call as the user
scrolls down.
Without custom draw listeners the SWT.SetData works fine. It is able to
calculate how many rows are visible on the viewer. Somehow when the custom
draw listeners are added (especially SWT.MeasureItem) the behaviour of
SWT.SetData changes. It keeps making iterating calls to be backend and
gets all the data and then does the calculation of how many rows to
display on the viewer.
This behaviour is causing a big problem for us. Since some of the views
have large amount of data to display. When the TableViewer is created it
takes a long time for the view to display since it keeps making call to
get data from backend. Can anyone explain whats causing this behaviour.
Any co-relation between the SWT.SetData and SWT.MeasureItem listeners? I
have some code snippets below to explain the problem.

Regards
Priyanka

private void createTable ()
{
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 4;
gridData.verticalSpan=2;
gridData.heightHint=350;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = SWT.FILL;
gridData.verticalAlignment = SWT.FILL;

tabViewer = new TableViewer(parent, SWT.VIRTUAL | SWT.BORDER |
SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
tabViewer.getTable().setLayoutData(gridData);

int i=0;
for (Iterator<ColumnAttributes> itr = columns.iterator();
itr.hasNext();i++)
{
ColumnAttributes colObj = itr.next();
TableViewerColumn column = new TableViewerColumn(tabViewer,
colObj.getColumnStyle());
column.getColumn().setWidth(colObj.getColumnWidth());
column.getColumn().setText(colObj.getColumnName());
column.getColumn().setMoveable(colObj.getColumnMoveable());
..........
..........

} //end for loop

if (this.contentProvider != null) {
this.tabViewer.setContentProvider(contentProvider);
}
else
tabViewer.setContentProvider(new TopsContentProvider());

if (this.labelProvider != null)
tabViewer.setLabelProvider(labelProvider);
else
tabViewer.setLabelProvider(new TopsLabelProvider());
tabViewer.setUseHashlookup(true);

// call to get backend data
if (daoComponent != null) {
if (this.dataAdapter != null)
model = this.dataAdapter.getData(0);
else
model = daoComponent.getData(0);
}
else
model = new java.util.ArrayList<ITopsModelObject>();

tabViewer.setInput(model);
tabViewer.getTable().setLinesVisible(true);
tabViewer.getTable().setHeaderVisible(true);
tabViewer.getTable().getVerticalBar().setEnabled(true);
tabViewer.getTable().getHorizontalBar().setEnabled(true);
tabViewer.getControl().setEnabled(true);
tabViewer.refresh();

final Table table = tabViewer.getTable();

// Provide the callback handler--this handler
// is invoked when the table needs new rows
tabViewer.getTable().addListener(SWT.SetData, new Listener() {

public void handleEvent(Event event) {
int rowIndex = table.indexOf((TableItem)event.item);
TableItem row = table.getItem(rowIndex);
Object dataRow = model.get(rowIndex);
/* This logic is added to get more data from back end. In most
cases
* services return only finite subset of data.
* Model gets updated every time after making subsequent service
calls.
* DAO Component holds the max count a model can hold. If the max
count
* is reached or no more data, fetch data call is no longer made.
*/
if ( ((rowIndex+1) == (model.size())) && getMoreDataFlag()) {
int modelLengthBefore = model.size();
List<ITopsModelObject> newModel;
// call to get backend data
if (daoComponent != null) {
if (dataAdapter != null)
newModel = dataAdapter.getData(rowIndex+1);
else
newModel = daoComponent.getData(rowIndex + 1);
// ideally Lists should never be returned as null
if (newModel == null || (newModel.size() ==
modelLengthBefore) )
setMoreDataFlag(false);
else {
updateModel(newModel);
}
}
}
row.setData(dataRow);
}
});

table.addListener(SWT.MeasureItem, new Listener() {
public void handleEvent(Event event) {
TableItem item = (TableItem)event.item;
String text = item.getText(event.index);
// this if clause is added for non-text items like image. For
some reason
// item.getImage() is returning null for checkbox cell editors.
We dont
// want to repaint the images.
if (text != null && !text.equals("")) {
Point size = event.gc.textExtent(text);
event.width =
tabViewer.getTable().getColumn(event.index).getWidth();
int lines = size.x / (event.width + 1);
event.height = Math.max(event.height, size.y + lines);
}
}
});
table.addListener(SWT.EraseItem, new Listener() {
public void handleEvent(Event event) {
TableItem item = (TableItem)event.item;
String text = item.getText(event.index);
if (text != null && !text.equals("")) {
event.detail &= ~SWT.FOREGROUND;
}
}
});
table.addListener(SWT.PaintItem, new Listener() {
public void handleEvent(Event event) {
TableItem item = (TableItem)event.item;
String text = item.getText(event.index);
if (text != null && !text.equals("")) {
event.gc.drawText(text, event.x, event.y, true);
}
}
});

}

public void updateModel(List<ITopsModelObject> newModel)
{
setMoreDataFlag(true);
this.model = newModel;
this.tabViewer.setInput(model);
tabViewer.setItemCount(model.size());
this.tabViewer.refresh();
//packColumns();

}
Re: SWT.SetData in virtual table not working when SWT.MeasureItem is added [message #482160 is a reply to message #479624] Tue, 25 August 2009 14:47 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi, sorry for the late reply,

The use of SWT.VIRTUAL and the custom draw callbacks should not interfere
with each other. I did a quick swt-only test of this and it seems to work
fine (tried on win2000).

My guess is that the TableViewer is getting confused because you're hooking
some callbacks, such as the custom draw callbacks, on the underlying Table.
The viewer provides facilities for doing all of this at the jface level, and
I think it expects them to be used. See if the snippets below are helpful,
and if not then you may wish to re-ask this on the eclipse.platform.jface
newsgroup.

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.s nippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippet s/viewers/Snippet029VirtualTableViewer.java?view=markup
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.s nippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippet s/viewers/Snippet010OwnerDraw.java?revision=1.8&view=mar kup

HTH,
Grant


"PM " <priyanka.mitra@etrade.com> wrote in message
news:e310c097c4f4054884e409d951fee2e8$1@www.eclipse.org...
> Hi,
>
> I have a TableViewer created with VIRTUAL flag. To this table i have added
> some listeners of the following type, SWT.SetData, SWT.MeasureItem,
> SWT.EraseItem, SWT.PaintItem (custom draw to display multi line in
> columns. Without these listeners i am not able to display multi lines in
> columns).
> SWT.SetData has a logic to get more data from backend with scroll. Our
> goal is to not get all the data from backend at once since this could
> choke our backend servers. So we make this iterative call as the user
> scrolls down.
> Without custom draw listeners the SWT.SetData works fine. It is able to
> calculate how many rows are visible on the viewer. Somehow when the custom
> draw listeners are added (especially SWT.MeasureItem) the behaviour of
> SWT.SetData changes. It keeps making iterating calls to be backend and
> gets all the data and then does the calculation of how many rows to
> display on the viewer.
> This behaviour is causing a big problem for us. Since some of the views
> have large amount of data to display. When the TableViewer is created it
> takes a long time for the view to display since it keeps making call to
> get data from backend. Can anyone explain whats causing this behaviour.
> Any co-relation between the SWT.SetData and SWT.MeasureItem listeners? I
> have some code snippets below to explain the problem.
>
> Regards
> Priyanka
>
> private void createTable ()
> {
> GridData gridData = new GridData(GridData.FILL_BOTH);
> gridData.horizontalSpan = 4;
> gridData.verticalSpan=2;
> gridData.heightHint=350;
> gridData.grabExcessHorizontalSpace = true;
> gridData.grabExcessVerticalSpace = true;
> gridData.horizontalAlignment = SWT.FILL;
> gridData.verticalAlignment = SWT.FILL;
>
> tabViewer = new TableViewer(parent, SWT.VIRTUAL | SWT.BORDER |
> SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
> tabViewer.getTable().setLayoutData(gridData);
>
> int i=0;
> for (Iterator<ColumnAttributes> itr = columns.iterator();
> itr.hasNext();i++)
> {
> ColumnAttributes colObj = itr.next();
> TableViewerColumn column = new TableViewerColumn(tabViewer,
> colObj.getColumnStyle());
> column.getColumn().setWidth(colObj.getColumnWidth());
> column.getColumn().setText(colObj.getColumnName());
> column.getColumn().setMoveable(colObj.getColumnMoveable());
> ..........
> ..........
>
> } //end for loop
>
> if (this.contentProvider != null) {
> this.tabViewer.setContentProvider(contentProvider);
> }
> else
> tabViewer.setContentProvider(new TopsContentProvider());
>
> if (this.labelProvider != null)
> tabViewer.setLabelProvider(labelProvider);
> else
> tabViewer.setLabelProvider(new TopsLabelProvider());
> tabViewer.setUseHashlookup(true);
>
> // call to get backend data
> if (daoComponent != null) {
> if (this.dataAdapter != null)
> model = this.dataAdapter.getData(0);
> else
> model = daoComponent.getData(0);
> }
> else
> model = new java.util.ArrayList<ITopsModelObject>();
>
> tabViewer.setInput(model);
> tabViewer.getTable().setLinesVisible(true);
> tabViewer.getTable().setHeaderVisible(true);
> tabViewer.getTable().getVerticalBar().setEnabled(true);
> tabViewer.getTable().getHorizontalBar().setEnabled(true);
> tabViewer.getControl().setEnabled(true);
> tabViewer.refresh();
>
> final Table table = tabViewer.getTable();
>
> // Provide the callback handler--this handler
> // is invoked when the table needs new rows
> tabViewer.getTable().addListener(SWT.SetData, new Listener() {
>
> public void handleEvent(Event event) {
> int rowIndex = table.indexOf((TableItem)event.item);
> TableItem row = table.getItem(rowIndex);
> Object dataRow = model.get(rowIndex);
> /* This logic is added to get more data from back end. In most
> cases
> * services return only finite subset of data.
> * Model gets updated every time after making subsequent service
> calls.
> * DAO Component holds the max count a model can hold. If the max
> count
> * is reached or no more data, fetch data call is no longer made.
> */
> if ( ((rowIndex+1) == (model.size())) && getMoreDataFlag()) {
> int modelLengthBefore = model.size();
> List<ITopsModelObject> newModel;
> // call to get backend data
> if (daoComponent != null) {
> if (dataAdapter != null)
> newModel = dataAdapter.getData(rowIndex+1);
> else
> newModel = daoComponent.getData(rowIndex + 1);
> // ideally Lists should never be returned as null
> if (newModel == null || (newModel.size() ==
> modelLengthBefore) )
> setMoreDataFlag(false);
> else {
> updateModel(newModel);
> }
> }
> }
> row.setData(dataRow);
> }
> });
>
> table.addListener(SWT.MeasureItem, new Listener() {
> public void handleEvent(Event event) {
> TableItem item = (TableItem)event.item;
> String text = item.getText(event.index);
> // this if clause is added for non-text items like image. For
> some reason
> // item.getImage() is returning null for checkbox cell editors.
> We dont
> // want to repaint the images.
> if (text != null && !text.equals("")) {
> Point size = event.gc.textExtent(text);
> event.width =
> tabViewer.getTable().getColumn(event.index).getWidth();
> int lines = size.x / (event.width + 1);
> event.height = Math.max(event.height, size.y + lines);
> }
> }
> });
> table.addListener(SWT.EraseItem, new Listener() {
> public void handleEvent(Event event) {
> TableItem item = (TableItem)event.item;
> String text = item.getText(event.index);
> if (text != null && !text.equals("")) {
> event.detail &= ~SWT.FOREGROUND;
> }
> }
> });
> table.addListener(SWT.PaintItem, new Listener() {
> public void handleEvent(Event event) {
> TableItem item = (TableItem)event.item;
> String text = item.getText(event.index);
> if (text != null && !text.equals("")) {
> event.gc.drawText(text, event.x, event.y, true);
> }
> }
> });
>
> }
>
> public void updateModel(List<ITopsModelObject> newModel)
> {
> setMoreDataFlag(true);
> this.model = newModel;
> this.tabViewer.setInput(model);
> tabViewer.setItemCount(model.size());
> this.tabViewer.refresh();
> //packColumns();
>
> }
>
>
>
>
Re: SWT.SetData in virtual table not working when SWT.MeasureItem is added [message #482254 is a reply to message #482160] Tue, 25 August 2009 20:45 Go to previous message
PM  is currently offline PM Friend
Messages: 14
Registered: July 2009
Junior Member
Thanx Grant for your response. Strangely enough, i see this behaviour only
on Linux platform. On windows it just works fine. I recently noticed this
when i deployed my app on windows.
Previous Topic:adding a menubar to two a composite
Next Topic:Auto-sized TreeViewer columns
Goto Forum:
  


Current Time: Thu Dec 12 23:41:22 GMT 2019

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

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

Back to the top