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.component.propertyeditors;
042:
043: import com.sun.rave.designtime.DesignBean;
044: import java.util.Iterator;
045: import java.util.Set;
046: import java.util.TreeSet;
047: import javax.faces.component.UIComponent;
048: import com.sun.rave.designtime.DesignContext;
049: import com.sun.rave.designtime.faces.FacesDesignContext;
050: import com.sun.rave.propertyeditors.domains.AttachedDomain;
051: import com.sun.rave.propertyeditors.domains.Element;
052: import javax.faces.context.FacesContext;
053:
054: /**
055: * Domain of identifiers of components in scope that meet the criteria of the
056: * abstract method <code>isDomainComponent(UIComponent)</code>.
057: */
058: public abstract class ClientIdsDomain extends AttachedDomain {
059:
060: /**
061: * Returns a list of all the non-<code>null</code> component client identifiers.
062: */
063: public Element[] getElements() {
064:
065: // If we have not been attached yet, there is nothing we can do
066: // except return an empty list
067: if (getDesignProperty() == null) {
068: return Element.EMPTY_ARRAY;
069: }
070:
071: // Scan all the beans on this page and accumulate identifiers of all
072: // of them that implement ActionSource or EditableValueHolder
073: Set set = new TreeSet();
074: DesignBean designBeans[] = getDesignProperty().getDesignBean()
075: .getDesignContext().getBeansOfType(UIComponent.class);
076: if (designBeans == null) {
077: return Element.EMPTY_ARRAY;
078: }
079: for (int i = 0; i < designBeans.length; i++) {
080: Object instance = designBeans[i].getInstance();
081: if (instance instanceof UIComponent
082: && isDomainComponent((UIComponent) instance)) {
083: String id = getId(designBeans[i]);
084: if (id != null)
085: set.add(id);
086: }
087: }
088:
089: // Construct a list of elements of the retained identifiers
090: Element elements[] = new Element[set.size()];
091: Iterator ids = set.iterator();
092: int n = 0;
093: while (ids.hasNext()) {
094: elements[n++] = new Element((String) ids.next());
095: }
096: return elements;
097:
098: }
099:
100: protected abstract boolean isDomainComponent(UIComponent component);
101:
102: protected String getId(DesignBean bean) {
103: if (hasTableParent(bean)) {
104: return null;
105: }
106: Object instance = bean.getInstance();
107: if (!(instance instanceof UIComponent)) {
108: return null;
109: }
110: UIComponent uic = (UIComponent) instance;
111: DesignContext dcontext = getDesignProperty().getDesignBean()
112: .getDesignContext();
113: FacesContext fcontext = ((FacesDesignContext) dcontext)
114: .getFacesContext();
115: return uic.getClientId(fcontext);
116: }
117:
118: private boolean hasTableParent(DesignBean bean) {
119: DesignBean parent = bean.getBeanParent();
120: if (parent == null) {
121: return false;
122: }
123: Object instance = parent.getInstance();
124: if (instance instanceof javax.faces.component.UIData
125: || instance instanceof com.sun.rave.web.ui.component.TableRowGroup) {
126: return true;
127: }
128: return hasTableParent(parent);
129: }
130:
131: }
|