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 WizardCard()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 */ resetUI()35 protected void resetUI() {} 36 37 /** 38 * Resets card data. 39 */ resetData()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 */ switchToMode(ConfigWizard.Mode mode)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 */ updateModeUI()58 protected void updateModeUI() {} 59 getMode()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 */ canGoNext()67 public boolean canGoNext() { 68 return true; 69 } 70 setCardContentWidget(Widget widget)71 protected void setCardContentWidget(Widget widget) { 72 pnlCard.setWidget(widget); 73 } 74 75 // Asks the card to validate the data. validate(CardValidationCallback callback)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 */ getEditTitle()86 public String getEditTitle() { 87 return editTitle; 88 } 89 90 /** 91 * @param editTitle the editTitle to set 92 */ setEditTitle(String editTitle)93 public void setEditTitle(String editTitle) { 94 this.editTitle = editTitle; 95 } 96 setViewTitle(String viewTitle)97 public void setViewTitle(String viewTitle) { 98 this.viewTitle = viewTitle; 99 } 100 getViewTitle()101 public String getViewTitle() { 102 return viewTitle; 103 } 104 setDataStatusListener(CardDataStatusListener listener)105 public void setDataStatusListener(CardDataStatusListener listener) { 106 this.listener = listener; 107 } 108 fireDataStatusChanged()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 { onValidationStatus(OperationStatus status)120 public void onValidationStatus(OperationStatus status); 121 } 122 123 /** 124 * Listener on card data status changed. 125 */ 126 public interface CardDataStatusListener { onDataStatusChange()127 public void onDataStatusChange(); 128 } 129 130 /** 131 * A dummy card for testing purpose. 132 */ 133 public static class DummyCard extends WizardCard { DummyCard()134 public DummyCard() { 135 setViewTitle("Dummy view"); 136 setEditTitle("Dummy Edit"); 137 } 138 139 @Override updateModeUI()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 */ collectConfigData(@uppressWarnings"unused") HashMap<String, JSONObject> map)155 public void collectConfigData(@SuppressWarnings("unused") HashMap<String, JSONObject> map) {} 156 } 157