package com.antonytrupe.client.ui;
import com.antonytrupe.client.Index;
import com.antonytrupe.client.PageServiceAsync;
import com.antonytrupe.client.place.DisplayPlace;
import com.antonytrupe.shared.Page;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.http.client.URL;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
public class EditViewImpl extends Composite implements EditView {
interface EditViewImplUiBinder extends UiBinder<Widget, EditViewImpl> {
}
private static EditViewImplUiBinder uiBinder = GWT
.create(EditViewImplUiBinder.class);
private Presenter listener;
private String pageName;
@UiField
ContentViewImpl content;
@UiField
TextArea editContentBox;
private PageServiceAsync pageService;
@UiField
Label messages;
@UiField
CheckBox update;
public EditViewImpl() {
initWidget(uiBinder.createAndBindUi(this));
}
private String getContent() {
return this.editContentBox.getText();
}
private Boolean getUpdate() {
return this.update.getValue();
}
@UiHandler("cancel")
void onCancel(ClickEvent e) {
this.listener.goTo(new DisplayPlace(URL
.encodeQueryString(this.pageName)));
}
@UiHandler("editContentBox")
void onContentChange(KeyUpEvent e) {
// update the preview
try {
this.content.setContent(SafeHtmlUtils
.fromTrustedString(this.editContentBox.getValue()));
this.messages.setText("");
} catch (IllegalArgumentException iae) {
this.messages.setText(iae.getMessage());
}
}
@UiHandler("save")
void onSave(ClickEvent e) {
// do the save
save();
this.listener.goTo(new DisplayPlace(URL
.encodeQueryString(this.pageName)));
}
@Override
public void save() {
// do the save
Page p = new Page(this.pageName, getContent(), getUpdate());
this.pageService.save(p, new AsyncCallback<Void>() {
@Override
public void onFailure(Throwable caught) {
}
@Override
public void onSuccess(Void result) {
}
});
}
@Override
public void setContent(SafeHtml content) {
this.content.setContent(content);
this.editContentBox.setValue(content.asString());
}
@Override
public void setPageName(String pageName) {
this.pageName = pageName;
Window.setTitle(Index.ANTONY_TRUPE + pageName + "(EDIT)");
}
@Override
public void setPageService(PageServiceAsync pageService) {
this.pageService = pageService;
}
@Override
public void setPresenter(Presenter listener) {
this.listener = listener;
}
}