1 25 package org.snipsnap.snip.label; 26 27 import org.radeox.util.Service; 28 import org.radeox.util.i18n.ResourceManager; 29 import org.snipsnap.app.Application; 30 import org.snipsnap.net.ServletPluginLoader; 31 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 public class TypeLabel extends BaseLabel { 38 private static Map types = null; 39 40 46 private static Map getTypeMap() { 47 if (types == null) { 48 types = new HashMap (); 49 Iterator iter = Service.providerNames(TypeLabel.class); 50 51 while (iter.hasNext()) { 52 String typeInfo = ((String ) iter.next()).trim(); 53 if (!"".equals(typeInfo) && !typeInfo.startsWith("#")) { 54 String [] entry = typeInfo.split("\\p{Space}+|:"); 55 String [] handlers = new String [2]; 56 switch (entry.length) { 57 case 3: 58 handlers[1] = entry[2]; 59 case 2: 60 handlers[0] = entry[1]; 61 case 1: 62 types.put(entry[0], handlers); 63 break; 64 } 65 } 66 } 67 } 68 return types; 69 } 70 71 76 public static Set getTypes() { 77 return getTypeMap().keySet(); 78 } 79 80 86 public static String getViewHandler(String type) { 87 String [] handlers = (String []) getTypeMap().get(type); 88 if (null != handlers) { 89 return handlers[0]; 90 } 91 return null; 92 } 93 94 100 public static String getEditHandler(String type) { 101 String [] handlers = (String []) getTypeMap().get(type); 102 if (null != handlers) { 103 return handlers[1]; 104 } 105 return null; 106 } 107 108 private String type; 109 private String viewHandler; 110 private String editHandler; 111 112 public TypeLabel() { 113 name = "Type"; 114 setValue(value); 115 } 116 117 public TypeLabel(String value) { 118 this(); 119 setValue(value); 120 } 121 122 public String getType() { 123 return "TypeLabel"; 124 } 125 126 public void setValue(String value) { 127 if (null != value && !"".equals(value)) { 128 String values[] = value.split(","); 129 type = (values.length > 0 ? values[0] : ""); 130 viewHandler = (values.length > 1 ? values[1] : ""); 131 editHandler = (values.length > 2 ? values[2] : ""); 132 } 133 super.setValue(value); 134 } 135 136 public String getValue() { 137 return 138 (isNull(type) ? "" : type) + 139 (isNull(viewHandler) ? (isNull(editHandler) ? "" : ",") : "," + viewHandler) + 140 (isNull(editHandler) ? "" : "," + editHandler); 141 } 142 143 private boolean isNull(String var) { 144 return var == null || "".equals(var); 145 } 146 147 public String getTypeValue() { 148 return type; 149 } 150 151 public String getViewHandler() { 152 return "".equals(viewHandler) ? null : viewHandler; 153 } 154 155 public String getEditHandler() { 156 return "".equals(editHandler) ? null : editHandler; 157 } 158 159 public String getInputProxy() { 160 StringBuffer buffer = new StringBuffer (); 161 if (Application.get().getUser().isAdmin()) { 162 buffer.append("<input type=\"hidden\" name=\"label.name\" value=\""); 163 buffer.append(name); 164 buffer.append("\"/>"); 165 buffer.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"2\">"); 166 buffer.append("<tr>"); 167 buffer.append("<td>"); 168 buffer.append(ResourceManager.getString("i18n.messages", "label.type.type")); 169 buffer.append("</td>"); 170 buffer.append("<td>"); 171 172 Iterator iterator = getTypes().iterator(); 173 buffer.append("<select name=\"label.type\" size=\"1\">"); 174 while (iterator.hasNext()) { 175 String typeString = (String ) iterator.next(); 176 buffer.append("<option"); 177 if (typeString.equals(type)) { 178 buffer.append(" selected=\"selected\""); 179 } 180 buffer.append(">"); 181 buffer.append(typeString); 183 buffer.append("</option>"); 184 } 185 buffer.append("</select>"); 186 187 buffer.append("</tr><tr>"); 188 buffer.append("<td>"); 189 buffer.append(ResourceManager.getString("i18n.messages", "label.type.view")); 190 buffer.append("</td>"); 191 buffer.append("<td>"); 192 193 Map handlers = ServletPluginLoader.getPlugins(); 194 buffer.append("<select name=\"label.viewhandler\" size=\"1\">"); 196 buffer.append("<option value=\"\">"); 197 buffer.append(ResourceManager.getString("i18n.messages", "label.type.nohandler")); 198 buffer.append("</option>"); 199 Iterator it = handlers.keySet().iterator(); 200 while (it.hasNext()) { 201 String handlername = (String ) it.next(); 202 buffer.append("<option"); 203 if (handlername.equals(getViewHandler())) { 204 buffer.append(" selected=\"selected\""); 205 } 206 buffer.append(">"); 207 buffer.append(handlername); 208 buffer.append("</option>"); 209 } 210 211 buffer.append(handlers); 212 buffer.append("</select>"); 213 buffer.append("</td>"); 214 buffer.append("</tr><tr>"); 215 buffer.append("<td>"); 216 buffer.append(ResourceManager.getString("i18n.messages", "label.type.edit")); 217 buffer.append("</td>"); 218 buffer.append("<td>"); 219 220 buffer.append("<select name=\"label.edithandler\" size=\"1\">"); 222 buffer.append("<option value=\"\">"); 223 buffer.append(ResourceManager.getString("i18n.messages", "label.type.nohandler")); 224 buffer.append("</option>"); 225 it = handlers.keySet().iterator(); 226 while (it.hasNext()) { 227 String handlername = (String ) it.next(); 228 buffer.append("<option"); 229 if (handlername.equals(getEditHandler())) { 230 buffer.append(" selected=\"selected\""); 231 } 232 buffer.append(">"); 233 buffer.append(handlername); 234 buffer.append("</option>"); 235 } 236 buffer.append("</select>"); 237 buffer.append("</td></tr></table>"); 238 } else { 239 buffer.append(ResourceManager.getString("i18n.messages", "label.type.adminonly")); 240 } 241 242 return buffer.toString(); 243 } 244 245 public String getListProxy() { 246 StringBuffer buffer = new StringBuffer (); 247 buffer.append("<td>"); 248 buffer.append(name); 249 buffer.append("</td><td>"); 250 buffer.append(type); 251 buffer.append("</td>"); 252 return buffer.toString(); 253 } 254 255 public void handleInput(Map input) { 256 if (Application.get().getUser().isAdmin()) { 258 super.handleInput(input); 259 if (input.containsKey("label.type")) { 260 type = (String ) input.get("label.type"); 261 } 262 if (input.containsKey("label.viewhandler")) { 263 viewHandler = (String ) input.get("label.viewhandler"); 264 } 265 if (input.containsKey("label.edithandler")) { 266 editHandler = (String ) input.get("label.edithandler"); 267 } 268 super.setValue(getValue()); 270 } 271 } 272 273 } 274 | Popular Tags |