Source Code Cross Referenced for WizardHelpers.java in  » IDE-Netbeans » jmx » org » netbeans » modules » jmx » 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 » jmx » org.netbeans.modules.jmx 
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:         * Code is Sun Microsystems, Inc. Portions Copyright 2004-2005 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:        package org.netbeans.modules.jmx;
0042:
0043:        import org.netbeans.modules.jmx.mbeanwizard.tablemodel.AbstractJMXTableModel;
0044:        import java.io.*;
0045:        import java.util.Iterator;
0046:        import java.util.List;
0047:        import java.util.Map;
0048:        import java.util.Collections;
0049:        import java.util.Enumeration;
0050:
0051:        import java.util.HashMap;
0052:        import java.net.URL;
0053:        import java.util.ArrayList;
0054:        import java.util.Set;
0055:        import javax.swing.JComboBox;
0056:        import javax.swing.JTextField;
0057:
0058:        import org.netbeans.api.java.queries.UnitTestForSourceQuery;
0059:        import org.netbeans.api.project.FileOwnerQuery;
0060:        import org.netbeans.spi.project.ui.templates.support.Templates;
0061:        import org.netbeans.api.project.Project;
0062:        import org.netbeans.api.project.ProjectInformation;
0063:        import org.openide.loaders.DataFolder;
0064:        import org.openide.loaders.DataObject;
0065:        import org.openide.util.Lookup;
0066:        import org.openide.WizardDescriptor;
0067:        import org.openide.filesystems.FileObject;
0068:        import org.openide.filesystems.FileUtil;
0069:        import org.openide.filesystems.FileLock;
0070:        import org.openide.ErrorManager;
0071:        import org.netbeans.api.project.SourceGroup;
0072:        import org.netbeans.api.project.ProjectUtils;
0073:        import org.netbeans.api.project.ui.OpenProjects;
0074:        import org.openide.cookies.SaveCookie;
0075:
0076:        import org.openide.filesystems.URLMapper;
0077:        import org.openide.util.Utilities;
0078:
0079:        /**
0080:         * Helper class : all methods are static.
0081:         */
0082:        public class WizardHelpers {
0083:
0084:            /**
0085:             * Checks if a name is a valid class name.
0086:             * @param className <CODE>String</CODE> the class name to check.
0087:             * @return <CODE>boolean</CODE> true if className is a valid class name.
0088:             */
0089:            public static boolean isValidClassName(String className) {
0090:                if (className.length() == 0) {
0091:                    return false;
0092:                }
0093:                char[] chars = className.toCharArray();
0094:                int segmentStart = 0;
0095:                int i;
0096:                for (i = 0; i < chars.length; i++) {
0097:                    if (chars[i] == '.') {
0098:                        if (i == segmentStart) {
0099:                            return false; //empty segment
0100:                        }
0101:                        if (!Utilities.isJavaIdentifier(className.substring(
0102:                                segmentStart, i))) {
0103:                            return false; //illegal name of the segment
0104:                        }
0105:                        segmentStart = i + 1;
0106:                    }
0107:                }
0108:                if (i == segmentStart) {
0109:                    return false; //empty last segment
0110:                }
0111:                if (!Utilities.isJavaIdentifier(className.substring(
0112:                        segmentStart, chars.length))) {
0113:                    return false; //illegal name of the last segment
0114:                }
0115:                return true;
0116:            }
0117:
0118:            /**
0119:             * Builds a map that containing relation between <code>SourceGroup</code>s
0120:             * and their respective test <code>SourceGroup</code>s.
0121:             * Each entry of the map contains a <code>SourceGroup</code> as a key
0122:             * and an array of test <code>SourceGroup</code>s returned by
0123:             * <code>UnitTestForSourceQuery</code> for that <code>SourceGroup</code>
0124:             * as a value. <code>SourceGroup</code>s that have no test
0125:             * <code>SourceGroup</code>s assigned are omitted, i.e. the resulting
0126:             * map does not contain entries that would have empty arrays as their
0127:             * values.
0128:             *
0129:             * @param  project           return only <code>SourceGroup</code>s
0130:             *                           - ignore test folders not having
0131:             *                           a corresponding <code>SourceGroup</code>
0132:             * @param  sourceGroupsOnly  return only <code>SourceGroup</code>s
0133:             *                           - ignore test folders not having
0134:             *                           a corresponding <code>SourceGroup</code>
0135:             * @return  created map - may be empty, may be unmodifiable,
0136:             *                        cannot be <code>null</code>
0137:             */
0138:            public static Map getSourcesToTestsMap(Project project,
0139:                    final boolean sourceGroupsOnly) {
0140:                /*
0141:                 * Idea:
0142:                 * 1) Get all SourceGroups
0143:                 * 2) For each SourceGroup, ask UnitTestForSourceQuery for its related
0144:                 *    test SourceGroups
0145:                 */
0146:
0147:                /* .) get all SourceGroups: */
0148:                final SourceGroup[] sourceGroups = getSourceGroups(project);
0149:                if (sourceGroups.length == 0) {
0150:                    return Collections.EMPTY_MAP;
0151:                }
0152:
0153:                /* .) get test SourceGroups for each SourceGroup: */
0154:                Map foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups);
0155:                Object testTargetsUnion[] = new Object[sourceGroups.length];
0156:                Map map = new HashMap(
0157:                        (int) (sourceGroups.length * 1.33f + 0.5f), .75f);
0158:                for (int i = 0; i < sourceGroups.length; i++) {
0159:                    Object[] testTargets = getTestTargets(sourceGroups[i],
0160:                            foldersToSourceGroupsMap, project, sourceGroupsOnly);
0161:                    if (testTargets.length != 0) {
0162:                        map.put(sourceGroups[i], testTargets);
0163:                    }
0164:                }
0165:                if (map.isEmpty()) {
0166:                    return Collections.EMPTY_MAP;
0167:                }
0168:                if (map.size() == 1) {
0169:                    Map.Entry entry = (Map.Entry) map.entrySet().iterator()
0170:                            .next();
0171:                    return Collections.singletonMap(entry.getKey(), entry
0172:                            .getValue());
0173:                }
0174:
0175:                final int finalMapSize = map.size();
0176:                if (finalMapSize >= (sourceGroups.length - 5)) {
0177:                    return map;
0178:                }
0179:
0180:                final Map targetMap;
0181:                targetMap = new HashMap((int) (finalMapSize * 1.25f + .5f), .8f);
0182:                targetMap.putAll(map);
0183:                return targetMap;
0184:            }
0185:
0186:            /**
0187:             * Returns test targets for the given <code>SourceGroup</code>.
0188:             * The test targets are folders which are searched when tests for a class
0189:             * from the <code>SourceGroup</code> are to be found. Each of the returned
0190:             * test targets may be either <code>SourceGroup</code> (representing
0191:             * a folder plus additional information such as display name) or simply
0192:             * a <code>FileObject</code> representing a folder.
0193:             * If parameter <code>includeSourceGroups</code> is <code>false</code>,
0194:             * only <code>SourceGroup<code>s are returned (target folders without
0195:             * corresponding <code>SourceGroup</code>s are ignored).
0196:             *
0197:             * @param  src  source group to find test targets for
0198:             * @param  sourceGroupsOnly  skip target folders without matching
0199:             *                           <code>SourceGroup</code>
0200:             * @return  array which may contain <code>FileObject</code>s
0201:             *          or <code>SourceGroup</code>s (or both);
0202:             *          it may be empty but not <code>null</code>
0203:             * @see  TestUtil#getFileObject2SourceGroupMap
0204:             */
0205:            private static Object[] getTestTargets(SourceGroup sourceGroup,
0206:                    Map foldersToSourceGroupsMap, Project project,
0207:                    final boolean sourceGroupsOnly) {
0208:                FileObject[] sourceRoots = getTestsFileObject(sourceGroup,
0209:                        project);
0210:                /* .) find SourceGroups corresponding to the FileObjects: */
0211:                final Object[] targets = new Object[sourceRoots.length];
0212:                int targetIndex = 0;
0213:                for (int i = 0; i < sourceRoots.length; i++) {
0214:                    final FileObject sourceRoot = sourceRoots[i];
0215:                    if (sourceRoot == null) {
0216:                        continue;
0217:                    }
0218:                    Object srcGroup = foldersToSourceGroupsMap.get(sourceRoot);
0219:                    targets[targetIndex++] = (srcGroup != null) ? srcGroup
0220:                            : sourceGroupsOnly ? null : sourceRoot;
0221:                }
0222:                return skipNulls(targets);
0223:            }
0224:
0225:            public static FileObject[] getTestsFileObject(
0226:                    SourceGroup sourceGroup, Project project) {
0227:                /* .) get URLs of target SourceGroup's roots: */
0228:                URL[] rootURLs = UnitTestForSourceQuery
0229:                        .findUnitTests(sourceGroup.getRootFolder());
0230:
0231:                if (rootURLs.length == 0) {
0232:                    return new FileObject[0];
0233:                }
0234:
0235:                /* .) convert the URLs to FileObjects: */
0236:                FileObject[] sourceRoots = new FileObject[rootURLs.length];
0237:                int count = 0;
0238:                for (int i = 0; i < rootURLs.length; i++) {
0239:                    if ((sourceRoots[i] = URLMapper.findFileObject(rootURLs[i])) == null) {
0240:                        int severity = ErrorManager.INFORMATIONAL;
0241:                        if (ErrorManager.getDefault().isNotifiable(severity)) {
0242:                            ErrorManager.getDefault().notify(
0243:                                    severity,
0244:                                    new IllegalStateException(
0245:                                            "No FileObject found for the following URL: "//NOI18N
0246:                                                    + rootURLs[i]));
0247:                        }
0248:                        continue;
0249:                    }
0250:                    if (FileOwnerQuery.getOwner(sourceRoots[i]) != project) {
0251:                        int severity = ErrorManager.INFORMATIONAL;
0252:                        if (ErrorManager.getDefault().isNotifiable(severity)) {
0253:                            ErrorManager
0254:                                    .getDefault()
0255:                                    .notify(
0256:                                            severity,
0257:                                            new IllegalStateException(
0258:                                                    "Source root found by FileOwnerQuery points " //NOI18N
0259:                                                            + "to a different project for the following URL: " //NOI18N
0260:                                                            + rootURLs[i]));
0261:                        }
0262:                        continue;
0263:                    }
0264:                    count++;
0265:                }
0266:
0267:                return sourceRoots;
0268:            }
0269:
0270:            /**
0271:             * Creates a map mapping folders to source groups.
0272:             * For a folder as a key, the map returns the source group having that
0273:             * folder as a root. The created map is stored to variable
0274:             * {@link #foldersToSourceGroupsMap}.
0275:             *
0276:             * @param  sourceGroup  source group to create a map from
0277:             * @author  Marian Petras
0278:             */
0279:            private static Map createFoldersToSourceGroupsMap(
0280:                    final SourceGroup[] sourceGroups) {
0281:                Map result;
0282:
0283:                if (sourceGroups.length == 0) {
0284:                    result = Collections.EMPTY_MAP;
0285:                } else {
0286:                    result = new HashMap(2 * sourceGroups.length, .5f);
0287:                    for (int i = 0; i < sourceGroups.length; i++) {
0288:                        SourceGroup sourceGroup = sourceGroups[i];
0289:                        result.put(sourceGroup.getRootFolder(), sourceGroup);
0290:                    }
0291:                }
0292:
0293:                return result;
0294:            }
0295:
0296:            /**
0297:             * Returns the equivalent object type of the type if it is primitive type
0298:             * else the same type.
0299:             * @param type <CODE>String</CODE> a type name 
0300:             * @return <CODE>String</CODE> an Object type (not primitive type).
0301:             */
0302:            public static String getTypeObject(String type) {
0303:                String resultType = type;
0304:                if (type.equals(WizardConstants.BOOLEAN_NAME)) {
0305:                    resultType = WizardConstants.BOOLEAN_OBJ_NAME;
0306:                } else if (type.equals(WizardConstants.BYTE_NAME)) {
0307:                    resultType = WizardConstants.BYTE_OBJ_NAME;
0308:                } else if (type.equals(WizardConstants.CHAR_NAME)) {
0309:                    resultType = WizardConstants.CHAR_OBJ_NAME;
0310:                } else if (type.equals(WizardConstants.INT_NAME)) {
0311:                    resultType = WizardConstants.INTEGER_OBJ_NAME;
0312:                } else if (type.equals(WizardConstants.LONG_NAME)) {
0313:                    resultType = WizardConstants.LONG_OBJ_NAME;
0314:                } else if (type.equals(WizardConstants.DOUBLE_NAME)) {
0315:                    resultType = WizardConstants.DOUBLE_OBJ_NAME;
0316:                } else if (type.equals(WizardConstants.FLOAT_NAME)) {
0317:                    resultType = WizardConstants.FLOAT_OBJ_NAME;
0318:                }
0319:                return resultType;
0320:            }
0321:
0322:            /**
0323:             * Returns the full type name code needed of the type.
0324:             * @param type <CODE>String</CODE> a type name
0325:             * @return <CODE>String</CODE> code to get full type name.
0326:             */
0327:            public static String getFullTypeNameCode(String type) {
0328:                String resultType = type;
0329:                if (type.equals(WizardConstants.BOOLEAN_NAME)) {
0330:                    resultType = WizardConstants.BOOLEAN_OBJ_FULLNAME
0331:                            + WizardConstants.TYPE + WizardConstants.GETNAME;
0332:                } else if (type.equals(WizardConstants.BOOLEAN_OBJ_NAME)) {
0333:                    resultType = WizardConstants.BOOLEAN_OBJ_FULLNAME
0334:                            + WizardConstants.CLASS + WizardConstants.GETNAME;
0335:                } else if (type.equals(WizardConstants.BYTE_NAME)) {
0336:                    resultType = WizardConstants.BYTE_OBJ_FULLNAME
0337:                            + WizardConstants.TYPE + WizardConstants.GETNAME;
0338:                } else if (type.equals(WizardConstants.BYTE_OBJ_NAME)) {
0339:                    resultType = WizardConstants.BYTE_OBJ_FULLNAME
0340:                            + WizardConstants.CLASS + WizardConstants.GETNAME;
0341:                } else if (type.equals(WizardConstants.CHAR_NAME)) {
0342:                    resultType = WizardConstants.CHAR_OBJ_FULLNAME
0343:                            + WizardConstants.TYPE + WizardConstants.GETNAME;
0344:                } else if (type.equals(WizardConstants.CHAR_OBJ_NAME)) {
0345:                    resultType = WizardConstants.CHAR_OBJ_FULLNAME
0346:                            + WizardConstants.CLASS + WizardConstants.GETNAME;
0347:                } else if (type.equals(WizardConstants.INT_NAME)) {
0348:                    resultType = WizardConstants.INTEGER_OBJ_FULLNAME
0349:                            + WizardConstants.TYPE + WizardConstants.GETNAME;
0350:                } else if (type.equals(WizardConstants.INTEGER_OBJ_NAME)) {
0351:                    resultType = WizardConstants.INTEGER_OBJ_FULLNAME
0352:                            + WizardConstants.CLASS + WizardConstants.GETNAME;
0353:                } else if (type.equals(WizardConstants.LONG_NAME)) {
0354:                    resultType = WizardConstants.LONG_OBJ_FULLNAME
0355:                            + WizardConstants.TYPE + WizardConstants.GETNAME;
0356:                } else if (type.equals(WizardConstants.LONG_OBJ_NAME)) {
0357:                    resultType = WizardConstants.LONG_OBJ_FULLNAME
0358:                            + WizardConstants.CLASS + WizardConstants.GETNAME;
0359:                } else if (type.equals(WizardConstants.VOID_NAME)) {
0360:                    resultType = WizardConstants.VOID_OBJ_FULLNAME
0361:                            + WizardConstants.TYPE + WizardConstants.GETNAME;
0362:                } else if (type.equals(WizardConstants.DATE_OBJ_NAME)) {
0363:                    resultType = WizardConstants.DATE_OBJ_FULLNAME
0364:                            + WizardConstants.CLASS + WizardConstants.GETNAME;
0365:                } else if (type.equals(WizardConstants.STRING_OBJ_NAME)) {
0366:                    resultType = WizardConstants.STRING_OBJ_FULLNAME
0367:                            + WizardConstants.CLASS + WizardConstants.GETNAME;
0368:                } else if (type.equals(WizardConstants.OBJECTNAME_NAME)) {
0369:                    resultType = WizardConstants.OBJECTNAME_FULLNAME
0370:                            + WizardConstants.CLASS + WizardConstants.GETNAME;
0371:                } else if (type.equals(WizardConstants.FLOAT_NAME)) {
0372:                    resultType = WizardConstants.FLOAT_OBJ_FULLNAME
0373:                            + WizardConstants.TYPE + WizardConstants.GETNAME;
0374:                } else if (type.equals(WizardConstants.FLOAT_OBJ_NAME)) {
0375:                    resultType = WizardConstants.FLOAT_OBJ_FULLNAME
0376:                            + WizardConstants.CLASS + WizardConstants.GETNAME;
0377:                } else if (type.equals(WizardConstants.DOUBLE_NAME)) {
0378:                    resultType = WizardConstants.DOUBLE_OBJ_FULLNAME
0379:                            + WizardConstants.TYPE + WizardConstants.GETNAME;
0380:                } else if (type.equals(WizardConstants.DOUBLE_OBJ_NAME)) {
0381:                    resultType = WizardConstants.DOUBLE_OBJ_FULLNAME
0382:                            + WizardConstants.CLASS + WizardConstants.GETNAME;
0383:                } else {
0384:                    resultType = type + WizardConstants.CLASS
0385:                            + WizardConstants.GETNAME;
0386:                }
0387:                return resultType;
0388:            }
0389:
0390:            public static String getSimpleName(String type) {
0391:                if (type.endsWith("[]")) //NOI18N
0392:                    return getSimpleName(type.substring(0, type.length() - 2))
0393:                            + "[]"; //NOI18N
0394:                String resultType = type;
0395:                if (type.equals(WizardConstants.BOOLEAN_OBJ_FULLNAME)) {
0396:                    resultType = WizardConstants.BOOLEAN_OBJ_NAME;
0397:                } else if (type.equals(WizardConstants.BYTE_OBJ_FULLNAME)) {
0398:                    resultType = WizardConstants.BYTE_OBJ_NAME;
0399:                } else if (type.equals(WizardConstants.CHAR_OBJ_FULLNAME)) {
0400:                    resultType = WizardConstants.CHAR_OBJ_NAME;
0401:                } else if (type.equals(WizardConstants.INTEGER_OBJ_FULLNAME)) {
0402:                    resultType = WizardConstants.INTEGER_OBJ_NAME;
0403:                } else if (type.equals(WizardConstants.LONG_OBJ_FULLNAME)) {
0404:                    resultType = WizardConstants.LONG_OBJ_NAME;
0405:                } else if (type.equals(WizardConstants.DATE_OBJ_FULLNAME)) {
0406:                    resultType = WizardConstants.DATE_OBJ_NAME;
0407:                } else if (type.equals(WizardConstants.STRING_OBJ_FULLNAME)) {
0408:                    resultType = WizardConstants.STRING_OBJ_NAME;
0409:                } else if (type.equals(WizardConstants.OBJECTNAME_FULLNAME)) {
0410:                    resultType = WizardConstants.OBJECTNAME_NAME;
0411:                } else if (type.equals(WizardConstants.FLOAT_OBJ_FULLNAME)) {
0412:                    resultType = WizardConstants.FLOAT_OBJ_NAME;
0413:                } else if (type.equals(WizardConstants.DOUBLE_OBJ_FULLNAME)) {
0414:                    resultType = WizardConstants.DOUBLE_OBJ_NAME;
0415:                }
0416:                return resultType;
0417:            }
0418:
0419:            /**
0420:             * Returns the full type name of the type.
0421:             * @param type <CODE>String</CODE> a type name
0422:             * @return <CODE>String</CODE> code to get full type name.
0423:             */
0424:            public static String getFullTypeName(String type) {
0425:                if (type.endsWith("[]")) //NOI18N
0426:                    return getFullTypeName(type.substring(0, type.length() - 2))
0427:                            + "[]"; //NOI18N
0428:                String resultType = type;
0429:                if (type.equals(WizardConstants.BOOLEAN_OBJ_NAME)) {
0430:                    resultType = WizardConstants.BOOLEAN_OBJ_FULLNAME;
0431:                } else if (type.equals(WizardConstants.BYTE_OBJ_NAME)) {
0432:                    resultType = WizardConstants.BYTE_OBJ_FULLNAME;
0433:                } else if (type.equals(WizardConstants.CHAR_OBJ_NAME)) {
0434:                    resultType = WizardConstants.CHAR_OBJ_FULLNAME;
0435:                } else if (type.equals(WizardConstants.INTEGER_OBJ_NAME)) {
0436:                    resultType = WizardConstants.INTEGER_OBJ_FULLNAME;
0437:                } else if (type.equals(WizardConstants.LONG_OBJ_NAME)) {
0438:                    resultType = WizardConstants.LONG_OBJ_FULLNAME;
0439:                } else if (type.equals(WizardConstants.DATE_OBJ_NAME)) {
0440:                    resultType = WizardConstants.DATE_OBJ_FULLNAME;
0441:                } else if (type.equals(WizardConstants.STRING_OBJ_NAME)) {
0442:                    resultType = WizardConstants.STRING_OBJ_FULLNAME;
0443:                } else if (type.equals(WizardConstants.OBJECTNAME_NAME)) {
0444:                    resultType = WizardConstants.OBJECTNAME_FULLNAME;
0445:                } else if (type.equals(WizardConstants.FLOAT_OBJ_NAME)) {
0446:                    resultType = WizardConstants.FLOAT_OBJ_FULLNAME;
0447:                } else if (type.equals(WizardConstants.DOUBLE_OBJ_NAME)) {
0448:                    resultType = WizardConstants.DOUBLE_OBJ_FULLNAME;
0449:                } else {
0450:                    resultType = type;
0451:                }
0452:                return resultType;
0453:            }
0454:
0455:            /**
0456:             * Returns the full type name of the type.
0457:             * @param type <CODE>String</CODE> a type name
0458:             * @return <CODE>String</CODE> code to get full type name.
0459:             */
0460:            public static String getFullWrappedName(String type) {
0461:                if (type.endsWith("[]")) //NOI18N
0462:                    return getFullTypeName(type.substring(0, type.length() - 2))
0463:                            + "[]"; //NOI18N
0464:                String resultType = type;
0465:                if (type.equals(WizardConstants.BOOLEAN_NAME)) {
0466:                    resultType = WizardConstants.BOOLEAN_OBJ_FULLNAME;
0467:                } else if (type.equals(WizardConstants.BYTE_NAME)) {
0468:                    resultType = WizardConstants.BYTE_OBJ_FULLNAME;
0469:                } else if (type.equals(WizardConstants.CHAR_NAME)) {
0470:                    resultType = WizardConstants.CHAR_OBJ_FULLNAME;
0471:                } else if (type.equals(WizardConstants.INT_NAME)) {
0472:                    resultType = WizardConstants.INTEGER_OBJ_FULLNAME;
0473:                } else if (type.equals(WizardConstants.LONG_NAME)) {
0474:                    resultType = WizardConstants.LONG_OBJ_FULLNAME;
0475:                } else if (type.equals(WizardConstants.DATE_OBJ_NAME)) {
0476:                    resultType = WizardConstants.DATE_OBJ_FULLNAME;
0477:                } else if (type.equals(WizardConstants.STRING_OBJ_NAME)) {
0478:                    resultType = WizardConstants.STRING_OBJ_FULLNAME;
0479:                } else if (type.equals(WizardConstants.OBJECTNAME_NAME)) {
0480:                    resultType = WizardConstants.OBJECTNAME_FULLNAME;
0481:                } else if (type.equals(WizardConstants.FLOAT_NAME)) {
0482:                    resultType = WizardConstants.FLOAT_OBJ_FULLNAME;
0483:                } else if (type.equals(WizardConstants.DOUBLE_NAME)) {
0484:                    resultType = WizardConstants.DOUBLE_OBJ_FULLNAME;
0485:                } else {
0486:                    resultType = type;
0487:                }
0488:                return resultType;
0489:            }
0490:
0491:            public static String getSimpleTypeName(String type) {
0492:                String resultType = type;
0493:                if (type.equals(WizardConstants.BOOLEAN_OBJ_FULLNAME)) {
0494:                    resultType = WizardConstants.BOOLEAN_OBJ_NAME;
0495:                } else if (type.equals(WizardConstants.INTEGER_OBJ_FULLNAME)) {
0496:                    resultType = WizardConstants.INTEGER_OBJ_NAME;
0497:                } else if (type.equals(WizardConstants.LONG_OBJ_FULLNAME)) {
0498:                    resultType = WizardConstants.LONG_OBJ_NAME;
0499:                } else if (type.equals(WizardConstants.DATE_OBJ_FULLNAME)) {
0500:                    resultType = WizardConstants.DATE_OBJ_NAME;
0501:                } else if (type.equals(WizardConstants.STRING_OBJ_FULLNAME)) {
0502:                    resultType = WizardConstants.STRING_OBJ_NAME;
0503:                } else if (type.equals(WizardConstants.OBJECTNAME_FULLNAME)) {
0504:                    resultType = WizardConstants.OBJECTNAME_NAME;
0505:                } else if (type.equals(WizardConstants.FLOAT_OBJ_FULLNAME)) {
0506:                    resultType = WizardConstants.FLOAT_OBJ_NAME;
0507:                } else if (type.equals(WizardConstants.DOUBLE_OBJ_FULLNAME)) {
0508:                    resultType = WizardConstants.DOUBLE_OBJ_NAME;
0509:                } else {
0510:                    resultType = type;
0511:                }
0512:                return resultType;
0513:            }
0514:
0515:            /**
0516:             * Returns true if this type is primitive, else returns false
0517:             * @param type <CODE>String</CODE> a type name
0518:             * @return <CODE>boolean</CODE> true if it is a primitive type.
0519:             */
0520:            public static boolean isPrimitiveType(String type) {
0521:                boolean result = false;
0522:                if (type.equals(WizardConstants.BOOLEAN_NAME)) {
0523:                    result = true;
0524:                } else if (type.equals(WizardConstants.BYTE_NAME)) {
0525:                    result = true;
0526:                } else if (type.equals(WizardConstants.CHAR_NAME)) {
0527:                    result = true;
0528:                } else if (type.equals(WizardConstants.INT_NAME)) {
0529:                    result = true;
0530:                } else if (type.equals(WizardConstants.LONG_NAME)) {
0531:                    result = true;
0532:                } else if (type.equals(WizardConstants.FLOAT_NAME)) {
0533:                    result = true;
0534:                } else if (type.equals(WizardConstants.DOUBLE_NAME)) {
0535:                    result = true;
0536:                }
0537:                return result;
0538:            }
0539:
0540:            /**
0541:             * Returns true if this type is primitive, else returns false
0542:             * @param type <CODE>String</CODE> a type name
0543:             * @return <CODE>boolean</CODE> true if it is a primitive type.
0544:             */
0545:            public static boolean isStandardWrapperType(String type) {
0546:                boolean result = false;
0547:                if (type.equals(WizardConstants.BOOLEAN_OBJ_NAME)) {
0548:                    result = true;
0549:                } else if (type.equals(WizardConstants.BYTE_OBJ_NAME)) {
0550:                    result = true;
0551:                } else if (type.equals(WizardConstants.CHAR_OBJ_NAME)) {
0552:                    result = true;
0553:                } else if (type.equals(WizardConstants.INTEGER_OBJ_NAME)) {
0554:                    result = true;
0555:                } else if (type.equals(WizardConstants.LONG_OBJ_NAME)) {
0556:                    result = true;
0557:                } else if (type.equals(WizardConstants.FLOAT_OBJ_NAME)) {
0558:                    result = true;
0559:                } else if (type.equals(WizardConstants.DOUBLE_OBJ_NAME)) {
0560:                    result = true;
0561:                }
0562:                if (type.equals(WizardConstants.STRING_OBJ_NAME)) {
0563:                    result = true;
0564:                }
0565:                // XXX REVISIT
0566:                // As a workaround of types not being checked, we handle 
0567:                // implicit java.lang cases
0568:                // Experimental, for Add attributes Action checks
0569:                if (!type.contains("."))
0570:                    result = true;
0571:
0572:                return result;
0573:            }
0574:
0575:            /**
0576:             * Returns the default value of this type
0577:             * @param type <CODE>String</CODE> a type name
0578:             * @return <CODE>String</CODE> code which returns the corresponding default value.
0579:             */
0580:            public static String getDefaultValue(String type) {
0581:                String resultValue = "null";// NOI18N
0582:                if (type.equals(WizardConstants.BOOLEAN_NAME)) {
0583:                    resultValue = "false";// NOI18N
0584:                } else if (type.equals(WizardConstants.BYTE_NAME)) {
0585:                    resultValue = "(byte)0";// NOI18N
0586:                } else if (type.equals(WizardConstants.CHAR_NAME)) {
0587:                    resultValue = "(char)0";// NOI18N
0588:                } else if (type.equals(WizardConstants.INT_NAME)) {
0589:                    resultValue = "0";// NOI18N
0590:                } else if (type.equals(WizardConstants.LONG_NAME)) {
0591:                    resultValue = "0";// NOI18N
0592:                } else if (type.equals(WizardConstants.DOUBLE_NAME)) {
0593:                    resultValue = "0";// NOI18N
0594:                } else if (type.equals(WizardConstants.FLOAT_NAME)) {
0595:                    resultValue = "0";// NOI18N
0596:                }
0597:                return resultValue;
0598:            }
0599:
0600:            /**
0601:             * Returns the default value of this type
0602:             * @param type <CODE>String</CODE> a type name
0603:             * @return <CODE>String</CODE> code which returns the corresponding default value.
0604:             */
0605:            public static String getWrappedDefaultValue(String type) {
0606:                return "(" + getFullWrappedName(type) + ")null";// NOI18N
0607:            }
0608:
0609:            /**
0610:             * Returns all source groups which contains properties files related to this project.
0611:             * @param project <CODE>Project</CODE> a project
0612:             * @return <CODE>SourceGroup[]</CODE> source groups which contains properties files.
0613:             */
0614:            public static SourceGroup[] getPropSourceGroups(Project project) {
0615:                return ProjectUtils.getSources(project).getSourceGroups(
0616:                        WizardConstants.PROPERTIES_EXT);
0617:            }
0618:
0619:            /**
0620:             * Returns all source groups which contains java files related to this project.
0621:             * @param project <CODE>Project</CODE> a project
0622:             * @return <CODE>SourceGroup[]</CODE> source groups which contains java files.
0623:             */
0624:            public static SourceGroup[] getSourceGroups(Project project) {
0625:                return ProjectUtils
0626:                        .getSources(project)
0627:                        .getSourceGroups(
0628:                                org.netbeans.api.java.project.JavaProjectConstants.SOURCES_TYPE_JAVA);
0629:            }
0630:
0631:            /**
0632:             * Returns the corresponding package name of this folder.
0633:             * @param project <CODE>Project</CODE> a project
0634:             * @param packageFolder <CODE>FileObject</CODE> package folder 
0635:             * @return <CODE>String</CODE> corresponding package name of this folder.
0636:             */
0637:            public static String getPackageName(Project project,
0638:                    FileObject packageFolder) {
0639:                String packageName = null;
0640:                SourceGroup[] projectSrcGroups = getSourceGroups(project);
0641:                for (int i = 0; i < projectSrcGroups.length; i++) {
0642:                    FileObject srcFolder = projectSrcGroups[i].getRootFolder();
0643:                    if (srcFolder.equals(packageFolder)) {
0644:                        return "";// NOI18N
0645:                    }
0646:                    DataFolder srcGroupDataObj = DataFolder
0647:                            .findFolder(srcFolder);
0648:                    for (Enumeration files = srcGroupDataObj.children(true); files
0649:                            .hasMoreElements();) {
0650:                        DataObject file = (DataObject) files.nextElement();
0651:                        if (file.getPrimaryFile().equals(packageFolder)) {
0652:                            String srcFolderPath = FileUtil.toFile(srcFolder)
0653:                                    .getAbsolutePath();
0654:                            String packageFolderPath = FileUtil.toFile(
0655:                                    packageFolder).getAbsolutePath();
0656:                            String packageFolderPathTemp = packageFolderPath
0657:                                    .replace(File.separatorChar, '/');
0658:                            String srcFolderPathTemp = srcFolderPath.replace(
0659:                                    File.separatorChar, '/');
0660:                            packageName = packageFolderPathTemp.replaceFirst(
0661:                                    srcFolderPathTemp, "");// NOI18N
0662:                            if (packageName.indexOf("/") == 0) {// NOI18N
0663:                                packageName = packageName.replaceFirst("/", "");// NOI18N
0664:                            }
0665:                            packageName = packageName.replace('/', '.');
0666:                        }
0667:                    }
0668:                }
0669:                return packageName;
0670:            }
0671:
0672:            /**
0673:             * Returns the corresponding package name of this folder path.
0674:             * @param project <CODE>Project</CODE> a project
0675:             * @param packagePath <CODE>String</CODE> package folder path
0676:             * @return <CODE>String</CODE> corresponding package name of this folder path.
0677:             */
0678:            public static String getPackageName(Project project,
0679:                    String packagePath) {
0680:                String packageName = null;
0681:                SourceGroup[] projectSrcGroups = getSourceGroups(project);
0682:                for (int i = 0; i < projectSrcGroups.length; i++) {
0683:                    FileObject srcFolder = projectSrcGroups[i].getRootFolder();
0684:                    String srcFolderPath = FileUtil.toFile(srcFolder)
0685:                            .getAbsolutePath();
0686:                    if (srcFolderPath.equals(packagePath)) {
0687:                        return "";// NOI18N
0688:                    }
0689:                    if (packagePath.startsWith(srcFolderPath)) {
0690:                        packageName = packagePath.replace(File.separatorChar,
0691:                                '/').replaceFirst(
0692:                                srcFolderPath.replace(File.separatorChar, '/'),
0693:                                "");// NOI18N
0694:                        if (packageName.indexOf('/') == 0) {
0695:                            packageName = packageName.replaceFirst("/", "");// NOI18N
0696:                        }
0697:                        packageName = packageName.replace('/', '.');
0698:                        return packageName;
0699:                    }
0700:                }
0701:                return null;
0702:            }
0703:
0704:            /**
0705:             * Returns the absolute path of the folder of the class file path.
0706:             * ex: getFolderPath("/home/toto/class.java") returns "/home/toto/".
0707:             * @param classFilePath <CODE>String</CODE> class file path
0708:             * @return <CODE>String</CODE> class folder path.
0709:             */
0710:            public static String getFolderPath(String classFilePath) {
0711:                String folderPath = classFilePath;
0712:                int indexPoint = classFilePath.lastIndexOf(File.separatorChar);
0713:                if (indexPoint != -1) {
0714:                    folderPath = classFilePath.substring(0, indexPoint);
0715:                }
0716:                return folderPath;
0717:            }
0718:
0719:            /**
0720:             * Returns the class name of the complete class name
0721:             * @param competeClassName <CODE>String</CODE> a class name
0722:             * @return <CODE>String</CODE> a class name without package.
0723:             */
0724:            public static String getClassName(String competeClassName) {
0725:                String className = competeClassName;
0726:                int indexPoint = competeClassName.lastIndexOf('.');
0727:                if (indexPoint != -1) {
0728:                    className = competeClassName.substring(indexPoint + 1);
0729:                }
0730:                return className;
0731:            }
0732:
0733:            /**
0734:             * Returns the reversed package name as DNS model.
0735:             * @param packageName <CODE>String</CODE> a class name
0736:             * @return <CODE>String</CODE> reversed package name.
0737:             */
0738:            public static String reversePackageName(String packageName) {
0739:                String[] parts = packageName.replace('.', '/').split("/");// NOI18N
0740:                StringBuffer result = new StringBuffer();
0741:                for (int i = 0; i < parts.length; i++) {
0742:                    result.append(parts[parts.length - 1 - i]);
0743:                    if (i != parts.length - 1)
0744:                        result.append(".");// NOI18N
0745:                }
0746:                return result.toString();
0747:            }
0748:
0749:            /**
0750:             * Returns an array of this method param types.
0751:             * @param completeSignature <CODE>String</CODE> a method signature
0752:             * @return <CODE>String[]</CODE> an array of param types.
0753:             */
0754:            public static String[] getSignature(String completeSignature) {
0755:                int signBegin = completeSignature.lastIndexOf("(");// NOI18N
0756:                int signEnd = completeSignature.lastIndexOf(")");// NOI18N
0757:                String[] params = completeSignature.substring(signBegin + 1,
0758:                        signEnd).split(",");// NOI18N
0759:                if ((params.length == 1) && (params[0].equals("")))// NOI18N
0760:                    params = new String[] {};
0761:                for (int i = 0; i < params.length; i++)
0762:                    params[i] = params[i].trim();
0763:                return params;
0764:            }
0765:
0766:            /**
0767:             * Returns the file name of the complete file name with path
0768:             * @param completeFileName <CODE>String</CODE> a file path
0769:             * @return <CODE>String</CODE> the file name with the extension.
0770:             */
0771:            public static String getFileName(String completeFileName) {
0772:                String fileName = completeFileName;
0773:                int indexPoint = completeFileName
0774:                        .lastIndexOf(File.separatorChar);
0775:                if (indexPoint != -1) {
0776:                    fileName = completeFileName.substring(indexPoint + 1);
0777:                }
0778:                return fileName;
0779:            }
0780:
0781:            /**
0782:             * Returns the package name of the complete class name
0783:             * @param className <CODE>String</CODE> a class name
0784:             * @return <CODE>String</CODE> package name of this class.
0785:             */
0786:            public static String getPackageName(String className) {
0787:                String packageName = "";// NOI18N
0788:                int indexPoint = className.lastIndexOf('.');
0789:                if (indexPoint != -1) {
0790:                    packageName = className.substring(0, indexPoint);
0791:                }
0792:                return packageName;
0793:            }
0794:
0795:            /**
0796:             * Extracts info from the netbeans project to put it into the wizard map.
0797:             * @param wiz <CODE>WizardDescriptor</CODE> the current wizard
0798:             * @throws java.lang.Exception if Project Location is null
0799:             */
0800:            public static void setProjectValues(WizardDescriptor wiz)
0801:                    throws Exception {
0802:                String locationPath = getProjectLocation(wiz);
0803:
0804:                if (locationPath == null) {
0805:                    throw new Exception("Project Location is null");// NOI18N
0806:                }
0807:
0808:                wiz.putProperty(WizardConstants.PROP_PROJECT_LOCATION,
0809:                        locationPath);
0810:
0811:                FileObject targetFolder = Templates.getTargetFolder(wiz);
0812:                String target = "";// NOI18N
0813:
0814:                if (targetFolder != null) {
0815:                    String targetPath = File.separatorChar
0816:                            + targetFolder.getPath();
0817:
0818:                    String srcPath = locationPath + File.separatorChar
0819:                            + WizardConstants.SRC_DIR;
0820:
0821:                    int idx = targetPath.indexOf(srcPath);
0822:
0823:                    if (idx == 0) {
0824:                        if (targetPath.length() > srcPath.length()) {
0825:                            String tmp = targetPath.substring(1 + srcPath
0826:                                    .length());
0827:
0828:                            if (tmp != null)
0829:                                target = tmp.replace(File.separatorChar, '.');
0830:                        }
0831:                    }
0832:                }
0833:
0834:                wiz
0835:                        .putProperty(WizardConstants.PROP_MBEAN_PACKAGE_NAME,
0836:                                target);
0837:                wiz.putProperty(WizardConstants.PROP_PROJECT_NAME,
0838:                        getProjectName(wiz));
0839:            }
0840:
0841:            /**
0842:             * Logs an info message (used for debuging).Display a message in Netbeans 
0843:             * log file with severity = WARNING.
0844:             * @param message <CODE>String</CODE> message to log
0845:             */
0846:            public static void logInfoMessage(String message) {
0847:                ErrorManager.getDefault().log(ErrorManager.WARNING,
0848:                        "JMX Module : " + message);// NOI18N
0849:            }
0850:
0851:            /**
0852:             * Logs an error message (used for debuging).Display a message in Netbeans 
0853:             * log file with severity = EXCEPTION.
0854:             * @param message <CODE>String</CODE> message of this error
0855:             * @param ex <CODE>Exception</CODE> an exception
0856:             */
0857:            public static void logErrorMessage(String message, Exception ex) {
0858:                if (message != null) {
0859:                    if (ex != null) {
0860:                        System.out.println("JMX Module : " + message + // NOI18N
0861:                                " exception : " + ex.toString());// NOI18N
0862:                    } else {
0863:                        System.out.println("JMX Module : " + message);// NOI18N
0864:                    }
0865:                }
0866:
0867:                if (ex != null) {
0868:                    ErrorManager.getDefault().notify(ex);
0869:                } else {
0870:                    Exception e = new Exception("JMX Module : " + message);// NOI18N
0871:                    ErrorManager.getDefault().notify(e);
0872:                }
0873:            }
0874:
0875:            /**
0876:             * Reads one boolean option from the wizard map
0877:             * @param wiz <CODE>WizardDescriptor</CODE>
0878:             * @param prop <CODE>String</CODE> the key of the property
0879:             * @return <CODE>boolean</CODE> the correponding boolean property.
0880:             */
0881:            public static boolean readBoolOption(WizardDescriptor wiz,
0882:                    String prop) {
0883:                Boolean b = (Boolean) wiz.getProperty(prop);
0884:                return (b == null) || b.booleanValue();
0885:            }
0886:
0887:            /**
0888:             * Forces upper case for the first char of the given string.
0889:             * @param name <CODE>String</CODE> the text to force the first letter uppercase
0890:             * @return <CODE>String</CODE> name with its first letter upper case.
0891:             */
0892:            public static String capitalizeFirstLetter(String name) {
0893:                if (name.equals("")) {// NOI18N
0894:                    return "";// NOI18N
0895:                } else {
0896:                    String temp = name.trim();
0897:                    String temp1 = temp.substring(0, 1);
0898:                    String temp2 = temp.substring(1);
0899:                    temp1 = temp1.toUpperCase();
0900:                    return temp1 + temp2;
0901:                }
0902:            }
0903:
0904:            /**
0905:             * Forces lower case for the first char of the given string.
0906:             * @param name <CODE>String</CODE> the text to force the first letter lowercase
0907:             * @return <CODE>String</CODE> name with its first letter lower case.
0908:             */
0909:            public static String forceFirstLetterLowerCase(String name) {
0910:                if (name.equals("")) {// NOI18N
0911:                    return "";// NOI18N
0912:                } else {
0913:                    String temp = name.trim();
0914:                    String temp1 = temp.substring(0, 1);
0915:                    String temp2 = temp.substring(1);
0916:                    temp1 = temp1.toLowerCase();
0917:                    return temp1 + temp2;
0918:                }
0919:            }
0920:
0921:            /**
0922:             * Creates a file.
0923:             * @param filename <CODE>String</CODE>a file name
0924:             * @param path <CODE>String</CODE> a folder path
0925:             * @param extension <CODE>String</CODE> an file extension
0926:             * @param content <CODE>String</CODE> expected file content
0927:             * @throws java.io.IOException thrown by create file method
0928:             * @return <CODE>FileObject</CODE> the created file.
0929:             */
0930:            public static FileObject createFile(String filename, String path,
0931:                    String extension, String content) throws IOException {
0932:                String outputName = new String(path + File.separatorChar
0933:                        + filename + "." + extension);// NOI18N
0934:                // keep folder object
0935:                FileObject folder = createDir(path);
0936:                FileObject createdFile = FileUtil.createData(folder, filename
0937:                        + "." + extension);// NOI18N
0938:                FileLock lock = createdFile.lock();
0939:                OutputStream os = createdFile.getOutputStream(lock);
0940:                PrintStream ps = new PrintStream(os);
0941:                ps.print(content);
0942:                ps.flush();
0943:                ps.close();
0944:                lock.releaseLock();
0945:                return createdFile;
0946:
0947:            }
0948:
0949:            /**
0950:             * Creates a file.
0951:             * @param filename <CODE>String</CODE>a file name
0952:             * @param folder <CODE>FileObject</CODE> a folder object
0953:             * @param extension <CODE>String</CODE> an file extension
0954:             * @param content <CODE>String</CODE> expected file content
0955:             * @throws java.io.IOException thrown by create file method
0956:             * @return <CODE>FileObject</CODE> the created file.
0957:             */
0958:            public static FileObject createFile(String filename,
0959:                    FileObject folder, String extension, String content)
0960:                    throws IOException {
0961:                // keep folder object
0962:                FileObject createdFile = FileUtil.createData(folder, filename
0963:                        + "." + extension);// NOI18N
0964:                FileLock lock = createdFile.lock();
0965:                OutputStream os = createdFile.getOutputStream(lock);
0966:                PrintStream ps = new PrintStream(os);
0967:                ps.print(content);
0968:                ps.flush();
0969:                ps.close();
0970:                lock.releaseLock();
0971:                return createdFile;
0972:            }
0973:
0974:            /**
0975:             * Creates a directory if it doesn't already exist.
0976:             * @param dirPath <CODE>String</CODE> folder path
0977:             * @throws java.io.IOException thrown by create file methods
0978:             * @return <CODE>FileObject</CODE> the created folder object
0979:             */
0980:            public static FileObject createDir(String dirPath)
0981:                    throws IOException {
0982:                File dir = new File(dirPath);
0983:                if (!dir.exists()) {
0984:                    if (!dir.mkdirs())
0985:                        throw new IOException("Impossible to create " + dirPath);// NOI18N
0986:                }
0987:
0988:                FileObject dirFO = FileUtil.toFileObject(dir);
0989:                return dirFO;
0990:            }
0991:
0992:            /**
0993:             * Returns the project location of the given wiz.
0994:             * @param wiz <CODE>WizardDescriptor</CODE>
0995:             * @return <CODE>String</CODE> project location path
0996:             */
0997:            public static String getProjectLocation(WizardDescriptor wiz) {
0998:                try {
0999:                    Project project = Templates.getProject(wiz);
1000:                    FileObject proj_dir = project.getProjectDirectory();
1001:                    String path = proj_dir.getPath();
1002:                    return new String(File.separatorChar + path);
1003:
1004:                } catch (Exception ex) {
1005:                    return null;
1006:                }
1007:            }
1008:
1009:            /**
1010:             * Refresh the project logical tree view
1011:             * @param wiz <CODE>WizardDescriptor</CODE>
1012:             * @throws java.lang.Exception <CODE>Exception</CODE>
1013:             */
1014:            public static void refreshProjectTree(WizardDescriptor wiz)
1015:                    throws Exception {
1016:                Project project = Templates.getProject(wiz);
1017:                FileObject projectDir = project.getProjectDirectory();
1018:                projectDir.getFileSystem().getRoot().refresh();
1019:            }
1020:
1021:            /**
1022:             * Extracts the project name from the given wiz.
1023:             * @param wiz <CODE>WizardDescriptor</CODE> a wizard 
1024:             * @return <CODE>String</CODE> a project name
1025:             */
1026:            public static String getProjectName(WizardDescriptor wiz) {
1027:                try {
1028:                    Project project = Templates.getProject(wiz);
1029:                    Lookup projectLookup = project.getLookup();
1030:                    ProjectInformation info = (ProjectInformation) projectLookup
1031:                            .lookup(ProjectInformation.class);
1032:                    String name = info.getName();
1033:                    return name;
1034:                } catch (Exception ex) {
1035:                    return null;
1036:                }
1037:            }
1038:
1039:            public static Project getProject(FileObject file) {
1040:                Project[] projects = OpenProjects.getDefault()
1041:                        .getOpenProjects();
1042:                Project result = null;
1043:                for (int i = 0; i < projects.length; i++) {
1044:                    SourceGroup[] sources = getSourceGroups(projects[i]);
1045:                    try {
1046:                        for (int j = 0; j < sources.length; j++) {
1047:                            if (sources[j].contains(file))
1048:                                return projects[i];
1049:                        }
1050:                    } catch (IllegalArgumentException e) {
1051:                        // case of file is not contained in this project
1052:                    }
1053:                }
1054:
1055:                return FileOwnerQuery.getOwner(file);
1056:            }
1057:
1058:            /**
1059:             * Checks whether the given mbean already exists or not
1060:             * at the given location ONLY.
1061:             * @param name <CODE>String</CODE> a MBean name
1062:             * @param locationPath <CODE>String</CODE> a folder path
1063:             * @return <CODE>boolean</CODE> true if corresponding MBean files exist
1064:             */
1065:            public static boolean mbeanExists(String name, String locationPath) {
1066:                File locDir = new File(locationPath);
1067:
1068:                if (!locDir.exists())
1069:                    return false;
1070:
1071:                File mbeanFile = new File(locationPath + File.separatorChar
1072:                        + name + WizardConstants.MBEAN_ITF_SUFFIX + "."
1073:                        + WizardConstants.JAVA_EXT);// NOI18N
1074:
1075:                if (mbeanFile.exists())
1076:                    return true;
1077:
1078:                File supportFile = new File(locationPath + File.separatorChar
1079:                        + name + WizardConstants.MBEAN_SUPPORT_SUFFIX + "."
1080:                        + WizardConstants.JAVA_EXT);// NOI18N
1081:
1082:                if (supportFile.exists())
1083:                    return true;
1084:
1085:                File implfile = new File(locationPath + File.separatorChar
1086:                        + name + "." + WizardConstants.JAVA_EXT);// NOI18N
1087:
1088:                if (implfile.exists())
1089:                    return true;
1090:
1091:                return false;
1092:            }
1093:
1094:            /**
1095:             * Checks whether the given agent already exists or not
1096:             * at the given location ONLY.
1097:             * @param name <CODE>String</CODE> a file name
1098:             * @param locationPath <CODE>String</CODE> a folder path
1099:             * @return <CODE>boolean</CODE> true if the corresponding file exists
1100:             */
1101:            public static boolean fileExists(String name, String locationPath) {
1102:                File locDir = new File(locationPath);
1103:
1104:                if (!locDir.exists())
1105:                    return false;
1106:
1107:                File file = new File(locationPath + File.separatorChar + name);
1108:
1109:                if (file.exists())
1110:                    return true;
1111:                else
1112:                    return false;
1113:            }
1114:
1115:            /**
1116:             * Delete the file if already exists, else do nothing.
1117:             * @param name <CODE>String</CODE> a file name
1118:             * @param locationPath <CODE>String</CODE> a folder path
1119:             */
1120:            public static void removeFile(String name, String locationPath) {
1121:                File file = new File(locationPath + File.separatorChar + name);
1122:
1123:                if (file.exists())
1124:                    file.delete();
1125:            }
1126:
1127:            /**
1128:             * Checks whether the given agent already exists or not
1129:             * at the given location ONLY.
1130:             * @param fileName <CODE>String</CODE> a file path
1131:             * @return <CODE>boolean</CODE> true if the corresponding file exists
1132:             */
1133:            public static boolean fileExists(String fileName) {
1134:                File file = new File(fileName);
1135:
1136:                return file.exists();
1137:            }
1138:
1139:            public static boolean checkFile(String name, String locationPath) {
1140:                File locDir = new File(locationPath);
1141:
1142:                if (!locDir.exists())
1143:                    return false;
1144:
1145:                File file = new File(locationPath + File.separatorChar + name);
1146:
1147:                if (file.exists() && !file.isDirectory())
1148:                    return true;
1149:                else
1150:                    return false;
1151:            }
1152:
1153:            /**
1154:             * Display a dialog with expected content.
1155:             * @param content <CODE>String</CODE> expected content of the displayed dialog
1156:             * @param messageTitle <CODE>String</CODE> title of the dialog
1157:             * @param option <CODE>int</CODE> option of the dialog
1158:             * @param typeMessage <CODE>int</CODE> type of message
1159:             */
1160:            public static void displayMessage(String content,
1161:                    String messageTitle, int option, int typeMessage) {
1162:
1163:                int response = javax.swing.JOptionPane.showConfirmDialog(null,
1164:                        content, messageTitle, option, typeMessage);
1165:            }
1166:
1167:            /**
1168:             * Display a dialog with expected content.
1169:             * @param content <CODE>String</CODE> expected content
1170:             */
1171:            public static void displayMessage(String content) {
1172:
1173:                int response = javax.swing.JOptionPane.showConfirmDialog(null,
1174:                        content,
1175:                        "Error",// NOI18N
1176:                        javax.swing.JOptionPane.OK_OPTION,
1177:                        javax.swing.JOptionPane.ERROR_MESSAGE);
1178:            }
1179:
1180:            /**
1181:             * Returns if this string is already contained in the set.
1182:             * @return <code>boolean</code> false if this method is already contained 
1183:             * in the set, else true.
1184:             */
1185:            public static boolean containsString(Set set, String st) {
1186:                for (Iterator<String> it = set.iterator(); it.hasNext();) {
1187:                    String current = it.next();
1188:                    if (st.equals(current))
1189:                        return true;
1190:                }
1191:                return false;
1192:            }
1193:
1194:            /**
1195:             * Converts filename to the fully qualified name of the main class
1196:             * residing in the file.<br />
1197:             * For example: <tt>test/myapp/App.java</tt> --&gt; <tt>test.myapp.App</tt>
1198:             * @return corresponding package name. Null if the input is not
1199:             *          well formed.
1200:             * @param fileName <CODE>String</CODE> a file name
1201:             */
1202:            static String fileToClassName(String fileName) {
1203:                if (fileName.endsWith(".java")) { //NOI18N
1204:                    return (fileName.substring(0, fileName.length() - 5))
1205:                            .replace('/', '.');
1206:                } else
1207:                    return null;
1208:            }
1209:
1210:            /**
1211:             * Prints out the content of parts of the operation exception table model(used to debug).
1212:             * @param model <code>AbstractJMXTableModel</code> the model to parse
1213:             * @param toParse <code>int</code> the parameter in the model to parse
1214:             */
1215:            public static void printOperationExceptionModel(
1216:                    AbstractJMXTableModel model, int toParse) {
1217:                System.out
1218:                        .println("************* PARSE START *****************");// NOI18N
1219:
1220:                for (int i = 0; i < model.size(); i++) {
1221:                    ArrayList<MBeanOperationException> excepts = (ArrayList<MBeanOperationException>) model
1222:                            .getValueAt(i, toParse);
1223:                    for (int j = 0; j < excepts.size(); j++)
1224:                        System.out.println("Value of row " + i + ": "
1225:                                + excepts.get(j).getExceptionClass());// NOI18N
1226:                }
1227:            }
1228:
1229:            /**
1230:             * Creates a copy of the given array, except that <code>null</code> objects
1231:             * are omitted.
1232:             * The length of the returned array is (<var>l</var> - <var>n</var>), where
1233:             * <var>l</var> is length of the passed array and <var>n</var> is number
1234:             * of <code>null</code> elements of the array. Order of
1235:             * non-<code>null</code> elements is kept in the returned array.
1236:             * The returned array is always a new array, even if the passed
1237:             * array does not contain any <code>null</code> elements.
1238:             * @param objs array to copy
1239:             * @return array containing the same objects as the passed array, in the
1240:             *          same order, just with <code>null</code> elements missing
1241:             */
1242:            public static Object[] skipNulls(final Object[] objs) {
1243:                List resultList = new ArrayList(objs.length);
1244:
1245:                for (int i = 0; i < objs.length; i++) {
1246:                    if (objs[i] != null) {
1247:                        resultList.add(objs[i]);
1248:                    }
1249:                }
1250:
1251:                return resultList.isEmpty() ? new Object[0] : resultList
1252:                        .toArray();
1253:            }
1254:
1255:            /**
1256:             * Instantiates a new JTextField
1257:             * @return JTextField
1258:             */
1259:
1260:            public static JTextField instanciateJTextField() {
1261:
1262:                JTextField text = new JTextField();
1263:                text.setDragEnabled(true);
1264:                text.setEditable(true);
1265:
1266:                return text;
1267:            }
1268:
1269:            /**
1270:             * Instanciates a JComboBox with the variable types we provide
1271:             * @return JComboBox
1272:             */
1273:            public static JComboBox instanciateTypeJComboBox() {
1274:
1275:                JComboBox typeCombo = new JComboBox();
1276:
1277:                // the attribute's type combo box     
1278:                typeCombo.addItem(WizardConstants.BOOLEAN_NAME);
1279:                typeCombo.addItem(WizardConstants.BYTE_NAME);
1280:                typeCombo.addItem(WizardConstants.CHAR_NAME);
1281:                typeCombo.addItem(WizardConstants.DATE_OBJ_FULLNAME);
1282:                typeCombo.addItem(WizardConstants.INT_NAME);
1283:                typeCombo.addItem(WizardConstants.LONG_NAME);
1284:                typeCombo.addItem(WizardConstants.FLOAT_NAME);
1285:                typeCombo.addItem(WizardConstants.DOUBLE_NAME);
1286:                typeCombo.addItem(WizardConstants.OBJECTNAME_FULLNAME);
1287:                typeCombo.addItem(WizardConstants.STRING_OBJ_FULLNAME);
1288:                typeCombo.setSelectedItem(WizardConstants.STRING_OBJ_FULLNAME);
1289:                typeCombo.setEditable(true);
1290:
1291:                return typeCombo;
1292:            }
1293:
1294:            /**
1295:             * Instanciates a JComboBox with the return types we provide
1296:             * @return JComboBox
1297:             */
1298:            public static JComboBox instanciateRetTypeJComboBox() {
1299:
1300:                JComboBox retTypeJCombo = instanciateTypeJComboBox();
1301:                retTypeJCombo.addItem(WizardConstants.VOID_RET_TYPE);
1302:                retTypeJCombo.setSelectedItem(WizardConstants.VOID_RET_TYPE);
1303:
1304:                return retTypeJCombo;
1305:            }
1306:
1307:            /**
1308:             * Instanciates a JComboBox with the access types we provide (RO or R/W)
1309:             * @return JComboBox
1310:             */
1311:            public static JComboBox instanciateAccessJComboBox() {
1312:
1313:                JComboBox accessCombo = new JComboBox();
1314:
1315:                // the attribute's acces mode combo box
1316:                accessCombo.addItem(WizardConstants.ATTR_ACCESS_READ_WRITE);
1317:                accessCombo.addItem(WizardConstants.ATTR_ACCESS_READ_ONLY);
1318:                accessCombo
1319:                        .setSelectedItem(WizardConstants.ATTR_ACCESS_READ_WRITE);
1320:
1321:                return accessCombo;
1322:            }
1323:
1324:            /*public static boolean isOpened(FileObject fo) {
1325:                Set comps = TopComponent.getRegistry().getOpened();
1326:                for (Iterator<TopComponent> it = comps.iterator(); it.hasNext();) {
1327:                    Node[] arr = it.next().getActivatedNodes();
1328:                    if (arr != null) {
1329:                        for (int j = 0; j < arr.length; j++) {
1330:                            EditorCookie ec = (EditorCookie) arr[j].getCookie(EditorCookie.class);
1331:                            if (ec != null) {
1332:                                JEditorPane[] panes = ec.getOpenedPanes();
1333:                                if (panes != null) {
1334:                                    try {
1335:                                        System.out.println("panes path :" + panes[j].getPage());
1336:                                        if (fo.getURL().equals(panes[j].getPage()))
1337:                                            return true;
1338:                                    } catch (FileStateInvalidException e) {
1339:                                        e.printStackTrace();
1340:                                    }
1341:                                }
1342:                            }
1343:                        }
1344:                    }
1345:                }
1346:                return false;
1347:            }*/
1348:            public static void save(DataObject dO) throws IOException {
1349:                SaveCookie sc = (SaveCookie) dO.getCookie(SaveCookie.class);
1350:                if (null != sc)
1351:                    sc.save();
1352:            }
1353:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.