• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package autotest.tko;
2 
3 import autotest.common.JsonRpcCallback;
4 import autotest.common.JsonRpcProxy;
5 import autotest.common.StaticDataRepository;
6 import autotest.common.Utils;
7 import autotest.common.ui.ExtendedListBox;
8 
9 import com.google.gwt.event.dom.client.ChangeEvent;
10 import com.google.gwt.event.dom.client.ChangeHandler;
11 import com.google.gwt.json.client.JSONObject;
12 import com.google.gwt.json.client.JSONString;
13 import com.google.gwt.json.client.JSONValue;
14 import com.google.gwt.user.client.ui.Composite;
15 
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Set;
19 
20 public class PreconfigSelector extends Composite {
21     public static final String NO_PRECONFIG = "----------";
22 
23     public static interface PreconfigHandler {
handlePreconfig(Map<String, String> preconfigParameters)24         public void handlePreconfig(Map<String, String> preconfigParameters);
25     }
26 
27     private ExtendedListBox selector = new ExtendedListBox();
28     private JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
29     private String preconfigType;
30     private PreconfigHandler listener;
31 
PreconfigSelector(final String preconfigType, final PreconfigHandler listener)32     public PreconfigSelector(final String preconfigType, final PreconfigHandler listener) {
33         this.preconfigType = preconfigType;
34         this.listener = listener;
35 
36         initializePreconfigList(preconfigType);
37 
38         selector.addChangeHandler(new ChangeHandler() {
39             public void onChange(ChangeEvent event) {
40                 loadSelectedPreconfig();
41             }
42         });
43 
44         initWidget(selector);
45     }
46 
initializePreconfigList(final String preconfigType)47     private void initializePreconfigList(final String preconfigType) {
48         selector.addItem(NO_PRECONFIG);
49         StaticDataRepository staticData = StaticDataRepository.getRepository();
50         JSONObject preconfigs = staticData.getData("preconfigs").isObject();
51         Set<String> keys = preconfigs.get(preconfigType).isObject().keySet();
52         for (String key : keys) {
53             selector.addItem(key);
54         }
55     }
56 
loadSelectedPreconfig()57     private void loadSelectedPreconfig() {
58         String name = selector.getSelectedValue();
59         if (name.equals(NO_PRECONFIG)) {
60             return;
61         }
62         selector.setSelectedIndex(0);
63 
64         JSONObject params = new JSONObject();
65         params.put("name", new JSONString(name));
66         params.put("type", new JSONString(preconfigType));
67         rpcProxy.rpcCall("get_preconfig", params, new JsonRpcCallback() {
68             @Override
69             public void onSuccess(JSONValue result) {
70                 JSONObject config = result.isObject();
71                 Map<String, String> map = new HashMap<String, String>();
72                 for (String key : config.keySet()) {
73                     map.put(key, Utils.jsonToString(config.get(key)));
74                 }
75                 listener.handlePreconfig(map);
76             }
77         });
78     }
79 }
80