1 16 package org.apache.myfaces.custom.navigation; 17 18 import org.apache.myfaces.component.html.ext.HtmlCommandLink; 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.context.FacesContext; 25 import javax.faces.event.AbortProcessingException; 26 import javax.faces.event.ActionEvent; 27 import javax.faces.event.FacesEvent; 28 import javax.faces.event.PhaseId; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 38 public class HtmlCommandNavigation 39 extends HtmlCommandLink 40 { 41 private static final Log log = LogFactory.getLog(HtmlCommandNavigation.class); 42 43 private boolean _open = false; 44 private boolean _active = false; 45 46 public boolean isImmediate() 47 { 48 return true; 50 } 51 52 public void setImmediate(boolean immediate) 53 { 54 if (log.isWarnEnabled()) log.warn("Immediate property of HtmlCommandNavigation cannot be set --> ignored."); 55 } 56 57 public boolean isOpen() 58 { 59 return _open; 60 } 61 62 public void setOpen(boolean open) 63 { 64 _open = open; 65 } 66 67 public boolean isActive() 68 { 69 return _active; 70 } 71 72 public void setActive(boolean active) 73 { 74 _active = active; 75 } 76 77 80 public boolean isRendered() 81 { 82 if (! super.isRendered()) { 83 return false; 84 } 85 UIComponent parent = getParent(); 86 while (parent != null) 87 { 88 if (parent instanceof HtmlCommandNavigation) 89 { 90 if (!((HtmlCommandNavigation)parent).isOpen()) 91 { 92 return false; 93 } 94 } 95 96 if (parent instanceof HtmlPanelNavigation) 97 { 98 break; 99 } 100 else 101 { 102 parent = parent.getParent(); 103 } 104 } 105 106 return true; 107 } 108 109 110 public void toggleOpen() 111 { 112 if (isOpen()) 113 { 114 if (getChildCount() > 0) 115 { 116 setOpen(false); 118 } 119 } 120 else 121 { 122 UIComponent parent = getParent(); 123 124 closeAllChildren(parent.getChildren().iterator()); 126 127 UIComponent p = parent; 129 while (p != null && !(p instanceof HtmlPanelNavigation)) 130 { 131 if (p instanceof HtmlCommandNavigation) 132 { 133 ((HtmlCommandNavigation)p).setOpen(true); 134 } 135 p = p.getParent(); 136 } 137 139 if (!hasCommandNavigationChildren()) 140 { 141 if (!(p instanceof HtmlPanelNavigation)) 143 { 144 log.error("HtmlCommandNavigation without parent HtmlPanelNavigation ?!"); 145 } 146 else 147 { 148 deactivateAllChildren(p.getChildren().iterator()); 150 } 151 setActive(true); 153 } 154 else 155 { 156 setOpen(true); 158 } 159 } 160 } 161 162 private boolean hasCommandNavigationChildren() 163 { 164 if (getChildCount() == 0) 165 { 166 return false; 167 } 168 List list = getChildren(); 169 for (int i = 0, sizei = list.size(); i < sizei; i++) 170 { 171 if (list.get(i) instanceof HtmlCommandNavigation) 172 { 173 return true; 174 } 175 } 176 return false; 177 } 178 179 180 private static void deactivateAllChildren(Iterator children) 181 { 182 while (children.hasNext()) 183 { 184 UIComponent ni = (UIComponent)children.next(); 185 if (ni instanceof HtmlCommandNavigation) 186 { 187 ((HtmlCommandNavigation)ni).setActive(false); 188 if (ni.getChildCount() > 0) 189 { 190 deactivateAllChildren(ni.getChildren().iterator()); 191 } 192 } 193 } 194 } 195 196 private static void closeAllChildren(Iterator children) 197 { 198 while (children.hasNext()) 199 { 200 UIComponent ni = (UIComponent)children.next(); 201 if (ni instanceof HtmlCommandNavigation) 202 { 203 ((HtmlCommandNavigation)ni).setOpen(false); 204 if (ni.getChildCount() > 0) 205 { 206 closeAllChildren(ni.getChildren().iterator()); 207 } 208 } 209 } 210 } 211 212 213 public void broadcast(FacesEvent event) throws AbortProcessingException 214 { 215 if (event instanceof ActionEvent) 216 { 217 ActionEvent actionEvent = (ActionEvent)event; 218 if (actionEvent.getPhaseId() == PhaseId.APPLY_REQUEST_VALUES) 219 { 220 HtmlCommandNavigation navItem = (HtmlCommandNavigation)actionEvent.getComponent(); 221 navItem.toggleOpen(); 222 FacesContext.getCurrentInstance().renderResponse(); 223 } 224 } 225 super.broadcast(event); 226 } 227 228 229 public Object saveState(FacesContext context) 230 { 231 Object values[] = new Object [3]; 232 values[0] = super.saveState(context); 233 values[1] = Boolean.valueOf(_open); 234 values[2] = Boolean.valueOf(_active); 235 return ((Object ) (values)); 236 } 237 238 public void restoreState(FacesContext context, Object state) 239 { 240 Object values[] = (Object [])state; 241 super.restoreState(context, values[0]); 242 _open = ((Boolean )values[1]).booleanValue(); 243 _active = ((Boolean )values[2]).booleanValue(); 244 } 245 246 247 249 public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlCommandNavigation"; 250 public static final String COMPONENT_FAMILY = "javax.faces.Command"; 251 private static final String DEFAULT_RENDERER_TYPE = "javax.faces.Link"; 252 253 254 public HtmlCommandNavigation() 255 { 256 setRendererType(DEFAULT_RENDERER_TYPE); 257 } 258 259 public String getFamily() 260 { 261 return COMPONENT_FAMILY; 262 } 263 264 265 267 } 268 | Popular Tags |