NotifyManager.java revision fb0762ed9e6e20c987d0613ffdf4859d14bd720a
1package autotest.common.ui;
2
3import com.google.gwt.user.client.ui.Composite;
4import com.google.gwt.user.client.ui.DisclosurePanel;
5import com.google.gwt.user.client.ui.HorizontalPanel;
6import com.google.gwt.user.client.ui.Image;
7import com.google.gwt.user.client.ui.Label;
8import com.google.gwt.user.client.ui.PopupPanel;
9import com.google.gwt.user.client.ui.RootPanel;
10import com.google.gwt.user.client.ui.TextArea;
11import com.google.gwt.user.client.ui.Widget;
12
13/**
14 * A singleton class to manage popup notifications, including error messages and
15 * the "loading..." box.
16 */
17public class NotifyManager {
18    private static final String SPINNER_IMAGE = "mbligh_spinner.gif";
19
20    // singleton
21    public static final NotifyManager theInstance = new NotifyManager();
22
23    static class NotifyBox {
24        private PopupPanel panel;
25        private Label message = new Label();
26
27        public NotifyBox(boolean autoHide) {
28            message.setStyleName("notify");
29            panel = new PopupPanel(autoHide);
30            panel.getElement().getStyle().setProperty("vertical-align", "middle");
31        }
32
33        public void addStyle(String style) {
34            message.addStyleName(style);
35        }
36
37        public void hide() {
38            panel.hide();
39        }
40
41        public void show() {
42            panel.setPopupPosition(0, 0);
43            panel.show();
44        }
45
46        public void showMessage(String messageString) {
47            message.setText(messageString);
48            showWidget(message);
49        }
50
51        public void showWidget(Widget widget) {
52            panel.clear();
53            panel.add(widget);
54            show();
55        }
56    }
57
58    static class ErrorLog extends Composite {
59        protected DisclosurePanel disclosurePanel =
60            new DisclosurePanel("Error log");
61        protected TextArea errorTextArea = new TextArea();
62
63        public ErrorLog() {
64            errorTextArea.setCharacterWidth(120);
65            errorTextArea.setVisibleLines(30);
66            errorTextArea.setReadOnly(true);
67            disclosurePanel.add(errorTextArea);
68            initWidget(disclosurePanel);
69        }
70
71        public void logError(String error) {
72            String errorText = errorTextArea.getText();
73            if (!errorText.equals(""))
74                errorText += "\n------------------------------\n";
75            errorText += error;
76            errorTextArea.setText(errorText);
77        }
78    }
79
80    protected NotifyBox errorNotify = new NotifyBox(true);
81    protected NotifyBox messageNotify = new NotifyBox(true);
82    protected NotifyBox loadingNotify = new NotifyBox(false);
83    private HorizontalPanel loadingPanel = new HorizontalPanel();
84    protected ErrorLog errorLog = new ErrorLog();
85    private int loadingCount = 0;
86
87    private NotifyManager() {
88        errorNotify.addStyle("error");
89    }
90
91    /**
92     * Should be called a page loading time.
93     */
94    public void initialize() {
95        errorNotify.hide();
96        messageNotify.hide();
97
98        loadingPanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
99        loadingPanel.setSpacing(3);
100        loadingPanel.add(new Image(SPINNER_IMAGE));
101        loadingPanel.add(new Label("Loading..."));
102        loadingPanel.add(new Image(SPINNER_IMAGE));
103
104        RootPanel.get("error_log").add(errorLog);
105        errorLog.setVisible(false);
106    }
107
108    public static NotifyManager getInstance() {
109        return theInstance;
110    }
111
112    /**
113     * Show an error message.
114     */
115    public void showError(String error, String logMessage) {
116        String errorLogText = error;
117        if (logMessage != null)
118            errorLogText += "\n" + logMessage;
119        errorNotify.showMessage(error);
120        log(errorLogText);
121    }
122
123    public void showError(String error) {
124        showError(error, null);
125    }
126
127    /**
128     * Log a message to the error log without showing any popup.
129     */
130    public void log(String message) {
131        errorLog.logError(message);
132        errorLog.setVisible(true);
133    }
134
135    /**
136     * Show a notification message.
137     */
138    public void showMessage(String message) {
139        messageNotify.showMessage(message);
140    }
141
142    /**
143     * Set whether the loading box is displayed or not.
144     */
145    public void setLoading(boolean visible) {
146        if (visible) {
147            if (loadingCount == 0) {
148                loadingNotify.showWidget(loadingPanel);
149            }
150            loadingCount++;
151        } else {
152            loadingCount--;
153            if (loadingCount == 0) {
154                loadingNotify.hide();
155            }
156        }
157    }
158}
159