1package autotest.tko;
2
3import autotest.common.Utils;
4import autotest.common.ui.MultiListSelectPresenter;
5import autotest.common.ui.MultiListSelectPresenter.DoubleListDisplay;
6import autotest.common.ui.MultiListSelectPresenter.GeneratorHandler;
7import autotest.common.ui.MultiListSelectPresenter.Item;
8
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.List;
12import java.util.Map;
13
14class HeaderSelect {
15    public static class State {
16        private List<HeaderField> selectedFields;
17
18        public List<HeaderField> getSelectedFields() {
19            return new ArrayList<HeaderField>(selectedFields);
20        }
21    }
22
23    private HeaderFieldCollection headerFields;
24    private final State savedState;
25
26    protected MultiListSelectPresenter multiListSelect = new MultiListSelectPresenter();
27
28    public HeaderSelect(HeaderFieldCollection headerFields, State state) {
29        this.headerFields = headerFields;
30        savedState = state;
31    }
32
33    public void bindDisplay(DoubleListDisplay display) {
34        multiListSelect.bindDisplay(display);
35        refreshFields();
36    }
37
38    public void refreshFields() {
39        List<Item> selection = multiListSelect.getSelectedItems();
40        multiListSelect.clearItems();
41        for (HeaderField field : headerFields) {
42            if (field.isUserSelectable()) {
43                multiListSelect.addItem(field.getItem());
44            }
45        }
46        multiListSelect.restoreSelectedItems(selection);
47    }
48
49    public void updateStateFromView() {
50        saveToState(savedState);
51    }
52
53    protected void saveToState(State state) {
54        state.selectedFields = getSelectedItemsFromView();
55    }
56
57    public State getStateFromView() {
58        State state = new State();
59        saveToState(state);
60        return state;
61    }
62
63    private List<HeaderField> getSelectedItemsFromView() {
64        List<HeaderField> selectedFields = new ArrayList<HeaderField>();
65        for (Item item : multiListSelect.getSelectedItems()) {
66            selectedFields.add(headerFields.getFieldBySqlName(item.value));
67        }
68        return selectedFields;
69    }
70
71    public List<HeaderField> getSelectedItems() {
72        return savedState.getSelectedFields();
73    }
74
75    public void updateViewFromState() {
76        loadFromState(savedState);
77    }
78
79    public void loadFromState(State state) {
80        setSelectedItemsInView(state.selectedFields);
81    }
82
83    private void setSelectedItemsInView(List<HeaderField> fields) {
84        List<String> fieldNames = new ArrayList<String>();
85        for (HeaderField field : fields) {
86            Item item = field.getItem();
87            if (item.isGeneratedItem) {
88                multiListSelect.addItem(item);
89            }
90            fieldNames.add(field.getName());
91        }
92        multiListSelect.setSelectedItemsByName(fieldNames);
93    }
94
95    public void setSelectedItems(List<HeaderField> fields) {
96        savedState.selectedFields = new ArrayList<HeaderField>(fields);
97    }
98
99    public void setSelectedItem(HeaderField field) {
100        setSelectedItems(Arrays.asList(new HeaderField[] {field}));
101    }
102
103    public void selectItemInView(HeaderField field) {
104        List<HeaderField> fields = getSelectedItemsFromView();
105        if (!fields.contains(field)) {
106            fields.add(field);
107            setSelectedItemsInView(fields);
108        }
109    }
110
111    public void deselectItemInView(HeaderField field) {
112        List<HeaderField> fields = getSelectedItemsFromView();
113        if (fields.remove(field)) {
114            setSelectedItemsInView(fields);
115        }
116    }
117
118    public void addHistoryArguments(Map<String, String> arguments, String name) {
119        List<String> fields = new ArrayList<String>();
120        for (HeaderField field : getSelectedItems()) {
121            fields.add(field.getSqlName());
122        }
123        String fieldList = Utils.joinStrings(",", fields);
124        arguments.put(name, fieldList);
125    }
126
127    public void handleHistoryArguments(Map<String, String> arguments, String name) {
128        String[] fields = arguments.get(name).split(",");
129        List<HeaderField> selectedFields = getHeaderFieldsFromValues(fields);
130        setSelectedItems(selectedFields);
131    }
132
133    private List<HeaderField> getHeaderFieldsFromValues(String[] fieldSqlNames) {
134        List<HeaderField> fields = new ArrayList<HeaderField>();
135        for (String sqlName : fieldSqlNames) {
136            fields.add(headerFields.getFieldBySqlName(sqlName));
137        }
138        return fields;
139    }
140
141    protected State getState() {
142        return savedState;
143    }
144
145    public void setGeneratorHandler(GeneratorHandler handler) {
146        multiListSelect.setGeneratorHandler(handler);
147    }
148}
149