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.insync.live;
042:
043: import java.beans.PropertyDescriptor;
044: import java.beans.PropertyEditorSupport;
045:
046: import org.netbeans.modules.visualweb.extension.openide.util.Trace;
047:
048: import com.sun.rave.designtime.DesignBean;
049: import com.sun.rave.designtime.DesignContext;
050: import com.sun.rave.designtime.DesignProperty;
051:
052: /**
053: *
054: */
055: public class BeanSelectionEditor extends PropertyEditorSupport {
056:
057: final DesignProperty property;
058: DesignBean[] liveBeans;
059: int selection;
060:
061: public BeanSelectionEditor(DesignProperty property) {
062: this .property = property;
063: assert Trace.trace("insync.live", "BSE.BSE: " + property);
064:
065: // lookup current DesignBean in list from current property value
066: setValue(property.getValue());
067: }
068:
069: // load up-to-date list of all sibling beans
070: private void loadList() {
071: assert Trace.trace("insync.live", "BSE.loadList");
072: DesignBean bean = property.getDesignBean();
073: DesignContext context = bean.getDesignContext();
074: PropertyDescriptor pd = property.getPropertyDescriptor();
075: Class type = pd.getPropertyType();
076: liveBeans = context.getBeansOfType(type);
077: selection = -1;
078: }
079:
080: public String getAsText() {
081: String text = selection >= 0 ? liveBeans[selection]
082: .getInstanceName() : "";
083: assert Trace.trace("insync.live", "BSE.getAsText: " + text);
084: return text;
085: }
086:
087: public String getJavaInitializationString() {
088: String init = selection >= 0 ? liveBeans[selection]
089: .getInstanceName() : "";
090: assert Trace.trace("insync.live",
091: "BSE.getJavaInitializationString: " + init);
092: return init;
093: }
094:
095: public String[] getTags() {
096: String[] tags = new String[liveBeans.length];
097: for (int i = 0; i < liveBeans.length; i++)
098: tags[i] = liveBeans[i].getInstanceName();
099: return tags;
100: }
101:
102: public Object getValue() {
103: Object value = selection >= 0 ? liveBeans[selection]
104: .getInstance() : null;
105: assert Trace.trace("insync.live", "BSE.getValue: " + value);
106: return value;
107: }
108:
109: public void setAsText(String text) {
110: assert Trace.trace("insync.live", "BSE.setAsText: " + text);
111: loadList();
112: for (int i = 0; i < liveBeans.length; i++) {
113: if (liveBeans[i].getInstanceName().equals(text)) {
114: selection = i;
115: return;
116: }
117: }
118: //!CQ could throw an invalid arg exception
119: }
120:
121: public void setValue(Object value) {
122: assert Trace.trace("insync.live", "BSE.setValue: " + value);
123: loadList();
124: for (int i = 0; i < liveBeans.length; i++) {
125: if (liveBeans[i].getInstance() == value) {
126: selection = i;
127: return;
128: }
129: }
130: //!CQ could throw an invalid arg exception
131: }
132:
133: }
|