/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.projects.importing; import java.io.File; import java.io.IOException; import org.openide.NotifyDescriptor; import org.openide.TopManager; import org.openide.ErrorManager; import org.openide.filesystems.LocalFileSystem; import org.openide.filesystems.FileObject; import org.openide.loaders.DataFolder; import org.openide.util.NbBundle; import org.netbeans.modules.projects.*; /** * This class imports NetBeans project. * * @author Martin Ryzl * @version 1.0 */ public class ImportNetbeansProject implements Runnable { /** Name of project dir. */ private static final String projectsDir = "Projects"; // NOI18N File projectFile; public ImportNetbeansProject(File projectFile) { this.projectFile = projectFile; } public void run () { TopManager.getDefault ().setStatusText ( NbBundle.getMessage (ImportNetbeansProject.class, "MSG_ImportStarted")); // NOI18N try { LocalFileSystem lfsSource = getSourceFileSystem (); if (lfsSource == null) { NotifyDescriptor.Message msg = new NotifyDescriptor.Message ( NbBundle.getMessage (ImportNetbeansProject.class, "MSG_InvalidProjectDir", projectFile.getName ()), // NOI18N NotifyDescriptor.ERROR_MESSAGE ); TopManager.getDefault ().notify (msg); return; } if (isImportingAll ()) { NotifyDescriptor.Confirmation conf = new NotifyDescriptor.Confirmation ( NbBundle.getMessage (ImportNetbeansProject.class, "MSG_ImportAll"), // NOI18N NotifyDescriptor.YES_NO_OPTION ); if (NotifyDescriptor.YES_OPTION != TopManager.getDefault ().notify (conf)) { return; } // import all projects within the *Projects* directory FileObject fobjs[] = lfsSource.getRoot ().getChildren (); for (int i = 0; i < fobjs.length; i++) { if (fobjs[i].isFolder ()) importProject (fobjs[i]); } } else { FileObject fobj = lfsSource.getRoot ().getFileObject (projectFile.getName ()); importProject (fobj); } } finally { TopManager.getDefault ().setStatusText ( NbBundle.getMessage (ImportNetbeansProject.class, "MSG_ImportFinished")); // NOI18N } } // ------------------- private implementation ------------------------ private LocalFileSystem getSourceFileSystem () { File root = isImportingAll () ? projectFile : projectFile.getParentFile (); LocalFileSystem lfs = null; if (root != null && root.exists ()) { try { lfs = new LocalFileSystem (); lfs.setRootDirectory (root); } catch (Exception e) { lfs = null; } } return lfs; } private boolean isImportingAll () { return projectsDir.equals (projectFile.getName ()); } private void importProject (FileObject projectFolder) { ProjectDataObject impPrj = null; TopManager.getDefault ().setStatusText ( NbBundle.getMessage ( ImportNetbeansProject.class, "MSG_ProjectImportProgress", // NOI18N projectFolder.getNameExt ())); try { DataFolder pdo = null; DataFolder projects = TopManager.getDefault ().getPlaces ().folders ().projects (); pdo = DataFolder.findFolder (projectFolder); if (pdo instanceof ProjectDataObject) { impPrj = (ProjectDataObject) pdo.copy (projects); } else { NotifyDescriptor.Confirmation conf = new NotifyDescriptor.Confirmation ( NbBundle.getMessage (ImportNetbeansProject.class, "MSG_ImportAnyway"), // NOI18N NotifyDescriptor.YES_NO_OPTION ); if (NotifyDescriptor.YES_OPTION != TopManager.getDefault ().notify (conf)) { return; } // create a new project with the same name impPrj = ProjectDataObject.createProject (projects, projectFolder.getNameExt ()); ProjectDataObject.copyProjectContent (pdo, impPrj); } } catch (Exception e) { ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e); if (impPrj != null) { try { impPrj.delete (); } catch (IOException ioe) { // ignore } impPrj = null; } } if (impPrj != null) { // update status TopManager.getDefault ().setStatusText (NbBundle.getMessage ( ImportNetbeansProject.class, "MSG_ProjectImported", // NOI18N impPrj.getName ())); } else { NotifyDescriptor.Message msg = new NotifyDescriptor.Message ( NbBundle.getMessage (ImportNetbeansProject.class, "MSG_ProjectImportFailed", projectFolder.getName ()), // NOI18N NotifyDescriptor.WARNING_MESSAGE ); TopManager.getDefault ().notify (msg); } } }