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.propertyeditors.domains;
042:
043: import java.util.Iterator;
044: import java.util.TreeSet;
045: import com.sun.data.provider.FieldKey;
046: import com.sun.data.provider.DataProvider;
047: import com.sun.rave.designtime.DesignProperty;
048:
049: /**
050: * Domain of field keys available for this property's data provider.
051: */
052: public class FieldKeyDomain extends AttachedDomain {
053:
054: public static final String DATA_PROVIDER_PROPERTY_NAME = "dataProviderPropertyName";
055:
056: /**
057: *
058: */
059: public Element[] getElements() {
060:
061: DesignProperty prop = getDesignProperty();
062:
063: // If we have not been attached yet, there is nothing we can do
064: // except return an empty list
065: if (prop == null) {
066: return Element.EMPTY_ARRAY;
067: }
068:
069: TreeSet set = new TreeSet();
070:
071: String dpPropName = (String) prop.getPropertyDescriptor()
072: .getValue(DATA_PROVIDER_PROPERTY_NAME);
073:
074: // see if the DATA_PROVIDER_PROPERTY_NAME key has been set on the
075: // PropertyDescriptor for this property. If so, we can fetch the keys
076: // from the DataProvider set on that property
077: if (dpPropName != null && dpPropName.length() > 0) {
078: DesignProperty dpProp = prop.getDesignBean().getProperty(
079: dpPropName);
080: if (dpProp != null
081: && DataProvider.class.isAssignableFrom(dpProp
082: .getPropertyDescriptor().getPropertyType())) {
083: DataProvider dp = (DataProvider) dpProp.getValue();
084: if (dp != null) {
085: try {
086: FieldKey[] keys = dp.getFieldKeys();
087: for (int j = 0; j < keys.length; j++) {
088: if (!set.contains(keys[j])) {
089: set.add(keys[j]);
090: }
091: }
092: } catch (Exception exc) {
093: exc.printStackTrace();
094: }
095: }
096: }
097:
098: } else {
099:
100: // see if the DATA_PROVIDER_PROPERTY_NAME key has NOT been set on the
101: // PropertyDescriptor for this property, scan all the properties on
102: // the bean for properties of type DataProvider and fetch all the keys
103: DesignProperty[] props = prop.getDesignBean()
104: .getProperties();
105: for (int i = 0; i < props.length; i++) {
106: Class propType = props[i].getPropertyDescriptor()
107: .getPropertyType();
108: if (DataProvider.class.isAssignableFrom(propType)) {
109: DataProvider dp = (DataProvider) props[i]
110: .getValue();
111: if (dp != null) {
112: try {
113: FieldKey[] keys = dp.getFieldKeys();
114: for (int j = 0; j < keys.length; j++) {
115: if (!set.contains(keys[j])) {
116: set.add(keys[j]);
117: }
118: }
119: } catch (Exception exc) {
120: exc.printStackTrace();
121: }
122: }
123: }
124: }
125: }
126:
127: // Construct a list of elements of the found keys
128: Element elements[] = new Element[set.size()];
129: Iterator keys = set.iterator();
130: int n = 0;
131: while (keys.hasNext()) {
132: FieldKey key = (FieldKey) keys.next();
133: elements[n++] = new Element(key.getFieldId(), key
134: .getDisplayName());
135: }
136: return elements;
137:
138: }
139: }
|