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.web.bean.repository.Node; 34 import org.alfresco.web.ui.common.Utils; 35 import org.alfresco.web.ui.repo.WebResources; 36 37 42 public class UIRecentSpacesShelfItem extends UIShelfItem 43 { 44 47 50 public void restoreState(FacesContext context, Object state) 51 { 52 Object values[] = (Object [])state; 53 super.restoreState(context, values[0]); 55 this.value = values[1]; 56 this.navigateActionListener = (MethodBinding)values[2]; 57 } 58 59 62 public Object saveState(FacesContext context) 63 { 64 Object values[] = new Object [3]; 65 values[0] = super.saveState(context); 67 values[1] = this.value; 68 values[2] = this.navigateActionListener; 69 70 return (values); 71 } 72 73 78 public Object getValue() 79 { 80 if (this.value == null) 81 { 82 ValueBinding vb = getValueBinding("value"); 83 if (vb != null) 84 { 85 this.value = vb.getValue(getFacesContext()); 86 } 87 } 88 return this.value; 89 } 90 91 96 public void setValue(Object value) 97 { 98 this.value = value; 99 } 100 101 104 public void setNavigateActionListener(MethodBinding binding) 105 { 106 this.navigateActionListener = binding; 107 } 108 109 112 public MethodBinding getNavigateActionListener() 113 { 114 return this.navigateActionListener; 115 } 116 117 120 public void decode(FacesContext context) 121 { 122 Map requestMap = context.getExternalContext().getRequestParameterMap(); 123 String fieldId = getHiddenFieldName(); 124 String value = (String )requestMap.get(fieldId); 125 126 if (value != null && value.length() != 0) 127 { 128 int sepIndex = value.indexOf(NamingContainer.SEPARATOR_CHAR); 130 int action = Integer.parseInt(value.substring(0, sepIndex)); 131 int index = Integer.parseInt(value.substring(sepIndex + 1)); 132 133 RecentSpacesEvent event = new RecentSpacesEvent(this, action, index); 135 this.queueEvent(event); 136 } 137 } 138 139 142 public void encodeBegin(FacesContext context) throws IOException 143 { 144 if (isRendered() == false) 145 { 146 return; 147 } 148 149 ResponseWriter out = context.getResponseWriter(); 150 List <Node> items = (List <Node>)getValue(); 151 out.write(SHELF_START); 152 for (int i=0; i<items.size(); i++) 153 { 154 Node item = items.get(i); 155 156 out.write("<tr><td>"); 158 out.write(Utils.buildImageTag(context, WebResources.IMAGE_SPACE, 16, 16, null, null, "absmiddle")); 159 160 out.write("</td><td width=100%><nobr> "); 163 out.write(buildActionLink(ACTION_NAVIGATE_ITEM, i, item.getName())); 164 165 out.write("</nobr></td></tr>"); 167 } 168 169 out.write(SHELF_END); 170 } 171 172 175 public void broadcast(FacesEvent event) throws AbortProcessingException 176 { 177 if (event instanceof RecentSpacesEvent) 178 { 179 RecentSpacesEvent spaceEvent = (RecentSpacesEvent)event; 181 182 List <Node> items = (List <Node>)getValue(); 183 if (items.size() > spaceEvent.Index) 184 { 185 switch (spaceEvent.Action) 187 { 188 case ACTION_NAVIGATE_ITEM: 189 Utils.processActionMethod(getFacesContext(), getNavigateActionListener(), spaceEvent); 190 break; 191 } 192 } 193 } 194 else 195 { 196 super.broadcast(event); 197 } 198 } 199 200 201 204 210 private String getHiddenFieldName() 211 { 212 return getClientId(getFacesContext()); 213 } 214 215 224 private String buildActionLink(int action, int index, String text) 225 { 226 FacesContext context = getFacesContext(); 227 228 StringBuilder buf = new StringBuilder (200); 229 230 buf.append("<a HREF='#' onclick=\""); 231 buf.append(Utils.generateFormSubmit(context, this, getHiddenFieldName(), encodeValues(action, index))); 234 buf.append("\">"); 235 236 buf.append(Utils.cropEncode(text)); 237 238 buf.append("</a>"); 239 240 return buf.toString(); 241 } 242 243 251 private static String encodeValues(int action, int index) 252 { 253 return Integer.toString(action) + NamingContainer.SEPARATOR_CHAR + Integer.toString(index); 254 } 255 256 257 260 263 public static class RecentSpacesEvent extends ActionEvent 264 { 265 public RecentSpacesEvent(UIComponent component, int action, int index) 266 { 267 super(component); 268 Action = action; 269 Index = index; 270 } 271 272 public int Action; 273 public int Index; 274 } 275 276 277 280 private final static int ACTION_NAVIGATE_ITEM = 0; 281 282 283 private Object value = null; 284 285 286 private MethodBinding navigateActionListener; 287 } 288 | Popular Tags |