1 17 package org.alfresco.web.ui.common.component.data; 18 19 import java.io.IOException ; 20 import java.util.Map ; 21 22 import javax.faces.component.NamingContainer; 23 import javax.faces.component.UICommand; 24 import javax.faces.component.UIComponent; 25 import javax.faces.context.FacesContext; 26 import javax.faces.context.ResponseWriter; 27 import javax.faces.el.ValueBinding; 28 import javax.faces.event.AbortProcessingException; 29 import javax.faces.event.ActionEvent; 30 import javax.faces.event.FacesEvent; 31 32 import org.apache.log4j.Logger; 33 34 import org.alfresco.web.data.IDataContainer; 35 import org.alfresco.web.ui.common.Utils; 36 import org.alfresco.web.ui.common.WebResources; 37 38 41 public class UISortLink extends UICommand 42 { 43 46 public UISortLink() 47 { 48 setRendererType(null); 49 } 50 51 54 public void encodeBegin(FacesContext context) throws IOException 55 { 56 ResponseWriter out = context.getResponseWriter(); 57 58 IDataContainer dataContainer = getDataContainer(); 59 if (dataContainer == null) 60 { 61 throw new IllegalStateException ("Must nest UISortLink inside component implementing IDataContainer!"); 62 } 63 64 boolean bPreviouslySorted = false; 66 boolean descending = true; 67 String lastSortedColumn = dataContainer.getCurrentSortColumn(); 68 if (lastSortedColumn == (String )getValue()) 69 { 70 descending = !dataContainer.isCurrentSortDescending(); 71 bPreviouslySorted = true; 72 } 73 74 StringBuilder buf = new StringBuilder (256); 76 buf.append("<nobr><a HREF='#' onclick=\""); 77 buf.append(Utils.generateFormSubmit(context, this, getHiddenFieldName(context), getClientId(context))); 80 buf.append('"'); 81 82 if (getAttributes().get("style") != null) 83 { 84 buf.append(" style=\"") 85 .append(getAttributes().get("style")) 86 .append('"'); 87 } 88 if (getAttributes().get("styleClass") != null) 89 { 90 buf.append(" class=") 91 .append(getAttributes().get("styleClass")); 92 } 93 if (getAttributes().get("tooltip") != null) 94 { 95 buf.append(" title=\"") 96 .append(getAttributes().get("tooltip")) 97 .append('"'); 98 } 99 buf.append('>'); 100 101 buf.append((String )getAttributes().get("label")); 103 104 if (bPreviouslySorted == true) 105 { 106 if (descending == true) 107 { 108 buf.append(" ") 109 .append(Utils.buildImageTag(context, WebResources.IMAGE_SORTUP, 10, 6, null)); 110 } 111 else 112 { 113 buf.append(" ") 114 .append(Utils.buildImageTag(context, WebResources.IMAGE_SORTDOWN, 10, 6, null)); 115 } 116 } 117 else 118 { 119 buf.append(" ") 120 .append(Utils.buildImageTag(context, WebResources.IMAGE_SORTNONE, 10, 7, null)); 121 } 122 buf.append("</a></nobr>"); 123 124 out.write(buf.toString()); 125 } 126 127 130 public void decode(FacesContext context) 131 { 132 Map requestMap = context.getExternalContext().getRequestParameterMap(); 133 String fieldId = getHiddenFieldName(context); 134 String value = (String )requestMap.get(fieldId); 135 if (value != null && value.equals(getClientId(context))) 136 { 137 SortEvent actionEvent = new SortEvent(this, (String )this.getValue()); 141 this.queueEvent(actionEvent); 142 } 143 } 144 145 148 public void broadcast(FacesEvent event) throws AbortProcessingException 149 { 150 if (event instanceof SortEvent == false) 151 { 152 super.broadcast(event); 154 } 155 else if ( ((SortEvent)event).Column.equals(getColumn()) ) 156 { 157 if (s_logger.isDebugEnabled()) 159 s_logger.debug("Handling sort event for column: " + ((SortEvent)event).Column); 160 161 if (getColumn().equals(getDataContainer().getCurrentSortColumn()) == true) 162 { 163 this.descending = !this.descending; 165 } 166 else 167 { 168 this.descending = true; 170 } 171 getDataContainer().sort(getColumn(), this.descending, getMode()); 172 } 173 } 174 175 184 private String getHiddenFieldName(FacesContext context) 185 { 186 UIComponent dataContainer = (UIComponent)Utils.getParentDataContainer(context, this); 187 return dataContainer.getClientId(context) + NamingContainer.SEPARATOR_CHAR + "sort"; 188 } 189 190 195 public String getColumn() 196 { 197 return (String )getValue(); 198 } 199 200 205 public String getMode() 206 { 207 return this.mode; 208 } 209 210 215 public void setMode(String sortMode) 216 { 217 this.mode = sortMode; 218 } 219 220 225 public boolean isDescending() 226 { 227 return this.descending; 228 } 229 230 233 public String getLabel() 234 { 235 ValueBinding vb = getValueBinding("label"); 236 if (vb != null) 237 { 238 this.label = (String )vb.getValue(getFacesContext()); 239 } 240 return this.label; 241 } 242 243 246 public void setLabel(String label) 247 { 248 this.label = label; 249 } 250 251 254 public String getTooltip() 255 { 256 ValueBinding vb = getValueBinding("tooltip"); 257 if (vb != null) 258 { 259 this.tooltip = (String )vb.getValue(getFacesContext()); 260 } 261 return this.tooltip; 262 } 263 264 267 public void setTooltip(String tooltip) 268 { 269 this.tooltip = tooltip; 270 } 271 272 275 public void restoreState(FacesContext context, Object state) 276 { 277 Object values[] = (Object [])state; 278 super.restoreState(context, values[0]); 280 this.descending = ((Boolean )values[1]).booleanValue(); 281 this.mode = (String )values[2]; 282 this.label = (String )values[3]; 283 this.tooltip = (String )values[4]; 284 } 285 286 289 public Object saveState(FacesContext context) 290 { 291 Object values[] = new Object [5]; 292 values[0] = super.saveState(context); 294 values[1] = (this.descending ? Boolean.TRUE : Boolean.FALSE); 295 values[2] = this.mode; 296 values[3] = this.label; 297 values[4] = this.tooltip; 298 return values; 299 } 300 301 304 private IDataContainer getDataContainer() 305 { 306 return Utils.getParentDataContainer(getFacesContext(), this); 307 } 308 309 310 313 316 private static class SortEvent extends ActionEvent 317 { 318 public SortEvent(UIComponent component, String column) 319 { 320 super(component); 321 Column = column; 322 } 323 324 public String Column = null; 325 } 326 327 328 331 private static Logger s_logger = Logger.getLogger(IDataContainer.class); 332 333 334 private String mode = IDataContainer.SORT_CASEINSENSITIVE; 335 336 private String label; 337 338 private String tooltip; 339 340 341 private boolean descending = false; 342 } 343 | Popular Tags |