Class ProjectManager

java.lang.Object
org.netbeans.api.project.ProjectManager

public final class ProjectManager extends Object
Manages loaded projects.
  • Method Details

    • getDefault

      @NonNull public static ProjectManager getDefault()
      Returns the singleton project manager instance.
      Returns:
      the default instance
    • mutex

      @NonNull public static org.openide.util.Mutex mutex()
      Get a read/write lock to be used for all project metadata accesses. All methods relating to recognizing and loading projects, saving them, getting or setting their metadata, etc. should be controlled by this mutex and be marked as read operations or write operations. Unless otherwise stated, project-related methods automatically acquire the mutex for you, so you do not necessarily need to pay attention to it; but you may directly acquire the mutex in order to ensure that a block of reads does not have any interspersed writes, or in order to ensure that a write is not clobbering an unrelated write, etc.
      Returns:
      a general read/write lock for project metadata operations of all sorts
    • mutex

      @NonNull public static org.openide.util.Mutex mutex(boolean autoSave, @NonNull Project project, @NonNull Project... otherProjects)
      Get a read/write lock to be used for project metadata accesses. The returned lock may be optimized to limit contention only on given project(s).
      Parameters:
      autoSave - if true the other most write operation automatically saves the passed project(s)
      project - the project to lock
      otherProjects - other projects to lock
      Returns:
      a general read/write lock for project metadata operations
      Since:
      1.59
    • findProject

      @CheckForNull public Project findProject(@NonNull org.openide.filesystems.FileObject projectDirectory) throws IOException, IllegalArgumentException
      Find an open project corresponding to a given project directory. Will be created in memory if necessary.

      Acquires read access.

      It is not guaranteed that the returned instance will be identical to that which is created by the appropriate ProjectFactory. In particular, the project manager is free to return only wrapper Project instances which delegate to the factory's implementation. If you know your factory created a particular project, you cannot safely cast the return value of this method to your project type implementation class; you should instead place an implementation of some suitable private interface into your project's lookup, which would be safely proxied.

      Parameters:
      projectDirectory - the project top directory
      Returns:
      the project (object identity may or may not vary between calls) or null if the directory is not recognized as a project by any registered ProjectFactory (might be null even if isProject(org.openide.filesystems.FileObject) returns true)
      Throws:
      IOException - if the project was recognized but could not be loaded
      IllegalArgumentException - if the supplied file object is null or not a folder
    • isProject

      public boolean isProject(@NonNull org.openide.filesystems.FileObject projectDirectory) throws IllegalArgumentException
      Check whether a given directory is likely to contain a project without actually loading it. Should be faster and use less memory than findProject(org.openide.filesystems.FileObject) when called on a large number of directories.

      The result is not guaranteed to be accurate; there may be false positives (directories for which isProject is true but findProject(org.openide.filesystems.FileObject) will return null), for example if there is trouble loading the project. False negatives are possible only if there are bugs in the project factory.

      Acquires read access.

      You do not need to call this method if you just plan to call findProject(org.openide.filesystems.FileObject) afterwards. It is intended for only those clients which would discard the result of findProject(org.openide.filesystems.FileObject) other than to check for null, and which can also tolerate false positives.

      Parameters:
      projectDirectory - a directory which may be some project's top directory
      Returns:
      true if the directory is likely to contain a project according to some registered ProjectFactory
      Throws:
      IllegalArgumentException - if the supplied file object is null or not a folder
    • isProject2

      @CheckForNull public ProjectManager.Result isProject2(@NonNull org.openide.filesystems.FileObject projectDirectory) throws IllegalArgumentException
      Check whether a given directory is likely to contain a project without actually loading it. The returned ProjectManager.Result object contains additional information about the found project. Should be faster and use less memory than findProject(org.openide.filesystems.FileObject) when called on a large number of directories.

      The result is not guaranteed to be accurate; there may be false positives (directories for which isProject2 is non-null but findProject(org.openide.filesystems.FileObject) will return null), for example if there is trouble loading the project. False negatives are possible only if there are bugs in the project factory.

      Acquires read access.

      You do not need to call this method if you just plan to call findProject(org.openide.filesystems.FileObject) afterwards. It is intended for only those clients which would discard the result of findProject(org.openide.filesystems.FileObject) other than to check for null, and which can also tolerate false positives.

      Parameters:
      projectDirectory - a directory which may be some project's top directory
      Returns:
      Result object if the directory is likely to contain a project according to some registered ProjectFactory, or null if not a project folder.
      Throws:
      IllegalArgumentException - if the supplied file object is null or not a folder
      Since:
      org.netbeans.modules.projectapi 1.22
    • clearNonProjectCache

      public void clearNonProjectCache()
      Clear the cached list of folders thought not to be projects. This may be useful after creating project metadata in a folder, etc. Cached project objects, i.e. folders that are known to be projects, are not affected.
    • getModifiedProjects

      @NonNull public Set<Project> getModifiedProjects()
      Get a list of all projects which are modified and need to be saved.

      Acquires read access.

      Returns:
      an immutable set of projects
    • isModified

      public boolean isModified(@NonNull Project p)
      Check whether a given project is current modified.

      Acquires read access.

      Parameters:
      p - a project loaded by this manager
      Returns:
      true if it is modified, false if has been saved since the last modification
    • saveProject

      public void saveProject(@NonNull Project p) throws IOException
      Save one project (if it was in fact modified).

      Acquires write access.

      Although the project infrastructure permits a modified project to be saved at any time, current UI principles dictate that the "save project" concept should be internal only - i.e. a project customizer should automatically save the project when it is closed e.g. with an "OK" button. Currently there is no UI display of modified projects; this module does not ensure that modified projects are saved at system exit time the way modified files are, though the Project UI implementation module currently does this check.

      Parameters:
      p - the project to save
      Throws:
      IOException - if it cannot be saved
      See Also:
    • saveAllProjects

      public void saveAllProjects() throws IOException
      Save all modified projects.

      Acquires write access.

      Throws:
      IOException - if any of them cannot be saved
      See Also:
    • isValid

      public boolean isValid(@NonNull Project p)
      Checks whether a project is still valid.

      Acquires read access.

      Parameters:
      p - a project
      Returns:
      true if the project is still valid, false if it has been deleted
      Since:
      1.6