001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client.widget;
009:
010: import net.mygwt.ui.client.MyDOM;
011: import net.mygwt.ui.client.event.BaseEvent;
012: import net.mygwt.ui.client.event.EffectListenerAdapter;
013: import net.mygwt.ui.client.fx.FXStyle;
014:
015: import com.google.gwt.user.client.DOM;
016: import com.google.gwt.user.client.Element;
017: import com.google.gwt.user.client.Event;
018: import com.google.gwt.user.client.EventPreview;
019: import com.google.gwt.user.client.Window;
020: import com.google.gwt.user.client.ui.Composite;
021: import com.google.gwt.user.client.ui.RootPanel;
022: import com.google.gwt.user.client.ui.SimplePanel;
023: import com.google.gwt.user.client.ui.Widget;
024:
025: /**
026: * A panel that grays out the view port and displays a widget above it.
027: */
028: public class ModalPanel extends Composite implements EventPreview {
029:
030: private boolean blink = true;
031: private SimplePanel panel;
032: private Widget widget;
033: private boolean blinking;
034: private FramePanel framePanel;
035:
036: /**
037: * Creates a new model panel.
038: */
039: public ModalPanel() {
040: panel = new SimplePanel();
041: initWidget(panel);
042: panel.setStyleName("my-modal");
043: panel.setWidth("100%");
044: }
045:
046: /**
047: * Hides the panel.
048: */
049: public void hide() {
050: framePanel.onHide(getElement());
051: FramePanel.push(framePanel);
052: MyDOM.setZIndex(getElement(), -1);
053: DOM.removeEventPreview(this );
054: RootPanel.get().remove(this );
055: RootPanel.get().remove(widget);
056: }
057:
058: /**
059: * Returns <code>true</code> if the panel will blink when clicks occur
060: * outside the it's boundaries.
061: *
062: * @return the blink state
063: */
064: public boolean isBlinkEnabled() {
065: return blink;
066: }
067:
068: public boolean onEventPreview(Event event) {
069: Element target = DOM.eventGetTarget(event);
070: if (DOM.isOrHasChild(widget.getElement(), target)) {
071: return true;
072: }
073: switch (DOM.eventGetType(event)) {
074: case Event.ONCLICK: {
075: String tagName = DOM.getElementProperty(target, "tagName");
076: // IE hack to stop flashing on a resize
077: if (tagName.equals("BODY"))
078: return false;
079: if (blink && !blinking) {
080: blinking = true;
081: FXStyle fx = new FXStyle(widget.getElement());
082: fx.duration = 400;
083: if (widget instanceof Shell) {
084: final Shell shell = (Shell) widget;
085: fx.addEffectListener(new EffectListenerAdapter() {
086: public void effectComplete(BaseEvent be) {
087: if (shell.shadow != null) {
088: shell.shadow.setVisible(true);
089: }
090: blinking = false;
091: }
092:
093: public void effectStart(BaseEvent be) {
094: if (shell.shadow != null) {
095: shell.shadow.setVisible(false);
096: }
097: }
098: });
099: } else {
100: fx.addEffectListener(new EffectListenerAdapter() {
101: public void effectComplete(BaseEvent be) {
102: blinking = false;
103: }
104: });
105: }
106: fx.blink();
107: }
108: break;
109: }
110: }
111: return false;
112: }
113:
114: /**
115: * Sets the blink state. When <code>true</code> the modal widget will blink.
116: * Default value is <code>true</code>.
117: *
118: * @param blink <code>true</code> to enable blinking
119: */
120: public void setBlinkEnabled(boolean blink) {
121: this .blink = blink;
122: }
123:
124: /**
125: * Displays the panel.
126: */
127: public void show(Widget widget) {
128: this .widget = widget;
129: RootPanel.get().add(this );
130: RootPanel.get().add(widget);
131:
132: // body height may be less than viewport
133: int bh = MyDOM.getScrollHeight(MyDOM.getBody());
134: bh = Math.max(bh, Window.getClientHeight());
135: setHeight(bh + "px");
136:
137: framePanel = FramePanel.pop();
138: framePanel.onShow(getElement());
139:
140: framePanel.setZIndex(MyDOM.getZIndex());
141: MyDOM.setZIndex(panel.getElement(), MyDOM.getZIndex());
142: MyDOM.setZIndex(widget.getElement(), MyDOM.getZIndex());
143:
144: DOM.addEventPreview(this);
145: }
146:
147: }
|