Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » Calling refresh() on tableViewer clears the table data(Calling refresh() on tableViewer clears the table data but does not filter it)
Calling refresh() on tableViewer clears the table data [message #1303905] Sat, 19 April 2014 14:50 Go to next message
Pete DeLine is currently offline Pete DeLineFriend
Messages: 6
Registered: April 2014
Junior Member
I have a ViewerFilter on my TableViewer. When I set new filter data and call refresh on the TableViewer the table data is cleared and the select method of the ViewerFilter is not called. It works if I re-add the data back to the table viewer. I'm sure there is something very basic that I am missing. I've tried the following snippet in OSx and Windows.

Any advice is appreciated.
package org.eclipse.swt.snippets;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

public class SimpleTableExample {

  private static int count = 20;

  MyObject[] data = null;
  CheckboxTableViewer tableViewer;
  FileFilter          fileFilter;
  Text                searchText;

  public static void main( String[] args ) {
    new SimpleTableExample();
  }

  public SimpleTableExample() {
    Display display = new Display();
    Shell shell = new Shell( display );
    shell.setLayout( new GridLayout() );

    searchText = new Text( shell, SWT.SEARCH | SWT.ICON_CANCEL | SWT.ICON_SEARCH );
    GridData stData = new GridData( SWT.LEFT, SWT.CENTER, true, false );
    stData.widthHint = 300;
    searchText.setLayoutData( stData );
    searchText.setMessage( "Filter Results" );
    searchText.addKeyListener( new SearchKeyListener() );

    Table table = new Table( shell, SWT.BORDER | SWT.MULTI | SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL );
    TableLayout tableLayout = new TableLayout();
    table.setLayout( tableLayout );

    tableViewer = new CheckboxTableViewer( table );
    GridData data = new GridData( GridData.FILL_BOTH );
    tableViewer.getTable().setLayoutData( data );
    tableViewer.getTable().setLinesVisible( true );
    tableViewer.getTable().setHeaderVisible( true );

    TableViewerColumn column = new TableViewerColumn( tableViewer, SWT.NONE );

    column.getColumn().setText( "Column 1" );
    column.getColumn().setMoveable( true );
    column.setLabelProvider( new MyLabelProvider() );
    tableLayout.addColumnData( new ColumnPixelData( 400, true ) );
    
    tableViewer.setContentProvider( ArrayContentProvider.getInstance() );
    fileFilter = new FileFilter();
    tableViewer.setFilters( new ViewerFilter[] { fileFilter } );
    
    table.pack();
    addData();

    shell.pack();
    shell.open();
    while( !shell.isDisposed() ) {
      if ( !display.readAndDispatch() )
        display.sleep();
    }
    display.dispose();
  }

  private void addData() {
    if (data == null) {
      data = new MyObject[count];
      for( int i = 0; i < count; i++ ) {
        int r = 1 + (int) (Math.random() * ((50 - 1) + 1));
        MyObject file = new MyObject( "File " + i, r );
        data[i] = file;
      }
    }
    for( int i = 0; i < data.length; i++ ) {
      tableViewer.add( data[i] );
    }
  }

  class MyObject {
    private String  name;
    private Integer age;

    public MyObject( String name, int age ) {
      this.name = name;
      this.age = age;
    }

    public String getName() {
      return name;
    }

    public Integer getAge() {
      return age;
    }
  }

  private class MyLabelProvider extends CellLabelProvider implements ITableLabelProvider {

    @Override
    public Image getColumnImage( Object element, int columnIndex ) {
      return null;
    }
    @Override
    public String getColumnText( Object element, int columnIndex ) {
      MyObject mo = (MyObject) element;
      if ( columnIndex == 0 ) {
        return mo.getName();
      }
      return mo.getAge().toString();
    }
    @Override
    public void update( ViewerCell cell ) {
      cell.setText( getColumnText( cell.getElement(), cell.getColumnIndex() ) );
    }
  }

  class SearchKeyListener implements KeyListener {

    @Override
    public void keyPressed( KeyEvent e ) {
    }

    @Override
    public void keyReleased( KeyEvent e ) {
      String text = searchText.getText();
      fileFilter.setSearchText( text );
    }
  }

  public class FileFilter extends ViewerFilter {

    private String searchString = ".*";

    public void setSearchText( String s ) {
      this.searchString = ".*" + s + ".*";
      tableViewer.refresh();
      // if I uncomment the following, everything works. 
//      addData();
    }

    @Override
    public boolean select( Viewer viewer, Object parentElement, Object element ) {
      if ( searchString == null || searchString.length() == 0 ) {
        return true;
      }
      MyObject file = (MyObject) element;
      if ( file.getName().matches( searchString ) ) {
        return true;
      }
      return false;
    }
  }
}
Re: Calling refresh() on tableViewer clears the table data [message #1397101 is a reply to message #1303905] Wed, 09 July 2014 07:45 Go to previous messageGo to next message
Hussein MHANNA is currently offline Hussein MHANNAFriend
Messages: 45
Registered: February 2014
Location: LAVAL
Member
Hi,

The problem in your code is that you have'nt set an input for your TableViewer but you have the rows bu using the method add of your TableViewer.

To fix the issue in your code you must modify the method addData as follows (instead of using TableViewer#addData use TableViewer#setInput) :

private void addData() {
if (data == null) {
data = new MyObject[count];
for( int i = 0; i < count; i++ ) {
int r = 1 + (int) (Math.random() * ((50 - 1) + 1));
MyObject file = new MyObject( "File " + i, r );
data[i] = file;
}
}
tableViewer.setInput( data );
}

Kind Regards,

Hussein


ALL4TEC
Re: Calling refresh() on tableViewer clears the table data [message #1397306 is a reply to message #1397101] Wed, 09 July 2014 14:25 Go to previous message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 788
Registered: January 2012
Senior Member

To add to what Hussein wrote, the javadocs for #add(Object) says:

	 * This method should be called (by the content provider) when a single
	 * element has been added to the model, in order to cause the viewer to
	 * accurately reflect the model. This method only affects the viewer, not
	 * the model. Note that there is another method for efficiently processing
	 * the simultaneous addition of multiple elements.


Brian.


Eclipse Platform committer. Ask me about Eclipse support, training, and consulting.
Previous Topic:Not scroll when related scrollbar disposed
Next Topic:Getting/Setting EReferences of a EMF model instance in a TableViewer with ComboBox
Goto Forum:
  


Current Time: Fri Nov 16 12:17:27 GMT 2018

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

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

Back to the top