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.descriptors;
042:
043: import com.sun.rave.web.ui.component.util.Util;
044: import com.sun.rave.web.ui.component.util.event.Handler;
045: import com.sun.rave.web.ui.component.util.event.HandlerContext;
046: import com.sun.rave.web.ui.component.util.event.HandlerDefinition;
047:
048: import java.io.IOException;
049: import java.util.ArrayList;
050: import java.util.List;
051:
052: import javax.faces.component.UIComponent;
053: import javax.faces.context.FacesContext;
054: import javax.faces.context.ResponseWriter;
055:
056: /**
057: * <p> This class defines a LayoutMarkup. A LayoutMarkup provides a means to
058: * start a markup tag and associate the current UIComponent with it for
059: * tool support. It also has the benefit of properly closing the markup
060: * tag for you.</p>
061: *
062: * @author Ken Paulsen (ken.paulsen@sun.com)
063: */
064: public class LayoutMarkup extends LayoutElementBase implements
065: LayoutElement {
066:
067: /**
068: * <P> Constructor.</P>
069: */
070: public LayoutMarkup(LayoutElement parent, String tag, String type) {
071: super (parent, tag);
072: _tag = tag;
073: _type = type;
074:
075: // Add "afterEncode" handler to close the tag (if there is a close tag)
076: if (!type.equals(TYPE_OPEN)) {
077: List handlers = new ArrayList();
078: handlers.add(afterEncodeHandler);
079: setHandlers(AFTER_ENCODE, handlers);
080: }
081: }
082:
083: /**
084: *
085: */
086: public String getTag() {
087: return _tag;
088: }
089:
090: /**
091: *
092: */
093: public String getType() {
094: return _type;
095: }
096:
097: /**
098: * <P> This method displays the text described by this component. If the
099: * text includes an EL expression, it will be evaluated. It returns
100: * true to render children.</P>
101: *
102: * @param context The <code>FacesContext</code>
103: * @param component The <code>UIComponent</code>
104: *
105: * @return false
106: */
107: protected boolean encodeThis(FacesContext context,
108: UIComponent component) throws IOException {
109: if (getType().equals(TYPE_CLOSE)) {
110: return true;
111: }
112:
113: // Get the ResponseWriter
114: ResponseWriter writer = context.getResponseWriter();
115:
116: // Render...
117: Object value = resolveValue(context, component, getTag());
118: if (value != null) {
119: writer.startElement(value.toString(), component);
120: }
121:
122: // Always render children
123: return true;
124: }
125:
126: /**
127: * <p> This handler takes care of closing the tag.</p>
128: *
129: * @param context The HandlerContext.
130: */
131: public static void afterEncodeHandler(HandlerContext context)
132: throws IOException {
133: ResponseWriter writer = context.getFacesContext()
134: .getResponseWriter();
135: LayoutMarkup markup = (LayoutMarkup) context.getLayoutElement();
136: Object value = Util.resolveValue(context.getFacesContext(),
137: markup, (UIComponent) context.getEventObject()
138: .getSource(), markup.getTag());
139: if (value != null) {
140: writer.endElement(value.toString());
141: }
142: }
143:
144: /**
145: *
146: */
147: public static final HandlerDefinition afterEncodeHandlerDef = new HandlerDefinition(
148: "_markupAfterEncode");
149:
150: /**
151: *
152: */
153: public static final Handler afterEncodeHandler = new Handler(
154: afterEncodeHandlerDef);
155:
156: static {
157: afterEncodeHandlerDef.setHandlerMethod(LayoutMarkup.class
158: .getName(), "afterEncodeHandler");
159: }
160:
161: /**
162: * <p> This markup type writes out both the opening and closing tags.</p>
163: */
164: public static final String TYPE_BOTH = "both";
165:
166: /**
167: * <p> This markup type writes out the closing tag.</p>
168: */
169: public static final String TYPE_CLOSE = "close";
170:
171: /**
172: * <p> This markup type writes out the opening tag.</p>
173: */
174: public static final String TYPE_OPEN = "open";
175:
176: private String _tag = null;
177: private String _type = null;
178: }
|