1 16 package org.apache.myfaces.custom.navigation; 17 18 import org.apache.myfaces.component.html.ext.HtmlPanelGroup; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import javax.faces.component.UIComponent; 24 import javax.faces.component.UIViewRoot; 25 import javax.faces.context.FacesContext; 26 import javax.faces.el.ValueBinding; 27 import java.io.IOException ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 38 public class HtmlPanelNavigation 39 extends HtmlPanelGroup 40 { 41 private static final Log log = LogFactory.getLog(HtmlPanelNavigation.class); 42 43 private static final String PREVIOUS_VIEW_ROOT = HtmlPanelNavigation.class.getName() + ".PREVIOUS_VIEW_ROOT"; 44 private boolean _itemOpenActiveStatesRestored = false; 45 46 public void decode(FacesContext context) 47 { 48 super.decode(context); 50 context.getExternalContext().getRequestMap().put(PREVIOUS_VIEW_ROOT, context.getViewRoot()); 52 _itemOpenActiveStatesRestored = true; 54 } 55 56 public void encodeBegin(FacesContext context) throws IOException 57 { 58 if (!_itemOpenActiveStatesRestored && getChildCount() > 0) 59 { 60 UIViewRoot previousRoot = (UIViewRoot)context.getExternalContext().getRequestMap().get(PREVIOUS_VIEW_ROOT); 61 if (previousRoot != null) 62 { 63 restoreOpenActiveStates(context, previousRoot, getChildren()); 64 } 65 else 66 { 67 } 70 } 71 72 super.encodeBegin(context); } 74 75 public void restoreOpenActiveStates(FacesContext facesContext, 76 UIViewRoot previousRoot, 77 List children) 78 { 79 for (Iterator it = children.iterator(); it.hasNext(); ) 80 { 81 UIComponent child = (UIComponent)it.next(); 82 if (child instanceof HtmlCommandNavigation) 83 { 84 HtmlCommandNavigation previousItem = (HtmlCommandNavigation)previousRoot.findComponent(child.getClientId(facesContext)); 85 if (previousItem != null) 86 { 87 ((HtmlCommandNavigation)child).setOpen(previousItem.isOpen()); 88 ((HtmlCommandNavigation)child).setActive(previousItem.isActive()); 89 } 90 else 91 { 92 log.error("Navigation item " + child.getClientId(facesContext) + " not found in previous view."); 93 } 94 if (child.getChildCount() > 0) 95 { 96 restoreOpenActiveStates(facesContext, previousRoot, child.getChildren()); 97 } 98 } 99 } 100 } 101 102 104 public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlPanelNavigation"; 105 public static final String COMPONENT_FAMILY = "javax.faces.Panel"; 106 private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Navigation"; 107 108 private String _itemClass = null; 109 private String _openItemClass = null; 110 private String _activeItemClass = null; 111 private String _separatorClass = null; 112 private String _itemStyle = null; 113 private String _openItemStyle = null; 114 private String _activeItemStyle = null; 115 private String _separatorStyle = null; 116 117 public HtmlPanelNavigation() 118 { 119 setRendererType(DEFAULT_RENDERER_TYPE); 120 } 121 122 public String getFamily() 123 { 124 return COMPONENT_FAMILY; 125 } 126 127 public void setItemClass(String itemClass) 128 { 129 _itemClass = itemClass; 130 } 131 132 public String getItemClass() 133 { 134 if (_itemClass != null) return _itemClass; 135 ValueBinding vb = getValueBinding("itemClass"); 136 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 137 } 138 139 public void setOpenItemClass(String openItemClass) 140 { 141 _openItemClass = openItemClass; 142 } 143 144 public String getOpenItemClass() 145 { 146 if (_openItemClass != null) return _openItemClass; 147 ValueBinding vb = getValueBinding("openItemClass"); 148 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 149 } 150 151 public void setActiveItemClass(String activeItemClass) 152 { 153 _activeItemClass = activeItemClass; 154 } 155 156 public String getActiveItemClass() 157 { 158 if (_activeItemClass != null) return _activeItemClass; 159 ValueBinding vb = getValueBinding("activeItemClass"); 160 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 161 } 162 163 public void setSeparatorClass(String separatorClass) 164 { 165 _separatorClass = separatorClass; 166 } 167 168 public String getSeparatorClass() 169 { 170 if (_separatorClass != null) return _separatorClass; 171 ValueBinding vb = getValueBinding("separatorClass"); 172 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 173 } 174 175 public void setItemStyle(String itemStyle) 176 { 177 _itemStyle = itemStyle; 178 } 179 180 public String getItemStyle() 181 { 182 if (_itemStyle != null) return _itemStyle; 183 ValueBinding vb = getValueBinding("itemStyle"); 184 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 185 } 186 187 public void setOpenItemStyle(String openItemStyle) 188 { 189 _openItemStyle = openItemStyle; 190 } 191 192 public String getOpenItemStyle() 193 { 194 if (_openItemStyle != null) return _openItemStyle; 195 ValueBinding vb = getValueBinding("openItemStyle"); 196 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 197 } 198 199 public void setActiveItemStyle(String activeItemStyle) 200 { 201 _activeItemStyle = activeItemStyle; 202 } 203 204 public String getActiveItemStyle() 205 { 206 if (_activeItemStyle != null) return _activeItemStyle; 207 ValueBinding vb = getValueBinding("activeItemStyle"); 208 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 209 } 210 211 public void setSeparatorStyle(String separatorStyle) 212 { 213 _separatorStyle = separatorStyle; 214 } 215 216 public String getSeparatorStyle() 217 { 218 if (_separatorStyle != null) return _separatorStyle; 219 ValueBinding vb = getValueBinding("separatorStyle"); 220 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 221 } 222 223 224 public Object saveState(FacesContext context) 225 { 226 Object values[] = new Object [9]; 227 values[0] = super.saveState(context); 228 values[1] = _itemClass; 229 values[2] = _openItemClass; 230 values[3] = _activeItemClass; 231 values[4] = _separatorClass; 232 values[5] = _itemStyle; 233 values[6] = _openItemStyle; 234 values[7] = _activeItemStyle; 235 values[8] = _separatorStyle; 236 return ((Object ) (values)); 237 } 238 239 public void restoreState(FacesContext context, Object state) 240 { 241 Object values[] = (Object [])state; 242 super.restoreState(context, values[0]); 243 _itemClass = (String )values[1]; 244 _openItemClass = (String )values[2]; 245 _activeItemClass = (String )values[3]; 246 _separatorClass = (String )values[4]; 247 _itemStyle = (String )values[5]; 248 _openItemStyle = (String )values[6]; 249 _activeItemStyle = (String )values[7]; 250 _separatorStyle = (String )values[8]; 251 } 252 } 254 | Popular Tags |