package org.gwtoolbox.sample.widget.client.popup;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.Button;
import org.gwtoolbox.ioc.core.client.annotation.Component;
import org.gwtoolbox.sample.widget.client.SamplePanel;
import org.gwtoolbox.widget.client.button.SimpleButton;
import org.gwtoolbox.widget.client.button.version2.*;
import org.gwtoolbox.widget.client.panel.layout.SplitLayout;
import org.gwtoolbox.widget.client.popup.Popup;
import org.gwtoolbox.widget.client.popup.dialog.Dialog;
import static org.gwtoolbox.widget.client.panel.LayoutUtils.addGap;
/**
* @author Uri Boness
*/
@Component
@PopupSample
public class DialogSample extends Composite implements SamplePanel {
private TextBox xBox;
private TextBox yBox;
private CheckBox closable;
private CheckBox resizable;
private CheckBox center;
private Popup.AnimationType animationType;
private HandlerRegistration hideHandlerRegistration;
private Dialog dialog;
public DialogSample() {
SimpleButton openButton = new SimpleButton("Open Dialog", new ClickHandler() {
public void onClick(ClickEvent event) {
if (dialog != null) {
dialog.hide();
dialog = null;
}
if (hideHandlerRegistration != null) {
hideHandlerRegistration.removeHandler();
}
HTML html = new HTML("Lorem ipsum ferri meliore principes mei an, pri in nonummy electram elaboraret, " + "regione mediocrem vel in. In nam tibique oportere temporibus, eum munere facilis repudiandae " + "an. In aliquid incorrupte quo, et habemus principes nam, his eu omnium iuvaret delicatissimi." + " Detracto urbanitas comprehensam sed an, mea prima referrentur id, et vim essent aperiam " + "adipiscing. Ut vis meis blandit constituam.");
org.gwtoolbox.widget.client.button.version2.Button closeButton = new org.gwtoolbox.widget.client.button.version2.Button("Close Dialog");
hideHandlerRegistration = closeButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
dialog.setHeight("200px");
}
});
SplitLayout main = new SplitLayout();
main.addWest(html, 100);
main.add(closeButton);
main.setSize("100%", "100%");
dialog = new Dialog(false, true);
dialog.setAnimationEnabled(animationType != null);
if (animationType != null) {
dialog.setAnimationType(animationType);
}
dialog.setWidget(main);
dialog.setCaption("This is a caption");
dialog.setClosable(closable.getValue());
dialog.setResizable(resizable.getValue());
dialog.setPixelSize(300, 250);
if (center.getValue()) {
dialog.center();
} else {
final int x = parseX();
final int y = parseY();
if (x < 0 || y < 0) {
return;
}
dialog.setPopupPositionAndShow(new Popup.PositionCallback() {
public void setPosition(int offsetWidth, int offsetHeight) {
GWT.log("offsetWidth: " + offsetWidth + ", offsetHeight: " + offsetHeight, null);
dialog.setPopupPosition(x, y);
}
});
}
// hideHandlerRegistration = closeButton.addClickHandler(new ClickHandler() {
// public void onClick(ClickEvent event) {
// dialog.hide();
// }
// });
}
});
HorizontalPanel buttonRow = new HorizontalPanel();
buttonRow.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
buttonRow.add(openButton);
addGap(buttonRow, "10px");
buttonRow.add(new Label("X:"));
addGap(buttonRow, "3px");
buttonRow.add(xBox = new TextBox());
addGap(buttonRow, "10px");
buttonRow.add(new Label("Y:"));
addGap(buttonRow, "3px");
buttonRow.add(yBox = new TextBox());
VerticalPanel main = new VerticalPanel();
main.add(buttonRow);
addGap(main, "10px");
HorizontalPanel checks = new HorizontalPanel();
main.add(checks);
addGap(main, "10px");
closable = new CheckBox("Closable");
checks.add(closable);
addGap(checks, "10px");
resizable = new CheckBox("Risizable");
checks.add(resizable);
addGap(checks, "10px");
center = new CheckBox("Center");
center.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
public void onValueChange(ValueChangeEvent<Boolean> event) {
xBox.setEnabled(!event.getValue());
yBox.setEnabled(!event.getValue());
}
});
checks.add(center);
addGap(checks, "10px");
HorizontalPanel animations = new HorizontalPanel();
main.add(animations);
addGap(main, "100%");
final RadioButton slideRight = new RadioButton("animation", "Slide (from left)");
slideRight.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
animationType = Popup.AnimationType.SLIDE_LEFT;
}
});
animations.add(slideRight);
addGap(animations, "10px");
final RadioButton slideLeft = new RadioButton("animation", "Slide (from right)");
slideLeft.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
animationType = Popup.AnimationType.SLIDE_RIGHT;
}
});
animations.add(slideLeft);
addGap(animations, "10px");
final RadioButton slideDown = new RadioButton("animation", "Roll (down)");
slideDown.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
animationType = Popup.AnimationType.ROLL_DOWN;
}
});
animations.add(slideDown);
addGap(animations, "10px");
final RadioButton slideUp = new RadioButton("animation", "Roll (up)");
slideUp.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
animationType = Popup.AnimationType.ROLL_UP;
}
});
animations.add(slideUp);
addGap(animations, "10px");
final RadioButton expandCorner = new RadioButton("animation", "Expand (corner)");
expandCorner.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
animationType = Popup.AnimationType.ONE_WAY_CORNER;
}
});
animations.add(expandCorner);
addGap(animations, "10px");
final RadioButton expandCenter = new RadioButton("animation", "Expand (center)");
expandCenter.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
animationType = Popup.AnimationType.CENTER;
}
});
animations.add(expandCenter);
initWidget(main);
}
private int parseX() {
try {
int x = Integer.parseInt(xBox.getValue());
DOM.setStyleAttribute(xBox.getElement(), "background", "none");
return x;
} catch (NumberFormatException nfe) {
DOM.setStyleAttribute(xBox.getElement(), "backgroundColor", "#ffcccc");
return -1;
}
}
private int parseY() {
try {
int y = Integer.parseInt(yBox.getValue());
DOM.setStyleAttribute(yBox.getElement(), "background", "none");
return y;
} catch (NumberFormatException nfe) {
DOM.setStyleAttribute(yBox.getElement(), "backgroundColor", "#ffcccc");
return -1;
}
}
public Widget getContentWidget() {
return this;
}
public String getName() {
return "Basic Dialogs";
}
public void reset() {
if (dialog != null) {
dialog.hide();
dialog = null;
}
closable.setValue(false);
resizable.setValue(false);
}
}