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.MyGWT;
012: import net.mygwt.ui.client.util.Point;
013: import net.mygwt.ui.client.util.Rectangle;
014: import net.mygwt.ui.client.util.Size;
015: import net.mygwt.ui.client.util.TextMetrics;
016:
017: import com.google.gwt.user.client.DOM;
018: import com.google.gwt.user.client.Element;
019: import com.google.gwt.user.client.ui.RootPanel;
020: import com.google.gwt.user.client.ui.Widget;
021:
022: /**
023: * Displays a loading message and adds a gray overlay.
024: */
025: public class LoadingPanel extends Widget {
026:
027: private static LoadingPanel instance;
028:
029: /**
030: * Returns the singleton instance.
031: *
032: * @return the panel
033: */
034: public static LoadingPanel get() {
035: if (instance == null) {
036: instance = new LoadingPanel();
037: }
038: return instance;
039: }
040:
041: private boolean enabled = true;
042: private Element panel;
043: private Widget container;
044: private Element root;
045:
046: private LoadingPanel() {
047: root = DOM.createDiv();
048: MyDOM.setStyleAttribute(root, "position", "absolute");
049: MyDOM.setStyleName(root, "my-mask");
050: DOM.appendChild(MyDOM.getBody(), root);
051:
052: panel = DOM.createSpan();
053: setElement(DOM.createDiv());
054: DOM.appendChild(getElement(), panel);
055: setStyleName("my-loading-panel");
056: RootPanel.get().add(this );
057: hide();
058: }
059:
060: /**
061: * Hides the panel.
062: */
063: public void hide() {
064: MyDOM.setLeftTop(root, -1000, -1000);
065: MyDOM.setLeftTop(getElement(), -1000, -1000);
066: }
067:
068: /**
069: * Returns <code>true</code> if enabled, <code>false</code> otherwise.
070: *
071: * @return the enabled state
072: */
073: public boolean isEnabled() {
074: return enabled;
075: }
076:
077: /**
078: * Enables and disables the loading panel.
079: *
080: * @param enable <code>true</code> to enable
081: */
082: public void setEnabled(boolean enable) {
083: this .enabled = enable;
084: }
085:
086: /**
087: * Displays the panel filling the viewport.
088: */
089: public void show() {
090: show(RootPanel.get(), null);
091: }
092:
093: /**
094: * Desplays the panel filling the viewport.
095: *
096: * @param text the loading text
097: */
098: public void show(String text) {
099: show(RootPanel.get(), text);
100: }
101:
102: /**
103: * Displays the panel filling the given container.
104: *
105: * @param container the container
106: */
107: public void show(Widget container) {
108: show(container, null);
109: }
110:
111: /**
112: * Displays the panel filling the given container.
113: *
114: * @param container the container
115: * @param text the loading text
116: */
117: public void show(Widget container, String text) {
118: if (!enabled) {
119: return;
120: }
121:
122: this .container = container;
123:
124: String txt = text == null ? MyGWT.MESSAGES.loading() : text;
125: MyDOM.setInnerHTML(panel, txt);
126: DOM.setIntStyleAttribute(root, "zIndex", MyDOM.getZIndex());
127: DOM.setIntStyleAttribute(getElement(), "zIndex", MyDOM
128: .getZIndex());
129:
130: TextMetrics metrics = TextMetrics.get();
131: metrics.bind(panel);
132: int width = metrics.getWidth(txt);
133: setWidth(width + 26 + "px");
134: update();
135: }
136:
137: private void update() {
138: if (isAttached()) {
139: Rectangle rect = null;
140: if (container == RootPanel.get()) {
141: Size size = MyDOM.getViewportSize();
142: rect = new Rectangle(0, 0, size.width, size.height);
143: } else {
144: rect = MyDOM.getBounds(container.getElement());
145: }
146: MyDOM.setBounds(root, rect);
147:
148: int w = rect.x + (rect.width / 2) - (getOffsetWidth() / 2);
149: int h = rect.y + (rect.height / 2)
150: - (getOffsetHeight() / 2);
151:
152: if (w < 0 || h < 0) {
153: return;
154: }
155:
156: MyDOM.setXY(getElement(), new Point(w, h));
157: }
158: }
159:
160: }
|