1 16 package org.apache.myfaces.custom.sortheader; 17 18 import org.apache.myfaces.component.html.ext.HtmlCommandLink; 19 import org.apache.myfaces.component.html.ext.HtmlDataTable; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import javax.faces.component.UIComponent; 25 import javax.faces.context.FacesContext; 26 import javax.faces.el.ValueBinding; 27 import javax.faces.event.AbortProcessingException; 28 import javax.faces.event.ActionEvent; 29 import javax.faces.event.FacesEvent; 30 31 35 public class HtmlCommandSortHeader 36 extends HtmlCommandLink 37 { 38 private static final Log log = LogFactory.getLog(HtmlCommandSortHeader.class); 39 40 46 47 public void broadcast(FacesEvent event) throws AbortProcessingException 48 { 49 super.broadcast(event); 50 51 if (event instanceof ActionEvent) 52 { 53 HtmlDataTable dataTable = findParentDataTable(); 54 if (dataTable == null) 55 { 56 log.error("CommandSortHeader has no MyFacesHtmlDataTable parent"); 57 } 58 else 59 { 60 String colName = getColumnName(); 61 String currentSortColumn = dataTable.getSortColumn(); 62 boolean currentAscending = dataTable.isSortAscending(); 63 if (colName.equals(currentSortColumn)) 64 { 65 dataTable.setSortColumn(getColumnName()); 66 dataTable.setSortAscending(!currentAscending); 67 } 68 else 69 { 70 dataTable.setSortColumn(getColumnName()); 71 dataTable.setSortAscending(true); 72 } 73 } 74 } 75 } 76 77 78 public HtmlDataTable findParentDataTable() 79 { 80 UIComponent parent = getParent(); 81 while (parent != null) 82 { 83 if (parent instanceof HtmlDataTable) 84 { 85 return (HtmlDataTable)parent; 86 } 87 parent = parent.getParent(); 88 } 89 return null; 90 } 91 92 93 public Object saveState(FacesContext context) 94 { 95 Object values[] = new Object [3]; 96 values[0] = super.saveState(context); 97 values[1] = _columnName; 98 values[2] = _arrow; 99 return ((Object ) (values)); 100 } 101 102 public void restoreState(FacesContext context, Object state) 103 { 104 Object values[] = (Object [])state; 105 super.restoreState(context, values[0]); 106 _columnName = (String )values[1]; 107 _arrow = (Boolean )values[2]; 108 } 109 110 112 public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlCommandSortHeader"; 113 public static final String COMPONENT_FAMILY = "javax.faces.Command"; 114 private static final String DEFAULT_RENDERER_TYPE = "javax.faces.Link"; 115 116 private String _columnName = null; 117 private Boolean _arrow = null; 118 119 public HtmlCommandSortHeader() 120 { 121 setRendererType(DEFAULT_RENDERER_TYPE); 122 } 123 124 public String getFamily() 125 { 126 return COMPONENT_FAMILY; 127 } 128 129 public void setColumnName(String columnName) 130 { 131 _columnName = columnName; 132 } 133 134 public String getColumnName() 135 { 136 if (_columnName != null) return _columnName; 137 ValueBinding vb = getValueBinding("columnName"); 138 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 139 } 140 141 public void setArrow(boolean arrow) 142 { 143 _arrow = Boolean.valueOf(arrow); 144 } 145 146 public boolean isArrow() 147 { 148 if (_arrow != null) return _arrow.booleanValue(); 149 ValueBinding vb = getValueBinding("arrow"); 150 Boolean v = vb != null ? (Boolean )vb.getValue(getFacesContext()) : null; 151 return v != null ? v.booleanValue() : false; 152 } 153 154 155 } 157 | Popular Tags |