1 23 package org.infoglue.cms.workflow.taglib; 24 25 import java.util.Iterator ; 26 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.JspTagException ; 29 30 import org.infoglue.cms.controllers.kernel.impl.simple.CategoryController; 31 import org.infoglue.cms.entities.management.CategoryVO; 32 33 40 public class CategorySelector extends ElementTag 41 { 42 45 private static final long serialVersionUID = 7831749037593672903L; 46 47 50 private CategoryVO rootCategoryVO; 51 52 55 private String selected; 56 57 60 public CategorySelector() 61 { 62 super(); 63 } 64 65 72 public int doEndTag() throws JspException 73 { 74 createOptions(); 75 rootCategoryVO = null; 76 selected = null; 77 return super.doEndTag(); 78 } 79 80 83 private void createOptions() 84 { 85 for(final Iterator i = rootCategoryVO.getChildren().iterator(); i.hasNext();) 86 { 87 final CategoryVO categoryVO = (CategoryVO) i.next(); 88 final String name = categoryVO.getName(); 89 final String value = categoryVO.getId().toString(); 90 91 getElement().addChild("option") 92 .addText(name) 93 .addAttribute("value", value) 94 .addAttribute("selected", "selected", value != null && selected != null && value.equals(selected)); 95 } 96 } 97 98 103 protected Element createElement() 104 { 105 return new Element("select"); 106 } 107 108 113 public void setDefaultLabel(final String label) 114 { 115 getElement().addChildFirst("option").addText(label); 116 } 117 118 124 public void setCategoryPath(final String path) throws JspException 125 { 126 rootCategoryVO = getRootCategory(path); 127 } 128 129 134 public void setName(final String name) 135 { 136 getElement().addAttribute("name", name); 137 selected = getPropertySet().getDataString(name); 138 } 139 140 146 private CategoryVO getRootCategory(final String path) throws JspException 147 { 148 try 149 { 150 final CategoryVO categoryVO = CategoryController.getController().findByPath(path); 151 if(categoryVO != null) 152 return CategoryController.getController().findWithChildren(categoryVO.getId()); 153 else 154 throw new Exception ("No category with path " + path + " was found."); 155 } 156 catch(Exception e) 157 { 158 e.printStackTrace(); 159 throw new JspTagException (e.getMessage()); 160 } 161 } 162 } 163 | Popular Tags |