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 java.util.ArrayList;
011: import java.util.List;
012:
013: import net.mygwt.ui.client.Events;
014: import net.mygwt.ui.client.MyDOM;
015: import net.mygwt.ui.client.event.BaseEvent;
016: import net.mygwt.ui.client.event.Listener;
017: import net.mygwt.ui.client.fx.FXStyle;
018: import net.mygwt.ui.client.util.Format;
019:
020: import com.google.gwt.user.client.Element;
021: import com.google.gwt.user.client.Timer;
022: import com.google.gwt.user.client.Window;
023: import com.google.gwt.user.client.ui.RootPanel;
024: import com.google.gwt.user.client.ui.Widget;
025:
026: /**
027: * Displays information in the bottom right region of the browser for a
028: * specified amount of time.
029: *
030: * <dt><b>CSS:</b></dt>
031: * <dd>.my-info (the info itself)</dd>
032: * <dd>.my-info .my-info-text (the info text)</dd>
033: * </dl>
034: *
035: * <p>
036: * Note: At this time, the widget size is fixed and should not be modified.
037: * </p>
038: */
039: public class Info extends Component {
040:
041: /**
042: * display specifies the time in millseconds to display a message. Default
043: * value is 2500.
044: */
045: public static int display = 2500;
046:
047: private static int width = 225;
048: private static int height = 75;
049:
050: private static List slots = new ArrayList();
051:
052: /**
053: * Displays a message.
054: *
055: * @param title the message title
056: * @param text the message
057: * @param value the value to be substitute
058: */
059: public static void show(String title, String text, String value) {
060: show(title, text, new String[] { value });
061: }
062:
063: /**
064: * Displays a message.
065: *
066: * @param title the message title
067: * @param text the message
068: * @param value1 the value to be substituted
069: * @param value2 the value to be substituted
070: */
071: public static void show(String title, String text, String value1,
072: String value2) {
073: show(title, text, new String[] { value1, value2 });
074: }
075:
076: /**
077: * Displays the given text.
078: *
079: * @param title the title text
080: * @param text the message
081: * @param values the values to be substituted
082: */
083: public static void show(String title, String text, String[] values) {
084: int avail = firstAvail();
085: if (text != null && values != null)
086: text = Format.substitute(text, values);
087:
088: Info message = new Info(avail, title, text);
089: slots.add(avail, message);
090: message.showInternal();
091: }
092:
093: private static int firstAvail() {
094: int size = slots.size();
095: for (int i = 0; i < size; i++) {
096: if (slots.get(i) == null) {
097: return i;
098: }
099: }
100: return size;
101: }
102:
103: private Element titleElem, textElem;
104: private int level;
105:
106: private Info(int level, String title, String text) {
107: this .level = level;
108: String html = "<div class={0}><div class={0}-title></div><div class={0}-text></div></div>";
109: html = Format.substitute(html, "my-info");
110: setElement(MyDOM.create(html));
111:
112: titleElem = MyDOM.findChild("my-info-title", getElement());
113: textElem = MyDOM.findChild("my-info-text", getElement());
114:
115: MyDOM.setInnerHTML(titleElem, title);
116: MyDOM.setInnerHTML(textElem, text);
117:
118: MyDOM.setStyleAttribute(getElement(), "position", "absolute");
119:
120: }
121:
122: private void showInternal() {
123: int cw = Window.getClientWidth();
124: int ch = Window.getClientHeight();
125:
126: int left = (cw - width - 10);
127: int top = ch - height - 10 - (level * (height + 10));
128:
129: MyDOM.setLeft(getElement(), left);
130: MyDOM.setTop(getElement(), top);
131: MyDOM.setVisibility(getElement(), false);
132:
133: setSize(width, height);
134:
135: RootPanel.get().add(this );
136:
137: FXStyle fx = new FXStyle(getElement());
138: fx.fadeIn();
139:
140: final Widget fWidget = this ;
141: Timer t = new Timer() {
142: public void run() {
143: FXStyle fx = new FXStyle(getElement());
144: fx.addListener(Events.EffectComplete, new Listener() {
145: public void handleEvent(BaseEvent be) {
146: slots.set(level, null);
147: RootPanel.get().remove(fWidget);
148: }
149: });
150: fx.fadeOut();
151: }
152: };
153: t.schedule(display);
154: }
155:
156: }
|