1 package autotest.tko; 2 3 import autotest.common.Utils; 4 import autotest.common.ui.MultiListSelectPresenter; 5 import autotest.common.ui.ToggleControl; 6 7 import com.google.gwt.event.dom.client.ClickEvent; 8 import com.google.gwt.event.dom.client.ClickHandler; 9 import com.google.gwt.json.client.JSONObject; 10 import com.google.gwt.user.client.ui.HasText; 11 12 import java.util.Arrays; 13 import java.util.List; 14 import java.util.Map; 15 16 public 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 { getDoubleListDisplay()25 public MultiListSelectPresenter.DoubleListDisplay getDoubleListDisplay(); getToggleDisplay()26 public MultiListSelectPresenter.ToggleDisplay getToggleDisplay(); getFixedValuesInput()27 public HasText getFixedValuesInput(); setFixedValuesVisible(boolean visible)28 public void setFixedValuesVisible(boolean visible); getFixedValuesToggle()29 public ToggleControl getFixedValuesToggle(); 30 } 31 32 private Display display; 33 private final State savedState = new State(); 34 private HeaderSelect headerSelect; 35 SpreadsheetHeaderSelect(HeaderFieldCollection headerFields)36 public SpreadsheetHeaderSelect(HeaderFieldCollection headerFields) { 37 headerSelect = new HeaderSelect(headerFields, savedState.baseState); 38 } 39 bindDisplay(Display display)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 saveToState(State state)49 protected void saveToState(State state) { 50 headerSelect.saveToState(state.baseState); 51 state.fixedValues = getFixedValuesText(); 52 } 53 getFixedValuesText()54 private String getFixedValuesText() { 55 if (!isFixedValuesActive()) { 56 return ""; 57 } 58 59 return display.getFixedValuesInput().getText(); 60 } 61 getFixedValues()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 isFixedValuesActive()70 private boolean isFixedValuesActive() { 71 return !display.getToggleDisplay().getToggleMultipleLink().isActive() 72 && display.getFixedValuesToggle().isActive(); 73 } 74 loadFromState(State state)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 setSelectedItems(List<HeaderField> fields)81 public void setSelectedItems(List<HeaderField> fields) { 82 headerSelect.setSelectedItems(fields); 83 savedState.fixedValues = ""; 84 } 85 onClick(ClickEvent event)86 public void onClick(ClickEvent event) { 87 assert event.getSource() == display.getFixedValuesToggle(); 88 display.setFixedValuesVisible(isFixedValuesActive()); 89 } 90 addHistoryArguments(Map<String, String> arguments, String name)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 handleHistoryArguments(Map<String, String> arguments, String name)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 addQueryParameters(JSONObject parameters)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 getSelectedItems()114 public List<HeaderField> getSelectedItems() { 115 return headerSelect.getSelectedItems(); 116 } 117 refreshFields()118 public void refreshFields() { 119 headerSelect.refreshFields(); 120 } 121 setSelectedItem(HeaderField field)122 public void setSelectedItem(HeaderField field) { 123 setSelectedItems(Arrays.asList(new HeaderField[] {field})); 124 } 125 getStateFromView()126 public State getStateFromView() { 127 State state = new State(); 128 saveToState(state); 129 return state; 130 } 131 updateStateFromView()132 public void updateStateFromView() { 133 saveToState(savedState); 134 } 135 updateViewFromState()136 public void updateViewFromState() { 137 loadFromState(savedState); 138 } 139 } 140