package fan.tutorial.client.ui.win;
import java.util.Random;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Format;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.ProgressBar;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Timer;
public class MessageBoxExample
extends LayoutContainer {
@Override
protected void onRender(Element parent,
int index) {
super.onRender(parent, index);
ButtonBar buttonBar =
new ButtonBar();
buttonBar.setMinButtonWidth(100);
buttonBar.add(
new Button("Alert",
new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
MessageBox.alert("System Message", "Access Denied!",
new Listener<MessageBoxEvent>() {
public void handleEvent(MessageBoxEvent be) {
Info.display(" ", "Got it.");
}
});
}
}));
buttonBar.add(
new Button("Confirm",
new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
MessageBox.confirm("System Message", "Are you sure you want to do that?",
new Listener<MessageBoxEvent>() {
public void handleEvent(MessageBoxEvent be) {
//如果点击YES按钮
if(be.getButtonClicked().getItemId().equals(Dialog.YES)){
Info.display(" ", "You really do that!");
}
else{
Info.display(" ", "Thank god you didn't do that!");
}
}
});
}
}));
buttonBar.add(
new Button("Prompt",
new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
MessageBox.prompt("Prompt", "Please input something.",
new Listener<MessageBoxEvent>() {
public void handleEvent(MessageBoxEvent be) {
//OK按钮被点击
if(be.getButtonClicked().getItemId().equals(Dialog.OK)){
Info.display(" ", "You input '{0}'", be.getValue());
}
}
});
}
}));
buttonBar.add(
new Button("Multiline Prompt",
new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
MessageBox.prompt("Multiline Prompt", "Please input something.",
true,
new Listener<MessageBoxEvent>() {
public void handleEvent(MessageBoxEvent be) {
//OK按钮被点击
if(be.getButtonClicked().getItemId().equals(Dialog.OK)){
Info.display(" ", "You input '{0}'", Format.ellipse(be.getValue(), 10));
}
}
});
}
}));
buttonBar.add(
new Button("Yes / No / Cancel",
new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
MessageBox messageBox =
new MessageBox();
//设置图标
messageBox.setIcon(MessageBox.QUESTION);
//设置按钮样式
messageBox.setButtons(Dialog.YESNOCANCEL);
messageBox.setTitleHtml("System Message");
messageBox.setMessage("Are you sure you want to do that?");
messageBox.addCallback(
new Listener<MessageBoxEvent>() {
public void handleEvent(MessageBoxEvent be) {
if(be.getButtonClicked().getItemId().equals(Dialog.YES)){
Info.display(" ", "You really do that!");
}
else if(be.getButtonClicked().getItemId().equals(Dialog.NO)){
Info.display(" ", "Thank god you didn't do that!");
}
}
});
messageBox.show();
}
}));
buttonBar.add(
new Button("Progress",
new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
final MessageBox messageBox = MessageBox.progress("", "Loading items, please wait
", "0%");
final ProgressBar progressBar = messageBox.getProgressBar();
Timer timer =
new Timer() {
private double i = 0;
//不能是int类型, 否则进度条不会滚动
private static final int MAX = 100;
private Random random =
new Random();
public void run() {
progressBar.updateProgress(i / MAX, i + "%");
i += random.nextInt(5) + 5;
if(i > MAX + 5){
cancel();
messageBox.close();
Info.display(" ", "Items were loaded!");
}
}
};
timer.scheduleRepeating(300);
}
}));
buttonBar.add(
new Button("Wait",
new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
final MessageBox messageBox = MessageBox.wait("", "Saving data, please wait
", "Saving
");
Timer timer =
new Timer() {
private int i;
private static final int MAX = 100;
private Random random =
new Random();
public void run() {
i += random.nextInt(5) + 5;
if(i > MAX + 5){
cancel();
messageBox.close();
Info.display(" ", "Data is successfully saved.");
}
}
};
timer.scheduleRepeating(300);
}
}));
add(buttonBar);
}
}