1 19 20 package com.sslexplorer.input; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Comparator ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import org.apache.struts.util.LabelValueBean; 31 32 import com.sslexplorer.boot.PropertyList; 33 import com.sslexplorer.security.SessionInfo; 34 35 36 41 public class MultiSelectSelectionModel { 42 private List <LabelValueBean> availableValues; 43 private List <LabelValueBean> selectedValues; 44 private Map <String , LabelValueBean> availableMap; 45 private MultiSelectDataSource dataSource; 46 private PropertyList propertyList; 47 48 55 public MultiSelectSelectionModel(SessionInfo session, MultiSelectDataSource dataSource, PropertyList propertyList) { 56 super(); 57 this.dataSource = dataSource; 58 this.propertyList = propertyList; 59 rebuild(session); 60 } 61 62 67 public void selectAll(SessionInfo session) { 68 for(Iterator i = availableValues.iterator(); i.hasNext(); ) { 69 propertyList.add(((LabelValueBean)i.next()).getValue()); 70 } 71 rebuild(session); 72 } 73 74 81 public boolean contains(String value) { 82 return available(value) || selected(value); 83 } 84 85 92 public boolean available(String value) { 93 for(Iterator i = availableValues.iterator(); i.hasNext(); ) { 94 if(((LabelValueBean)i.next()).getValue().equals(value)) { 95 return true; 96 } 97 } 98 return false; 99 } 100 101 107 public boolean selected(String value) { 108 for(Iterator i = selectedValues.iterator(); i.hasNext(); ) { 109 if(((LabelValueBean)i.next()).getValue().equals(value)) { 110 return true; 111 } 112 } 113 return false; 114 } 115 116 121 public void rebuild(SessionInfo session) { 122 availableValues = new ArrayList <LabelValueBean>(dataSource.getValues(session)); 123 selectedValues = new ArrayList <LabelValueBean>(); 124 availableMap = new HashMap <String , LabelValueBean>(); 125 for(Iterator i = availableValues.iterator(); i.hasNext(); ) { 126 LabelValueBean lvb = (LabelValueBean)i.next(); 127 availableMap.put(lvb.getValue(), lvb); 128 } 129 for(Iterator i = propertyList.iterator(); i.hasNext(); ) { 130 String v = (String )i.next(); 131 LabelValueBean lvb = (LabelValueBean)availableMap.get(v); 132 if(lvb != null) { 133 selectedValues.add(lvb); 134 availableValues.remove(lvb); 135 availableMap.remove(lvb.getValue()); 136 } 137 } 138 } 139 140 146 public String getAsPropertyText() { 147 StringBuffer buf = new StringBuffer (); 148 for (Iterator i = selectedValues.iterator(); i.hasNext();) { 149 LabelValueBean lvb = (LabelValueBean)i.next(); 150 if (buf.length() > 0) { 151 buf.append("!"); 152 } 153 buf.append(lvb.getValue().replaceAll("\\!", "!!")); 154 } 155 return buf.toString(); 156 } 157 158 163 public List <LabelValueBean> getAvailableValues() { 164 return availableValues; 165 } 166 167 168 173 public List <LabelValueBean> getSelectedValues() { 174 return selectedValues; 175 } 176 177 182 public void sortSelection(Comparator comparator) { 183 Collections.sort(selectedValues, comparator); 184 } 185 } 186 | Popular Tags |