Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Table referesh problem
Table referesh problem [message #465407] Mon, 12 December 2005 19:20 Go to next message
Eclipse UserFriend
Originally posted by: wei.xie.lehman.com

I wrote a simple tableViewer which is need update at lease three times a
scond, and background for each row maybe changed every time, I got a
refresh problem, the table is not stable ( reflash every time when I
update the table), any help will very welcome.

BTW: this is what I did:
1) suing style SWT.DOUBLE_BUFFERED to initialize the table viewer.
2) using IColorProvider to return defferen color for method
getBackground() for the rows.

work on Window XP, eclipse 3.1.1

Thanks.
Re: Table referesh problem [message #465443 is a reply to message #465407] Tue, 13 December 2005 13:45 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
Do not create the table with style SWT.DOUBLE_BUFFERED. This style is only
valid for emulated widgets and should only be applied by the class
implementing the emulated widget.

Please give a code snippet showing how you are updating the table.

"W Xie" <wei.xie@lehman.com> wrote in message
news:02449832f75a43d8737bc97bab1316cf$1@www.eclipse.org...
>I wrote a simple tableViewer which is need update at lease three times a
>scond, and background for each row maybe changed every time, I got a
>refresh problem, the table is not stable ( reflash every time when I update
>the table), any help will very welcome.
>
> BTW: this is what I did:
> 1) suing style SWT.DOUBLE_BUFFERED to initialize the table viewer.
> 2) using IColorProvider to return defferen color for method
> getBackground() for the rows.
>
> work on Window XP, eclipse 3.1.1
>
> Thanks.
>
Re: Table referesh problem [message #465447 is a reply to message #465443] Tue, 13 December 2005 18:54 Go to previous message
Eclipse UserFriend
Originally posted by: wei.xie.lehman.com

Thanks you for your replay, the following is my code, basically it has two
tabls and only has one scrollbar, you should able to run it as a SWT
application.

BTW: what is emulated widget? is a interface?







import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
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 TableTest extends Composite {

private MyTableViewer leftTable;
private MyTableViewer rightTable;

private Color[] bgColors = new Color[3];

public TableTest(Composite parent) {
super(parent, SWT.BORDER |SWT.DOUBLE_BUFFERED);
initTableViewers();
}


private void initTableViewers() {
initBGColor();
setLayout(new FillLayout());
Color grey = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
BiTableScrolledComposite levelComp = new
BiTableScrolledComposite(this, SWT.V_SCROLL, grey);

leftTable = new MyTableViewer(levelComp, grey);
rightTable = new MyTableViewer(levelComp, grey);
levelComp.refresh();
}



private void initBGColor() {
// TODO Auto-generated method stub
bgColors[0] = Display.getCurrent().getSystemColor(SWT.COLOR_GREEN);
bgColors[1] = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
bgColors[2] = Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW);
}

private Color getBGColorForDepth(int depth){
return bgColors[depth%3];
}




public class MyTableViewer extends TableViewer{

private MarketDataTableContentProvider contentProvider = new
MarketDataTableContentProvider();
private BiTableScrolledComposite biTableScrolledComposite;
private Color tableBG;

public MyTableViewer(BiTableScrolledComposite parent, Color bg ) {
super(parent.getTableParentComposite(), SWT.SINGLE |
SWT.HIDE_SELECTION |SWT.FULL_SELECTION | SWT.DOUBLE_BUFFERED);
this.biTableScrolledComposite = parent;
this.tableBG = bg;
init();
}

private void init( ) {
setLabelProvider(new MarketDataTableLabelProvider());
setContentProvider(contentProvider);
setInput("updata");
final Table table = getTable();
table.setHeaderVisible(true);
table.setLinesVisible(false);
int totalCoumnWith = 0;
String[] columnNames = {"Prod", "Price", "Quantity"};
for(int i=0; i< columnNames.length; i++){
TableColumn column = new TableColumn(table, SWT.CENTER );
column.setText(columnNames[i]);
column.setWidth(55);
column.setResizable(false);
totalCoumnWith += column.getWidth();
}


if(tableBG != null){
table.setBackground(tableBG);
}

GridData data = new GridData(SWT.CENTER, SWT.TOP, false, true);
data.widthHint = totalCoumnWith - table.computeTrim(0, 0, 0,
SWT.DEFAULT).width;
table.setLayoutData(data);
}

public void updateTable(final List list){
if(!getTable().isDisposed()){
Display display = getDisplay();
if(display != null){
display.asyncExec(new Runnable() {
public void run() {
getTable().setRedraw(false);
MyTableViewer.this.setInput(list);
biTableScrolledComposite.refresh();
getTable().setRedraw(true);
}
});
}
}
}
}


private class MarketDataTableLabelProvider implements
ITableLabelProvider, IColorProvider{
Color bgColor = Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);

public Color getBackground(Object element) {
if(element instanceof RowData){
RowData rowData = (RowData)element;
int depth = rowData.depth;
bgColor = getBGColorForDepth(depth);
}
return bgColor;
}

public Color getForeground(Object element) {
return Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
}

public Image getColumnImage(Object element, int columnIndex) {
return null;
}


public String getColumnText(Object element, int columnIndex) {
RowData rowData = (RowData)element;
return rowData.getDisplay(columnIndex);
}


public void addListener(ILabelProviderListener listener) {
}

public void dispose() {
// TODO Auto-generated method stub
}

public boolean isLabelProperty(Object element, String property) {
// TODO Auto-generated method stub
return false;
}

public void removeListener(ILabelProviderListener listener) {
}
}


