KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > layout > descriptor > LayoutMarkup


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 package com.sun.enterprise.tools.jsfext.layout.descriptor;
24
25 import com.sun.enterprise.tools.jsfext.component.ComponentUtil;
26 import com.sun.enterprise.tools.jsfext.event.handlers.Handler;
27 import com.sun.enterprise.tools.jsfext.event.handlers.HandlerContext;
28 import com.sun.enterprise.tools.jsfext.event.handlers.HandlerDefinition;
29
30 import java.io.IOException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32
33 import javax.faces.component.UIComponent;
34 import javax.faces.context.FacesContext;
35 import javax.faces.context.ResponseWriter;
36
37
38 /**
39  * <p> This class defines a LayoutMarkup. A LayoutMarkup provides a means to
40  * start a markup tag and associate the current UIComponent with it for
41  * tool support. It also has the benefit of properly closing the markup
42  * tag for you.</p>
43  *
44  * @author Ken Paulsen (ken.paulsen@sun.com)
45  */

46 public class LayoutMarkup extends LayoutElementBase implements LayoutElement {
47
48     /**
49      * <p> Constructor.</p>
50      */

51     public LayoutMarkup(LayoutElement parent, String JavaDoc tag, String JavaDoc type) {
52     super(parent, tag);
53     _tag = tag;
54     _type = type;
55
56     // Add "afterEncode" handler to close the tag (if there is a close tag)
57
if (!type.equals(TYPE_OPEN)) {
58         ArrayList JavaDoc handlers = new ArrayList JavaDoc();
59         handlers.add(afterEncodeHandler);
60         setHandlers(AFTER_ENCODE, handlers);
61     }
62     }
63
64     /**
65      *
66      */

67     public String JavaDoc getTag() {
68     return _tag;
69     }
70
71     /**
72      *
73      */

74     public String JavaDoc getType() {
75     return _type;
76     }
77
78     /**
79      * <p> This method displays the text described by this component. If the
80      * text includes an EL expression, it will be evaluated. It returns
81      * true to render children.</p>
82      *
83      * @param context The <code>FacesContext</code>
84      * @param component The <code>UIComponent</code>
85      *
86      * @return false
87      */

88     protected boolean encodeThis(FacesContext context, UIComponent component) throws IOException JavaDoc {
89     if (getType().equals(TYPE_CLOSE)) {
90         return true;
91     }
92
93     // Get the ResponseWriter
94
ResponseWriter writer = context.getResponseWriter();
95
96     // Render...
97
Object JavaDoc value = resolveValue(context, component, getTag());
98     if (value != null) {
99         writer.startElement(value.toString(), component);
100     }
101
102     // Always render children
103
return true;
104     }
105
106     /**
107      * <p> This handler takes care of closing the tag.</p>
108      *
109      * @param context The HandlerContext.
110      */

111     public static void afterEncodeHandler(HandlerContext context) throws IOException JavaDoc {
112     ResponseWriter writer = context.getFacesContext().getResponseWriter();
113     LayoutMarkup markup = (LayoutMarkup) context.getLayoutElement();
114     Object JavaDoc value = ComponentUtil.resolveValue(context.getFacesContext(),
115         markup, (UIComponent) context.getEventObject().getSource(),
116         markup.getTag());
117     if (value != null) {
118         writer.endElement(value.toString());
119     }
120     }
121
122     /**
123      *
124      */

125     public static final HandlerDefinition afterEncodeHandlerDef =
126     new HandlerDefinition("_markupAfterEncode");
127
128     /**
129      *
130      */

131     public static final Handler afterEncodeHandler =
132     new Handler(afterEncodeHandlerDef);
133
134     static {
135     afterEncodeHandlerDef.setHandlerMethod(
136         LayoutMarkup.class.getName(), "afterEncodeHandler");
137     }
138
139     /**
140      * <p> This markup type writes out both the opening and closing tags.</p>
141      */

142     public static final String JavaDoc TYPE_BOTH = "both";
143
144     /**
145      * <p> This markup type writes out the closing tag.</p>
146      */

147     public static final String JavaDoc TYPE_CLOSE = "close";
148
149     /**
150      * <p> This markup type writes out the opening tag.</p>
151      */

152     public static final String JavaDoc TYPE_OPEN = "open";
153
154     private String JavaDoc _tag = null;
155     private String JavaDoc _type = null;
156 }
157
Popular Tags