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