private class MarketDataTableContentProvider implements
IStructuredContentProvider{
private List rowDataList = new ArrayList();

public Object[] getElements(Object inputElement) {
return rowDataList.toArray(new RowData[rowDataList.size()]);
}
public void dispose() {
}

public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {
if(newInput instanceof List){
rowDataList = (List)newInput;
}
}

}



public void updateData(Object newData){
//temp for testing data...
if(newData instanceof String &&
((String)newData).equalsIgnoreCase("test")){//testing... fack data.
if(leftTable != null && rightTable != null){
System.out.println(" update..."+newData);
leftTable.updateTable(getBit(15));
rightTable.updateTable(getBit(20));
}
}

}



private List getBit(int total){
List rv = new ArrayList();

for(int i =0 ; i< total; i++){
int rows = (int) System.currentTimeMillis()%4;
for(int j=0; j<rows+1; j++){
rv.add(new RowData("Prod "+i+" - "+j, 10000* Math.random(),
Math.random(), i));
}
}
return rv;
}


public static void main (String [] args) {

final Display display = new Display ();
Shell shell = new Shell(display);
shell.setLayout (new FillLayout());

final TableTest fastTraderTables = new TableTest(shell);
fastTraderTables.updateData("Test");

TimerTask tt = new TimerTask(){
@Override
public void run() {
fastTraderTables.updateData("test");
}
};
Timer dispatcherTimer = new Timer(true);
dispatcherTimer.schedule(tt, new Date(), 500);

Point DEFAULT_SIZE = new Point(360,450);
shell.setSize(DEFAULT_SIZE.x, DEFAULT_SIZE.y);
//shell.pack ();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}




class RowData {
String prod;
double price;
double quan;
int depth;
public RowData(String prod, double price,double quan, int depth) {
super();
// TODO Auto-generated constructor stub
this.prod = prod;
this.price = price;
this.quan = quan;
this.depth = depth;
}

public String getDisplay(int col){
switch(col){
case 0: return prod;
case 1: return Double.toString(price);
case 2: return Double.toString(quan);
}
return "";
}

@Override
public boolean equals(Object obj) {
boolean rv = false;
if(obj instanceof RowData){
RowData other = (RowData)obj;
rv = prod.equals(other.prod) && price == other.price && quan ==
other.quan;
}
return rv;
}

@Override
public int hashCode() {
return prod.hashCode();
}



}
public void dispose () {

if(!this.isDisposed()){
super.dispose();
for(int i=0; i<bgColors.length; i++){
Color color = bgColors[i];
if(!color.isDisposed()){
color.dispose();
}
}
}
}
}

class BiTableScrolledComposite extends ScrolledComposite {
private Color scrollBgColor;
private Composite composite;

public BiTableScrolledComposite(Composite parent, int style, Color
bgColor) {
super(parent, style);
this.scrollBgColor = bgColor;
init();
}


private void init() {
composite = new Composite(this, SWT.NONE|SWT.DOUBLE_BUFFERED);
setContent(composite);

//composite.setBackground(Display.getCurrent().getSystemColo r(SWT.COLOR_BLACK));//for
testing
if(scrollBgColor != null){
setBackground(scrollBgColor);
composite.setBackground(scrollBgColor);//for testing
}
GridLayout layout = new GridLayout();
layout.marginWidth = layout.marginHeight = 0;
layout.horizontalSpacing = 1;
layout.numColumns =2;
composite.setLayout(layout);
composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}


/**
* this will be used as the parent composite for the tables,
* the layout of it is GridaLayout with two columns.
* @return
*/
public Composite getTableParentComposite(){
return composite;
}

/**
* after update the table, should call this function to refresh the GUI.
*/
public void refresh(){
composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
composite.layout();
}

}
Previous Topic:Questions about the capability of a table / tableviewer
Next Topic:Switched to CCombo from Text default button does not work when focus in CCombo
Goto Forum:
  


Current Time: Tue Dec 03 00:26:42 GMT 2024

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

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

Back to the top