Source Code Cross Referenced for ImportUtils.java in  » IDE-Netbeans » project.ant » org » netbeans » modules » projectimport » j2seimport » 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 » project.ant » org.netbeans.modules.projectimport.j2seimport 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * The Original Software is NetBeans. The Initial Developer of the Original
027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028:         * Microsystems, Inc. All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         */
041:
042:        package org.netbeans.modules.projectimport.j2seimport;
043:
044:        import java.io.File;
045:        import java.io.IOException;
046:        import java.net.URL;
047:        import java.util.ArrayList;
048:        import java.util.Collection;
049:        import java.util.HashMap;
050:        import java.util.Iterator;
051:        import java.util.List;
052:        import java.util.Map;
053:        import java.util.logging.Logger;
054:        import org.netbeans.api.java.platform.JavaPlatform;
055:        import org.netbeans.api.java.platform.JavaPlatformManager;
056:        import org.netbeans.api.java.project.JavaProjectConstants;
057:        import org.netbeans.api.progress.ProgressHandle;
058:        import org.netbeans.api.progress.ProgressHandleFactory;
059:        import org.netbeans.api.project.Project;
060:        import org.netbeans.api.project.ProjectManager;
061:        import org.netbeans.api.project.ant.AntArtifact;
062:        import org.netbeans.api.project.ant.AntArtifactQuery;
063:        import org.netbeans.modules.java.api.common.SourceRoots;
064:        import org.netbeans.modules.java.j2seplatform.platformdefinition.PlatformConvertor;
065:        import org.netbeans.modules.java.j2seplatform.wizard.NewJ2SEPlatform;
066:        import org.netbeans.modules.java.j2seproject.J2SEProject;
067:        import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator;
068:        import org.netbeans.modules.java.j2seproject.J2SEProjectType;
069:        import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
070:        import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender;
071:        import org.netbeans.spi.project.support.ant.AntProjectHelper;
072:        import org.netbeans.spi.project.support.ant.EditableProperties;
073:        import org.netbeans.spi.project.support.ant.PropertyUtils;
074:        import org.openide.filesystems.FileObject;
075:        import org.openide.filesystems.FileUtil;
076:        import org.openide.filesystems.Repository;
077:        import org.openide.loaders.DataFolder;
078:        import org.openide.loaders.DataObject;
079:        import org.openide.util.NbBundle;
080:        import org.openide.util.RequestProcessor;
081:        import org.w3c.dom.Element;
082:
083:        /**
084:         *
085:         * @author Radek Matous
086:         */
087:        public final class ImportUtils {
088:            public static final Logger logger = LoggerFactory.getDefault()
089:                    .createLogger(ImportUtils.class);
090:
091:            ImportProcessImpl importProcess = null;
092:
093:            public static final ImportProcess createImportProcess(
094:                    final FileObject projectDirectory,
095:                    final Collection allPrjDefs, boolean includeDependencies) {
096:                return new ImportProcessImpl(projectDirectory, allPrjDefs,
097:                        includeDependencies);
098:            }
099:
100:            //This method is expected just for testing purposes
101:            public static ImportUtils createInstance() {
102:                return new ImportUtils();
103:            }
104:
105:            public final void importAllProjects(
106:                    final FileObject projectDirectory,
107:                    final Collection allPrjDefs, WarningContainer warnings,
108:                    boolean includeDependencies) throws IOException {
109:                for (Iterator it = allPrjDefs.iterator(); it.hasNext();) {
110:                    ProjectModel projectDefinition = (ProjectModel) it.next();
111:                    FileObject importLocation = FileUtil.createFolder(
112:                            projectDirectory, projectDefinition.getName());
113:                    if (includeDependencies) {
114:                        importProject(importLocation, projectDefinition,
115:                                warnings, false);
116:                    } else {
117:                        J2SEProject nbProject = importProjectWithoutDependencies(
118:                                importLocation, projectDefinition, warnings,
119:                                false);
120:                        ProjectManager.getDefault().saveProject(nbProject);
121:                    }
122:                }
123:            }
124:
125:            public final J2SEProject importProject(
126:                    final FileObject projectDirectory,
127:                    final ProjectModel projectDefinition,
128:                    WarningContainer warnings, boolean isDependency)
129:                    throws IOException {
130:
131:                J2SEProject nbProject = importProjectWithoutDependencies(
132:                        projectDirectory, projectDefinition, warnings,
133:                        isDependency);
134:
135:                for (Iterator it = projectDefinition.getDependencies()
136:                        .iterator(); it.hasNext();) {
137:                    ProjectModel subPrjDef = (ProjectModel) it.next();
138:                    FileObject importLocation = FileUtil.createFolder(
139:                            projectDirectory.getParent(), subPrjDef.getName());
140:                    J2SEProject subJ2SEProject = importProject(importLocation,
141:                            subPrjDef, warnings, true);
142:                    addDependency(nbProject, subJ2SEProject);
143:                }
144:
145:                ProjectManager.getDefault().saveProject(nbProject);
146:                return nbProject;
147:            }
148:
149:            public final J2SEProject importProjectWithoutDependencies(
150:                    final FileObject projectDirectory,
151:                    final ProjectModel projectDefinition,
152:                    WarningContainer warnings, boolean isDependency)
153:                    throws IOException {
154:
155:                J2SEProject nbProject = null;
156:                {
157:                    Project prj = ProjectManager.getDefault().findProject(
158:                            projectDirectory);
159:                    if (prj != null) {
160:                        if (prj instanceof  J2SEProject) {
161:                            nbProject = (J2SEProject) prj;
162:                            logger.warning("Project already exists: "
163:                                    + projectDirectory.getPath());
164:                        } else {
165:                            throw new IllegalStateException();
166:                        }
167:                    }
168:                }
169:
170:                if (nbProject == null) {
171:                    if (!projectDirectory.isFolder()) {
172:                        throw new IllegalArgumentException();//NOI18N
173:                    }
174:                    File[] srcFiles = getSourceRoots(projectDefinition);
175:                    File destination = FileUtil.toFile(projectDirectory);
176:
177:                    if (!isDependency) {
178:                        addProgresInfo(projectDefinition.getName());
179:                    }
180:                    AntProjectHelper helper = J2SEProjectGenerator
181:                            .createProject(destination, projectDefinition
182:                                    .getName(), srcFiles, new File[] {}, null,
183:                                    null);
184:
185:                    assert helper != null;
186:                    nbProject = (J2SEProject) ProjectManager.getDefault()
187:                            .findProject(projectDirectory);
188:                    assert nbProject != null;
189:
190:                    if (!isDependency) {
191:                        addProgresInfo(projectDefinition.getName());
192:                    }
193:
194:                    //import source roots
195:                    warnings
196:                            .addAll(addSourceRoots(projectDefinition, nbProject));
197:
198:                    if (!isDependency) {
199:                        addProgresInfo(projectDefinition.getName());
200:                    }
201:                    //import libraries
202:                    try {
203:                        warnings.addAll(addLibraries(projectDefinition,
204:                                nbProject));
205:                    } catch (IOException iex) {
206:                        ImportUtils.addWarning(warnings, iex
207:                                .getLocalizedMessage());
208:                    }
209:
210:                    if (!isDependency) {
211:                        addProgresInfo(projectDefinition.getName());
212:                    }
213:                    //import user libraries
214:                    try {
215:                        warnings.addAll(addUserLibraries(projectDefinition,
216:                                nbProject));
217:                    } catch (IOException iex) {
218:                        ImportUtils.addWarning(warnings, iex
219:                                .getLocalizedMessage());
220:                    }
221:
222:                    if (!isDependency) {
223:                        addProgresInfo(projectDefinition.getName());
224:                    }
225:                    if (projectDefinition.getJDKDirectory() != null) {
226:                        warnings.addAll(addJavaPlatform(projectDefinition,
227:                                helper));
228:                    }
229:
230:                    if (importProcess != null) {
231:                        importProcess.addWarnings(projectDefinition
232:                                .getWarnings());
233:                        importProcess.addProjectToOpen(nbProject);
234:                    }
235:
236:                }
237:                return nbProject;
238:            }
239:
240:            private WarningContainer/*<String> warnings*/addJavaPlatform(
241:                    final ProjectModel prjDefinition,
242:                    final AntProjectHelper helper) {
243:
244:                WarningContainer warnings = new WarningContainer();
245:                JavaPlatform platform = null;
246:
247:                if (JavaPlatformManager.getDefault() == null) {
248:                    ImportUtils
249:                            .addWarning(warnings,
250:                                    "critical error: default platform manager isn't reachable");//NOI18N
251:                    return warnings;
252:                }
253:
254:                {
255:                    JavaPlatform defaultPlatform = JavaPlatformManager
256:                            .getDefault().getDefaultPlatform();
257:                    if (defaultPlatform != null
258:                            && isRepresentationOfPlatform(defaultPlatform,
259:                                    prjDefinition.getJDKDirectory(), warnings)) {
260:                        //no special handling for default platform
261:                        return warnings;
262:                    }
263:                }
264:
265:                JavaPlatform[] platforms = JavaPlatformManager.getDefault()
266:                        .getInstalledPlatforms();
267:                for (int i = 0; i < platforms.length; i++) {
268:                    if (isRepresentationOfPlatform(platforms[i], prjDefinition
269:                            .getJDKDirectory(), warnings)) {
270:                        platform = platforms[i];
271:                        break;
272:                    }
273:                }
274:
275:                if (platform == null) {
276:                    try {
277:                        platform = createNewJavaPlatform(prjDefinition,
278:                                warnings);
279:                    } catch (IOException iex) {
280:                        ImportUtils.addWarning(warnings, iex
281:                                .getLocalizedMessage());
282:                    }
283:                }
284:
285:                if (platform != null) {
286:                    setJavaPlatform(platform, helper);
287:                } else {
288:                    ImportUtils.addWarning(warnings,
289:                            "Setting of platform for project \"" // NOI18N
290:                                    + prjDefinition.getName() + "\" failed."); // NOI18N();
291:                }
292:
293:                return warnings;
294:            }
295:
296:            private void setJavaPlatform(final JavaPlatform platform,
297:                    final AntProjectHelper helper) {
298:                Element pcd = helper.getPrimaryConfigurationData(true);
299:                Element el = pcd.getOwnerDocument().createElementNS(
300:                        J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,
301:                        "explicit-platform"); // NOI18N
302:
303:                pcd.appendChild(el);
304:                helper.putPrimaryConfigurationData(pcd, true);
305:                EditableProperties prop = helper
306:                        .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
307:                String ver = platform.getSpecification().getVersion()
308:                        .toString();
309:                String normalizedName = (String) platform.getProperties().get(
310:                        "platform.ant.name"); // NOI18N
311:
312:                prop.setProperty(J2SEProjectProperties.JAVAC_SOURCE, ver);
313:                prop.setProperty(J2SEProjectProperties.JAVAC_TARGET, ver);
314:                prop.setProperty(J2SEProjectProperties.JAVA_PLATFORM,
315:                        normalizedName);
316:                helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,
317:                        prop);
318:            }
319:
320:            private JavaPlatform createNewJavaPlatform(
321:                    final ProjectModel prjDefinition,
322:                    final WarningContainer warnings) throws IOException {
323:                JavaPlatform retVal = null;
324:                FileObject foForJDKDirectory = FileUtil
325:                        .toFileObject(prjDefinition.getJDKDirectory());
326:
327:                if (foForJDKDirectory == null) {
328:                    addWarning(warnings, NbBundle.getMessage(ImportUtils.class,
329:                            "MSG_JDKDoesnExistUseDefault", // NOI18N
330:                            prjDefinition.getName(), prjDefinition
331:                                    .getJDKDirectory().getAbsolutePath()));
332:
333:                    return null;
334:                }
335:
336:                JavaPlatform[] installedPlatforms = JavaPlatformManager
337:                        .getDefault().getInstalledPlatforms();
338:                NewJ2SEPlatform platform = NewJ2SEPlatform
339:                        .create(foForJDKDirectory);
340:                platform.run();
341:
342:                if (platform.isValid() && platform.findTool("javac") != null) {//NOI18N
343:                    platform
344:                            .setDisplayName(createPlatformDisplayName(platform));
345:                    platform.setAntName(createPlatformAntName(platform
346:                            .getDisplayName(), installedPlatforms));
347:
348:                    FileObject platformsFolder = Repository
349:                            .getDefault()
350:                            .getDefaultFileSystem()
351:                            .findResource(
352:                                    "Services/Platforms/org-netbeans-api-java-Platform"); //NOI18N
353:                    assert platformsFolder != null;
354:
355:                    DataObject dobj = PlatformConvertor.create(platform,
356:                            DataFolder.findFolder(platformsFolder), platform
357:                                    .getAntName());
358:
359:                    retVal = (JavaPlatform) dobj.getNodeDelegate().getLookup()
360:                            .lookup(JavaPlatform.class);
361:
362:                    // update installed platform - probably some trick
363:                    JavaPlatformManager.getDefault().getInstalledPlatforms();
364:                }
365:
366:                return retVal;
367:            }
368:
369:            private String createPlatformDisplayName(JavaPlatform plat) {
370:                Map m = plat.getSystemProperties();
371:                String vmName = (String) m.get("java.vm.name"); // NOI18N
372:                String vmVersion = (String) m.get("java.vm.version"); // NOI18N
373:                StringBuffer displayName = new StringBuffer();
374:                if (vmName != null)
375:                    displayName.append(vmName);
376:                if (vmVersion != null) {
377:                    if (displayName.length() > 0) {
378:                        displayName.append(" ");
379:                    }
380:                    displayName.append(vmVersion);
381:                }
382:                return displayName.toString();
383:            }
384:
385:            private String createPlatformAntName(String displayName,
386:                    JavaPlatform[] installedPlatforms) {
387:                assert displayName != null && displayName.length() > 0;
388:                String antName = PropertyUtils
389:                        .getUsablePropertyName(displayName);
390:                if (platformExists(antName, installedPlatforms)) {
391:                    String baseName = antName;
392:                    int index = 1;
393:                    antName = baseName + Integer.toString(index);
394:                    while (platformExists(antName, installedPlatforms)) {
395:                        index++;
396:                        antName = baseName + Integer.toString(index);
397:                    }
398:                }
399:                return antName;
400:            }
401:
402:            /**
403:             * Checks if the platform of given antName is already installed
404:             */
405:            private boolean platformExists(String antName,
406:                    JavaPlatform[] installedPlatforms) {
407:                assert antName != null && antName.length() > 0;
408:                for (int i = 0; i < installedPlatforms.length; i++) {
409:                    String otherName = (String) installedPlatforms[i]
410:                            .getProperties().get("platform.ant.name"); //NOI18N
411:                    if (antName.equals(otherName)) {
412:                        return true;
413:                    }
414:                }
415:                return false;
416:            }
417:
418:            private boolean isRepresentationOfPlatform(
419:                    final JavaPlatform platform, final File jdkDirectory,
420:                    final WarningContainer warnings) {
421:                Collection installFolders = platform.getInstallFolders();
422:
423:                //shouldn't occure according to javadoc but there is possible some sort of inconsistency
424:                boolean invalidDefaultPlatform = installFolders.isEmpty();
425:
426:                if (invalidDefaultPlatform) {
427:                    addWarning(warnings, NbBundle.getMessage(ImportUtils.class,
428:                            "MSG_NotValidPlatformsInNB"));//NOI18N
429:                }
430:
431:                return invalidDefaultPlatform ? false : jdkDirectory
432:                        .equals(FileUtil.toFile((FileObject) installFolders
433:                                .toArray()[0]));
434:            }
435:
436:            public void addDependency(final J2SEProject nbProject,
437:                    final J2SEProject nbSubProject) throws IOException {
438:                ProjectClassPathExtender nbClsPath = (ProjectClassPathExtender) nbProject
439:                        .getLookup().lookup(ProjectClassPathExtender.class);
440:
441:                AntArtifact[] artifact = AntArtifactQuery.findArtifactsByType(
442:                        nbSubProject, JavaProjectConstants.ARTIFACT_TYPE_JAR);
443:
444:                nbClsPath.addAntArtifact(artifact[0], artifact[0]
445:                        .getArtifactLocations()[0]);
446:            }
447:
448:            private WarningContainer/*<String> warnings*/addSourceRoots(
449:                    final ProjectModel projectDefinition,
450:                    final J2SEProject nbProject) {
451:
452:                SourceRoots roots = nbProject.getSourceRoots();
453:                URL[] rootURLs = roots.getRootURLs();
454:                String[] labels = new String[rootURLs.length];
455:                Map srcRoots2import = new HashMap();
456:
457:                for (Iterator it = projectDefinition.getSourceRoots()
458:                        .iterator(); it.hasNext();) {
459:                    ProjectModel.SourceRoot srcEntry = (ProjectModel.SourceRoot) it
460:                            .next();
461:                    File srcFolder = FileUtil.normalizeFile(srcEntry
462:                            .getDirectory());
463:                    //assert srcFolder.exists();
464:                    srcRoots2import.put(srcFolder, srcEntry.getLabel());
465:                }
466:
467:                for (int i = 0; i < rootURLs.length; i++) {
468:                    File f = new File(rootURLs[i].getFile());
469:                    String lb = (String) srcRoots2import.get(f);
470:                    labels[i] = (lb != null) ? lb : f.getName();
471:                    //assert labels[i] != null;
472:                }
473:
474:                roots.putRoots(rootURLs, labels);
475:                return WarningContainer.EMPTY;
476:            }
477:
478:            private WarningContainer/*<String> warnings*/addUserLibraries(
479:                    final ProjectModel projectDefinition,
480:                    final J2SEProject nbProject) throws IOException {
481:
482:                WarningContainer warnings = new WarningContainer();
483:
484:                for (Iterator it = projectDefinition.getUserLibraries()
485:                        .iterator(); it.hasNext();) {
486:                    ProjectModel.UserLibrary userLibrary = (ProjectModel.UserLibrary) it
487:                            .next();
488:                    ProjectClassPathExtender nbClsPath = (ProjectClassPathExtender) nbProject
489:                            .getLookup().lookup(ProjectClassPathExtender.class);
490:                    assert nbClsPath != null;
491:                    List allLibs = getAllLibraries(null, userLibrary);
492:
493:                    for (Iterator itUL = allLibs.iterator(); itUL.hasNext();) {
494:                        ProjectModel.Library lEntry = (ProjectModel.Library) itUL
495:                                .next();
496:                        try {
497:                            warnings.addAll(addLibrary(nbClsPath, lEntry,
498:                                    projectDefinition));
499:                        } catch (IOException iex) {
500:                            ImportUtils.addWarning(warnings, iex
501:                                    .getLocalizedMessage());
502:                        }
503:
504:                    }
505:                }
506:
507:                return warnings;
508:            }
509:
510:            private List getAllLibraries(List allLibs,
511:                    final ProjectModel.UserLibrary userLibrary) {
512:                allLibs = (allLibs == null) ? new ArrayList() : allLibs;
513:                allLibs.addAll(userLibrary.getLibraries());
514:                for (Iterator it = userLibrary.getDependencies().iterator(); it
515:                        .hasNext();) {
516:                    ProjectModel.UserLibrary uLDep = (ProjectModel.UserLibrary) it
517:                            .next();
518:                    getAllLibraries(allLibs, uLDep);
519:                }
520:                return allLibs;
521:            }
522:
523:            private WarningContainer/*<String> warnings*/addLibraries(
524:                    final ProjectModel projectDefinition,
525:                    final J2SEProject nbProject) throws IOException {
526:
527:                WarningContainer warnings = new WarningContainer();
528:                ProjectClassPathExtender nbClsPath = (ProjectClassPathExtender) nbProject
529:                        .getLookup().lookup(ProjectClassPathExtender.class);
530:                assert nbClsPath != null;
531:
532:                for (Iterator it = projectDefinition.getLibraries().iterator(); it
533:                        .hasNext();) {
534:                    ProjectModel.Library lEntry = (ProjectModel.Library) it
535:                            .next();
536:                    try {
537:                        warnings.addAll(addLibrary(nbClsPath, lEntry,
538:                                projectDefinition));
539:                    } catch (IOException iex) {
540:                        ImportUtils.addWarning(warnings, iex
541:                                .getLocalizedMessage());
542:                    }
543:                }
544:
545:                return warnings;
546:            }
547:
548:            private WarningContainer addLibrary(
549:                    final ProjectClassPathExtender nbClsPath,
550:                    final ProjectModel.Library lEntry,
551:                    final ProjectModel projectDefinition) throws IOException {
552:                WarningContainer warnings = new WarningContainer();
553:                FileObject archiv = FileUtil.toFileObject(lEntry.getArchiv());
554:
555:                nbClsPath.addArchiveFile(archiv);
556:                return warnings;
557:            }
558:
559:            private static void addWarning(final WarningContainer warnings,
560:                    final String warning) {
561:                StringBuffer sbuf = new StringBuffer(NbBundle.getMessage(
562:                        ImportUtils.class, "MSG_ImportWarning"));//NOI18N
563:                sbuf.append(" ").append(warning);//NOI18N
564:
565:                String warningPlusPrefix = sbuf.toString();
566:                warnings.add(warningPlusPrefix, false);
567:                logger.warning(warningPlusPrefix);
568:            }
569:
570:            private File[] getSourceRoots(final ProjectModel projectDefinition) {
571:                Collection/*<ProjectDefinition.SourceRootEntry>*/sourceRootEntries = projectDefinition
572:                        .getSourceRoots();
573:                File[] retVal = new File[sourceRootEntries.size()];
574:
575:                int i = 0;
576:                for (Iterator it = sourceRootEntries.iterator(); it.hasNext(); i++) {
577:                    ProjectModel.SourceRoot entry = (ProjectModel.SourceRoot) it
578:                            .next();
579:                    retVal[i] = entry.getDirectory();
580:                }
581:
582:                return retVal;
583:            }
584:
585:            private void addProgresInfo(String projectName) {
586:                if (importProcess != null) {
587:                    importProcess.increase();
588:                    int step = importProcess.getCurrentStep();
589:                    int idx = (step % ImportProcessImpl.PROGRESS_MESSAGES.length);
590:                    importProcess.setCurrentStatus(NbBundle.getMessage(
591:                            ImportUtils.class,
592:                            ImportProcessImpl.PROGRESS_MESSAGES[idx],
593:                            projectName));
594:                    logger.fine(importProcess.toString());
595:                }
596:            }
597:
598:            /** Creates a new instance of Utilities */
599:            private ImportUtils() {
600:            }
601:
602:            private static final class ImportProcessImpl implements 
603:                    ImportProcess {
604:                final int numberOfSteps;
605:                int currentStep = -1;
606:                boolean isFinished;
607:                String currentStatus = "";//NOI18N
608:                final FileObject projectDirectory;
609:                final Collection allPrjDefs;
610:                final WarningContainer warnings;
611:                final ImportUtils importer;
612:                Collection projectsToOpen;
613:                final ProgressHandle ph;
614:                boolean includeDependencies;
615:                static final String[] PROGRESS_MESSAGES = new String[] {
616:                        "PRGS_ProjectImportStarted", "PRGS_ImportSourceRoots",
617:                        "PRGS_ImportLibraries", "PRGS_ImportUserLibraries",
618:                        "PRGS_ImportPlatform" };
619:
620:                private ImportProcessImpl(final FileObject projectDirectory,
621:                        final Collection allPrjDefs, boolean includeDependencies) {
622:                    this .projectDirectory = projectDirectory;
623:                    this .allPrjDefs = allPrjDefs;
624:                    this .warnings = new WarningContainer();
625:                    this .importer = ImportUtils.createInstance();
626:                    this .importer.importProcess = this ;
627:                    this .numberOfSteps = allPrjDefs.size()
628:                            * PROGRESS_MESSAGES.length;
629:                    projectsToOpen = new ArrayList();
630:                    this .includeDependencies = includeDependencies;
631:                    this .ph = ProgressHandleFactory
632:                            .createHandle(projectDirectory.getPath());//NOI18N
633:                    assert this .ph != null;
634:                }
635:
636:                public synchronized int getNumberOfSteps() {
637:                    return numberOfSteps;
638:                }
639:
640:                public synchronized int getCurrentStep() {
641:                    return currentStep;
642:                }
643:
644:                public synchronized String getCurrentStatus() {
645:                    return currentStatus;
646:                }
647:
648:                private synchronized void setCurrentStatus(String currentStatus) {
649:                    this .currentStatus = currentStatus;
650:                }
651:
652:                public void startImport(boolean asynchronous) {
653:                    if (isFinished()) {
654:                        throw new IllegalStateException();
655:                    }
656:
657:                    ph.start(getNumberOfSteps());
658:                    if (asynchronous) {
659:                        RequestProcessor.getDefault().post(new Runnable() {
660:                            public void run() {
661:                                ProjectManager.mutex().writeAccess(
662:                                        new Runnable() {
663:                                            public void run() {
664:                                                startImport();
665:                                            }
666:                                        });
667:                            }
668:                        });
669:
670:                    } else {
671:                        ProjectManager.mutex().writeAccess(new Runnable() {
672:                            public void run() {
673:                                startImport();
674:                            }
675:                        });
676:                    }
677:                }
678:
679:                private void startImport() {
680:                    increase();
681:                    try {
682:                        importer.importAllProjects(projectDirectory,
683:                                allPrjDefs, warnings, includeDependencies);
684:                    } catch (IOException iex) {
685:                        //warnings.add(iex.getLocalizedMessage());
686:                        addWarning(warnings, iex.getLocalizedMessage());
687:                    } finally {
688:                        setFinished();
689:                    }
690:                }
691:
692:                public synchronized boolean isFinished() {
693:                    return isFinished;
694:                }
695:
696:                private synchronized void increase() {
697:                    assert currentStep <= numberOfSteps;
698:
699:                    this .currentStep = this .currentStep + 1;
700:                    ph.progress(getCurrentStatus(), this .currentStep);
701:                }
702:
703:                private synchronized void setFinished() {
704:                    ph.finish();
705:                    currentStep = numberOfSteps;
706:                    isFinished = true;
707:                }
708:
709:                public synchronized WarningContainer getWarnings() {
710:                    return warnings;
711:                }
712:
713:                private synchronized void addWarnings(WarningContainer warnings) {
714:                    this .warnings.addAll(warnings);
715:                }
716:
717:                public synchronized String toString() {
718:                    StringBuffer sb = new StringBuffer();
719:                    sb.append("current step: ").append(
720:                            new Integer(getCurrentStep()).toString())
721:                            .//NOI18N
722:                            append(" from: ").append(getNumberOfSteps())
723:                            .append(" current info: ").append(
724:                                    getCurrentStatus());//NOI18N
725:                    return sb.toString();
726:                }
727:
728:                private void addProjectToOpen(Project prj) {
729:                    projectsToOpen.add(prj);
730:                }
731:
732:                public Project[] getProjectsToOpen() {
733:                    return (Project[]) projectsToOpen
734:                            .toArray(new Project[projectsToOpen.size()]);
735:                }
736:
737:                public ProgressHandle getProgressHandle() {
738:                    return ph;
739:                }
740:            }
741:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.