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.jsfcl.std;
042:
043: import java.awt.Component;
044: import java.beans.PropertyEditorSupport;
045: import javax.faces.application.Application;
046: import javax.faces.component.UIComponent;
047: import javax.faces.context.FacesContext;
048: import javax.faces.el.MethodBinding;
049: import javax.faces.validator.DoubleRangeValidator;
050: import javax.faces.validator.LengthValidator;
051: import javax.faces.validator.LongRangeValidator;
052: import javax.faces.validator.Validator;
053: import com.sun.jsfcl.util.ComponentBundle;
054: import com.sun.rave.designtime.DesignBean;
055: import com.sun.rave.designtime.DesignProperty;
056: import com.sun.rave.designtime.PropertyEditor2;
057: import com.sun.rave.designtime.faces.FacesDesignContext;
058: import com.sun.rave.designtime.faces.ResolveResult;
059: import org.openide.explorer.propertysheet.ExPropertyEditor;
060: import org.openide.explorer.propertysheet.PropertyEnv;
061:
062: /**
063: * @deprecated
064: */
065: public class ValidatorRefPropertyEditor extends PropertyEditorSupport
066: implements PropertyEditor2 {
067:
068: private static final ComponentBundle bundle = ComponentBundle
069: .getBundle(ValidatorRefPropertyEditor.class);
070:
071: private DesignProperty liveProperty;
072: private PropertyEnv propertyEnv;
073:
074: /* (non-Javadoc)
075: * @see java.beans.PropertyEditor#getValue()
076: */
077: public Object getValue() {
078: Object value = super .getValue();
079: return value;
080: }
081:
082: /* (non-Javadoc)
083: * @see java.beans.PropertyEditor#setValue(java.lang.Object)
084: */
085: public void setValue(Object v) {
086: super .setValue(v);
087: }
088:
089: private static final Class[] classes = new Class[] {
090: DoubleRangeValidator.class, LengthValidator.class,
091: LongRangeValidator.class };
092:
093: private static final String[] prettyClasses = new String[] {
094: bundle.getMessage("parenNewDRV"), //NOI18N
095: bundle.getMessage("parenNewLV"), //NOI18N
096: bundle.getMessage("parenNewLRV") //NOI18N
097: };
098:
099: public String[] getTags() {
100: DesignBean[] lbeans = getValidatorBeans();
101: String[] tags = new String[lbeans.length + prettyClasses.length
102: + 1];
103:
104: int index = 0;
105: tags[index++] = "";
106: for (int i = 0; i < lbeans.length; i++) {
107: tags[index++] = lbeans[i].getInstanceName();
108: }
109: for (int i = 0; i < prettyClasses.length; i++) {
110: tags[index++] = prettyClasses[i];
111: }
112: return tags;
113: }
114:
115: private void setValidatorMethodBinding(String binding) {
116: FacesDesignContext fctx = (FacesDesignContext) liveProperty
117: .getDesignBean().getDesignContext();
118: Application app = fctx.getFacesContext().getApplication();
119: MethodBinding mb = app.createMethodBinding(binding,
120: new Class[] { FacesContext.class, UIComponent.class,
121: Object.class });
122: setValue(mb);
123: }
124:
125: private void setValidatorBeanBinding(DesignBean sourceBean) {
126: FacesDesignContext fctx = (FacesDesignContext) liveProperty
127: .getDesignBean().getDesignContext();
128: String binding = fctx.getBindingExpr(sourceBean, ".validate"); //NOI18N
129: setValidatorMethodBinding(binding);
130: }
131:
132: public void setAsText(String text) throws IllegalArgumentException {
133:
134: if (text == null || text.trim().length() == 0) {
135: // A hack to prevent user from overwriting a binding that is persisting
136: // the validate event handler
137: Object value = this .getValue();
138: if (liveProperty != null && value != null
139: && value instanceof MethodBinding) {
140: FacesDesignContext designContext = (FacesDesignContext) liveProperty
141: .getDesignBean().getDesignContext();
142: ResolveResult result = designContext
143: .resolveBindingExprToBean(((MethodBinding) value)
144: .getExpressionString());
145: DesignBean lbean = result.getDesignBean();
146: if (!(lbean.getInstance() instanceof Validator))
147: return;
148: }
149: setValue(null);
150: return;
151: }
152:
153: if (text.startsWith("#{")) { //NOI18N
154: setValidatorMethodBinding(text);
155: } else {
156: DesignBean[] lbeans = getValidatorBeans();
157: for (int i = 0; i < lbeans.length; i++) {
158: if (lbeans[i].getInstanceName().equals(text)) {
159: setValidatorBeanBinding(lbeans[i]);
160: return;
161: }
162: }
163: for (int i = 0; i < prettyClasses.length; i++) {
164: if (prettyClasses[i].equals(text)) {
165: DesignBean lbean = liveProperty.getDesignBean()
166: .getDesignContext().createBean(
167: classes[i].getName(), null, null);
168: if (lbean != null) {
169: setValidatorBeanBinding(lbean);
170: return;
171: }
172: }
173: }
174: }
175: }
176:
177: public String getAsText() {
178: Object value = getValue();
179: if (value instanceof MethodBinding) {
180: String expression = ((MethodBinding) value)
181: .getExpressionString();
182: // If the method binding expression refers to a method on a validator
183: // bean, return just the bean's instance name. Otherwise, the method
184: // binding must refer to a validate method defined on a page, request,
185: // session, or application bean. In this case return the expression.
186: if (liveProperty != null) {
187: FacesDesignContext designContext = (FacesDesignContext) liveProperty
188: .getDesignBean().getDesignContext();
189: ResolveResult result = designContext
190: .resolveBindingExprToBean(expression);
191: DesignBean lbean = result.getDesignBean();
192: if (lbean.getInstance() instanceof Validator)
193: return lbean.getInstanceName();
194: return expression;
195: }
196: }
197: return (value == null) ? "" : value.toString(); //NOI18N
198: }
199:
200: public String getJavaInitializationString() {
201: return "null"; //NOI18N
202: }
203:
204: public boolean supportsCustomEditor() {
205: return false;
206: }
207:
208: public Component getCustomEditor() {
209: return null;
210: }
211:
212: public void setDesignProperty(DesignProperty liveProperty) {
213: this .liveProperty = liveProperty;
214: }
215:
216: private DesignBean[] getValidatorBeans() {
217: return (liveProperty == null) ? new DesignBean[0]
218: : liveProperty.getDesignBean().getDesignContext()
219: .getBeansOfType(Validator.class);
220: }
221:
222: }
|