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