1package autotest.tko; 2 3import autotest.common.Utils; 4import autotest.common.CustomHistory.HistoryToken; 5 6import com.google.gwt.event.dom.client.ChangeEvent; 7import com.google.gwt.event.dom.client.ChangeHandler; 8import com.google.gwt.event.dom.client.ClickEvent; 9import com.google.gwt.event.dom.client.ClickHandler; 10import com.google.gwt.event.logical.shared.HasValueChangeHandlers; 11import com.google.gwt.event.logical.shared.ValueChangeEvent; 12import com.google.gwt.event.logical.shared.ValueChangeHandler; 13import com.google.gwt.event.shared.HandlerRegistration; 14import com.google.gwt.json.client.JSONArray; 15import com.google.gwt.json.client.JSONObject; 16import com.google.gwt.json.client.JSONString; 17import com.google.gwt.user.client.ui.Anchor; 18import com.google.gwt.user.client.ui.Composite; 19import com.google.gwt.user.client.ui.ListBox; 20import com.google.gwt.user.client.ui.Panel; 21import com.google.gwt.user.client.ui.VerticalPanel; 22 23import java.util.ArrayList; 24import java.util.Arrays; 25import java.util.HashSet; 26import java.util.List; 27import java.util.Map; 28import java.util.Set; 29 30public class ContentSelect extends Composite implements HasValueChangeHandlers<Boolean> { 31 32 public static final String HISTORY_OPENED = "_opened"; 33 34 public static final String ADD_ADDITIONAL_CONTENT = "Add additional content..."; 35 public static final String CANCEL_ADDITIONAL_CONTENT = "Don't use additional content"; 36 37 private HeaderFieldCollection headerFields; 38 private Anchor addLink = new Anchor(ADD_ADDITIONAL_CONTENT); 39 private ListBox contentSelect = new ListBox(true); 40 41 public ContentSelect(HeaderFieldCollection headerFields) { 42 this.headerFields = headerFields; 43 44 Panel panel = new VerticalPanel(); 45 contentSelect.setVisible(false); 46 47 panel.add(addLink); 48 panel.add(contentSelect); 49 50 initWidget(panel); 51 52 addLink.addClickHandler(new ClickHandler() { 53 public void onClick(ClickEvent event) { 54 if (contentSelect.isVisible()) { 55 addLink.setText(ADD_ADDITIONAL_CONTENT); 56 contentSelect.setVisible(false); 57 notifyHandlers(); 58 } else { 59 openContentSelect(); 60 } 61 } 62 }); 63 64 contentSelect.addChangeHandler(new ChangeHandler() { 65 public void onChange(ChangeEvent event) { 66 notifyHandlers(); 67 } 68 }); 69 } 70 71 private void notifyHandlers() { 72 ValueChangeEvent.fire(this, hasSelection()); 73 } 74 75 private void openContentSelect() { 76 addLink.setText(CANCEL_ADDITIONAL_CONTENT); 77 contentSelect.setVisible(true); 78 notifyHandlers(); 79 } 80 81 public void refreshFields() { 82 contentSelect.clear(); 83 for (HeaderField field : headerFields) { 84 contentSelect.addItem(field.getName(), field.getSqlName()); 85 } 86 } 87 88 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Boolean> handler) { 89 return addHandler(handler, ValueChangeEvent.getType()); 90 } 91 92 public boolean hasSelection() { 93 return contentSelect.isVisible() && contentSelect.getSelectedIndex() != -1; 94 } 95 96 public void addToCondition(JSONObject condition) { 97 if (hasSelection()) { 98 JSONArray extraInfo = new JSONArray(); 99 for (int i = 0; i < contentSelect.getItemCount(); i++) { 100 if (contentSelect.isItemSelected(i)) { 101 extraInfo.set(extraInfo.size(), new JSONString(contentSelect.getValue(i))); 102 } 103 } 104 condition.put("extra_info", extraInfo); 105 } 106 } 107 108 public void addHistoryArguments(HistoryToken arguments, String name) { 109 List<String> fields = new ArrayList<String>(); 110 for (int i = 0; i < contentSelect.getItemCount(); i++) { 111 if (contentSelect.isItemSelected(i)) { 112 fields.add(contentSelect.getValue(i)); 113 } 114 } 115 String fieldList = Utils.joinStrings(",", fields); 116 arguments.put(name, fieldList); 117 118 if (contentSelect.isVisible()) { 119 arguments.put(name + HISTORY_OPENED, "true"); 120 } 121 } 122 123 public void handleHistoryArguments(Map<String, String> arguments, String name) { 124 if (!arguments.containsKey(name)) { 125 return; 126 } 127 128 Set<String> fields = new HashSet<String>(Arrays.asList(arguments.get(name).split(","))); 129 for (int i = 0; i < contentSelect.getItemCount(); i++) { 130 if (fields.contains(contentSelect.getValue(i))) { 131 contentSelect.setItemSelected(i, true); 132 } 133 } 134 135 if (arguments.containsKey(name + HISTORY_OPENED)) { 136 openContentSelect(); 137 } 138 } 139} 140