1 package autotest.moblab.wizard; 2 3 import autotest.moblab.rpc.MoblabRpcCallbacks; 4 import autotest.moblab.rpc.VersionInfo; 5 import com.google.gwt.event.dom.client.ClickEvent; 6 import com.google.gwt.event.dom.client.ClickHandler; 7 import com.google.gwt.json.client.JSONValue; 8 import com.google.gwt.user.client.Window; 9 import com.google.gwt.user.client.ui.Button; 10 import com.google.gwt.user.client.ui.Composite; 11 import com.google.gwt.user.client.ui.FlexTable; 12 import com.google.gwt.user.client.ui.FlowPanel; 13 import com.google.gwt.user.client.ui.Label; 14 import com.google.gwt.user.client.ui.InlineLabel; 15 import com.google.gwt.user.client.ui.SimplePanel; 16 import com.google.gwt.user.client.ui.VerticalPanel; 17 import com.google.gwt.user.client.ui.Widget; 18 19 import autotest.common.JsonRpcCallback; 20 import autotest.common.ui.NotifyManager; 21 import autotest.moblab.rpc.MoblabRpcHelper; 22 23 import java.util.Collection; 24 25 /** 26 * The configuration wizard for Moblab. A wizard supports multiple modes - 27 * the view mode and the edit mode. Each mode has its own UI. A wizard has 28 * a list of cards. Each card is responsible for the configuration of one 29 * piece of information. 30 */ 31 public class ConfigWizard extends Composite { 32 // All the wizard cards. 33 private WizardCard[] cards; 34 35 // The wizard header widget 36 private Widget headerContainer; 37 38 // The wizard footer widget 39 private Widget footerContainer; 40 41 // The container panel for wizard content widget. 42 private SimplePanel contentContainer; 43 44 // The current mode of the wizard - edit vs view 45 private Mode mode; 46 47 // The view mode controller. 48 private ConfigViewMode viewMode; 49 50 // The edit mode controller. 51 private ConfigEditMode editMode; 52 53 // Constructor. ConfigWizard()54 public ConfigWizard() { 55 cards = new WizardCard[] {}; 56 57 VerticalPanel pnlWizard = new VerticalPanel(); 58 pnlWizard.setStyleName("wizard-layout"); 59 60 headerContainer = createWizardHeader(); 61 pnlWizard.add(headerContainer); 62 63 contentContainer = new SimplePanel(); 64 contentContainer.setStyleName("wizard-panel"); 65 pnlWizard.add(contentContainer); 66 67 footerContainer = createWizardFooter(); 68 pnlWizard.add(footerContainer); 69 70 initWidget(pnlWizard); 71 72 setStyleName("config-wizard"); 73 74 viewMode = new ConfigViewMode(this); 75 editMode = new ConfigEditMode(this); 76 77 // Default to view mode. 78 setMode(Mode.View); 79 } 80 81 /** 82 * Creates the wizard header widget. 83 */ createWizardHeader()84 protected Widget createWizardHeader() { 85 FlowPanel headerPanel = new FlowPanel(); 86 headerPanel.setStyleName("wizard-header"); 87 Button btnStartConfig = new Button("Configure"); 88 headerPanel.add(btnStartConfig); 89 btnStartConfig.addClickHandler(new ClickHandler() { 90 @Override 91 public void onClick(ClickEvent event) { 92 onStartEdit(); 93 } 94 }); 95 96 Button btnReboot = new Button("Reboot"); 97 headerPanel.add(btnReboot); 98 btnReboot.addClickHandler(new ClickHandler() { 99 @Override 100 public void onClick(ClickEvent event) { 101 if (Window.confirm("Are you sure you want to reboot the Moblab device?")) { 102 MoblabRpcHelper.rebootMoblab(new JsonRpcCallback() { 103 @Override 104 public void onSuccess(JSONValue result) { 105 NotifyManager.getInstance().showMessage("Reboot command has been issued."); 106 } 107 }); 108 } 109 } 110 }); 111 return headerPanel; 112 } 113 114 /** 115 * Creates the wizard header widget. 116 */ createWizardFooter()117 protected Widget createWizardFooter() { 118 final FlexTable layoutTable = new FlexTable(); 119 MoblabRpcHelper.fetchMoblabBuildVersionInfo(new MoblabRpcCallbacks.FetchVersionInfoCallback() { 120 @Override 121 public void onVersionInfoFetched(VersionInfo info) { 122 int row = 0; 123 layoutTable.setWidget(row, 0, new Label("Version")); 124 layoutTable.setWidget(row, 1, new Label(info.getVersion())); 125 row++; 126 127 layoutTable.setWidget(row, 0, new Label("Update")); 128 FlowPanel updatePanel = new FlowPanel(); 129 updatePanel.add(new InlineLabel(info.getUpdateString())); 130 Button btnUpdate = new Button(info.getUpdateAction()); 131 btnUpdate.addClickHandler(new ClickHandler() { 132 @Override 133 public void onClick(ClickEvent event) { 134 String windowText = "If an update is available, the device will be " 135 + "rebooted and all running jobs will be halted. " 136 + "This may take 5-10 minutes based on network speed. Proceed?"; 137 if (Window.confirm(windowText)) { 138 MoblabRpcHelper.updateMoblab(new JsonRpcCallback() { 139 @Override 140 public void onSuccess(JSONValue result) { 141 String messageText = "Device is rebooting"; 142 NotifyManager.getInstance().showMessage(messageText); 143 } 144 }); 145 } 146 } 147 }); 148 updatePanel.add(btnUpdate); 149 layoutTable.setWidget(row, 1, updatePanel); 150 row++; 151 152 layoutTable.setWidget(row, 0, new Label("Track")); 153 layoutTable.setWidget(row, 1, new Label(info.getReleaseTrack())); 154 row++; 155 layoutTable.setWidget(row, 0, new Label("Description")); 156 layoutTable.setWidget(row, 1, new Label(info.getReleaseDescription())); 157 row++; 158 layoutTable.setWidget(row, 0, new Label("Moblab Identification")); 159 layoutTable.setWidget(row, 1, new Label( 160 info.getMoblabIdentification())); 161 row++; 162 layoutTable.setWidget(row, 0, new Label("Moblab Serial Number")); 163 layoutTable.setWidget(row, 1, new Label(info.getMoblabSerialNumber())); 164 } 165 }); 166 return layoutTable; 167 } 168 getCards()169 public WizardCard[] getCards() { 170 return cards; 171 } 172 setCards(WizardCard[] cards)173 public void setCards(WizardCard[] cards) { 174 if (cards != null) { 175 this.cards = cards; 176 } else { 177 this.cards = new WizardCard[] {}; 178 } 179 for (WizardCard card : cards) { 180 card.setDataStatusListener(editMode); 181 } 182 updateModeUI(); 183 } 184 setCards(Collection<WizardCard> cards)185 public void setCards(Collection<WizardCard> cards) { 186 WizardCard[] cardArray = null; 187 if (cards != null) { 188 cardArray = (WizardCard[]) cards.toArray(); 189 } 190 setCards(cardArray); 191 updateModeUI(); 192 } 193 getMode()194 public Mode getMode() { 195 return mode; 196 } 197 onFinishEdit()198 public void onFinishEdit() { 199 setMode(Mode.View); 200 } 201 onCancelEdit()202 public void onCancelEdit() { 203 setMode(Mode.View); 204 } 205 onStartEdit()206 public void onStartEdit() { 207 setMode(Mode.Edit); 208 } 209 setMode(Mode mode)210 private void setMode(Mode mode) { 211 this.mode = mode; 212 headerContainer.setVisible(Mode.View == mode); 213 footerContainer.setVisible(Mode.View == mode); 214 updateModeUI(); 215 } 216 updateModeUI()217 private void updateModeUI() { 218 switch(this.mode) { 219 case Edit: 220 contentContainer.setWidget(editMode.display()); 221 viewMode.hide(); 222 break; 223 case View: 224 contentContainer.setWidget(viewMode.display()); 225 editMode.hide(); 226 break; 227 } 228 } 229 230 public enum Mode { 231 View, 232 Edit 233 } 234 235 public interface ConfigWizardMode { hide()236 public void hide(); display()237 public Widget display(); 238 } 239 } 240