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;
042:
043: import java.io.IOException;
044:
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048:
049: import javax.faces.context.FacesContext;
050: import javax.faces.component.UIComponent;
051:
052: /**
053: *
054: * @author Ken Paulsen
055: */
056: public class PropertySheet extends PropertySheetBase {
057:
058: /**
059: * Constructor.
060: */
061: public PropertySheet() {
062: super ();
063: }
064:
065: /**
066: * <p> This method calculates the number of visible
067: * {@link PropertySheetSection}s. A {@link PropertySheetSection} can
068: * be made not visible by setting its rendered propety to false. It
069: * is also considered not visible if it contains no children
070: * (sub-sections or properties).</p>
071: *
072: * @return The number of visible sections.
073: */
074: public int getSectionCount() {
075: // Return the answer
076: return getVisibleSections().size();
077: }
078:
079: /**
080: * <p> This method creates a <code>List</code> of visible (rendered=true)
081: * {@link PropertySheetSection} components.
082: * {@link PropertySheetSection}s must also contain some content to be
083: * considered visible.</p>
084: *
085: * @return A <code>List</code> of visible {@link PropertySheetSection}
086: * objects.
087: */
088: public List getVisibleSections() {
089: int numChildren = getChildCount();
090:
091: // See if we've already figured this out
092: if ((_visibleSections != null) && (_childCount == numChildren)) {
093: return _visibleSections;
094: }
095: _childCount = numChildren;
096:
097: // Make sure we have children
098: if (numChildren == 0) {
099: _visibleSections = new ArrayList(0);
100: return _visibleSections;
101: }
102:
103: // Add the visible sections to the result List
104: UIComponent child = null;
105: List visibleSections = new ArrayList();
106: Iterator it = getChildren().iterator();
107: while (it.hasNext()) {
108: child = (UIComponent) it.next();
109: if ((child instanceof PropertySheetSection)
110: && child.isRendered()) {
111: if (((PropertySheetSection) child)
112: .getVisibleSectionChildren().size() > 0) {
113: visibleSections.add(child);
114: }
115: }
116: }
117:
118: // Return the visible PropertySheetSections
119: _visibleSections = visibleSections;
120: return _visibleSections;
121: }
122:
123: /**
124: * <p> Used to cache the visible sections.</p>
125: */
126: private transient List _visibleSections = null;
127: private transient int _childCount = -1;
128:
129: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130: // UIComponent methods
131: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132:
133: /**
134: * If the rendered property is true, render the begining of the current
135: * state of this UIComponent to the response contained in the specified
136: * FacesContext.
137: *
138: * If a Renderer is associated with this UIComponent, the actual encoding
139: * will be delegated to Renderer.encodeBegin(FacesContext, UIComponent).
140: *
141: * @param context FacesContext for the current request.
142: *
143: * @exception IOException if an input/output error occurs while rendering.
144: * @exception NullPointerException if FacesContext is null.
145: */
146: public void encodeBegin(FacesContext context) throws IOException {
147: // Clear cached variables -- bugtraq #6270214.
148: _visibleSections = null;
149: _childCount = -1;
150: super.encodeBegin(context);
151: }
152: }
|