1 16 17 package org.apache.jetspeed.modules.parameters; 18 19 import org.apache.ecs.html.Select; 21 import org.apache.ecs.html.Option; 22 23 import java.util.Map ; 25 import java.util.Arrays ; 26 import java.util.StringTokenizer ; 27 28 import org.apache.turbine.util.RunData; 30 31 46 public class ListBox extends ParameterPresentationStyle 47 { 48 49 public static final String SORT = "sort"; 50 public static final String ITEMS = "items"; 51 public static final String LAYOUT = "layout"; 52 public static final String LAYOUT_COMBO = "$combo"; 53 public static final String LAYOUT_LIST = "$list"; 54 public static final String LIST_SIZE = "listsize"; 55 public static final String MULTIPLE_CHOICE = "multiplechoice"; 56 public static final String NULL_IF_EMPTY = "null-if-empty"; 57 58 protected String layout = null; 59 protected String items[] = null; 60 protected String size = null; 61 protected boolean multiple = false; 62 63 72 public String getContent(RunData data, String name, String value, Map parms) 73 { 74 75 init(data); 76 77 Select select = null; 78 if ( layout.equalsIgnoreCase(LAYOUT_LIST) ) 79 { 80 select = new Select(name, new Integer (size).intValue()); 81 } else 82 { 83 select = new Select(name); 84 } 85 86 if ( multiple ) 87 { 88 select.setMultiple(multiple); 89 } 90 91 if ( items != null ) 92 { 93 94 boolean sort = new Boolean ((String )this.getParm(SORT, "false")).booleanValue(); 95 if ( sort ) 96 { 97 Arrays.sort(items); 98 } 99 100 for ( int i=0; i < items.length; i++ ) 101 { 102 Option option = new Option(items[i]).addElement(items[i]); 103 if (multiple) 104 { 105 option.setSelected(value.indexOf(items[i]) >= 0); 106 } 107 else 108 { 109 option.setSelected(items[i].equalsIgnoreCase(value)); 110 } 111 select.addElement(option); 112 } 113 } 114 115 boolean nullIfEmpty = new Boolean ((String )this.getParm(NULL_IF_EMPTY, "false")).booleanValue(); 117 if ( this.items == null || (nullIfEmpty && items.length == 0) ) 118 { 119 return null; 120 } 121 122 return select.toString(); 123 124 } 125 126 131 protected void init(RunData data) 132 { 133 134 this.layout = (String )this.getParm(LAYOUT, LAYOUT_COMBO); 135 this.items = this.getItems(data); 136 this.size = (String )this.getParm(LIST_SIZE, "1"); 137 this.multiple = new Boolean ((String )this.getParm(MULTIPLE_CHOICE, "false")).booleanValue(); 138 139 } 140 141 147 protected String [] getItems(RunData data) 148 { 149 150 String [] result = null; 151 String list = (String )this.getParm(ITEMS, ""); 152 153 StringTokenizer it = new StringTokenizer (list, ","); 154 int size = it.countTokens(); 155 156 if ( size > 0 ) 157 { 158 result = new String [size]; 159 160 int i = 0; 161 while ( it.hasMoreTokens() ) 162 { 163 String item = (String )it.nextToken(); 164 result[i] = item; 165 i++; 166 } 167 } 168 169 return result; 170 171 } 172 173 } 174 | Popular Tags |