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.util;
042:
043: import com.sun.rave.designtime.Constants;
044: import com.sun.rave.faces.event.Action;
045:
046: import java.beans.BeanInfo;
047: import java.beans.EventSetDescriptor;
048: import java.beans.PropertyDescriptor;
049: import java.lang.reflect.Method;
050: import java.util.regex.Matcher;
051: import java.util.regex.Pattern;
052:
053: import javax.faces.component.UIComponent;
054: import javax.faces.context.FacesContext;
055: import javax.faces.event.ValueChangeEvent;
056: import javax.faces.event.ValueChangeListener;
057: import javax.faces.validator.Validator;
058:
059: /**
060: * Miscellaneous design-time utility methods
061: *
062: * @author Edwin Goei
063: * @author gjmurphy
064: */
065: public class DesignUtil {
066:
067: private static Pattern numericalSuffixPattern = Pattern
068: .compile("\\d*$");
069:
070: /**
071: * A utility method that locates the numerical suffix of a typical bean
072: * instance name, and returns it. If no numerical suffix is found the
073: * empty string is returned.
074: */
075: public static String getNumericalSuffix(String name) {
076: Matcher matcher = numericalSuffixPattern.matcher(name);
077: matcher.find();
078: return matcher.group();
079: }
080:
081: /**
082: * A utility method that creates <code>validate</code> and <code>valueChange</code>
083: * event descriptors for input components (components that implement {@link
084: * javax.faces.component.EditableValueHolder}). The events are associated with
085: * their respective JSF handler methods (<code>Validator.validate()</code> and
086: * <code>ValueChangeListener.processValueChange()</coded>), and with their respective
087: * properties (<code>valueChangeListener</code> and <code>validator</code>). As
088: * a result, if the user selects a new handler for the event in the properties sheet,
089: * a method with the correct signature will be created, and the corresponding
090: * property's value will be set to a method binding expression that points to
091: * the newly created method.
092: */
093: public static EventSetDescriptor[] generateInputEventSetDescriptors(
094: BeanInfo beanInfo) {
095: try {
096: PropertyDescriptor valueChangeDescriptor = null;
097: PropertyDescriptor validateDescriptor = null;
098: PropertyDescriptor[] propertyDescriptors = beanInfo
099: .getPropertyDescriptors();
100: for (int i = 0; i < propertyDescriptors.length
101: && (valueChangeDescriptor == null || validateDescriptor == null); i++) {
102: if (propertyDescriptors[i].getName().equals(
103: "valueChangeListener")) //NOI18N
104: valueChangeDescriptor = propertyDescriptors[i];
105: else if (propertyDescriptors[i].getName().equals(
106: "validator")) //NOI18N
107: validateDescriptor = propertyDescriptors[i];
108: }
109: EventSetDescriptor valueChangeEventDescriptor = new EventSetDescriptor(
110: "valueChangeListener",
111: ValueChangeListener.class, //NOI18N
112: new Method[] { ValueChangeListener.class.getMethod(
113: "processValueChange", //NOI18N
114: new Class[] { ValueChangeEvent.class }) },
115: null, null);
116: valueChangeEventDescriptor.setDisplayName(DesignMessageUtil
117: .getMessage(DesignUtil.class,
118: "DesignUtil.event.valueChange")); //NOI18N
119: valueChangeEventDescriptor.setValue(
120: Constants.EventSetDescriptor.BINDING_PROPERTY,
121: valueChangeDescriptor);
122: valueChangeEventDescriptor.setValue(
123: Constants.EventDescriptor.PARAMETER_NAMES,
124: new String[] { "event" }); //NOI18N
125: valueChangeEventDescriptor
126: .setShortDescription(valueChangeDescriptor
127: .getShortDescription());
128: EventSetDescriptor validateEventDescriptor = new EventSetDescriptor(
129: "validator",
130: Validator.class, //NOI18N
131: new Method[] { Validator.class.getMethod(
132: "validate", //NOI18N
133: new Class[] { FacesContext.class,
134: UIComponent.class, Object.class }) },
135: null, null);
136: validateEventDescriptor.setDisplayName(DesignMessageUtil
137: .getMessage(DesignUtil.class,
138: "DesignUtil.event.validate"));
139: validateEventDescriptor.setValue(
140: Constants.EventSetDescriptor.BINDING_PROPERTY,
141: validateDescriptor);
142: validateEventDescriptor.setValue(
143: Constants.EventDescriptor.PARAMETER_NAMES,
144: new String[] { "context", "component", "value" }); //NOI18N
145: validateEventDescriptor
146: .setValue(
147: Constants.EventDescriptor.REQUIRED_IMPORTS,
148: new String[] {
149: "javax.faces.application.FacesMessage",
150: "javax.faces.validator.ValidatorException" }); //NOI18N
151: validateEventDescriptor
152: .setShortDescription(validateDescriptor
153: .getShortDescription());
154: return new EventSetDescriptor[] {
155: valueChangeEventDescriptor, validateEventDescriptor };
156: } catch (Exception e) {
157: e.printStackTrace();
158: return null;
159: }
160: }
161:
162: /**
163: * A utility method that creates an <code>action</code> event descriptors for
164: * command components (components that implement {@link
165: * javax.faces.component.ActionSource}). The event is associated with its
166: * respective JSF handler method (<code>ActionListener.processAction()</coded>),
167: * and with its respective property (<code>action</code>). As a result, if
168: * the user selects a new handler for the event in the properties sheet,
169: * a method with the correct signature will be created, and the corresponding
170: * property's value will be set to a method binding expression that points to
171: * the newly created method.
172: */
173: public static EventSetDescriptor[] generateCommandEventSetDescriptors(
174: BeanInfo beanInfo) {
175: try {
176: PropertyDescriptor actionDescriptor = null;
177: PropertyDescriptor[] propertyDescriptors = beanInfo
178: .getPropertyDescriptors();
179: for (int i = 0; i < propertyDescriptors.length
180: && actionDescriptor == null; i++) {
181: if (propertyDescriptors[i].getName().equals("action")) //NOI18N
182: actionDescriptor = propertyDescriptors[i];
183: }
184: EventSetDescriptor actionEventDescriptor = new EventSetDescriptor(
185: "action", Action.class, //NOI18N
186: new Method[] { Action.class.getMethod("action",
187: new Class[] {}) }, //NOI18N
188: null, null);
189: actionEventDescriptor.setDisplayName(DesignMessageUtil
190: .getMessage(DesignUtil.class,
191: "DesignUtil.event.action"));
192: actionEventDescriptor.setValue(
193: Constants.EventSetDescriptor.BINDING_PROPERTY,
194: actionDescriptor);
195: actionEventDescriptor.setShortDescription(actionDescriptor
196: .getShortDescription());
197: return new EventSetDescriptor[] { actionEventDescriptor };
198: } catch (Exception e) {
199: e.printStackTrace();
200: return null;
201: }
202: }
203:
204: }
|