KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > workflow > taglib > CategorySelector


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.cms.workflow.taglib;
24
25 import java.util.Iterator JavaDoc;
26
27 import javax.servlet.jsp.JspException JavaDoc;
28 import javax.servlet.jsp.JspTagException JavaDoc;
29
30 import org.infoglue.cms.controllers.kernel.impl.simple.CategoryController;
31 import org.infoglue.cms.entities.management.CategoryVO;
32
33 /**
34  * This class implements the <iw:categorySelector> tag, which presents an <select ... >...</select>
35  * form element representing a category and where each option element represents a child of the root
36  * category.
37  * The value of the selected element is fetched (with the name of the select element as a key)
38  * from the propertyset associated with the workflow.
39  */

40 public class CategorySelector extends ElementTag
41 {
42     /**
43      * The universal version identifier.
44      */

45     private static final long serialVersionUID = 7831749037593672903L;
46
47     /**
48      * The category to use when populating the option elements.
49      */

50     private CategoryVO rootCategoryVO;
51     
52     /**
53      * The selected category.
54      */

55     private String JavaDoc selected;
56     
57     /**
58      * Default constructor.
59      */

60     public CategorySelector()
61     {
62         super();
63     }
64
65     /**
66      * Process the end tag. Creates the option elements and writes the select
67      * element to the output stream.
68      *
69      * @return indication of whether to continue evaluating the JSP page.
70      * @throws JspException if an I/O error occurs when writing to the output stream.
71      */

72     public int doEndTag() throws JspException JavaDoc
73     {
74         createOptions();
75         rootCategoryVO = null;
76         selected = null;
77         return super.doEndTag();
78     }
79     
80     /**
81      * Creates the option elements.
82      */

83     private void createOptions()
84     {
85         for(final Iterator JavaDoc i = rootCategoryVO.getChildren().iterator(); i.hasNext();)
86         {
87             final CategoryVO categoryVO = (CategoryVO) i.next();
88             final String JavaDoc name = categoryVO.getName();
89             final String JavaDoc 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     /**
99      * Creates the element to use when constructing this tag.
100      *
101      * @return the element to use when constructing this tag.
102      */

103     protected Element createElement()
104     {
105         return new Element("select");
106     }
107     
108     /**
109      * Sets the label of the first option element.
110      *
111      * @param label the label to use.
112      */

113     public void setDefaultLabel(final String JavaDoc label)
114     {
115         getElement().addChildFirst("option").addText(label);
116     }
117     
118     /**
119      * Sets the path of the root category.
120      *
121      * @param path the path to use.
122      * @throws JspException if an error occurs when fetching the category.
123      */

124     public void setCategoryPath(final String JavaDoc path) throws JspException JavaDoc
125     {
126         rootCategoryVO = getRootCategory(path);
127     }
128
129     /**
130      * Sets the name attribute of the select element to the specified value.
131      *
132      * @param name the name to use.
133      */

134     public void setName(final String JavaDoc name)
135     {
136         getElement().addAttribute("name", name);
137         selected = getPropertySet().getDataString(name);
138     }
139     
140     /**
141      * Finds the category with the specified path.
142      *
143      * @return the category value object with the children populated.
144      * @throws JspException if an error occurs when fetching the category.
145      */

146     private CategoryVO getRootCategory(final String JavaDoc path) throws JspException JavaDoc
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 JavaDoc("No category with path " + path + " was found.");
155         }
156         catch(Exception JavaDoc e)
157         {
158             e.printStackTrace();
159             throw new JspTagException JavaDoc(e.getMessage());
160         }
161     }
162 }
163
Popular Tags