KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > swap > SwapTag


1 package fr.improve.struts.taglib.layout.swap;
2
3 import java.util.StringTokenizer JavaDoc;
4
5 import javax.servlet.jsp.JspException JavaDoc;
6
7 import org.apache.struts.taglib.html.OptionsTag;
8 import org.apache.struts.taglib.html.SelectTag;
9
10 import fr.improve.struts.taglib.layout.ButtonTag;
11 import fr.improve.struts.taglib.layout.LayoutTag;
12 import fr.improve.struts.taglib.layout.LayoutTagSupport;
13 import fr.improve.struts.taglib.layout.collection.CollectionTag;
14 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
15 import fr.improve.struts.taglib.layout.event.LayoutEventListener;
16 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
17
18 /**
19  * @author jnribette
20  */

21 public class SwapTag extends LayoutTagSupport implements LayoutEventListener {
22     private static final String JavaDoc SWAP_KEY = "fr.improve.struts.taglib.layout.swap.SwapTag.SWAP_KEY";
23     private SelectTag selectTag = new SelectTag();
24     private OptionsTag optionsTag = new OptionsTag();
25     private ButtonTag buttonTag = new ButtonTag();
26     private StringTokenizer JavaDoc propertyTokenizer;
27     private StringTokenizer JavaDoc formPropertyTokenizer;
28     private boolean first = true;
29         
30     protected String JavaDoc selectedStyleClass = null;
31     protected int currentCollectionNumber = 0;
32     protected String JavaDoc property = null;
33     protected String JavaDoc formProperty = null;
34             
35     /**
36      * Start the tag.
37      * Generate an id for your collection.
38      */

39     public int doStartLayoutTag() throws JspException JavaDoc {
40         Integer JavaDoc lc_int = (Integer JavaDoc) pageContext.getRequest().getAttribute(SWAP_KEY);
41         if (lc_int==null) {
42             currentCollectionNumber = 0;
43         } else {
44             currentCollectionNumber = lc_int.intValue();
45         }
46         propertyTokenizer = new StringTokenizer JavaDoc(property, ",");
47         formPropertyTokenizer = new StringTokenizer JavaDoc(formProperty, ",");
48         return EVAL_BODY_INCLUDE;
49     }
50     
51     /**
52      * End the tag.
53      * Reset it.
54      */

55     public int doEndLayoutTag() throws JspException JavaDoc {
56         pageContext.getRequest().setAttribute(SWAP_KEY, new Integer JavaDoc(currentCollectionNumber));
57
58         propertyTokenizer = null;
59         formPropertyTokenizer = null;
60         first = true;
61         return EVAL_PAGE;
62     }
63     
64     /**
65      * Release the tag.
66      */

67     public void release() {
68         super.release();
69         selectTag.release();
70         optionsTag.release();
71         
72         selectedStyleClass = null;
73         property = null;
74         formProperty = null;
75     }
76     
77     /**
78      * Called by inner tag to inform this tag of what is going on.
79      */

80     public Object JavaDoc processStartLayoutEvent(StartLayoutEvent in_event) throws JspException JavaDoc {
81         LayoutTag lc_tag = in_event.getSource();
82         if (lc_tag instanceof CollectionTag) {
83             // Set the onRowClick and styleId of the collection.
84
CollectionTag lc_collectionTag = (CollectionTag) lc_tag;
85             lc_collectionTag.setOnRowClick(new StringBuffer JavaDoc("prepareSwap(this, '").append(selectedStyleClass).append("')").toString());
86             lc_collectionTag.setStyleId("stdLayoutSwap" + currentCollectionNumber);
87             if (!first) {
88                 doPrintSwapButtons();
89             }
90         }
91         return new StartLayoutEvent(lc_tag, in_event.getValue()).sendToParent(this);
92     }
93     /**
94      * Called by inner tag to inform this tag of what is going on.
95      */

96     public Object JavaDoc processEndLayoutEvent(EndLayoutEvent in_event) throws JspException JavaDoc {
97         LayoutTag lc_tag = in_event.getSource();
98         if (lc_tag instanceof CollectionTag) {
99             CollectionTag lc_collectionTag = (CollectionTag) lc_tag;
100
101             doPrintHiddenSelect(lc_collectionTag);
102
103             first = false;
104             currentCollectionNumber++;
105         }
106         return new EndLayoutEvent(lc_tag, in_event.getValue()).sendToParent(this);
107     }
108
109     /**
110      * Print an hidden select tag.
111      */

112     private void doPrintHiddenSelect(CollectionTag in_collectionTag) throws JspException JavaDoc {
113         if (!propertyTokenizer.hasMoreTokens() || !formPropertyTokenizer.hasMoreTokens()) {
114             throw new JspException JavaDoc("<layout:swap>: Bad formProperty / property value");
115         }
116         
117         selectTag.setPageContext(pageContext);
118         selectTag.setParent(this);
119         selectTag.setProperty(formPropertyTokenizer.nextToken());
120         selectTag.setStyleId("stdLayoutSwapHS" + currentCollectionNumber);
121         selectTag.setMultiple("true");
122         selectTag.setStyle("display:none");
123         selectTag.doStartTag();
124                     
125         optionsTag.setPageContext(pageContext);
126         optionsTag.setParent(this);
127         optionsTag.setCollection(in_collectionTag.getName());
128         optionsTag.setProperty(propertyTokenizer.nextToken());
129         optionsTag.doStartTag();
130         optionsTag.doEndTag();
131                     
132         selectTag.doEndTag();
133     }
134     
135     /**
136      * Print two buttons to swap items betwwen the two collections.
137      */

138     private void doPrintSwapButtons() throws JspException JavaDoc {
139         buttonTag.setPageContext(pageContext);
140         buttonTag.setParent(this);
141         buttonTag.setValign("top");
142         buttonTag.setOnclick("swap('stdLayoutSwap" + currentCollectionNumber + "', 'stdLayoutSwapHS" + currentCollectionNumber + "', 'stdLayoutSwap" + (currentCollectionNumber-1) + "', 'stdLayoutSwapHS" + (currentCollectionNumber-1) + "')");
143         buttonTag.setValue("<<");
144         buttonTag.doStartTag();
145         buttonTag.doEndTag();
146
147         buttonTag.setOnclick("swap('stdLayoutSwap" + (currentCollectionNumber-1) + "', 'stdLayoutSwapHS" + (currentCollectionNumber-1) + "', 'stdLayoutSwap" + currentCollectionNumber + "', 'stdLayoutSwapHS" + currentCollectionNumber + "')");
148         buttonTag.setValue(">>");
149         buttonTag.doStartTag();
150         buttonTag.doEndTag();
151     }
152
153     /**
154      * Sets the selectedStyleClass.
155      * @param selectedStyleClass The selectedStyleClass to set
156      */

157     public void setSelectedStyleClass(String JavaDoc selectedStyleClass) {
158         this.selectedStyleClass = selectedStyleClass;
159     }
160
161     /**
162      * Sets the formProperty.
163      * @param formProperty The formProperty to set
164      */

165     public void setFormProperty(String JavaDoc formProperty) {
166         this.formProperty = formProperty;
167     }
168
169     /**
170      * Sets the property.
171      * @param property The property to set
172      */

173     public void setProperty(String JavaDoc property) {
174         this.property = property;
175     }
176
177 }
178
Popular Tags