GWT – ListEditor and EditorSource renders an empty row

In my application, I was creating a widget that would display the list of automobiles that a user owns.
I used a ListEditor and EditorSource to accomplish this.

public class CarsTableView extends Composite implements Editor<CarDto> {
     @Inject Provider<CarView> cvProvider;
     public class CarEditorSource extends EditorSource<CarView> {
         //Each instance of CarView is a row in the table.
         @Override        
         public CarView create(int index) {
             CarView view = cvProvider.get();
             CarDto proxy = carsList.getList().get(index);
             view.setValues(car.getMakeAndModel()); //Updates widgets in CarView
             carsTable.setWidget(index, 0, view);
             return view;
         }
     }

     public CarsTableView() {
         initWidget(uiBinder.createAndBindUi(this));
         carsList= ListEditor.of(new CarEditorSource());
     }

     @UiField FlexTable carsTable;
     ListEditor<CarDto, CarView> carsList;
     //Other code omitted for brevity
 }

However, I found that this code does not work correctly if a user did not have any cars.

The problem is that when GWT is creating the Editors and EditorDelegates, it creates the first item at index zero.

The solution is to override the dispose method in the CarEditorSource class.  The ensures that the blank row is removed.

     public class CarEditorSource extends EditorSource<CarView> {
         //Each instance of CarView is a row in the table.
         @Override        
         public CarView create(int index) {
             CarView view = cvProvider.get();
             CarDto proxy = carsList.getList().get(index);
             view.setValues(car.getMakeAndModel()); //Updates widgets in CarView
             carsTable.setWidget(index, 0, view);
             return view;
         }

        @Override
        public void dispose(CarView subEditor) {
            carsTable.remove(subEditor);
        }
    }

And visually…

Advertisements

About stevewall123

I am a Lead Software Engineer in Minneapolis working for Thomson Reuters. I am currently working on projects using Java, JavaScript, Spring, Drools, Hazelcast, Liquibase and Tomcat. Previously, I used C#, GWT, Grails, Groovy, JMS and JBoss Drools Guvnor. In the past I have worked on projects using J2EE, Swing, Webwork, Hibernate, Spring, Spring-WS, JMS, JUnit and Ant.
This entry was posted in Editors, GWT, UiBinder and tagged , , . Bookmark the permalink.

3 Responses to GWT – ListEditor and EditorSource renders an empty row

  1. George Peter says:

    Hi Steve,

    I am really banging my head with GWT listEditor . is it possible to provide one full example of a list editor . Your car example looks very neat . i just need the full widget code + entry point class code so that i can debug it in my system.

    • stevewall123 says:

      George: Unfortunately I have since switched jobs and I no longer have access to that code. I am sure you have tried this already but take a look at the GWT documentation. ListEditors definitely take some time to get used to, but in the end, they are worth the effort.

  2. george peter says:

    Thanks steve. Yesterday i happen to look in to sample email editor and now iam getting a hang of list editor

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s