Question

I'm getting acquainted with GWTP. I tried to output a table, that would contain the JSON values, taken with a help of Piriti mappers. It's not a real project's code, it's just an attempt to understand GWTP, so this may be not the most beautiful solution (in fact, it's not one for sure). Here are the two presenters that are involved in this procedure:

The FirstPresenter (that uses ProductListPresenter, that is a widget, I'm not sure that widget should be used here, but, according to this conversation, widget may do the trick):

public class FirstPresenter extends
        Presenter<FirstPresenter.MyView, FirstPresenter.MyProxy> {

    public static final Object SLOT_RATE = new Object();
    public static final Object SLOT_PRODUCT = new Object();
    private IndirectProvider<ProductListPresenter> productListFactory;

    public interface MyView extends View {
        public Panel getListProductPanel();
    }

    @Inject ProductListPresenter productListPresenter;

    @ProxyCodeSplit
    @NameToken(NameTokens.first)
    public interface MyProxy extends ProxyPlace<FirstPresenter> {
    }

    @Inject
    public FirstPresenter(final EventBus eventBus, final MyView view,
            final MyProxy proxy, Provider<ProductListPresenter> productListFactory) {
        super(eventBus, view, proxy);

        this.productListFactory = new StandardProvider<ProductListPresenter>(productListFactory);
    }

    @Override
    protected void revealInParent() {
    }

    @Override
    protected void onBind() {
        super.onBind();
    }

    @Inject
    PlaceManager placeManager;

    @Override
    protected void onReset() {
        super.onReset();
        setInSlot(SLOT_PRODUCT, null);
        for (int i = 0; i < 2; i++) { //TODO: change hardcoded value 

            productListFactory.get(new AsyncCallback<ProductListPresenter>() {

                @Override
                public void onSuccess(ProductListPresenter result) {
                        addToSlot(SLOT_PRODUCT, result);
                }

                @Override
                public void onFailure(Throwable caught) {

                }
            });

        }
    }

}

The ProductListPresenter:

public class ProductListPresenter extends
        PresenterWidget<ProductListPresenter.MyView> {

    @Inject ProductListPiritiJsonReader reader;

    public interface MyView extends View {
        public Label getNameLabel();
        public Label getCompanyLabel();
        public Label getSerialLabel();
        public Label getPricesLabel();
    }

    @Inject
    public ProductListPresenter(final EventBus eventBus, final MyView view) {
        super(eventBus, view);
    }

    @Override
    protected void onBind() {
        super.onBind();
    }

    @Override
    protected void onReset() {
        super.onReset();

        try {
            RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, "/jsongwtproject/products.json");
            rb.setCallback(new RequestCallback() {
                @Override
                public void onResponseReceived(Request request, Response response) {
                    ProductList productList = reader.read(response.getText());
                    for (Product product : productList.getProductList()) {
                        fetchDataFromServer();
                    }
                }
                @Override
                public void onError(Request request, Throwable exception) {
                    Window.alert("Error occurred" + exception.getMessage());
                }
            });
            rb.send();
        } 
        catch (RequestException e) {
            Window.alert("Error occurred" + e.getMessage());
        }
    }
    //Takes the JSON string and uses showProductListData(String jsonString)  method
    public void fetchDataFromServer() {
        try {
            RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, "/jsongwtpproject/products.json");
            rb.setCallback(new RequestCallback() {
                @Override
                public void onResponseReceived(Request request, Response response) {
                    showProductListData(response.getText());                    
                }
                @Override
                public void onError(Request request, Throwable exception) {
                    Window.alert("Error occurred" + exception.getMessage());
                }
            });
            rb.send();
        } 
        catch (RequestException e) {
            Window.alert("Error occurred" + e.getMessage());
        }
    }
    //Uses Piriti mappers to take JSON values
    private void showProductListData(String jsonString) {
        ProductList productList = reader.read(jsonString);
        for (Product product : productList.getProductList()) {
            StringBuffer priceSb = new StringBuffer();
            for (Double price : product.getPrices()) {
                priceSb.append(price + ", ");
            }

            getView().getNameLabel().setText(product.getName());
            getView().getCompanyLabel().setText(product.getCompany());
            getView().getSerialLabel().setText(product.getSerialNumber());
            getView().getPricesLabel().setText(priceSb.toString());
            //break;

        }
    }   

}

And the ProductListView.ui.xml:

<g:HTMLPanel>
<table border="1">
    <tr>
        <td><g:Label ui:field="nameLabel" /> </td>
        <td><g:Label ui:field="companyLabel" /> </td> 
        <td><g:Label ui:field="serialLabel" /> </td>
        <td><g:Label ui:field="pricesLabel" /> </td> 
    </tr>
    </table>
</g:HTMLPanel> 

Currrently there are two products in the JSON.

Here is what happens with this code: the first row with Product1 appears, then it changes to the first row that contains Product2's values, then again it contains Product1's values, then again Product2's, after that the second row with Product1 appears, then it changes to the second row that contains Product2's values, then again it contains Product1's values, then again Product2's.

So, there are two products and two rows, and in this code the values are changed twice, but in the end the table contains only Product2's values. If the break; is uncommented, Product1's values output twice in the first row, then in the second row, then the table contain only these Product1's values.

I do understand why that happens. But I haven't yet figured out how to make the correct output. It'd be great if someone could tell me how to do the correct output, or, well, provide an example (or would tell me which part, e.g. the widget usage, is terribly wrong).

Was it helpful?

Solution

The problem with your code is that you really don't have a real table in your ProductListView.ui.xml.

Of course if there were two records retrieved from the server, this part of the code is called twice:

getView().getNameLabel().setText(product.getName());
getView().getCompanyLabel().setText(product.getCompany());
getView().getSerialLabel().setText(product.getSerialNumber());
getView().getPricesLabel().setText(priceSb.toString());

the second call overwriting the value from the first call.

Points to improve your code:

  1. You may want to read about CellTable for creating a real table view.
  2. Do not use the PresenterWidget itself as data holder, instead create a DTO that will be pass to the database and use this to retrieve the data.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top