MVP multiple buttons/fields

15 views
Skip to first unread message

Fendy Tjin

unread,
Jul 12, 2010, 1:12:49 PM7/12/10
to Google Web Toolkit
Hi, I would like to elegantly code my application. I am rather
confused as to how to code for multiple button/field.

such as: multiple delete buttons, or multiple update text fields

I have a working sample of it, however it is very messy and I would
like to know if there is any other way to do it.

Currently my code is as such:

SomePresenter.java

//.... some code
private void addStock(JsArray<Stock> stocks) {
FlexTable viewStockTable = new FlexTable();
viewStockTable.removeAllRows();
viewStockTable.setText(0, 0, "Symbol");
viewStockTable.setText(0, 1, "Name");
viewStockTable.setText(0, 2, "Price");
viewStockTable.setText(0, 3, "Change");
viewStockTable.setText(0, 4, "Remove");
int row = 1;

for (int i = 0; i < stocks.length(); i++) {
viewStockTable.setText(row, 0, stocks.get(i).getCode());
viewStockTable.setText(row, 1, stocks.get(i).getName());
viewStockTable.setText(row, 2, "$ " +
roundDouble(getPrice(stocks.get(i).getStockPrice(), 0), 2));
viewStockTable.setText(row, 3,
roundDouble(getPriceChange(stocks.get(i).getStockPrice(), 0), 2) + "
%");
Button removeButton = new Button("x");
final String code = stocks.get(i).getCode();

//--------------------Here is the problem------------------
removeButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
deleteStock(code);
}
});
//----------------------------------------------------------------
viewStockTable.setWidget(row, 4, removeButton);

row++;
}
display.addStock(viewStockTable);
}

SomeView.java

private DecoratorPanel leftPanel = new DecoratorPanel();
private VerticalPanel leftStack = new VerticalPanel();
private SimplePanel viewPanel = new SimplePanel();
private FlexTable viewStockTable = new FlexTable();

public HomeView() {
//... some code

// assemble left panel
leftStack.add(addPanel);
viewPanel.add(viewStockTable);
leftStack.add(viewPanel);
leftPanel.add(leftStack);

//... some code
}

//... some code

public void addStock(FlexTable table) {
viewPanel.clear();
viewPanel.add(table);
// -------------- Can't be done unless I put it in another container
like the panel -------
// viewStockPanel = table;
//
----------------------------------------------------------------------------------------------------------
}

My structure:
SomePresenter.java
- addStock method - retrieve data from rpc then is creates the table
to view and pass the table to SomeView.java

I also do not like all the buttons are created in the view, if so what
is the point of MVP while I can also create them in Model and
Presenter alone. Since I created all the buttons in the Presenter,
when I pass the table to View, It can't be done with (viewStockTable =
table).

I do not like this is structured, is there another way to do it?

Thank you.

Gal Dolber

unread,
Jul 12, 2010, 7:05:07 PM7/12/10
to google-we...@googlegroups.com
Start moving the code of addStock to the view. Try not to use widget code on the presenter.

Then create an interface and implement in the presenter:
class SomeViewHandlers {
    void deleteStock(Stock code);
}

You already have the deleteStock method... so just add "implements SomeViewHandlers" to it.

Then you need to give to the display that interface (generally in the constructor):
display.setViewHandlers(this);

Then in the presenter lease the method as:
private void addStock(JsArray<Stock> stocks) {
display.addStock(stocks);
}

And in the view copyPaste the code of the method and change this:
 removeButton.addClickHandler(new ClickHandler() {
       @Override
       public void onClick(ClickEvent event) {
         viewHandlers.deleteStock(code);
       }
     });


Best

2010/7/12 Fendy Tjin <fend...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
http://gwtupdates.blogspot.com/
Reply all
Reply to author
Forward
0 new messages