| 1 33 34 49 50 package com.icesoft.faces.component.commandsortheader; 51 52 import com.icesoft.faces.component.CSS_DEFAULT; 53 import com.icesoft.faces.component.ext.HtmlCommandLink; 54 import com.icesoft.faces.component.ext.HtmlDataTable; 55 import com.icesoft.faces.component.ext.taglib.Util; 56 57 import javax.faces.component.UIComponent; 58 import javax.faces.context.FacesContext; 59 import javax.faces.el.ValueBinding; 60 import javax.faces.event.AbortProcessingException; 61 import javax.faces.event.ActionEvent; 62 import javax.faces.event.FacesEvent; 63 64 68 public class CommandSortHeader 69 extends HtmlCommandLink { 70 73 public void broadcast(FacesEvent event) throws AbortProcessingException { 74 super.broadcast(event); 75 76 if (event instanceof ActionEvent) { 77 HtmlDataTable dataTable = findParentDataTable(); 78 if (dataTable == null) { 79 } else { 81 String colName = getColumnName(); 82 String currentSortColumn = dataTable.getSortColumn(); 83 boolean currentAscending = dataTable.isSortAscending(); 84 if (colName.equals(currentSortColumn)) { 85 dataTable.setSortColumn(getColumnName()); 86 dataTable.setSortAscending(!currentAscending); 87 } else { 88 dataTable.setSortColumn(getColumnName()); 89 dataTable.setSortAscending(true); 90 } 91 } 92 } 93 } 94 95 96 100 public HtmlDataTable findParentDataTable() { 101 UIComponent parent = getParent(); 102 while (parent != null) { 103 if (parent instanceof HtmlDataTable) { 104 return (HtmlDataTable) parent; 105 } 106 parent = parent.getParent(); 107 } 108 return null; 109 } 110 111 112 116 public Object saveState(FacesContext context) { 117 Object values[] = new Object [3]; 118 values[0] = super.saveState(context); 119 values[1] = _columnName; 120 values[2] = _arrow; 121 return ((Object ) (values)); 122 } 123 124 128 public void restoreState(FacesContext context, Object state) { 129 Object values[] = (Object []) state; 130 super.restoreState(context, values[0]); 131 _columnName = (String ) values[1]; 132 _arrow = (Boolean ) values[2]; 133 } 134 135 private String styleClass; 136 137 140 public void setStyleClass(String styleClass) { 141 this.styleClass = styleClass; 142 } 143 144 147 public String getStyleClass() { 148 return Util.getQualifiedStyleClass(this, styleClass, 149 CSS_DEFAULT.COMMAND_SORT_HEADER_STYLE_CLASS 150 , "styleClass", 151 isDisabled()); 152 } 153 154 public static final String COMPONENT_TYPE = "com.icesoft.faces.SortHeader"; 155 public static final String COMPONENT_FAMILY = "javax.faces.Command"; 156 public static final String DEFAULT_RENDERER_TYPE = 157 "com.icesoft.faces.SortHeader"; 158 159 private String _columnName = null; 160 private Boolean _arrow = null; 161 162 public CommandSortHeader() { 163 setRendererType(DEFAULT_RENDERER_TYPE); 164 } 165 166 170 public String getFamily() { 171 return COMPONENT_FAMILY; 172 } 173 174 177 public void setColumnName(String columnName) { 178 _columnName = columnName; 179 } 180 181 184 public String getColumnName() { 185 if (_columnName != null) { 186 return _columnName; 187 } 188 ValueBinding vb = getValueBinding("columnName"); 189 return vb != null ? vb.getValue(getFacesContext()).toString() : null; 190 } 191 192 195 public void setArrow(boolean arrow) { 196 _arrow = Boolean.valueOf(arrow); 197 } 198 199 202 public boolean isArrow() { 203 if (_arrow != null) { 204 return _arrow.booleanValue(); 205 } 206 ValueBinding vb = getValueBinding("arrow"); 207 Boolean v = 208 vb != null ? (Boolean ) vb.getValue(getFacesContext()) : null; 209 return v != null ? v.booleanValue() : false; 210 } 211 } 212 | Popular Tags |