1package autotest.tko;
2
3import autotest.common.Utils;
4import autotest.common.ui.MultiListSelectPresenter;
5import autotest.common.ui.ToggleControl;
6
7import com.google.gwt.event.dom.client.ClickEvent;
8import com.google.gwt.event.dom.client.ClickHandler;
9import com.google.gwt.json.client.JSONObject;
10import com.google.gwt.user.client.ui.HasText;
11
12import java.util.Arrays;
13import java.util.List;
14import java.util.Map;
15
16public class SpreadsheetHeaderSelect implements ClickHandler {
17    public static final String HISTORY_FIXED_VALUES = "_fixed_values";
18
19    public static class State {
20        private HeaderSelect.State baseState = new HeaderSelect.State();
21        private String fixedValues;
22    }
23
24    public interface Display {
25        public MultiListSelectPresenter.DoubleListDisplay getDoubleListDisplay();
26        public MultiListSelectPresenter.ToggleDisplay getToggleDisplay();
27        public HasText getFixedValuesInput();
28        public void setFixedValuesVisible(boolean visible);
29        public ToggleControl getFixedValuesToggle();
30    }
31
32    private Display display;
33    private final State savedState = new State();
34    private HeaderSelect headerSelect;
35
36    public SpreadsheetHeaderSelect(HeaderFieldCollection headerFields) {
37        headerSelect = new HeaderSelect(headerFields, savedState.baseState);
38    }
39
40    public void bindDisplay(Display display) {
41        this.display = display;
42
43        headerSelect.bindDisplay(display.getDoubleListDisplay());
44        headerSelect.multiListSelect.bindToggleDisplay(display.getToggleDisplay());
45        display.getFixedValuesToggle().addClickHandler(this);
46        display.setFixedValuesVisible(false);
47    }
48
49    protected void saveToState(State state) {
50        headerSelect.saveToState(state.baseState);
51        state.fixedValues = getFixedValuesText();
52    }
53
54    private String getFixedValuesText() {
55        if (!isFixedValuesActive()) {
56            return "";
57        }
58
59        return display.getFixedValuesInput().getText();
60    }
61
62    private List<String> getFixedValues() {
63        String valueText = savedState.fixedValues.trim();
64        if (valueText.equals("")) {
65            return null;
66        }
67        return Utils.splitListWithSpaces(valueText);
68    }
69
70    private boolean isFixedValuesActive() {
71        return !display.getToggleDisplay().getToggleMultipleLink().isActive()
72               && display.getFixedValuesToggle().isActive();
73    }
74
75    public void loadFromState(State state) {
76        headerSelect.loadFromState(state.baseState);
77        display.getFixedValuesInput().setText(state.fixedValues);
78        display.getFixedValuesToggle().setActive(!state.fixedValues.equals(""));
79    }
80
81    public void setSelectedItems(List<HeaderField> fields) {
82        headerSelect.setSelectedItems(fields);
83        savedState.fixedValues = "";
84    }
85
86    public void onClick(ClickEvent event) {
87        assert event.getSource() == display.getFixedValuesToggle();
88        display.setFixedValuesVisible(isFixedValuesActive());
89    }
90
91    public void addHistoryArguments(Map<String, String> arguments, String name) {
92        headerSelect.addHistoryArguments(arguments, name);
93        if (isFixedValuesActive()) {
94            arguments.put(name + HISTORY_FIXED_VALUES, display.getFixedValuesInput().getText());
95        }
96    }
97
98    public void handleHistoryArguments(Map<String, String> arguments, String name) {
99        headerSelect.handleHistoryArguments(arguments, name);
100        String fixedValuesText = arguments.get(name + HISTORY_FIXED_VALUES);
101        savedState.fixedValues = fixedValuesText;
102    }
103
104    public void addQueryParameters(JSONObject parameters) {
105        List<String> fixedValues = getFixedValues();
106        if (fixedValues != null) {
107            JSONObject fixedValuesObject =
108                Utils.setDefaultValue(parameters, "fixed_headers", new JSONObject()).isObject();
109            fixedValuesObject.put(getSelectedItems().get(0).getSqlName(),
110                                  Utils.stringsToJSON(fixedValues));
111        }
112    }
113
114    public List<HeaderField> getSelectedItems() {
115        return headerSelect.getSelectedItems();
116    }
117
118    public void refreshFields() {
119        headerSelect.refreshFields();
120    }
121
122    public void setSelectedItem(HeaderField field) {
123        setSelectedItems(Arrays.asList(new HeaderField[] {field}));
124    }
125
126    public State getStateFromView() {
127        State state = new State();
128        saveToState(state);
129        return state;
130    }
131
132    public void updateStateFromView() {
133        saveToState(savedState);
134    }
135
136    public void updateViewFromState() {
137        loadFromState(savedState);
138    }
139}
140