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