• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package autotest.afe;
2 
3 import autotest.common.JsonRpcCallback;
4 import autotest.common.JsonRpcProxy;
5 import autotest.common.StaticDataRepository;
6 import autotest.common.StaticDataRepository.FinishedCallback;
7 import autotest.common.Utils;
8 import autotest.common.ui.TabView;
9 import autotest.common.ui.ToolTip;
10 
11 import com.google.gwt.event.dom.client.ClickEvent;
12 import com.google.gwt.event.dom.client.ClickHandler;
13 import com.google.gwt.json.client.JSONBoolean;
14 import com.google.gwt.json.client.JSONObject;
15 import com.google.gwt.json.client.JSONString;
16 import com.google.gwt.json.client.JSONValue;
17 import com.google.gwt.user.client.ui.Button;
18 import com.google.gwt.user.client.ui.CheckBox;
19 import com.google.gwt.user.client.ui.FlexTable;
20 import com.google.gwt.user.client.ui.HTMLTable;
21 import com.google.gwt.user.client.ui.ListBox;
22 import com.google.gwt.user.client.ui.Panel;
23 import com.google.gwt.user.client.ui.VerticalPanel;
24 import com.google.gwt.user.client.ui.Widget;
25 
26 public class UserPreferencesView extends TabView implements ClickHandler {
27     private static final StaticDataRepository staticData = StaticDataRepository.getRepository();
28     private static final JsonRpcProxy proxy = JsonRpcProxy.getProxy();
29 
30     public static interface UserPreferencesListener {
onPreferencesChanged()31         public void onPreferencesChanged();
32     }
33 
34     private JSONObject user;
35     private UserPreferencesListener listener;
36 
37     private RadioChooser rebootBefore = new RadioChooser();
38     private RadioChooser rebootAfter = new RadioChooser();
39     private ListBox droneSet = new ListBox();
40     private CheckBox showExperimental = new CheckBox();
41     private Button saveButton = new Button("Save preferences");
42     private HTMLTable preferencesTable = new FlexTable();
43 
UserPreferencesView(UserPreferencesListener listener)44     public UserPreferencesView(UserPreferencesListener listener) {
45         this.listener = listener;
46     }
47 
48     @Override
getElementId()49     public String getElementId() {
50         return "user_preferences";
51     }
52 
53     @Override
initialize()54     public void initialize() {
55         super.initialize();
56 
57         RadioChooserDisplay rebootBeforeDisplay = new RadioChooserDisplay();
58         RadioChooserDisplay rebootAfterDisplay = new RadioChooserDisplay();
59         rebootBefore.bindDisplay(rebootBeforeDisplay);
60         rebootAfter.bindDisplay(rebootAfterDisplay);
61 
62         Panel container = new VerticalPanel();
63         AfeUtils.populateRadioChooser(rebootBefore, "reboot_before");
64         AfeUtils.populateRadioChooser(rebootAfter, "reboot_after");
65 
66         saveButton.addClickHandler(this);
67 
68         ToolTip rebootBeforeToolTip = new ToolTip(
69             "?",
70             "Reboots all assigned hosts before the job runs. " +
71             "Click If dirty to reboot the host only if it hasn’t been rebooted " +
72             "since it was added, locked, or after running the last job.");
73         ToolTip rebootAfterToolTip = new ToolTip(
74             "?",
75             "Reboots all assigned hosts after the job runs. Click If all tests passed " +
76             "to skip rebooting the host if any test in the job fails.");
77         ToolTip showExperimentalToolTip = new ToolTip(
78             "?",
79             "Make the Create Job page show tests that are " +
80             "marked as \"experimental\" in the control file");
81 
82         addOption("Default reboot before value", rebootBeforeDisplay, rebootBeforeToolTip);
83         addOption("Default reboot after value", rebootAfterDisplay, rebootAfterToolTip);
84         addOption("Show experimental tests", showExperimental, showExperimentalToolTip);
85         if (staticData.getData("drone_sets_enabled").isBoolean().booleanValue()) {
86             AfeUtils.populateListBox(droneSet, "drone_sets");
87             addOption("Drone set", droneSet);
88         }
89 
90         container.add(preferencesTable);
91         container.add(saveButton);
92         addWidget(container, "user_preferences_table");
93     }
94 
95     @Override
refresh()96     public void refresh() {
97         staticData.refresh(new FinishedCallback() {
98             public void onFinished() {
99                 user = staticData.getData("current_user").isObject();
100                 updateValues();
101                 if (listener != null) {
102                     listener.onPreferencesChanged();
103                 }
104             }
105         });
106     }
107 
updateValues()108     private void updateValues() {
109         rebootBefore.setSelectedChoice(getValue("reboot_before"));
110         rebootAfter.setSelectedChoice(getValue("reboot_after"));
111         AfeUtils.setSelectedItem(droneSet, getValue("drone_set"));
112 
113         showExperimental.setValue(user.get("show_experimental").isBoolean().booleanValue());
114     }
115 
getValue(String key)116     private String getValue(String key) {
117         return Utils.jsonToString(user.get(key));
118     }
119 
onClick(ClickEvent event)120     public void onClick(ClickEvent event) {
121         assert event.getSource() == saveButton;
122         saveValues();
123     }
124 
saveValues()125     private void saveValues() {
126         JSONObject values = new JSONObject();
127         values.put("id", user.get("id"));
128         values.put("reboot_before", new JSONString(rebootBefore.getSelectedChoice()));
129         values.put("reboot_after", new JSONString(rebootAfter.getSelectedChoice()));
130         if (staticData.getData("drone_sets_enabled").isBoolean().booleanValue()) {
131           values.put("drone_set", new JSONString(droneSet.getItemText(droneSet.getSelectedIndex())));
132         }
133         values.put("show_experimental", JSONBoolean.getInstance(showExperimental.getValue()));
134         proxy.rpcCall("modify_user", values, new JsonRpcCallback() {
135             @Override
136             public void onSuccess(JSONValue result) {
137                 refresh();
138             }
139         });
140     }
141 
addOption(String name, Widget widget)142     private void addOption(String name, Widget widget) {
143         int row = preferencesTable.getRowCount();
144         preferencesTable.setText(row, 0, name);
145         preferencesTable.setWidget(row, 1, widget);
146     }
147 
addOption(String name, Widget widget, Widget extraWidget)148     private void addOption(String name, Widget widget, Widget extraWidget) {
149         int row = preferencesTable.getRowCount();
150         preferencesTable.setText(row, 0, name);
151         preferencesTable.setWidget(row, 1, widget);
152         preferencesTable.setWidget(row, 2, extraWidget);
153     }
154 }
155