Showing posts with label gwt . dialogbox. Show all posts
Showing posts with label gwt . dialogbox. Show all posts

Close Button in the caption bar of a GWT DialogBox

3

The following code demonstrates how a 'close button' can be placed in the caption bar of a DialogBox . Note that the example uses a HTML object as the close button , it can be replaced by an Image to make it more eye-candy.

/** *PUBLIC SOFTWARE * *This source code has been placed in the public domain. You can use, modify, and distribute *the source code and executable programs based on the source code. * *However, note the following: * *DISCLAIMER OF WARRANTY * * This source code is provided "as is" and without warranties as to performance * or merchantability. The author and/or distributors of this source code may * have made statements about this source code. Any such statements do not constitute * warranties and shall not be relied on by the user in deciding whether to use * this source code.This source code is provided without any express or implied * warranties whatsoever. Because of the diversity of conditions and hardware * under which this source code may be used, no warranty of fitness for a * particular purpose is offered. The user is advised to test the source code * thoroughly before relying on it. The user must assume the entire risk of * using the source code. * */ import com.google.gwt.user.client.Element; import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Event.NativePreviewEvent; import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HorizontalPanel; /** * @author amal * @version 1.0 */ public class MyDialog extends DialogBox { HTML close = new HTML("[X]"); HTML title =new HTML(""); HorizontalPanel captionPanel = new HorizontalPanel(); public MyDialog(boolean autoHide, boolean modal) { super(autoHide, modal); Element td = getCellElement(0, 1); DOM.removeChild(td, (Element) td.getFirstChildElement()); DOM.appendChild(td, captionPanel.getElement()); captionPanel.setStyleName("Caption");//width-100% captionPanel.add(title); close.addStyleName("CloseButton");//float:right captionPanel.add(close); super.setGlassEnabled(true); super.setAnimationEnabled(true); } public MyDialog(boolean autoHide) { this(autoHide, true); } public MyDialog() { this(false); } @Override public String getHTML() { return this.title.getHTML(); } @Override public String getText() { return this.title.getText(); } @Override public void setHTML(String html) { this.title.setHTML(html); } @Override public void setText(String text) { this.title.setText(text); } @Override protected void onPreviewNativeEvent(NativePreviewEvent event) { NativeEvent nativeEvent = event.getNativeEvent(); if (!event.isCanceled() && (event.getTypeInt() == Event.ONCLICK) && isCloseEvent(nativeEvent)) { this.hide(); } super.onPreviewNativeEvent(event); } private boolean isCloseEvent(NativeEvent event) { return event.getEventTarget().equals(close.getElement());//compares equality of the underlying DOM elements } }

Read more »
Labels:

Closing a GWT DialogBox on 'Esc' keypress

0

To have a DialogBox do something on a keyboard event , its onPreviewNativeEvent should be overridden . A DialogBox can be made to hide itself on the 'Esc' key event by overriding it's OnPreviewNativeEvent method as follows

@Override protected void onPreviewNativeEvent(NativePreviewEvent event) { if (!event.isCanceled() && event.getTypeInt() == Event.ONKEYDOWN && event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ESCAPE) { this.hide(); } super.onPreviewNativeEvent(event); }

Read more »
Labels:

A custom confirm dialog using GWT

2

Ever grew frustrated by the lack of customizations available of the window.confirm() dialog ? Here's a customizable confirm dialog for gwt apps.


/** *PUBLIC SOFTWARE * *This source code has been placedin the public domain. You can use, modify, and distribute *the source code and executable programs based on the source code. * *However, note the following: * *DISCLAIMER OF WARRANTY * * This source code is provided "as is" and without warranties as to performance * or merchantability. The author and/or distributors of this source code may * have made statements about this source code. Any such statements do not constitute * warranties and shall not be relied on by the user in deciding whether to use * this source code.This source code is provided without any express or implied * warranties whatsoever. Because of the diversity of conditions and hardware * under which this source code may be used, no warranty of fitness for a * particular purpose is offered. The user is advised to test the source code * thoroughly before relying on it. The user must assume the entire risk of * using the source code. * */ import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.logical.shared.CloseHandler; import com.google.gwt.user.client.ui.*; /** * @author amal * @version 1.0 */ public final class MyGwtConfirmDialog extends DialogBox { private Button primaryBtn; private Button secondaryBtn; private VerticalPanel mainPanel; private HorizontalPanel btnPanel; private HTML messageLbl; private boolean primaryActionFired = false; private boolean secondaryActionFired = false; public boolean primaryActionFired() { return primaryActionFired; } public boolean secondaryActionFired() { return secondaryActionFired; } /** * * @param title * @param message * @param primaryActionText * @param secondaryActionText * @param closeHandler * * sample usage - retrieving the confirmation status * <pre> * CloseHandler<PopupPanel> closeHandler=new CloseHandler<PopupPanel>() * { * * public void onClose(CloseEvent<PopupPanel> event) * { * MyGwtConfirmDialog x=(MyGwtConfirmDialog)event.getSource(); * Window.alert("primary "+x.primaryActionFired()+" ; secondary "+x.secondaryActionFired()); * } * }; * MyGwtConfirmDialog dia=new MyGwtConfirmDialog(<title>,<message>,<okText>,<cancelText>,closeHandler); * </pre> */ public MyGwtConfirmDialog(String title, String message, String primaryActionText, String secondaryActionText, CloseHandler<PopupPanel> closeHandler) { super(false, true); super.addCloseHandler(closeHandler); super.setText(title); mainPanel = new VerticalPanel(); messageLbl = new HTML(message); btnPanel = new HorizontalPanel(); mainPanel.add(messageLbl); mainPanel.add(btnPanel); primaryBtn = new Button(primaryActionText, new ClickHandler() { public void onClick(ClickEvent event) { primaryActionFired = true; MyGwtConfirmDialog.this.hide(); } }); secondaryBtn = new Button(secondaryActionText, new ClickHandler() { public void onClick(ClickEvent event) { secondaryActionFired = true; MyGwtConfirmDialog.this.hide(); } }); btnPanel.add(primaryBtn); btnPanel.add(secondaryBtn); btnPanel.setCellHorizontalAlignment(primaryBtn, HorizontalPanel.ALIGN_RIGHT); btnPanel.setCellHorizontalAlignment(secondaryBtn, HorizontalPanel.ALIGN_LEFT); super.setWidget(mainPanel); mainPanel.setHeight("100px"); mainPanel.setWidth("300px"); btnPanel.setHeight("30px"); btnPanel.setVerticalAlignment(HorizontalPanel.ALIGN_BOTTOM); btnPanel.addStyleName("dia-btnPanel"); primaryBtn.addStyleName("dia-primaryBtn"); secondaryBtn.addStyleName("dia-secondaryBtn"); messageLbl.addStyleName("dia-message"); super.setGlassEnabled(true); super.setAnimationEnabled(true); } /** * * @param message * @param closeHandler * @see MyGwtConfirmDialog#MyGwtConfirmDialog(java.lang.String, java.lang.String, java.lang.String, java.lang.String, com.google.gwt.event.logical.shared.CloseHandler) */ public MyGwtConfirmDialog(String message, CloseHandler<PopupPanel> closeHandler) { this("Confirmation", message, "Ok", "Cancel", closeHandler); } /** * should be used for displaying the dialog . */ public void paint() { primaryActionFired = false; secondaryActionFired = false; super.center(); primaryBtn.setFocus(true); } /** * * @param width */ public void setMainPanelWidth(String width) { mainPanel.setWidth(width); } }

Read more »
Labels:
© Zone817. Powered by Blogger.