Source Code Cross Referenced for BeanPatternGenerator.java in  » IDE-Netbeans » beans » org » netbeans » modules » beans » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » beans » org.netbeans.modules.beans 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003:         *
0004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005:         *
0006:         * The contents of this file are subject to the terms of either the GNU
0007:         * General Public License Version 2 only ("GPL") or the Common
0008:         * Development and Distribution License("CDDL") (collectively, the
0009:         * "License"). You may not use this file except in compliance with the
0010:         * License. You can obtain a copy of the License at
0011:         * http://www.netbeans.org/cddl-gplv2.html
0012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013:         * specific language governing permissions and limitations under the
0014:         * License.  When distributing the software, include this License Header
0015:         * Notice in each file and include the License file at
0016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
0017:         * particular file as subject to the "Classpath" exception as provided
0018:         * by Sun in the GPL Version 2 section of the License file that
0019:         * accompanied this code. If applicable, add the following below the
0020:         * License Header, with the fields enclosed by brackets [] replaced by
0021:         * your own identifying information:
0022:         * "Portions Copyrighted [year] [name of copyright owner]"
0023:         *
0024:         * Contributor(s):
0025:         *
0026:         * The Original Software is NetBeans. The Initial Developer of the Original
0027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
0028:         * Microsystems, Inc. All Rights Reserved.
0029:         *
0030:         * If you wish your version of this file to be governed by only the CDDL
0031:         * or only the GPL Version 2, indicate your decision by adding
0032:         * "[Contributor] elects to include this software in this distribution
0033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034:         * single choice of license, a recipient has the option to distribute
0035:         * your version of this file under either the CDDL, the GPL Version 2 or
0036:         * to extend the choice of license to its licensees as provided above.
0037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
0038:         * Version 2 license, then the option applies only if the new code is
0039:         * made subject to such option by the copyright holder.
0040:         */
0041:
0042:        package org.netbeans.modules.beans;
0043:
0044:        import java.lang.reflect.Modifier;
0045:        import java.beans.Introspector;
0046:        import java.text.MessageFormat;
0047:        import java.util.List;
0048:        import java.util.Iterator;
0049:        import java.util.Collections;
0050:        import java.util.ArrayList;
0051:
0052:        /** Singleton with static methods for generating bodies of and
0053:         * additional elements for bean patterns.
0054:         * @author Petr Hrebejk
0055:         */
0056:        final class BeanPatternGenerator {
0057:            //    private static final String THIS_QUALIFIER = "this."; // NOI18N
0058:            //    /** Constant for one Tab */
0059:            //    private static final String TAB = "  "; // NOI18N
0060:            //    /** Constant for two Tabs */
0061:            //    private static final String TABx2 = TAB + TAB;
0062:            //    /** Constant for three Tabs */
0063:            //    private static final String TABx3 = TABx2 + TAB;
0064:            //
0065:            //    /**
0066:            //     * Helper method; creates a suitable string for referencing an instance field.
0067:            //     * @param base the base name to create the string from
0068:            //     * @param adjustName 
0069:            //     */
0070:            //    static String createFieldName(String base, boolean adjustName, boolean paramClash) {
0071:            //        if (!adjustName) {
0072:            //            if (!paramClash)
0073:            //                return base;
0074:            //            else
0075:            //                return new StringBuffer(THIS_QUALIFIER).append(base).toString();
0076:            //        } else {
0077:            //            String propertyStyle = PropertyActionSettings.getDefault().getPropStyle();
0078:            //            return  new StringBuffer(propertyStyle).append(base).toString();
0079:            //        }
0080:            //    }
0081:            //
0082:            //    /** Generates the body of the setter method of Property.
0083:            //     * @param name Name of the property
0084:            //     * @param type Type of the property
0085:            //     * @param bound Is the property bound?
0086:            //     * @param constrained Is the property constrained?
0087:            //     * @param withSet Should be the set command of property private field generated.
0088:            //     * @param withSupport Generate the firing of (Veto|Property)Change Events?
0089:            //     * @param supportName Name of field containing <CODE>PropertyChangeListeners</CODE>.
0090:            //     * @param vetoSupportName Name of field containing <CODE>VetoableChangeListeners</CODE>.
0091:            //     * @return Sring containing the body of the setter method.
0092:            //     */
0093:            //    static String propertySetterBody(String name, Type type,
0094:            //                                     boolean bound, boolean constrained,
0095:            //                                     boolean withSet, boolean withSupport,
0096:            //                                     String supportName, String vetoSupportName) throws JmiException {
0097:            //         return propertySetterBody(name, type, bound, constrained, withSet, withSupport, supportName,
0098:            //            vetoSupportName, true);
0099:            //    }
0100:            //    
0101:            //    static String propertySetterBody(String name, Type type,
0102:            //                                     boolean bound, boolean constrained,
0103:            //                                     boolean withSet, boolean withSupport,
0104:            //                                     String supportName, String vetoSupportName, boolean adjustName) throws JmiException {
0105:            //        assert JMIUtils.isInsideTrans();
0106:            //        StringBuffer setterBody = new StringBuffer( 200 );
0107:            //        String decoratedName = createFieldName(name, adjustName, true);
0108:            //        
0109:            //        setterBody.append( "\n" ); // NOI18N
0110:            //        if ( withSupport ) {
0111:            //            /* Generates body in the form:
0112:            //               PropType oldPropName = this.propName;
0113:            //               this.propName = propName;
0114:            //               changes.firePropertyChange(propName, oldPropName, propName );
0115:            //            */
0116:            //
0117:            //            setterBody.append( TAB + type.getName() );
0118:            //            setterBody.append( " old" ).append( Pattern.capitalizeFirstLetter( name ) ); // NOI18N
0119:            //            setterBody.append( " = " ).append( decoratedName ).append( ";\n"); // NOI18N
0120:            //
0121:            //            if ( constrained ) {
0122:            //                setterBody.append( TAB + vetoSupportName ).append( ".fireVetoableChange(\"").append( name ).append( "\", " ); // NOI18N
0123:            //
0124:            //                if ( type instanceof PrimitiveType) {
0125:            //                    setterBody.append( "new ").append( getWrapperClassName( type )).append( " (" ); // NOI18N
0126:            //                    setterBody.append( "old" ).append( Pattern.capitalizeFirstLetter( name ) ); // NOI18N
0127:            //                    setterBody.append( "), " ); // NOI18N
0128:            //                    setterBody.append( "new ").append( getWrapperClassName( type )).append( " (" ); // NOI18N
0129:            //                    setterBody.append( name ).append( "));\n" ); // NOI18N
0130:            //                }
0131:            //                else {
0132:            //                    setterBody.append( "old" ).append( Pattern.capitalizeFirstLetter( name ) ); // NOI18N
0133:            //                    setterBody.append( ", " ).append( name ).append( ");\n" ); // NOI18N
0134:            //                }
0135:            //                if ( !bound ) {
0136:            //                    setterBody.append( TAB ).append( decoratedName ); // NOI18N
0137:            //                    setterBody.append( " = " ).append( name ).append( ";\n"); // NOI18N
0138:            //                }
0139:            //            }
0140:            //            if ( bound ) {
0141:            //                setterBody.append( TAB ).append( decoratedName ); // NOI18N
0142:            //                setterBody.append( " = " ).append( name ).append( ";\n"); // NOI18N
0143:            //                setterBody.append( TAB + supportName ).append( ".firePropertyChange (\"").append( name ).append( "\", " ); // NOI18N
0144:            //
0145:            //                if ( type instanceof PrimitiveType) {
0146:            //                    setterBody.append( "new ").append( getWrapperClassName( type )).append( " (" ); // NOI18N
0147:            //                    setterBody.append( "old" ).append( Pattern.capitalizeFirstLetter( name ) ); // NOI18N
0148:            //                    setterBody.append( "), " ); // NOI18N
0149:            //                    setterBody.append( "new ").append( getWrapperClassName( type )).append( " (" ); // NOI18N
0150:            //                    setterBody.append( name ).append( "));\n" ); // NOI18N
0151:            //                }
0152:            //                else {
0153:            //                    setterBody.append( "old" ).append( Pattern.capitalizeFirstLetter( name ) ); // NOI18N
0154:            //                    setterBody.append( ", " ).append( name ).append( ");\n" ); // NOI18N
0155:            //                }
0156:            //            }
0157:            //        }
0158:            //        else if ( withSet ) {
0159:            //            /* Generates body in the form:
0160:            //               this.propName = propName;
0161:            //             */
0162:            //            setterBody.append( TAB ).append( decoratedName ); // NOI18N
0163:            //            setterBody.append( " = " ).append( name ).append( ";\n" ); // NOI18N
0164:            //        }
0165:            //        return setterBody.toString();
0166:            //    }
0167:            //
0168:            //    /** Generates the body of the setter method of IndexedProperty.
0169:            //     * @param name Name of the property
0170:            //     * @param indexedType Indexed type of the property
0171:            //     * @param bound Is the property bound?
0172:            //     * @param constrained Is the property constrained?
0173:            //     * @param withSet Should be the set command of property private field generated.
0174:            //     * @param withSupport Generate the firing of (Veto|Property)Change Events?
0175:            //     * @param supportName Name of field containing <CODE>PropertyChangeListeners</CODE>.
0176:            //     * @param vetoSupportName Name of field containing <CODE>VetoableChangeListeners</CODE>.
0177:            //     * @return Sring containing the body of the setter method.
0178:            //     */
0179:            //    static String idxPropertySetterBody( String name, Type indexedType,
0180:            //                                         boolean bound, boolean constrained,
0181:            //                                         boolean withSet, boolean withSupport,
0182:            //                                         String supportName,
0183:            //                                         String vetoSupportName ) throws JmiException {
0184:            //        return idxPropertySetterBody(name, indexedType, bound, constrained,
0185:            //            withSet, withSupport, supportName, vetoSupportName, true);
0186:            //    }
0187:            //    
0188:            //    static String idxPropertySetterBody( String name, Type indexedType,
0189:            //                                         boolean bound, boolean constrained,
0190:            //                                         boolean withSet, boolean withSupport,
0191:            //                                         String supportName,
0192:            //                                         String vetoSupportName, boolean adjustName ) throws JmiException {
0193:            //        assert JMIUtils.isInsideTrans();
0194:            //        StringBuffer setterBody = new StringBuffer( 200 );
0195:            //        String decoratedName = createFieldName(name, adjustName, true);
0196:            //        setterBody.append( "\n" ); // NOI18N
0197:            //
0198:            //        if ( withSupport && constrained ) {
0199:            //
0200:            //            setterBody.append( TAB + indexedType.getName() );
0201:            //            setterBody.append( " old" ).append( Pattern.capitalizeFirstLetter( name ) ); // NOI18N
0202:            //            setterBody.append( " = " ).append( decoratedName ); // NOI18N
0203:            //            setterBody.append( "[index];\n"); // NOI18N
0204:            //        }
0205:            //
0206:            //        if ( withSet || withSupport ) {
0207:            //            /* Generates body in the form:
0208:            //               this.propName = propName;
0209:            //            */
0210:            //            setterBody.append( TAB ).append( decoratedName ); // NOI18N
0211:            //            setterBody.append( "[index] = " ).append( name ).append( ";\n" ); // NOI18N
0212:            //        }
0213:            //
0214:            //        if ( withSupport && constrained ) {
0215:            //            setterBody.append( TAB + "try {\n" ); // NOI18N
0216:            //            setterBody.append( TABx2 + vetoSupportName ).append( ".fireVetoableChange (\"").append( name ).append( "\", " ); // NOI18N
0217:            //            setterBody.append( "null, null );\n" ); // NOI18N
0218:            //            setterBody.append( TAB + "}\n" ); // NOI18N
0219:            //            setterBody.append( TAB + "catch(java.beans.PropertyVetoException vetoException ) {\n" ); //NOI18N
0220:            //            setterBody.append( TABx2 ).append( decoratedName ); // NOI18N
0221:            //            setterBody.append( "[index] = old").append( Pattern.capitalizeFirstLetter( name ) ).append( ";\n" ) ; // NOI18N
0222:            //            setterBody.append( TABx2 + "throw vetoException;\n" ); //NOI18N
0223:            //            setterBody.append( TAB  + "}\n" ); //NOI18N
0224:            //        }
0225:            //
0226:            //        if ( withSupport && bound ) {
0227:            //            setterBody.append( TAB + supportName ).append( ".firePropertyChange (\"").append( name ).append( "\", " ); // NOI18N
0228:            //            setterBody.append( "null, null );\n" ); // NOI18N
0229:            //        }
0230:            //
0231:            //        return setterBody.toString();
0232:            //    }
0233:            //    
0234:            //    static String propertyGetterBody( String name, boolean withReturn ) {
0235:            //        return propertyGetterBody(name, withReturn, true);
0236:            //    }
0237:            //
0238:            //    /** Generates the body of the getter method of Property.
0239:            //     * @param name Name of the property.
0240:            //     * @param withReturn Should be the return command with property private field generated?
0241:            //     * @return Sring containing the body of the getter method.
0242:            //     */
0243:            //    static String propertyGetterBody( String name, boolean withReturn, boolean adjustName ) {
0244:            //        StringBuffer getterBody = new StringBuffer( 50 );
0245:            //        String decorated = createFieldName(name, adjustName, false);
0246:            //        getterBody.append( "\n"); // NOI18N
0247:            //        if ( withReturn ) {
0248:            //            /* Generates body in the form:
0249:            //               return propName;
0250:            //             */
0251:            //            getterBody.append( TAB + "return " ); // NOI18N
0252:            //            getterBody.append( decorated ).append( ";\n" ); // NOI18N
0253:            //        }
0254:            //        return getterBody.toString();
0255:            //    }
0256:            //    
0257:            //    /** Generates the body of the getter method of IndexedProperty.
0258:            //     * @param name Name of the property.
0259:            //     * @param withReturn Should be the return command with property private field generated?
0260:            //     * @return Sring containing the body of the getter method.
0261:            //     */
0262:            //    static String idxPropertyGetterBody( String name, boolean withReturn ) {
0263:            //        return idxPropertyGetterBody(name, withReturn, true);
0264:            //    }
0265:            //
0266:            //    static String idxPropertyGetterBody( String name, boolean withReturn, boolean adjustName ) {
0267:            //        StringBuffer getterBody = new StringBuffer( 50 );
0268:            //        String decorated = createFieldName(name, adjustName, false);
0269:            //        getterBody.append( "\n"); // NOI18N
0270:            //        if ( withReturn ) {
0271:            //            /* Generates body in the form:
0272:            //               return propName;
0273:            //             */
0274:            //            getterBody.append( TAB + "return " ); // NOI18N
0275:            //            getterBody.append( decorated ).append( "[index];\n" ); // NOI18N
0276:            //        }
0277:            //        return getterBody.toString();
0278:            //    }
0279:            //
0280:            //
0281:            //    /** Gets the <CODE>PropertyChangeSupport</CODE> field in Class. Tryes to find
0282:            //     * a field of type <CODE>PropertyChangeSupport</CODE>. If such field doesn't
0283:            //     * exist creates a new one with name <CODE>propertyChangeSupport</CODE>.
0284:            //     * @param ce Class to operate on.
0285:            //     * @throws JmiException If the modification of the source is impossible.
0286:            //     * @return Name of foun or newly created <CODE>PropertyChangeSupport</CODE> field.
0287:            //     */
0288:            //    static String supportField(JavaClass ce) throws JmiException {
0289:            //        assert JMIUtils.isInsideTrans();
0290:            //        String supportName = null;
0291:            //        JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(ce);
0292:            //        Type pcsType = jmodel.getType().resolve("java.beans.PropertyChangeSupport"); // NOI18N
0293:            //        List/*<Field>*/ fields = JMIUtils.getFields(ce);
0294:            //        
0295:            //        for (Iterator it = fields.iterator(); it.hasNext();) {
0296:            //            Field field = (Field) it.next();
0297:            //            if (JMIUtils.equalTypes(pcsType, field.getType())) {
0298:            //                supportName = field.getName();
0299:            //                break;
0300:            //            }
0301:            //        }
0302:            //
0303:            //        if ( supportName == null ) { // Field not found we create new
0304:            //            supportName = findFreeFieldName(ce, "propertyChangeSupport"); // NOI18N
0305:            //            Field supportField = jmodel.getField().createField();
0306:            //            supportField.setName(supportName);
0307:            //            supportField.setType(pcsType);
0308:            //            supportField.setModifiers( Modifier.PRIVATE );
0309:            //            supportField.setInitialValueText( " new java.beans.PropertyChangeSupport(this)" ); // NOI18N
0310:            //            supportField.setJavadocText( PatternNode.getString( "COMMENT_PropertyChangeSupport" ) );
0311:            //            ce.getFeatures().add( supportField );
0312:            //        }
0313:            //
0314:            //        return supportName;
0315:            //    }
0316:            //
0317:            //    /** Gets the <CODE>VetoableChangeSupport</CODE> field in Class. Tryes to find
0318:            //     * a field of type <CODE>VetoableChangeSupport</CODE>. If such field doesn't
0319:            //     * exist creates a new one with name <CODE>vetoableChangeSupport</CODE>.
0320:            //     * @param ce Class to operate on.
0321:            //     * @throws JmiException If the modification of the source is impossible.
0322:            //     * @return Name of foun or newly created <CODE>vetoableChangeSupport</CODE> field.
0323:            //     */  
0324:            //    static String vetoSupportField( JavaClass ce ) throws JmiException {
0325:            //        assert JMIUtils.isInsideTrans();
0326:            //        String vetoSupportName = null;
0327:            //        JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(ce);
0328:            //        Type vcsType = jmodel.getType().resolve("java.beans.VetoableChangeSupport"); // NOI18N
0329:            //        List/*<Field>*/ fields = JMIUtils.getFields(ce);
0330:            //        
0331:            //        for (Iterator it = fields.iterator(); it.hasNext();) {      // Try to find suitable field
0332:            //            Field field = (Field) it.next();
0333:            //            if (JMIUtils.equalTypes(vcsType, field.getType())) {
0334:            //                vetoSupportName = field.getName();
0335:            //                break;
0336:            //            }
0337:            //        }
0338:            //
0339:            //        if ( vetoSupportName == null ) { // Field not found we create new
0340:            //            vetoSupportName = findFreeFieldName(ce, "vetoableChangeSupport"); // NOI18N
0341:            //            Field supportField = jmodel.getField().createField();
0342:            //            supportField.setName( vetoSupportName );
0343:            //            supportField.setType( vcsType );
0344:            //            supportField.setModifiers( Modifier.PRIVATE );
0345:            //            supportField.setInitialValueText( " new java.beans.VetoableChangeSupport(this)" ); // NOI18N
0346:            //            supportField.setJavadocText( PatternNode.getString( "COMMENT_VetoableChangeSupport" ) );
0347:            //            ce.getFeatures().add( supportField );
0348:            //        }
0349:            //
0350:            //        return vetoSupportName;
0351:            //    }
0352:            //    
0353:            //    private static String findFreeFieldName(JavaClass ce, String defName) throws JmiException {
0354:            //        assert JMIUtils.isInsideTrans();
0355:            //        String name = defName;
0356:            //        for (int i = 1; ce.getField(name, true) != null; i++) {
0357:            //            name = defName + '_' + i;
0358:            //        }
0359:            //        return name;
0360:            //    }
0361:            //
0362:            //    /** If in the class don't exists methods for adding/removing PropertyChangeListeners
0363:            //     * for given field adds them.
0364:            //     * @param classElement Class to operate on.
0365:            //     * @param supportName The <CODE>PropertyChangeSupport</CODE> field the methods will be generated for.
0366:            //     * @throws JmiException If the modification of the source is impossible.
0367:            //     */
0368:            //    static void supportListenerMethods( JavaClass classElement, String supportName ) throws JmiException {
0369:            //        assert JMIUtils.isInsideTrans();
0370:            //        String addMethodId = "addPropertyChangeListener"; // NOI18N
0371:            //        Method addMethod = null;
0372:            //        String removeMethodId = "removePropertyChangeListener"; // NOI18N
0373:            //        Method removeMethod = null;
0374:            //        String listenerTypeId = "java.beans.PropertyChangeListener"; // NOI18N
0375:            //        JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(classElement);
0376:            //        Type listenerType = jmodel.getType().resolve(listenerTypeId);
0377:            //
0378:            //        addMethod = classElement.getMethod( addMethodId, Collections.singletonList(listenerType), false);
0379:            //        if ( addMethod == null ) {
0380:            //            addMethod = jmodel.getMethod().createMethod();
0381:            //            addMethod.setName( addMethodId );
0382:            //            addMethod.setType( jmodel.getType().resolve("void") ); // NOI18N
0383:            //            addMethod.setModifiers( Modifier.PUBLIC );
0384:            //            Parameter param = jmodel.getParameter().createParameter();
0385:            //            param.setName("l"); // NOI18N
0386:            //            param.setType(listenerType);
0387:            //            addMethod.getParameters().add(param);
0388:            //
0389:            //            StringBuffer body = new StringBuffer( 80 );
0390:            //            body.append( "\n" ).append( TAB + supportName ); // NOI18N
0391:            //            body.append( ".addPropertyChangeListener(l);\n" ); // NOI18N
0392:            //            addMethod.setBodyText( body.toString() );
0393:            //
0394:            //            /*
0395:            //            String comment = MessageFormat.format( PatternNode.getString( "COMMENT_AddPropertyChangeListener" ), 
0396:            //                                                   new Object[] { listenerType.getClassName().getName() } );
0397:            //            */                                          
0398:            //            addMethod.setJavadocText( PatternNode.getString( "COMMENT_AddPropertyChangeListener" ) );
0399:            //            classElement.getFeatures().add( addMethod );
0400:            //        }
0401:            //
0402:            //        removeMethod = classElement.getMethod(removeMethodId, Collections.singletonList(listenerType), false);
0403:            //        if ( removeMethod == null ) {
0404:            //            removeMethod = jmodel.getMethod().createMethod();
0405:            //            removeMethod.setName( removeMethodId );
0406:            //            removeMethod.setType( jmodel.getType().resolve("void") ); // NOI18N
0407:            //            removeMethod.setModifiers( Modifier.PUBLIC );
0408:            //            Parameter param = jmodel.getParameter().createParameter();
0409:            //            param.setName("l"); // NOI18N
0410:            //            param.setType(listenerType);
0411:            //            removeMethod.getParameters().add(param);
0412:            //
0413:            //            StringBuffer body = new StringBuffer( 80 );
0414:            //            body.append( "\n" ).append( TAB + supportName ); // NOI18N
0415:            //            body.append( ".removePropertyChangeListener(l);\n" ); // NOI18N
0416:            //            removeMethod.setBodyText( body.toString() );
0417:            //            removeMethod.setJavadocText( PatternNode.getString( "COMMENT_RemovePropertyChangeListener" ) );
0418:            //            classElement.getFeatures().add( removeMethod );
0419:            //        }
0420:            //    }
0421:            //
0422:            //
0423:            //    /** If in the class don't exists methods for adding/removing VetoableChangeListeners
0424:            //     * for given field adds them.
0425:            //     * @param classElement Class to operate on.
0426:            //     * @param supportName The <CODE>vetoableChangeSupport</CODE> field the methods will be generated for.
0427:            //     * @throws JmiException If the modification of the source is impossible.
0428:            //     */
0429:            //    static void vetoSupportListenerMethods( JavaClass classElement, String supportName ) throws JmiException {
0430:            //        assert JMIUtils.isInsideTrans();
0431:            //        String addMethodId = "addVetoableChangeListener"; // NOI18N
0432:            //        Method addMethod = null;
0433:            //        String removeMethodId = "removeVetoableChangeListener"; // NOI18N
0434:            //        Method removeMethod = null;
0435:            //        String listenerTypeId = "java.beans.VetoableChangeListener"; // NOI18N
0436:            //        JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(classElement);
0437:            //        Type listenerType = jmodel.getType().resolve(listenerTypeId);
0438:            //
0439:            //        addMethod = classElement.getMethod( addMethodId, Collections.singletonList(listenerType), false);
0440:            //        if ( addMethod == null ) {
0441:            //            addMethod = jmodel.getMethod().createMethod();
0442:            //            addMethod.setName( addMethodId );
0443:            //            addMethod.setType( jmodel.getType().resolve("void") ); // NOI18N
0444:            //            addMethod.setModifiers( Modifier.PUBLIC );
0445:            //            Parameter param = jmodel.getParameter().createParameter();
0446:            //            param.setName("l"); // NOI18N
0447:            //            param.setType(listenerType);
0448:            //            addMethod.getParameters().add(param);
0449:            //
0450:            //            StringBuffer body = new StringBuffer( 80 );
0451:            //            body.append( "\n" ).append( TAB + supportName ); // NOI18N
0452:            //            body.append( ".addVetoableChangeListener (l);\n" ); // NOI18N
0453:            //            addMethod.setBodyText( body.toString() );
0454:            //            addMethod.setJavadocText( PatternNode.getString( "COMMENT_AddVetoableChangeListener" ) );
0455:            //            classElement.getFeatures().add( addMethod );
0456:            //        }
0457:            //
0458:            //        removeMethod = classElement.getMethod(removeMethodId, Collections.singletonList(listenerType), false);
0459:            //        if ( removeMethod == null ) {
0460:            //            removeMethod = jmodel.getMethod().createMethod();
0461:            //            removeMethod.setName( removeMethodId );
0462:            //            removeMethod.setType( jmodel.getType().resolve("void") ); // NOI18N
0463:            //            removeMethod.setModifiers( Modifier.PUBLIC );
0464:            //            Parameter param = jmodel.getParameter().createParameter();
0465:            //            param.setName("l"); // NOI18N
0466:            //            param.setType(listenerType);
0467:            //            removeMethod.getParameters().add(param);
0468:            //
0469:            //            StringBuffer body = new StringBuffer( 80 );
0470:            //            body.append( "\n" ).append( TAB + supportName ); // NOI18N
0471:            //            body.append( ".removeVetoableChangeListener (l);\n" ); // NOI18N
0472:            //            removeMethod.setBodyText( body.toString() );
0473:            //            removeMethod.setJavadocText( PatternNode.getString( "COMMENT_RemoveVetoableChangeListener" ) );
0474:            //            classElement.getFeatures().add( removeMethod );
0475:            //        }
0476:            //    }
0477:            //
0478:            //    /** Ensures that the listeners array list exists. Used for generating
0479:            //     * multicast event source support implemented by java.util.ArrayList.
0480:            //     * Searches the source for suitable field. If the field does not exists
0481:            //     * creates new one.
0482:            //     * @param ce Class to operate on.
0483:            //     * @param type Type of the Event Listener.
0484:            //     * @throws JmiException If the modification of the source is impossible.
0485:            //     * @return found or newly created field.
0486:            //     */
0487:            //    static Field listenersArrayListField( JavaClass ce, Type type, boolean create ) throws JmiException {
0488:            //        assert JMIUtils.isInsideTrans();
0489:            //        Field ret = null;
0490:            //        String fieldName = null;
0491:            //        String simpleTypeName = ((JavaClass) type).getSimpleName();
0492:            //        String fieldNameToFind = Introspector.decapitalize( simpleTypeName ) + "List"; // NOI18N
0493:            //
0494:            //        String fieldTypeId = "java.util.ArrayList"; // NOI18N
0495:            //        JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(ce);
0496:            //        Type fieldType = jmodel.getType().resolve(fieldTypeId);
0497:            //        
0498:            //        List/*<Field>*/ fields = JMIUtils.getFields(ce);
0499:            //        for (Iterator it = fields.iterator(); it.hasNext();) {      // Try to find suitable field
0500:            //            Field field = (Field) it.next();
0501:            //            if (JMIUtils.equalTypes(fieldType, field.getType()) && fieldNameToFind.equals(field.getName())) {
0502:            //                fieldName = fieldNameToFind;
0503:            //                ret = field;
0504:            //                break;
0505:            //            }
0506:            //        }
0507:            //
0508:            //        if ( fieldName == null && create) { // Field not found we create new
0509:            //            fieldName = fieldNameToFind;
0510:            //            Field field = jmodel.getField().createField();
0511:            //            field.setName( fieldName );
0512:            //            field.setType( fieldType );
0513:            //            field.setModifiers( Modifier.PRIVATE | Modifier.TRANSIENT );
0514:            //            String comment = MessageFormat.format( PatternNode.getString( "COMMENT_ListenerArrayList" ),
0515:            //                                                   new Object[] { simpleTypeName } );
0516:            //            field.setJavadocText( comment );
0517:            //
0518:            //            ce.getFeatures().add( field );
0519:            //            ret = field;
0520:            //        }
0521:            //
0522:            //        return ret;
0523:            //    }
0524:            //
0525:            //    /** Ensure the listenersList  exists. Used for generating
0526:            //     * multicast event source support implemented by javax.swing.event.EventListenerList.
0527:            //     * Searches the source for suitable field. If the field does not exists
0528:            //     * creates new one.
0529:            //     * @param ce Class to operate on.
0530:            //     * @param type Type of the Event Listener.
0531:            //     * @throws JmiException If the modification of the source is impossible.
0532:            //     * @return Field newly created field.
0533:            //     */
0534:            //    static Field eventListenerListField( JavaClass ce, Type type, boolean create ) throws JmiException {
0535:            //        assert JMIUtils.isInsideTrans();
0536:            //        String fieldName = null;
0537:            //        Field ret = null;
0538:            //
0539:            //        String fieldTypeId = "javax.swing.event.EventListenerList"; // NOI18N
0540:            //        JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(ce);
0541:            //        Type fieldType = jmodel.getType().resolve(fieldTypeId);
0542:            //        List/*<Field>*/ fields = JMIUtils.getFields(ce);
0543:            //        
0544:            //        for (Iterator it = fields.iterator(); it.hasNext();) {      // Try to find suitable field
0545:            //            Field field = (Field) it.next();
0546:            //            if (JMIUtils.equalTypes(fieldType, field.getType())) {
0547:            //                fieldName = field.getName();
0548:            //                ret = field;
0549:            //                break;
0550:            //            }
0551:            //        }
0552:            //
0553:            //        if ( fieldName == null && create) { // Field not found we create new
0554:            //            fieldName = "listenerList"; // NOI18N
0555:            //            Field field = jmodel.getField().createField();
0556:            //            field.setName( fieldName );
0557:            //            field.setType( fieldType );
0558:            //            field.setModifiers( Modifier.PRIVATE );
0559:            //            field.setInitialValueText( " null" ); // NOI18N
0560:            //            String comment = MessageFormat.format( PatternNode.getString( "COMMENT_EventListenerList" ),
0561:            //                                                   new Object[] { ((JavaClass) type).getSimpleName() } );
0562:            //            field.setJavadocText( comment );
0563:            //
0564:            //            ce.getFeatures().add( field );
0565:            //            ret = field;
0566:            //        }
0567:            //
0568:            //        return ret;
0569:            //    }
0570:            //
0571:            //    /** Ensure that listener field for unicast exists. Used for generating
0572:            //     * unicast event source support.
0573:            //     * Searches the source for suitable field. If the field does not exists
0574:            //     * creates new one.
0575:            //     * @param ce Class to operate on.
0576:            //     * @param type Type of the Event Listener.
0577:            //     * @throws JmiException If the modification of the source is impossible.
0578:            //     * @return Field newly created field.
0579:            //     */
0580:            //    static Field unicastListenerField( JavaClass ce, Type type, boolean create ) throws JmiException {
0581:            //        assert JMIUtils.isInsideTrans();
0582:            //        Field ret = null;
0583:            //        String fieldName = null;
0584:            //        String typeSimpleName = ((JavaClass) type).getSimpleName();
0585:            //        String fieldNameToFind = Introspector.decapitalize( typeSimpleName );
0586:            //        if ( fieldNameToFind.equals( typeSimpleName ) ) {
0587:            //            fieldNameToFind = "listener" + fieldNameToFind; // NOI18N
0588:            //        }
0589:            //
0590:            //        List/*<Field>*/ fields = JMIUtils.getFields(ce);
0591:            //        
0592:            //        for (Iterator it = fields.iterator(); it.hasNext();) {      // Try to find suitable field
0593:            //            Field field = (Field) it.next();
0594:            //            if (JMIUtils.equalTypes(type, field.getType()) && fieldNameToFind.equals(field.getName())) {
0595:            //                fieldName = fieldNameToFind;
0596:            //                ret = field;
0597:            //                break;
0598:            //            }
0599:            //        }
0600:            //
0601:            //        if ( fieldName == null && create) { // Field not found we create new
0602:            //            fieldName = fieldNameToFind;
0603:            //            JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(ce);
0604:            //            Field field = jmodel.getField().createField();
0605:            //            field.setName( fieldName );
0606:            //            field.setType( type );
0607:            //            field.setModifiers( Modifier.PRIVATE  | Modifier.TRANSIENT );
0608:            //            field.setInitialValueText( " null" ); // NOI18N
0609:            //            String comment = MessageFormat.format( PatternNode.getString( "COMMENT_UnicastEventListener" ),
0610:            //                                                   new Object[] { ((JavaClass) type).getSimpleName() } );
0611:            //            field.setJavadocText( comment );
0612:            //            ce.getFeatures().add( field );
0613:            //            ret = field;
0614:            //        }
0615:            //        return ret;
0616:            //    }
0617:            //
0618:            //    static String mcAddBody( Type type, int implementation, String listenerList ) throws JmiException {
0619:            //        assert JMIUtils.isInsideTrans();
0620:            //        String fieldName = Introspector.decapitalize( ((JavaClass) type).getSimpleName() ) + "List"; // NOI18N
0621:            //
0622:            //        StringBuffer body = new StringBuffer( 50 );
0623:            //
0624:            //        if ( listenerList == null )
0625:            //            listenerList = "listenerList"; // NOI18N
0626:            //
0627:            //        body.append( "\n"); // NOI18N
0628:            //
0629:            //        if ( implementation == 1 ) {
0630:            //            body.append( TAB + "if (" ).append( fieldName ).append( " == null ) {\n" ); // NOI18N
0631:            //            body.append( TABx2 ).append( fieldName ).append( " = new java.util.ArrayList ();\n" ); // NOI18N
0632:            //            body.append( TAB ).append( "}\n" ); // NOI18N
0633:            //            body.append( TAB + fieldName ).append( ".add (listener);\n" ); // NOI18N
0634:            //        }
0635:            //        else if ( implementation == 2 ) {
0636:            //            body.append( TAB + "if (" ).append( listenerList ).append( " == null ) {\n" ); // NOI18N
0637:            //            body.append( TABx2 ).append( listenerList ).append( " = new javax.swing.event.EventListenerList();\n" ); // NOI18N
0638:            //            body.append( TAB ).append( "}\n" ); // NOI18N
0639:            //            body.append( TAB + listenerList ).append( ".add (" ); // NOI18N
0640:            //            body.append( type.getName()).append( ".class, listener);\n" ); // NOI18N
0641:            //        }
0642:            //
0643:            //        return body.toString();
0644:            //    }
0645:            //
0646:            //    static String mcRemoveBody( Type type, int implementation, String listenerList ) throws JmiException {
0647:            //        assert JMIUtils.isInsideTrans();
0648:            //        String fieldName = Introspector.decapitalize( ((JavaClass) type).getSimpleName() ) + "List"; // NOI18N
0649:            //
0650:            //        if ( listenerList == null )
0651:            //            listenerList = "listenerList"; // NOI18N
0652:            //
0653:            //        StringBuffer body = new StringBuffer( 50 );
0654:            //        body.append( "\n"); // NOI18N
0655:            //
0656:            //        if ( implementation == 1 ) {
0657:            //            body.append( TAB + "if (" ).append( fieldName ).append( " != null ) {\n" ); // NOI18N
0658:            //            body.append( TABx2 + fieldName ).append( ".remove (listener);\n" ); // NOI18N
0659:            //            body.append( TAB ).append( "}\n" ); // NOI18N
0660:            //        }
0661:            //        else if ( implementation == 2 ) {
0662:            //            body.append( TAB + listenerList ).append( ".remove (" ); // NOI18N
0663:            //            body.append( type.getName()).append( ".class, listener);\n" ); // NOI18N
0664:            //        }
0665:            //
0666:            //        return body.toString();
0667:            //    }
0668:            //
0669:            //    static String ucAddBody( Type type, int implementation ) throws JmiException {
0670:            //        assert JMIUtils.isInsideTrans();
0671:            //        String simpleTypeName = ((JavaClass) type).getSimpleName();
0672:            //        String fieldName = Introspector.decapitalize( simpleTypeName );
0673:            //        if ( fieldName.equals( simpleTypeName ) ) {
0674:            //            fieldName = "listener" + fieldName; // NOI18N
0675:            //        }
0676:            //
0677:            //        StringBuffer body = new StringBuffer( 50 );
0678:            //
0679:            //        body.append( "\n"); // NOI18N
0680:            //
0681:            //        if ( implementation == 1 ) {
0682:            //            body.append( TAB + "if (").append( fieldName ).append( " != null) {\n" ); // NOI18N
0683:            //            body.append( TABx2 + "throw new java.util.TooManyListenersException ();\n" ); // NOI18N
0684:            //            body.append( TAB + "}\n" ); // NOI18N
0685:            //            body.append( TAB + fieldName ).append( " = listener;\n" ); // NOI18N
0686:            //        }
0687:            //
0688:            //        return body.toString();
0689:            //    }
0690:            //
0691:            //    static String ucRemoveBody( Type type, int implementation ) throws JmiException {
0692:            //        assert JMIUtils.isInsideTrans();
0693:            //        String simpleTypeName = ((JavaClass) type).getSimpleName();
0694:            //        String fieldName = Introspector.decapitalize( simpleTypeName );
0695:            //        if ( fieldName.equals( simpleTypeName ) ) {
0696:            //            fieldName = "listener" + fieldName; // NOI18N
0697:            //        }
0698:            //
0699:            //        StringBuffer body = new StringBuffer( 50 );
0700:            //        body.append( "\n"); // NOI18N
0701:            //
0702:            //        if ( implementation == 1 ) {
0703:            //            body.append( TAB + fieldName ).append( " = null;\n" ); // NOI18N
0704:            //        }
0705:            //
0706:            //        return body.toString();
0707:            //    }
0708:            //
0709:            //
0710:            //    static void fireMethod( JavaClass classElement, Type type,
0711:            //                            Method method, int implementation,
0712:            //                            String listenerList,
0713:            //                            boolean passEvent )
0714:            //    throws JmiException {
0715:            //
0716:            //        assert JMIUtils.isInsideTrans();
0717:            //        if ( listenerList == null )
0718:            //            listenerList = "listenerList"; // NOI18N
0719:            //
0720:            //        String simpleTypeName = ((JavaClass) type).getSimpleName();
0721:            //        String methodId = "fire" + // NOI18N
0722:            //                                  Pattern.capitalizeFirstLetter( simpleTypeName ) +
0723:            //                                  Pattern.capitalizeFirstLetter( method.getName() );
0724:            //
0725:            //        Method newMethod = null;
0726:            //
0727:            //        Type eventType = null;
0728:            //        JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(classElement);
0729:            //        List/*<Parameter>*/ params = method.getParameters();
0730:            //        if ( params.isEmpty() ) {
0731:            //            eventType = jmodel.getType().resolve("java.util.EventObject"); // NOI18N
0732:            //        } else {
0733:            //            eventType = ((Parameter) params.get(0)).getType();
0734:            //        }
0735:            //
0736:            //        JavaClass eventClass = (JavaClass) eventType;
0737:            //
0738:            //        //addMethod = classElement.getMethod( addMethodId, new Type[] { listenerType }  );
0739:            //
0740:            //        //if ( addMethod == null ) {
0741:            //        newMethod = jmodel.getMethod().createMethod();
0742:            //        newMethod.setName( methodId );
0743:            //        newMethod.setType( jmodel.getType().resolve("void") ); // NOI18N
0744:            //        newMethod.setModifiers( Modifier.PRIVATE );
0745:            //
0746:            //        List/*<Parameter>*/ newMethodParams = generateFireParameters( eventClass, jmodel, passEvent );
0747:            //        newMethod.getParameters().addAll( newMethodParams );
0748:            //
0749:            //        StringBuffer body = new StringBuffer( 80 );
0750:            //        body.append( "\n" ); // NOI18N
0751:            //
0752:            //        if ( implementation == 1 ) {
0753:            //            String fieldName = Introspector.decapitalize( simpleTypeName ) + "List"; // NOI18N
0754:            //
0755:            //            body.append( TAB + "java.util.ArrayList list;\n" ); // NOI18N
0756:            //
0757:            //            if ( usesConstructorParameters( eventClass, passEvent ) ) {
0758:            //                body.append( TAB + eventType.getName() ).append( " e = new "); // NOI18N
0759:            //                body.append( eventType.getName() ).append( " (" ); // NOI18N
0760:            //                body.append( fireParameterstoString( newMethodParams ) );
0761:            //                body.append(");\n"); // NOI18N
0762:            //            }
0763:            //            body.append( TAB + "synchronized (this) {\n" ); // NOI18N
0764:            //            body.append( TABx2 + "if (" + fieldName + " == null) return;\n" ); // NOI18N
0765:            //            body.append( TABx2 + "list = (java.util.ArrayList)" ); // NOI18N
0766:            //            body.append( fieldName ).append( ".clone ();\n" + TAB +"}\n" ); // NOI18N
0767:            //            body.append( TAB + "for (int i = 0; i < list.size (); i++) {\n" ); // NOI18N
0768:            //            body.append( TABx2 + "((" ).append( type.getName() ); // NOI18N
0769:            //            body.append( ")list.get (i)).").append( method.getName() ); // NOI18N
0770:            //            body.append(" ("); // NOI18N
0771:            //            if ( usesConstructorParameters( eventClass, passEvent ) ) {
0772:            //                body.append( "e" ); // NOI18N
0773:            //            }
0774:            //            else {
0775:            //                body.append( fireParameterstoString( newMethodParams ) ); // the event parameter
0776:            //            }
0777:            //            body.append( ");\n" + TAB + "}\n" ); // NOI18N
0778:            //        }
0779:            //        else if ( implementation == 2 ) {
0780:            //            if ( usesConstructorParameters( eventClass, passEvent ) ) {
0781:            //                body.append( TAB + eventType.getName() ).append( " e = null;\n "); // NOI18N
0782:            //            }
0783:            //            body.append( TAB + "if (" + listenerList + " == null) return;\n"); // NOI18N
0784:            //            body.append( TAB + "Object[] listeners = ").append(listenerList).append(".getListenerList ();\n" ); // NOI18N
0785:            //            body.append( TAB + "for (int i = listeners.length - 2; i >= 0; i -= 2) {\n"); // NOI18N
0786:            //            body.append( TABx2 + "if (listeners[i]==" ).append( type.getName()).append( ".class) {\n" ); // NOI18N
0787:            //            if ( usesConstructorParameters( eventClass, passEvent ) ) {
0788:            //                body.append( TABx3 + "if (e == null)\n" ); // NOI18N
0789:            //                body.append( TABx2 + TABx2 + "e = new ").append( eventType.getName() ).append( " (" ); // NOI18N
0790:            //                body.append( fireParameterstoString( newMethodParams ) );
0791:            //                body.append( ");\n" ); // NOI18N
0792:            //            }
0793:            //            body.append( TABx3 + "((").append(type.getName()).append(")listeners[i+1]).").append(method.getName()); // NOI18N
0794:            //            body.append(" ("); // NOI18N
0795:            //            if ( usesConstructorParameters( eventClass, passEvent ) ) {
0796:            //                body.append( "e" ); // the created event // NOI18N
0797:            //            }
0798:            //            else {
0799:            //                body.append( fireParameterstoString( newMethodParams ) ); // the event parameter
0800:            //            }
0801:            //            body.append( ");\n" + TABx2 + "}\n" + TAB + "}\n"); // NOI18N
0802:            //        }
0803:            //
0804:            //        newMethod.setBodyText( body.toString() );
0805:            //
0806:            //        StringBuffer comment = new StringBuffer ( PatternNode.getString( "COMMENT_FireMethodMC" ) );
0807:            //        if ( !usesConstructorParameters( eventClass, passEvent ) ) {
0808:            //            comment.append( "\n@param event The event to be fired\n" ); // NOI18N
0809:            //        }
0810:            //        else {
0811:            //            comment.append( fireParametersComment( newMethodParams, ((JavaClass) eventType).getSimpleName() ) );
0812:            //        }
0813:            //        newMethod.setJavadocText( comment.toString() );
0814:            //
0815:            //        classElement.getFeatures().add( newMethod );
0816:            //        //}
0817:            //    }
0818:            //
0819:            //    static void unicastFireMethod( JavaClass classElement, Type type,
0820:            //                                   Method method, int implementation,
0821:            //                                   boolean passEvent )
0822:            //    throws JmiException {
0823:            //
0824:            //        assert JMIUtils.isInsideTrans();
0825:            //        String simpleTypeName = ((JavaClass) type).getSimpleName();
0826:            //        String methodId = "fire" + // NOI18N
0827:            //                          Pattern.capitalizeFirstLetter( simpleTypeName ) +
0828:            //                          Pattern.capitalizeFirstLetter( method.getName() );
0829:            //
0830:            //        Method newMethod = null;
0831:            //
0832:            //        Type eventType = null;
0833:            //        JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(classElement);
0834:            //        List/*<Parameter>*/ params = method.getParameters();
0835:            //        if ( params.isEmpty() ) {
0836:            //            eventType = jmodel.getType().resolve("java.util.EventObject"); // NOI18N
0837:            //        } else {
0838:            //            eventType = ((Parameter) params.get(0)).getType();
0839:            //        }
0840:            //
0841:            //        JavaClass eventClass = (JavaClass) eventType;
0842:            //
0843:            //        //addMethod = classElement.getMethod( addMethodId, new Type[] { listenerType }  );
0844:            //
0845:            //        //if ( addMethod == null ) {
0846:            //        newMethod = jmodel.getMethod().createMethod();
0847:            //        newMethod.setName( methodId );
0848:            //        newMethod.setType( jmodel.getType().resolve("void") ); // NOI18N
0849:            //        newMethod.setModifiers( Modifier.PRIVATE );
0850:            //
0851:            //        List/*<Parameter>*/ newMethodParams = generateFireParameters( eventClass, jmodel, passEvent );
0852:            //        newMethod.getParameters().addAll( newMethodParams );
0853:            //
0854:            //        StringBuffer body = new StringBuffer( 80 );
0855:            //        body.append( "\n" ); // NOI18N
0856:            //
0857:            //        if ( implementation == 1 ) {
0858:            //            String fieldName = Introspector.decapitalize( simpleTypeName );
0859:            //            if ( fieldName.equals( simpleTypeName ) ) {
0860:            //                fieldName = "listener" + fieldName; // NOI18N
0861:            //            }
0862:            //            
0863:            //            body.append(TAB + "if (" + fieldName + " == null) return;\n"); // NOI18N
0864:            //
0865:            //            if ( usesConstructorParameters( eventClass, passEvent ) ) {
0866:            //                body.append( TAB + eventType.getName() ).append( " e = new "); // NOI18N
0867:            //                body.append( eventType.getName() ).append( " (" ); // NOI18N
0868:            //                body.append( fireParameterstoString( newMethodParams ) );
0869:            //                body.append(");\n"); // NOI18N
0870:            //            }
0871:            //
0872:            //            body.append( TAB + fieldName ).append( "." ).append( method.getName() ); // NOI18N
0873:            //            body.append(" ("); // NOI18N
0874:            //            if ( usesConstructorParameters( eventClass, passEvent ) ) {
0875:            //                body.append( "e" ); // NOI18N
0876:            //            }
0877:            //            else {
0878:            //                body.append( fireParameterstoString( newMethodParams ) ); // the event parameter
0879:            //            }
0880:            //            body.append( ");\n" ); // NOI18N
0881:            //        }
0882:            //
0883:            //        newMethod.setBodyText( body.toString() );
0884:            //
0885:            //        StringBuffer comment = new StringBuffer ( PatternNode.getString( "COMMENT_FireMethodUC" ) );
0886:            //        if ( !usesConstructorParameters( eventClass, passEvent ) ) {
0887:            //            comment.append( "\n@param event The event to be fired\n" ); // NOI18N
0888:            //        }
0889:            //        else {
0890:            //            comment.append( fireParametersComment( newMethodParams, ((JavaClass) eventType).getSimpleName()) ); // the event parameter
0891:            //        }
0892:            //        newMethod.setJavadocText( comment.toString() );
0893:            //
0894:            //        classElement.getFeatures().add( newMethod );
0895:            //        //}
0896:            //    }
0897:            //
0898:            //
0899:            //
0900:            //    static boolean usesConstructorParameters( JavaClass eventClass, boolean passEvent ) throws JmiException {
0901:            //
0902:            //        assert JMIUtils.isInsideTrans();
0903:            //        if ( passEvent || eventClass == null || eventClass.isInterface() || JMIUtils.getConstructors(eventClass).size() > 1 )
0904:            //            return false;
0905:            //        else
0906:            //            return true;
0907:            //    }
0908:            //
0909:            //
0910:            //    static List/*<Parameter>*/ generateFireParameters( JavaClass eventClass, JavaModelPackage jmodel, boolean passEvent )
0911:            //            throws JmiException {
0912:            //
0913:            //        assert JMIUtils.isInsideTrans();
0914:            //        if ( !usesConstructorParameters( eventClass, passEvent ) ) {
0915:            //            Parameter param = jmodel.getParameter().createParameter();
0916:            //            param.setName("event"); // NOI18N
0917:            //            param.setType(eventClass);
0918:            //            return Collections.singletonList(param);
0919:            //        }
0920:            //        else {
0921:            //            Constructor constructor = (Constructor) JMIUtils.getConstructors(eventClass).get(0);
0922:            //            List/*<Parameter>*/ params = constructor.getParameters();
0923:            //            List/*<Parameter>*/ result = new ArrayList/*<Parameter>*/(params.size());
0924:            //            for (Iterator it = params.iterator(); it.hasNext();) {
0925:            //                Parameter param = (Parameter) it.next();
0926:            //                Parameter newParam = jmodel.getParameter().createParameter();
0927:            //                newParam.setName(param.getName()); // NOI18N
0928:            //                newParam.setType(param.getType());
0929:            //                result.add(newParam);
0930:            //            }
0931:            //            return result;
0932:            //        }
0933:            //
0934:            //    }
0935:            //
0936:            //    static String fireParameterstoString( List/*<Parameter>*/  params ) throws JmiException {
0937:            //        assert JMIUtils.isInsideTrans();
0938:            //        StringBuffer buffer = new StringBuffer( 60 );
0939:            //        
0940:            //        for (Iterator it = params.iterator(); it.hasNext();) {
0941:            //            Parameter param = (Parameter) it.next();
0942:            //            buffer.append( param.getName() );
0943:            //            buffer.append( ", " ); // NOI18N
0944:            //        }
0945:            //        if (buffer.length() > 2) {
0946:            //            return buffer.substring(0, buffer.length() - 2);
0947:            //        } else {
0948:            //            return buffer.toString();
0949:            //        }
0950:            //    }
0951:            //
0952:            //    static String fireParametersComment( List/*<Parameter>*/  params, String evntType ) throws JmiException {
0953:            //
0954:            //        assert JMIUtils.isInsideTrans();
0955:            //        StringBuffer buffer = new StringBuffer( 60 );
0956:            //        int i = 0;
0957:            //
0958:            //        for (Iterator it = params.iterator(); it.hasNext();) {
0959:            //            Parameter param = (Parameter) it.next();
0960:            //            buffer.append( "\n@param ").append( param.getName() ); // NOI18N
0961:            //            buffer.append( " Parameter #" ).append( (i++) + 1 ).append( " of the <CODE>" ); // NOI18N
0962:            //            buffer.append( evntType ).append( "<CODE> constructor." ); // NOI18N
0963:            //        }
0964:            //        buffer.append( "\n" ); // NOI18N
0965:            //
0966:            //        return buffer.toString();
0967:            //    }
0968:            //
0969:            //    // UTILITY METHODS ----------------------------------------------------------
0970:            //
0971:            //    /** For primitive {@link org.openide.src.Type type} finds class for wrapping it into object.
0972:            //     * E.g. <CODE>Type.BOOLEAN -> Boolean</CODE>
0973:            //     * @param type Primitive type.
0974:            //     * @return Class which wraps the primitive type.
0975:            //     */
0976:            //    public static String getWrapperClassName(Type type) throws JmiException {
0977:            //        assert JMIUtils.isInsideTrans();
0978:            //        if (type instanceof ClassDefinition)
0979:            //            return type.getName();
0980:            //        if (!(type instanceof PrimitiveType)) 
0981:            //            throw new IllegalStateException("Unknonw type: " + type); // NOI18N
0982:            //        
0983:            //        String typeName = type.getName();
0984:            //        char[] ctype = typeName.toCharArray();
0985:            //        switch (ctype[0]) {
0986:            //            case 'b':
0987:            //                if (ctype[1] == 'o') // boolean
0988:            //                    return "Boolean"; //NOI18N
0989:            //                else
0990:            //                    return "Byte"; // NOI18N
0991:            //            case 'd':
0992:            //                return "Double"; // NOI18N
0993:            //            case 'f':
0994:            //                return "Float"; // NOI18N
0995:            //            case 'c':
0996:            //                return "Character"; // NOI18N
0997:            //            case 'i':
0998:            //                return "Integer"; // NOI18N
0999:            //            case 'l':
1000:            //                return "Long"; // NOI18N
1001:            //            case 's':
1002:            //                return "Short"; // NOI18N
1003:            //            default:
1004:            //                return "Object"; // NOI18N
1005:            //        }
1006:            //    }
1007:            //    
1008:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.