Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Optional;
- import java.util.function.BiConsumer;
- import com.google.gwt.user.client.ui.*;
- import com.google.gwt.user.client.Event;
- import com.google.gwt.event.dom.client.*;
- @SuppressWarnings({"OptionalUsedAsFieldOrParameterType", "WeakerAccess", "unused"})
- public class OkCancelDialog<T extends Widget> extends DialogBox {
- protected final T widget;
- protected final BiConsumer<T, OkCancelDialog<T>> onClick;
- protected final Button btnOk;
- protected Optional<Focusable> onCloseFocusedWidget = Optional.empty();
- public OkCancelDialog(String title, T widget, BiConsumer<T, OkCancelDialog<T>> onClick) {
- this.widget = widget;
- this.onClick = onClick;
- final VerticalPanel mainPanel = new VerticalPanel();
- final Panel buttonsPanel = new HorizontalPanel();
- btnOk = new Button("OK", (ClickHandler) e -> onClick.accept(widget, this));
- mainPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
- mainPanel.add(new Anchor("×", "#") {{addClickHandler(e -> hide()); setStyleName("mg-CloseButton");}});
- mainPanel.add(widget);
- mainPanel.add(new HTML("<hr/>"));
- mainPanel.add(buttonsPanel);
- buttonsPanel.setStyleName("buttons-panel");
- buttonsPanel.add(btnOk);
- setText(title);
- setWidget(mainPanel);
- }
- public OkCancelDialog withOnCloseFocusable(Focusable widget) {
- onCloseFocusedWidget = Optional.of(widget);
- return this;
- }
- public OkCancelDialog withButtonText(String text) {
- btnOk.setText(text);
- return this;
- }
- @Override
- public void show() {
- super.show();
- if (widget instanceof Focusable)
- ((Focusable) widget).setFocus(true);
- }
- @Override
- public void center() {
- super.center();
- if (widget instanceof Focusable)
- ((Focusable) widget).setFocus(true);
- }
- @Override
- public void hide() {
- super.hide();
- onCloseFocusedWidget.ifPresent(f -> f.setFocus(true));
- }
- @Override
- protected void onPreviewNativeEvent(Event.NativePreviewEvent event) {
- super.onPreviewNativeEvent(event);
- if (event.getTypeInt() == Event.ONKEYDOWN) {
- final int code = event.getNativeEvent().getKeyCode();
- switch (code) {
- case KeyCodes.KEY_ENTER: btnOk.click(); break;
- case KeyCodes.KEY_ESCAPE: hide(); break;
- }
- }
- }
- }
- /*
- .mg-CloseButton, .mg-CloseButton:visited, .mg-CloseButton:hover {
- color: aliceblue;
- float: right;
- display: block;
- margin-top: -52px;
- font-size: 2.5rem;
- padding-right: .3rem;
- text-decoration: none;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement