Home | History | Annotate | Download | only in wizard
      1 package autotest.moblab.wizard;
      2 
      3 import com.google.gwt.json.client.JSONObject;
      4 import com.google.gwt.user.client.ui.Label;
      5 import com.google.gwt.user.client.ui.SimplePanel;
      6 import com.google.gwt.user.client.ui.Widget;
      7 
      8 import autotest.moblab.rpc.OperationStatus;
      9 import autotest.moblab.wizard.ConfigWizard.Mode;
     10 
     11 import java.util.HashMap;
     12 
     13 /**
     14  * The base class for cards that can be used with {@code ConfigWizard}. A card supports different
     15  * modes - currently view mode and edit mode. Each mode has its own UI and title. A card is used to
     16  * view and configure a piece of information.
     17  */
     18 public abstract class WizardCard {
     19   private static final OperationStatus STATUS_OK = new OperationStatus(true);
     20   private String editTitle;
     21   private String viewTitle;
     22   private ConfigWizard.Mode currentMode;
     23   private SimplePanel pnlCard;
     24   private CardDataStatusListener listener;
     25 
     26   public WizardCard() {
     27     currentMode = ConfigWizard.Mode.View;
     28     pnlCard = new SimplePanel();
     29     pnlCard.setStyleName("wizard-card-panel");
     30   }
     31 
     32   /**
     33    * Resets the UI for re-display.
     34    */
     35   protected void resetUI() {}
     36 
     37   /**
     38    * Resets card data.
     39    */
     40   public void resetData() {}
     41 
     42   /**
     43    * Switches to a mode and update the UI.
     44    *
     45    * @param mode the mode to switch to.
     46    *
     47    * @return the root UI widget for the new mode.
     48    */
     49   public Widget switchToMode(ConfigWizard.Mode mode) {
     50     currentMode = mode;
     51     updateModeUI();
     52     return pnlCard;
     53   }
     54 
     55   /**
     56    * Updates the card UI based on the current mode.
     57    */
     58   protected void updateModeUI() {}
     59 
     60   public ConfigWizard.Mode getMode() {
     61     return currentMode;
     62   }
     63 
     64   /**
     65    * Returns if the card can go next. This is used to enable and disable the "next" button.
     66    */
     67   public boolean canGoNext() {
     68     return true;
     69   }
     70 
     71   protected void setCardContentWidget(Widget widget) {
     72     pnlCard.setWidget(widget);
     73   }
     74 
     75   // Asks the card to validate the data.
     76   public void validate(CardValidationCallback callback) {
     77     if (callback != null) {
     78       callback.onValidationStatus(STATUS_OK);
     79     }
     80     return;
     81   }
     82 
     83   /**
     84    * @return the editTitle
     85    */
     86   public String getEditTitle() {
     87     return editTitle;
     88   }
     89 
     90   /**
     91    * @param editTitle the editTitle to set
     92    */
     93   public void setEditTitle(String editTitle) {
     94     this.editTitle = editTitle;
     95   }
     96 
     97   public void setViewTitle(String viewTitle) {
     98     this.viewTitle = viewTitle;
     99   }
    100 
    101   public String getViewTitle() {
    102     return viewTitle;
    103   }
    104 
    105   public void setDataStatusListener(CardDataStatusListener listener) {
    106     this.listener = listener;
    107   }
    108 
    109   protected void fireDataStatusChanged() {
    110     if (listener != null) {
    111       listener.onDataStatusChange();
    112     }
    113   }
    114 
    115   /**
    116    * Callback interface to support asynchronous validation. Asynchronous is necessary for server
    117    * side validation.
    118    */
    119   public interface CardValidationCallback {
    120     public void onValidationStatus(OperationStatus status);
    121   }
    122 
    123   /**
    124    * Listener on card data status changed.
    125    */
    126   public interface CardDataStatusListener {
    127     public void onDataStatusChange();
    128   }
    129 
    130   /**
    131    * A dummy card for testing purpose.
    132    */
    133   public static class DummyCard extends WizardCard {
    134     public DummyCard() {
    135       setViewTitle("Dummy view");
    136       setEditTitle("Dummy Edit");
    137     }
    138 
    139     @Override
    140     protected void updateModeUI() {
    141       Mode mode = getMode();
    142       switch (mode) {
    143         case Edit:
    144           setCardContentWidget(new Label("Edit content"));
    145           break;
    146         default:
    147           setCardContentWidget(new Label("View content"));
    148       }
    149     }
    150   }
    151 
    152   /**
    153    * Collects the configuration data and fills it in a map.
    154    */
    155   public void collectConfigData(@SuppressWarnings("unused") HashMap<String, JSONObject> map) {}
    156 }
    157