1 17 package org.alfresco.web.ui.common.component; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.StringTokenizer ; 22 23 import javax.faces.component.UICommand; 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 34 public class UIBreadcrumb extends UICommand 35 { 36 39 42 public UIBreadcrumb() 43 { 44 setRendererType("org.alfresco.faces.BreadcrumbRenderer"); 45 } 46 47 48 51 54 public String getFamily() 55 { 56 return "org.alfresco.faces.Controls"; 57 } 58 59 62 public void restoreState(FacesContext context, Object state) 63 { 64 Object values[] = (Object [])state; 65 super.restoreState(context, values[0]); 67 this.separator = (String )values[1]; 68 this.showRoot = (Boolean )values[2]; 69 } 70 71 74 public Object saveState(FacesContext context) 75 { 76 Object values[] = new Object [3]; 77 values[0] = super.saveState(context); 79 values[1] = this.separator; 80 values[2] = this.showRoot; 81 return (values); 82 } 83 84 87 public void broadcast(FacesEvent event) throws AbortProcessingException 88 { 89 if (event instanceof BreadcrumbEvent) 90 { 91 setSelectedPathIndex( ((BreadcrumbEvent)event).SelectedIndex ); 92 } 93 94 super.broadcast(event); 96 } 97 98 103 public void setSelectedPathIndex(int index) 104 { 105 List <IBreadcrumbHandler> elements = (List )getValue(); 107 108 if (elements.size() >= index) 109 { 110 List <IBreadcrumbHandler> path = new ArrayList <IBreadcrumbHandler>(index + 1); 112 path.addAll(elements.subList(0, index + 1)); 113 114 setValue(path); 116 117 String outcome = path.get(index).navigationOutcome(this); 119 if (outcome != null) 120 { 121 String viewId = getFacesContext().getViewRoot().getViewId(); 122 getFacesContext().getApplication().getNavigationHandler().handleNavigation( 123 getFacesContext(), viewId, outcome); 124 } 125 } 126 } 127 128 131 public Object getValue() 132 { 133 List <IBreadcrumbHandler> elements = null; 134 135 Object value = super.getValue(); 136 if (value instanceof String ) 137 { 138 elements = new ArrayList (8); 139 StringTokenizer t = new StringTokenizer ((String )value, SEPARATOR); 141 while (t.hasMoreTokens() == true) 142 { 143 IBreadcrumbHandler handler = new DefaultPathHandler(t.nextToken()); 144 elements.add(handler); 145 } 146 147 setValue(elements); 149 } 150 else if (value instanceof List ) 151 { 152 elements = (List )value; 153 } 154 else if (value != null) 155 { 156 throw new IllegalArgumentException ("UIBreadcrumb value must be a String path or List of IBreadcrumbHandler!"); 157 } 158 else 159 { 160 elements = new ArrayList (8); 161 } 162 163 return elements; 164 } 165 166 171 public void appendHandler(IBreadcrumbHandler handler) 172 { 173 if (handler == null) 174 { 175 throw new NullPointerException ("IBreadcrumbHandler instance cannot be null!"); 176 } 177 178 List elements = (List )getValue(); 179 elements.add(handler); 180 } 181 182 183 186 191 public String getSeparator() 192 { 193 ValueBinding vb = getValueBinding("separator"); 194 if (vb != null) 195 { 196 this.separator = (String )vb.getValue(getFacesContext()); 197 } 198 199 return this.separator; 200 } 201 202 207 public void setSeparator(String separator) 208 { 209 this.separator = separator; 210 } 211 212 217 public boolean getShowRoot() 218 { 219 ValueBinding vb = getValueBinding("showRoot"); 220 if (vb != null) 221 { 222 this.showRoot = (Boolean )vb.getValue(getFacesContext()); 223 } 224 225 if (this.showRoot != null) 226 { 227 return this.showRoot.booleanValue(); 228 } 229 else 230 { 231 return true; 233 } 234 } 235 236 241 public void setShowRoot(boolean showRoot) 242 { 243 this.showRoot = Boolean.valueOf(showRoot); 244 } 245 246 247 250 253 public static class BreadcrumbEvent extends ActionEvent 254 { 255 public BreadcrumbEvent(UIComponent component, int selectedIndex) 256 { 257 super(component); 258 SelectedIndex = selectedIndex; 259 } 260 261 public int SelectedIndex = 0; 262 } 263 264 267 private static class DefaultPathHandler implements IBreadcrumbHandler 268 { 269 274 public DefaultPathHandler(String label) 275 { 276 this.label = label; 277 } 278 279 282 public String toString() 283 { 284 return this.label; 285 } 286 287 290 public String navigationOutcome(UIBreadcrumb breadcrumb) 291 { 292 return null; 294 } 295 296 private String label; 297 } 298 299 300 303 304 private String separator = null; 305 306 307 private Boolean showRoot = null; 308 309 310 public final static String SEPARATOR = "/"; 311 } 312 | Popular Tags |