KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > event > handlers > basic > UtilHandlers


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 /*
24  * UtilHandlers.java
25  *
26  * Created on December 2, 2004, 3:06 AM
27  */

28 package com.sun.enterprise.tools.jsfext.event.handlers.basic;
29
30 import com.sun.enterprise.tools.jsfext.event.handlers.HandlerContext;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35
36 import javax.faces.component.UIComponent;
37
38
39 /**
40  * <p> This class contains
41  * {@link com.sun.enterprise.tools.jsfext.event.handlers.Handler}
42  * methods that perform common utility-type functions.</p>
43  *
44  * @author Ken Paulsen (ken.paulsen@sun.com)
45  */

46 public class UtilHandlers {
47
48     /**
49      * <p> Default Constructor.</p>
50      */

51     public UtilHandlers() {
52     }
53
54     /**
55      * <p> This handler writes using <CODE>System.out.println</CODE>. It
56      * requires that <code>value</code> be supplied as a String input
57      * parameter.</p>
58      *
59      * @param context The HandlerContext.
60      */

61     public void println(HandlerContext context) {
62     String JavaDoc value = (String JavaDoc) context.getInputValue("value");
63     System.out.println(value);
64     }
65
66     /**
67      * <p> This handler decrements a number by 1. This handler requires
68      * "number" to be supplied as an Integer input value. It sets an
69      * output value "value" to number-1.</p>
70      *
71      * @param context The HandlerContext.
72      */

73     public void dec(HandlerContext context) {
74     Integer JavaDoc value = (Integer JavaDoc) context.getInputValue("number");
75     context.setOutputValue("value", new Integer JavaDoc(value.intValue() - 1));
76     }
77
78     /**
79      * <p> This handler increments a number by 1. This handler requires
80      * "number" to be supplied as an Integer input value. It sets an
81      * output value "value" to number+1.</p>
82      *
83      * @param context The HandlerContext.
84      */

85     public void inc(HandlerContext context) {
86     Integer JavaDoc value = (Integer JavaDoc) context.getInputValue("number");
87     context.setOutputValue("value", new Integer JavaDoc(value.intValue() + 1));
88     }
89
90     /**
91      * <p> This handler sets a request attribute. It requires "key" and
92      * "value" input values to be passed in.</p>
93      *
94      * @param context The HandlerContext.
95      */

96     public void setAttribute(HandlerContext context) {
97     String JavaDoc key = (String JavaDoc) context.getInputValue("key");
98     Object JavaDoc value = context.getInputValue("value");
99     context.getFacesContext().getExternalContext().
100         getRequestMap().put(key, value);
101     }
102
103     /**
104      * <p> This method returns an <code>Iterator</code> for the given
105      * <code>List</code>. The <code>List</code> input value key is:
106      * "list". The output value key for the <code>Iterator</code> is:
107      * "iterator".</p>
108      *
109      * @param context The HandlerContext.
110      */

111     public void getIterator(HandlerContext context) {
112     List JavaDoc list = (List JavaDoc) context.getInputValue("list");
113     context.setOutputValue("iterator", list.iterator());
114     }
115
116     /**
117      * <p> This method returns a <code>Boolean</code> value representing
118      * whether another value exists for the given <code>Iterator</code>.
119      * The <code>Iterator</code> input value key is: "iterator". The
120      * output value key is "hasNext".</p>
121      *
122      * @param context The HandlerContext.
123      */

124     public void iteratorHasNext(HandlerContext context) {
125     Iterator JavaDoc it = (Iterator JavaDoc) context.getInputValue("iterator");
126     context.setOutputValue("hasNext", Boolean.valueOf(it.hasNext()));
127     }
128
129     /**
130      * <p> This method returns the next object in the <code>List</code> that
131      * the given <code>Iterator</code> is iterating over. The
132      * <code>Iterator</code> input value key is: "iterator". The
133      * output value key is "next".</p>
134      *
135      * @param context The HandlerContext.
136      */

137     public void iteratorNext(HandlerContext context) {
138     Iterator JavaDoc it = (Iterator JavaDoc) context.getInputValue("iterator");
139     context.setOutputValue("next", it.next());
140     }
141
142     /**
143      * <p> This method creates a List. Optionally you may supply "size" to
144      * create a List of blank "" values of the specified size. The
145      * output value from this handler is "result".</p>
146      *
147      * @param context The HandlerContext
148      */

149     public void createList(HandlerContext context) {
150     int size = ((Integer JavaDoc) context.getInputValue("size")).intValue();
151     List JavaDoc list = new ArrayList JavaDoc(size);
152     for (int count = 0; count < size; count++) {
153         list.add("");
154     }
155     context.setOutputValue("result", list);
156     }
157
158     /**
159      * <p> This method returns true. It does not take any input or provide
160      * any output values.</p>
161      *
162      * @param context The {@link HandlerContext}
163      */

164     public boolean returnTrue(HandlerContext context) {
165     return true;
166     }
167
168     /**
169      * <p> This method returns false. It does not take any input or provide
170      * any output values.</p>
171      *
172      * @param context The {@link HandlerContext}
173      */

174     public boolean returnFalse(HandlerContext context) {
175     return false;
176     }
177
178     /**
179      * <p> This method enables you to retrieve the clientId for the given
180      * <code>UIComponent</code>.</p>
181      *
182      * @param context The {@link HandlerContext}
183      */

184     public void getClientId(HandlerContext context) {
185     UIComponent comp = (UIComponent) context.getInputValue("component");
186     context.setOutputValue("clientId",
187         comp.getClientId(context.getFacesContext()));
188     }
189
190     /**
191      * <p> This method enables you to retrieve the id or clientId for the given
192      * <code>Object</code> which is expected to be a
193      * <code>UIComponent</code> or a <code>String</code> that already
194      * represents the clientId.</p>
195      *
196      * @param context The {@link HandlerContext}
197      */

198     public void getId(HandlerContext context) {
199     Object JavaDoc obj = context.getInputValue("object");
200     if (obj == null) {
201         return;
202     }
203
204     String JavaDoc clientId = null;
205     String JavaDoc id = null;
206     if (obj instanceof UIComponent) {
207         clientId =
208         ((UIComponent) obj).getClientId(context.getFacesContext());
209         id = ((UIComponent) obj).getId();
210     } else {
211         clientId = obj.toString();
212         id = clientId.substring(clientId.lastIndexOf(':') + 1);
213     }
214     context.setOutputValue("id", id);
215     context.setOutputValue("clientId", clientId);
216     }
217 }
218
Popular Tags