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 org.netbeans.modules.visualweb.web.ui.dt.renderer;
042:
043: import com.sun.rave.web.ui.component.Breadcrumbs;
044: import com.sun.rave.web.ui.component.Hyperlink;
045: import com.sun.rave.web.ui.component.ImageHyperlink;
046: import org.netbeans.modules.visualweb.web.ui.dt.component.util.DesignMessageUtil;
047: import org.netbeans.modules.visualweb.web.ui.dt.component.util.DesignMessageUtil;
048: import com.sun.rave.web.ui.renderer.BreadcrumbsRenderer;
049: import java.io.IOException;
050: import java.util.List;
051: import javax.faces.component.UIComponent;
052: import javax.faces.context.FacesContext;
053: import javax.faces.context.ResponseWriter;
054: import javax.faces.el.ValueBinding;
055:
056: /**
057: * A delegating renderer for {@link org.netbeans.modules.visualweb.web.ui.component.Breadcrumbs}. If
058: * the breadcrumbs has no children hyperlinks, then a minimual block of markup
059: * is output. If the breadcrummbs has children, any of them are missing both
060: * text and content will have their text temporarily set to the display name
061: * of the hyperlink component. If the breadcrumbs is bound to an array or list
062: * of hyperlinks, and design-time evaluation of the bound property returns an
063: * empty value, a default set of dummy hyperlinks is temporarily added to the
064: * component while it renders.
065: *
066: * @author gjmurphy
067: */
068: public class BreadcrumbsDesignTimeRenderer extends
069: AbstractDesignTimeRenderer {
070:
071: static int DUMMY_PAGES_SET = 1;
072: static int COMPONENT_SHADOWED = 2;
073: static int LINK_CHILDREN_SHADOWED = 3;
074:
075: int rendererStatus;
076:
077: public BreadcrumbsDesignTimeRenderer() {
078: super (new BreadcrumbsRenderer());
079: }
080:
081: public void encodeBegin(FacesContext context, UIComponent component)
082: throws IOException {
083: rendererStatus = 0;
084: if (component instanceof Breadcrumbs) {
085: if (component.getValueBinding("pages") != null) {
086: ValueBinding pagesBinding = component
087: .getValueBinding("pages");
088: Object value = pagesBinding.getValue(context);
089: if (value != null && value instanceof Hyperlink[]
090: && ((Hyperlink[]) value).length == 0) {
091: ((Breadcrumbs) component)
092: .setPages(getDummyHyperlinks());
093: rendererStatus = DUMMY_PAGES_SET;
094: }
095: } else if (component.getChildCount() == 0) {
096: ResponseWriter writer = context.getResponseWriter();
097: writer.startElement("div", component);
098: String style = ((Breadcrumbs) component).getStyle();
099: writer.writeAttribute("style", style, "style");
100: writer.startElement("span", component);
101: writer.writeAttribute("class",
102: super .UNINITITIALIZED_STYLE_CLASS, "class");
103: String label = DesignMessageUtil.getMessage(
104: BreadcrumbsDesignTimeRenderer.class,
105: "breadcrumbs.label");
106: char[] chars = label.toCharArray();
107: writer.writeText(chars, 0, chars.length);
108: writer.endElement("span");
109: writer.endElement("div");
110: rendererStatus = COMPONENT_SHADOWED;
111: } else {
112: List children = component.getChildren();
113: int i = children.size() - 1;
114: if (Hyperlink.class.isAssignableFrom(children.get(i)
115: .getClass())) {
116: Hyperlink link = (Hyperlink) children.get(i);
117: if (link.getText() == null
118: && link.getChildCount() == 0) {
119: if (link instanceof ImageHyperlink)
120: link
121: .setText(DesignMessageUtil
122: .getMessage(
123: BreadcrumbsDesignTimeRenderer.class,
124: "imageHyperlink.label"));
125: else
126: link
127: .setText(DesignMessageUtil
128: .getMessage(
129: BreadcrumbsDesignTimeRenderer.class,
130: "hyperlink.label"));
131: link.setStyleClass(addStyleClass(link
132: .getStyleClass(),
133: UNINITITIALIZED_STYLE_CLASS));
134: rendererStatus = LINK_CHILDREN_SHADOWED;
135: }
136: }
137: }
138: }
139: if (rendererStatus != COMPONENT_SHADOWED) {
140: super .encodeBegin(context, component);
141: }
142: }
143:
144: public void encodeEnd(FacesContext context, UIComponent component)
145: throws IOException {
146: if (rendererStatus != COMPONENT_SHADOWED) {
147: super .encodeEnd(context, component);
148: }
149: if (rendererStatus == DUMMY_PAGES_SET) {
150: ((Breadcrumbs) component).setPages(null);
151: } else if (rendererStatus == LINK_CHILDREN_SHADOWED) {
152: List children = component.getChildren();
153: int i = children.size() - 1;
154: Hyperlink link = (Hyperlink) children.get(i);
155: link.setText(null);
156: link.setStyleClass(removeStyleClass(link.getStyleClass(),
157: UNINITITIALIZED_STYLE_CLASS));
158: }
159: rendererStatus = 0;
160: }
161:
162: static Hyperlink[] dummyHyperlinks;
163:
164: static Hyperlink[] getDummyHyperlinks() {
165: if (dummyHyperlinks == null) {
166: Hyperlink dummyHyperlink = new Hyperlink();
167: String dummyText = getDummyData(String.class).toString();
168: dummyHyperlink.setText(dummyText);
169: dummyHyperlinks = new Hyperlink[] { dummyHyperlink,
170: dummyHyperlink, dummyHyperlink };
171: }
172: return dummyHyperlinks;
173: }
174:
175: }
|