1 17 package org.alfresco.web.ui.repo.component.shelf; 18 19 import java.io.IOException ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import javax.faces.component.NamingContainer; 24 import javax.faces.component.UIComponent; 25 import javax.faces.context.FacesContext; 26 import javax.faces.context.ResponseWriter; 27 import javax.faces.el.MethodBinding; 28 import javax.faces.el.ValueBinding; 29 import javax.faces.event.AbortProcessingException; 30 import javax.faces.event.ActionEvent; 31 import javax.faces.event.FacesEvent; 32 33 import org.alfresco.model.ContentModel; 34 import org.alfresco.service.cmr.dictionary.DictionaryService; 35 import org.alfresco.web.app.Application; 36 import org.alfresco.web.bean.repository.Node; 37 import org.alfresco.web.bean.repository.Repository; 38 import org.alfresco.web.ui.common.Utils; 39 import org.alfresco.web.ui.repo.WebResources; 40 41 46 public class UIShortcutsShelfItem extends UIShelfItem 47 { 48 51 54 public void restoreState(FacesContext context, Object state) 55 { 56 Object values[] = (Object [])state; 57 super.restoreState(context, values[0]); 59 this.value = values[1]; 60 this.clickActionListener = (MethodBinding)values[2]; 61 this.removeActionListener = (MethodBinding)values[3]; 62 } 63 64 67 public Object saveState(FacesContext context) 68 { 69 Object values[] = new Object [4]; 70 values[0] = super.saveState(context); 72 values[1] = this.value; 73 values[2] = this.clickActionListener; 74 values[3] = this.removeActionListener; 75 76 return (values); 77 } 78 79 84 public Object getValue() 85 { 86 if (this.value == null) 87 { 88 ValueBinding vb = getValueBinding("value"); 89 if (vb != null) 90 { 91 this.value = vb.getValue(getFacesContext()); 92 } 93 } 94 return this.value; 95 } 96 97 102 public void setValue(Object value) 103 { 104 this.value = value; 105 } 106 107 110 public void setClickActionListener(MethodBinding binding) 111 { 112 this.clickActionListener = binding; 113 } 114 115 118 public MethodBinding getClickActionListener() 119 { 120 return this.clickActionListener; 121 } 122 123 126 public void setRemoveActionListener(MethodBinding binding) 127 { 128 this.removeActionListener = binding; 129 } 130 131 134 public MethodBinding getRemoveActionListener() 135 { 136 return this.removeActionListener; 137 } 138 139 142 public void decode(FacesContext context) 143 { 144 Map requestMap = context.getExternalContext().getRequestParameterMap(); 145 String fieldId = getHiddenFieldName(); 146 String value = (String )requestMap.get(fieldId); 147 148 if (value != null && value.length() != 0) 149 { 150 int sepIndex = value.indexOf(NamingContainer.SEPARATOR_CHAR); 152 int action = Integer.parseInt(value.substring(0, sepIndex)); 153 int index = Integer.parseInt(value.substring(sepIndex + 1)); 154 155 ShortcutEvent event = new ShortcutEvent(this, action, index); 157 this.queueEvent(event); 158 } 159 } 160 161 164 public void encodeBegin(FacesContext context) throws IOException 165 { 166 if (isRendered() == false) 167 { 168 return; 169 } 170 171 ResponseWriter out = context.getResponseWriter(); 172 List <Node> items = (List <Node>)getValue(); 173 out.write(SHELF_START); 174 if (items != null) 175 { 176 DictionaryService dd = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getDictionaryService(); 177 178 for (int i=0; i<items.size(); i++) 179 { 180 Node item = items.get(i); 181 182 out.write("<tr><td>"); 183 if (dd.isSubClass(item.getType(), ContentModel.TYPE_FOLDER)) 184 { 185 out.write(Utils.buildImageTag(context, WebResources.IMAGE_SPACE, 16, 16, null, null, "absmiddle")); 187 } 188 else if (dd.isSubClass(item.getType(), ContentModel.TYPE_CONTENT)) 189 { 190 String image = Utils.getFileTypeImage(item.getName(), true); 191 out.write(Utils.buildImageTag(context, image, 16, 16, null, null, "absmiddle")); 192 } 193 194 out.write("</td><td width=100%><nobr> "); 197 out.write(buildActionLink(ACTION_CLICK_ITEM, i, item.getName())); 198 199 out.write("</nobr></td><td align=right><nobr>"); 201 out.write(buildActionLink(ACTION_REMOVE_ITEM, i, Application.getMessage(context, MSG_REMOVE_ITEM), WebResources.IMAGE_REMOVE)); 202 204 out.write("</nobr></td></tr>"); 206 } 207 } 208 209 out.write(SHELF_END); 210 } 211 212 215 public void broadcast(FacesEvent event) throws AbortProcessingException 216 { 217 if (event instanceof ShortcutEvent) 218 { 219 ShortcutEvent shortcutEvent = (ShortcutEvent)event; 221 222 List <Node> items = (List <Node>)getValue(); 223 if (items != null && items.size() > shortcutEvent.Index) 224 { 225 switch (shortcutEvent.Action) 227 { 228 case ACTION_CLICK_ITEM: 229 Utils.processActionMethod(getFacesContext(), getClickActionListener(), shortcutEvent); 230 break; 231 case ACTION_REMOVE_ITEM: 232 Utils.processActionMethod(getFacesContext(), getRemoveActionListener(), shortcutEvent); 233 break; 234 } 235 } 236 } 237 else 238 { 239 super.broadcast(event); 240 } 241 } 242 243 244 247 253 private String getHiddenFieldName() 254 { 255 return getClientId(getFacesContext()); 256 } 257 258 267 private String buildActionLink(int action, int index, String text) 268 { 269 FacesContext context = getFacesContext(); 270 271 StringBuilder buf = new StringBuilder (200); 272 273 buf.append("<a HREF='#' onclick=\""); 274 buf.append(Utils.generateFormSubmit(context, this, getHiddenFieldName(), encodeValues(action, index))); 277 buf.append("\">"); 278 279 buf.append(Utils.cropEncode(text)); 280 281 buf.append("</a>"); 282 283 return buf.toString(); 284 } 285 286 296 private String buildActionLink(int action, int index, String text, String image) 297 { 298 FacesContext context = getFacesContext(); 299 300 StringBuilder buf = new StringBuilder (256); 301 302 buf.append("<a HREF='#' onclick=\""); 303 buf.append(Utils.generateFormSubmit(context, this, getHiddenFieldName(), encodeValues(action, index))); 306 buf.append("\">"); 307 308 if (image != null) 309 { 310 buf.append(Utils.buildImageTag(context, image, text)); 311 } 312 else 313 { 314 buf.append(Utils.encode(text)); 315 } 316 317 buf.append("</a>"); 318 319 return buf.toString(); 320 } 321 322 330 private static String encodeValues(int action, int index) 331 { 332 return Integer.toString(action) + NamingContainer.SEPARATOR_CHAR + Integer.toString(index); 333 } 334 335 336 339 342 public static class ShortcutEvent extends ActionEvent 343 { 344 public ShortcutEvent(UIComponent component, int action, int index) 345 { 346 super(component); 347 Action = action; 348 Index = index; 349 } 350 351 public int Action; 352 public int Index; 353 } 354 355 356 359 360 private static final String MSG_REMOVE_ITEM = "remove_item"; 361 362 private final static int ACTION_CLICK_ITEM = 0; 363 private final static int ACTION_REMOVE_ITEM = 1; 364 365 366 private Object value = null; 367 368 369 private MethodBinding clickActionListener; 370 371 372 private MethodBinding removeActionListener; 373 } 374 | Popular Tags |