001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.component.util.handlers;
042:
043: import com.sun.rave.web.ui.component.util.event.HandlerContext;
044: import com.sun.rave.web.ui.util.RenderingUtilities;
045: import com.sun.rave.web.ui.util.ThemeUtilities;
046:
047: import java.io.IOException;
048: import java.util.ArrayList;
049: import java.util.Iterator;
050: import java.util.List;
051:
052: import javax.faces.component.UIComponent;
053: import javax.faces.context.FacesContext;
054:
055: /**
056: * <p> This class contains {@link com.sun.rave.web.ui.component.util.event.Handler}
057: * methods that perform common utility-type functions.</p>
058: *
059: * @author Ken Paulsen (ken.paulsen@sun.com)
060: */
061: public class UtilHandlers {
062:
063: /**
064: * <P> Default Constructor.</P>
065: */
066: public UtilHandlers() {
067: }
068:
069: /**
070: * <p> This handler writes using <CODE>System.out.println</CODE>. It
071: * requires that <code>value</code> be supplied as a String input
072: * parameter.</p>
073: *
074: * @param context The HandlerContext.
075: */
076: public void println(HandlerContext context) {
077: String value = (String) context.getInputValue("value");
078: System.out.println(value);
079: }
080:
081: /**
082: * <p> This handler decrements a number by 1. This handler requires
083: * "number" to be supplied as an Integer input value. It sets an
084: * output value "value" to number-1.</p>
085: *
086: * @param context The HandlerContext.
087: */
088: public void dec(HandlerContext context) {
089: Integer value = (Integer) context.getInputValue("number");
090: context.setOutputValue("value", new Integer(
091: value.intValue() - 1));
092: }
093:
094: /**
095: * <p> This handler increments a number by 1. This handler requires
096: * "number" to be supplied as an Integer input value. It sets an
097: * output value "value" to number+1.</p>
098: *
099: * @param context The HandlerContext.
100: */
101: public void inc(HandlerContext context) {
102: Integer value = (Integer) context.getInputValue("number");
103: context.setOutputValue("value", new Integer(
104: value.intValue() + 1));
105: }
106:
107: /**
108: * <p> This handler sets a request attribute. It requires "key" and
109: * "value" input values to be passed in.</p>
110: *
111: * @param context The HandlerContext.
112: */
113: public void setAttribute(HandlerContext context) {
114: String key = (String) context.getInputValue("key");
115: Object value = context.getInputValue("value");
116: context.getFacesContext().getExternalContext().getRequestMap()
117: .put(key, value);
118: }
119:
120: /**
121: * <p> This method returns an <code>Iterator</code> for the given
122: * <code>List</code>. The <code>List</code> input value key is:
123: * "list". The output value key for the <code>Iterator</code> is:
124: * "iterator".</p>
125: *
126: * @param context The HandlerContext.
127: */
128: public void getIterator(HandlerContext context) {
129: List list = (List) context.getInputValue("list");
130: context.setOutputValue("iterator", list.iterator());
131: }
132:
133: /**
134: * <p> This method returns a <code>Boolean</code> value representing
135: * whether another value exists for the given <code>Iterator</code>.
136: * The <code>Iterator</code> input value key is: "iterator". The
137: * output value key is "hasNext".</p>
138: *
139: * @param context The HandlerContext.
140: */
141: public void iteratorHasNext(HandlerContext context) {
142: Iterator it = (Iterator) context.getInputValue("iterator");
143: context
144: .setOutputValue("hasNext", Boolean
145: .valueOf(it.hasNext()));
146: }
147:
148: /**
149: * <p> This method returns the next object in the <code>List</code> that
150: * the given <code>Iterator</code> is iterating over. The
151: * <code>Iterator</code> input value key is: "iterator". The
152: * output value key is "next".</p>
153: *
154: * @param context The HandlerContext.
155: */
156: public void iteratorNext(HandlerContext context) {
157: Iterator it = (Iterator) context.getInputValue("iterator");
158: context.setOutputValue("next", it.next());
159: }
160:
161: /**
162: * <p> This method creates a List. Optionally you may supply "size" to
163: * create a List of blank "" values of the specified size. The
164: * output value from this handler is "result".</p>
165: *
166: * @param context The HandlerContext
167: */
168: public void createList(HandlerContext context) {
169: int size = ((Integer) context.getInputValue("size")).intValue();
170: List list = new ArrayList(size);
171: for (int count = 0; count < size; count++) {
172: list.add("");
173: }
174: context.setOutputValue("result", list);
175: }
176:
177: /**
178: * <p> This method returns true. It does not take any input or provide
179: * any output values.</p>
180: *
181: * @param context The {@link HandlerContext}
182: */
183: public boolean returnTrue(HandlerContext context) {
184: return true;
185: }
186:
187: /**
188: * <p> This method returns false. It does not take any input or provide
189: * any output values.</p>
190: *
191: * @param context The {@link HandlerContext}
192: */
193: public boolean returnFalse(HandlerContext context) {
194: return false;
195: }
196:
197: /**
198: * <p> This method enables you to retrieve the clientId for the given
199: * <code>UIComponent</code>.</p>
200: *
201: * @param context The {@link HandlerContext}
202: */
203: public void getClientId(HandlerContext context) {
204: UIComponent comp = (UIComponent) context
205: .getInputValue("component");
206: context.setOutputValue("clientId", comp.getClientId(context
207: .getFacesContext()));
208: }
209:
210: /**
211: * <p> This method enables you to retrieve the id or clientId for the given
212: * <code>Object</code> which is expected to be a
213: * <code>UIComponent</code> or a <code>String</code> that already
214: * represents the clientId.</p>
215: *
216: * @param context The {@link HandlerContext}
217: */
218: public void getId(HandlerContext context) {
219: Object obj = context.getInputValue("object");
220: if (obj == null) {
221: return;
222: }
223:
224: String clientId = null;
225: String id = null;
226: if (obj instanceof UIComponent) {
227: clientId = ((UIComponent) obj).getClientId(context
228: .getFacesContext());
229: id = ((UIComponent) obj).getId();
230: } else {
231: clientId = obj.toString();
232: id = clientId.substring(clientId.lastIndexOf(':') + 1);
233: }
234: context.setOutputValue("id", id);
235: context.setOutputValue("clientId", clientId);
236: }
237:
238: /**
239: * Render skip hyperlink -- see bugtarq #6329543.
240: *
241: * @param context The {@link HandlerContext}
242: */
243: public static void startSkipHyperlink(HandlerContext context)
244: throws IOException {
245: UIComponent component = (UIComponent) context
246: .getInputValue("component"); //NOI18N
247: String baseId = (String) context.getInputValue("baseID"); //NOI18N
248:
249: FacesContext fc = context.getFacesContext();
250: RenderingUtilities.renderSkipLink("skipHyperlink", null, null, //NOI18N
251: ThemeUtilities.getTheme(fc).getMessage(
252: "tree.skipTagAltText"), //NOI18N
253: null, component, fc);
254: }
255:
256: /**
257: * Render skip anchor -- see bugtarq #6329543.
258: *
259: * @param context The {@link HandlerContext}
260: */
261: public static void endSkipHyperlink(HandlerContext context)
262: throws IOException {
263: UIComponent component = (UIComponent) context
264: .getInputValue("component"); //NOI18N
265: String baseId = (String) context.getInputValue("baseID"); //NOI18N
266:
267: RenderingUtilities.renderAnchor("skipHyperlink", component, //NOI18N
268: context.getFacesContext());
269: }
270: }
|