package welcome.client.ui.store;
import java.util.Iterator;
import java.util.List;
import welcome.client.GreetingService;
import welcome.client.GreetingServiceAsync;
import welcome.client.ui.Content;
import welcome.client.ui.ContentContainer;
import welcome.client.ui.popup.Popup;
import welcome.shared.store.Article;
import com.google.gwt.user.client.Window;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
public class StoreForm2 extends Content {
/**
* Create a remote service proxy to talk to the server-side Greeting
* service.
*/
private final GreetingServiceAsync storeService = GWT.create(GreetingService.class);
private FlexTable articleTable = new FlexTable();
private VerticalPanel mainPanel = new VerticalPanel();
private CaptionPanel newArticleCptPanel;
private HorizontalPanel horizontalPanel;
private Label newNameLabel;
private TextBox newNameTB;
private Label newAmountLabel;
private IntegerBox newAmountTB;
private Button addBt;
private Button openInPopupBt;
public StoreForm2() {
this.storeService.getArticles(new AsyncCallback<List<Article>>() {
@Override
public void onFailure(Throwable caught) {
Window.alert(caught.getMessage());
}
@Override
public void onSuccess(List<Article> result) {
articleTable.setWidget(0, 0, new Label("Name"));
articleTable.setWidget(0, 1, new Label("Amount"));
articleTable.setWidget(0, 2, new Label("Ordering Amount"));
articleTable.setWidget(0, 3, new Label("Remove "));
// Label nameLabel;
Label amountLabel;
IntegerBox amountTB;
Button removeBT;
Button editBT;
Iterator<Article> it = result.iterator();
for (int i = 1; it.hasNext(); i++) {
final Article currentArticle = it.next();
final Label nameLabel = new Label(currentArticle.getName());
amountLabel = new Label(String.valueOf(currentArticle.getAmount()));
amountTB = new IntegerBox();
amountTB.setValue(0);
amountTB.setWidth("20px");
removeBT = new Button("Remove");
editBT=new Button("Edit");
editBT.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Popup pop = new Popup();
ArticleEditor articleEditor = new ArticleEditor(currentArticle.getId(), pop);
pop.setContent(articleEditor);
pop.center();
}
});
removeBT.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
removeArticle(currentArticle.getId());
}
});
articleTable.setWidget(i, 0, nameLabel);
articleTable.setWidget(i, 1, amountLabel);
articleTable.setWidget(i, 2, amountTB);
articleTable.setWidget(i, 3, removeBT);
articleTable.setWidget(i, 4, editBT);
articleTable.setCellSpacing(4);
}
}
});
mainPanel.add(articleTable);
initWidget(mainPanel);
mainPanel.setSize("558px", "126px");
this.newArticleCptPanel = new CaptionPanel("Add a new article");
mainPanel.add(this.newArticleCptPanel);
this.horizontalPanel = new HorizontalPanel();
this.newArticleCptPanel.setContentWidget(this.horizontalPanel);
this.horizontalPanel.setSize("506px", "55px");
this.newNameLabel = new Label("Article Name :");
this.horizontalPanel.add(this.newNameLabel);
this.newNameLabel.setWidth("90px");
this.newNameTB = new TextBox();
this.horizontalPanel.add(this.newNameTB);
this.newNameTB.setSize("109px", "18px");
this.newAmountLabel = new Label("Amount in stock :");
this.horizontalPanel.add(this.newAmountLabel);
this.newAmountTB = new IntegerBox();
this.horizontalPanel.add(this.newAmountTB);
this.newAmountTB.setSize("30px", "18px");
this.addBt = new Button("Add");
this.addBt.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if (newNameTB.getText() != null && newAmountTB.getValue() != null) {
Article article = new Article(newNameTB.getText(), newAmountTB.getValue());
storeService.addArticle(article, new AsyncCallback<Object>() {
@Override
public void onFailure(Throwable caught) {
Window.alert(caught.getLocalizedMessage());
}
@Override
public void onSuccess(Object result) {
ContentContainer.setContent(new StoreForm2(), "content");
}
});
} else
Window.alert("Cannot add article, please enter valids parameters");
}
});
this.horizontalPanel.add(this.addBt);
this.openInPopupBt = new Button("Open the store in a popup");
this.openInPopupBt.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Popup pop = new Popup("The Official Store !!");
// do not forget a store is a Content and a content is an
// extension of Widget.
pop.setContent(new StoreForm2());
pop.center();
}
});
mainPanel.add(this.openInPopupBt);
}
private void removeArticle(Long articleId) {
storeService.removeArticle(articleId, new AsyncCallback<Object>() {
@Override
public void onFailure(Throwable caught) {
Window.alert(caught.getLocalizedMessage());
}
@Override
public void onSuccess(Object result) {
// System.out.println(result + " articles removed");
ContentContainer.setContent(new StoreForm2(), "content");
}
});
}
}