Java Code Examples for com.intellij.openapi.project.ProjectManager#getInstance()

The following examples show how to use com.intellij.openapi.project.ProjectManager#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentUndoProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeDocumentChange(@Nonnull DocumentEvent e) {
  Document document = e.getDocument();
  if (!shouldProcess(document)) {
    return;
  }

  handleBeforeDocumentChange(getUndoManager(null), document);

  ProjectManager projectManager = ProjectManager.getInstance();
  if (projectManager != null) {
    for (Project project : projectManager.getOpenProjects()) {
      handleBeforeDocumentChange(getUndoManager(project), document);
    }
  }
}
 
Example 2
Source File: BlazeProjectOpenProcessor.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Project doOpenProject(
    VirtualFile file, @Nullable Project projectToClose, boolean forceOpenInNewFrame) {
  ProjectManager pm = ProjectManager.getInstance();
  if (projectToClose != null) {
    pm.closeProject(projectToClose);
  }
  try {
    VirtualFile ideaSubdirectory = getIdeaSubdirectory(file);
    if (ideaSubdirectory == null) {
      return null;
    }
    VirtualFile projectSubdirectory = ideaSubdirectory.getParent();
    return pm.loadAndOpenProject(projectSubdirectory.getPath());
  } catch (IOException | JDOMException e) {
    return null;
  }
}
 
Example 3
Source File: RunContentManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
private CloseListener(@Nonnull final Content content, @Nonnull Executor executor) {
  myContent = content;
  content.getManager().addContentManagerListener(this);
  ProjectManager projectManager = ProjectManager.getInstance();
  myExecutor = executor;

  projectManager.addProjectManagerListener(this);
  myCloseRegisterVetoRemover = ((ProjectManagerEx)projectManager).registerCloseProjectVeto(project -> {
    if (project != myProject) return true;

    if (myContent == null) return true;

    final boolean canClose = closeQuery(true);
    if (canClose) {
      myContent.getManager().removeContent(myContent, true);
      myContent = null;
    }
    return canClose;
  });
}
 
Example 4
Source File: ConsoleLog.java    From aem-ide-tooling-4-intellij with Apache License 2.0 6 votes vote down vote up
@Override
public void notify(@NotNull Notification notification) {
    try {
        final ProjectManager projectManager = ProjectManager.getInstance();
        if(projectManager != null) {
            final Project[] openProjects = projectManager.getOpenProjects();
            if(openProjects.length == 0) {
                if(myModel != null) {
                    myModel.addNotification(notification);
                }
            }
            for(Project p : openProjects) {
                ConsoleLogProjectTracker consoleLogProjectTracker = getProjectComponent(p);
                if(consoleLogProjectTracker != null) {
                    consoleLogProjectTracker.printNotification(notification);
                }
            }
        } else {
            PluginManager.getLogger().error("Project Manager could not be retrieved");
        }
    } catch(Exception e) {
        PluginManager.getLogger().error("Could not Notify to Console Log Project Tracker", e);
    }
}
 
Example 5
Source File: VcsHelper.java    From azure-devops-intellij with MIT License 6 votes vote down vote up
/**
 * Finds the TFSVcs instance associated with the given file path
 * or null if the file isn't under a TFVC repo
 *
 * @param file
 * @return
 */
public static TFSVcs getTFSVcsByPath(final VirtualFile file) {
    final ProjectManager projectManager = ProjectManager.getInstance();
    if (projectManager != null) {
        final Project[] projects = projectManager.getOpenProjects();
        for (final Project project : projects) {
            final ProjectLevelVcsManager projectLevelVcsManager = ProjectLevelVcsManager.getInstance(project);
            if (projectLevelVcsManager != null) {
                final AbstractVcs vcs = projectLevelVcsManager.getVcsFor(file);
                if (vcs instanceof TFSVcs) {
                    return (TFSVcs) vcs;
                }
            }
        }
    }
    return null;
}
 
Example 6
Source File: BaseApplication.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected boolean canExit() {
  ProjectManagerEx projectManager = (ProjectManagerEx)ProjectManager.getInstance();
  Project[] projects = projectManager.getOpenProjects();
  for (Project project : projects) {
    if (!projectManager.canClose(project)) {
      return false;
    }
  }

  return true;
}
 
Example 7
Source File: ExternalSystemFacadeManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static Project findProject(@Nonnull IntegrationKey key) {
  final ProjectManager projectManager = ProjectManager.getInstance();
  for (Project project : projectManager.getOpenProjects()) {
    if (key.getIdeProjectName().equals(project.getName()) && key.getIdeProjectLocationHash().equals(project.getLocationHash())) {
      return project;
    }
  }
  return projectManager.getDefaultProject();
}
 
Example 8
Source File: LightPlatformTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static PsiDocumentManagerImpl clearUncommittedDocuments(@Nonnull Project project) {
  PsiDocumentManagerImpl documentManager = (PsiDocumentManagerImpl)PsiDocumentManager.getInstance(project);
  documentManager.clearUncommittedDocuments();

  ProjectManagerImpl projectManager = (ProjectManagerImpl)ProjectManager.getInstance();
  if (((DefaultProjectFactoryImpl)DefaultProjectFactory.getInstance()).isDefaultProjectInitialized()) {
    Project defaultProject = projectManager.getDefaultProject();
    ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(defaultProject)).clearUncommittedDocuments();
  }
  return documentManager;
}
 
Example 9
Source File: ExternalSystemTaskId.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public Project findProject() {
  final ProjectManager projectManager = ProjectManager.getInstance();
  for (Project project : projectManager.getOpenProjects()) {
    if (myProjectId.equals(getProjectId(project))) return project;
  }
  return null;
}
 
Example 10
Source File: FloobitsApplication.java    From floobits-intellij with Apache License 2.0 5 votes vote down vote up
private Project getProject(String path) {
    ProjectManager pm = ProjectManager.getInstance();
    Project[] openProjects = pm.getOpenProjects();
    for (Project project : openProjects) {
        if (path.equals(project.getBasePath())) {
            return project;
        }
    }
    VirtualFile file = LocalFileSystem.getInstance().findFileByIoFile(new File(path));
    Project openedProject;
    if (ProjectAttachProcessor.canAttachToProject() && file != null) {
        openedProject = PlatformProjectOpenProcessor.doOpenProject(file, null, false, -1, null, false);
    } else {
        openedProject = ProjectUtil.openOrImport(path, null, false);
    }
    if (openedProject == null) {
        try {
            String projectFilePath = ".idea/misc.xml";
            if (path.endsWith(projectFilePath)) {
                Flog.error("Attempted to open the project misc.xml file instead of the directory.");
                path = path.replace(projectFilePath, "");
            }
            openedProject = ProjectManager.getInstance().loadAndOpenProject(path);
        } catch (Throwable e) {
            Flog.error(e);
            API.uploadCrash(null, null, e);
            return null;
        }
    }
    // This is something Intellij does when a user opens a project from the menu:
    FileChooserUtil.setLastOpenedFile(openedProject, file);
    return openedProject;
}
 
Example 11
Source File: DocumentUndoProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void documentChanged(@Nonnull DocumentEvent e) {
  Document document = e.getDocument();
  if (!shouldProcess(document)) {
    return;
  }

  handleDocumentChanged(getUndoManager(null), document, e);
  ProjectManager projectManager = ProjectManager.getInstance();
  if (projectManager != null) {
    for (Project project : projectManager.getOpenProjects()) {
      handleDocumentChanged(getUndoManager(project), document, e);
    }
  }
}
 
Example 12
Source File: ProjectUtils.java    From EasyCode with MIT License 5 votes vote down vote up
/**
 * 获取当前项目对象
 *
 * @return 当前项目对象
 */
public static Project getCurrProject() {

    ProjectManager projectManager = ProjectManager.getInstance();
    Project[] openProjects = projectManager.getOpenProjects();
    if (openProjects.length == 0) {
        return projectManager.getDefaultProject();//正常情况下不会发生
    } else if (openProjects.length == 1) {
        // 只存在一个打开的项目则使用打开的项目
        return openProjects[0];
    }

    //如果有项目窗口处于激活状态
    try {
        WindowManager wm = WindowManager.getInstance();
        for (Project project : openProjects) {
            Window window = wm.suggestParentWindow(project);
            if (window != null && window.isActive()) {
                return project;
            }
        }
    } catch (Exception ignored) {
    }

    //否则使用默认项目
    return projectManager.getDefaultProject();
}
 
Example 13
Source File: DefaultProjectLocator.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public Project guessProjectForFile(final VirtualFile file) {
  ProjectManager projectManager = ProjectManager.getInstance();
  if (projectManager == null) return null;

  final Project[] projects = projectManager.getOpenProjects();
  if (projects.length == 1) {
    return !projects[0].isDisposed() ? projects[0] : null;
  }
  else {
    return null;
  }
}
 
Example 14
Source File: SettingsManager.java    From Plugin with MIT License 5 votes vote down vote up
public Boolean ignoreExists(){

        ProjectManager pm = ProjectManager.getInstance();
        targetProjects = pm.getOpenProjects();

        if (targetProjects.length > 0){
            Project project = targetProjects[0];

            //Get ignore file
            File ignoreFile = new File(project.getBasePath()+ Utils.pathSeparator()+".gitignore");

            if (ignoreFile.exists()){
                try {
                    //Read back ignore file
                    String ignoreFileString = FileUtils.readFileToString(ignoreFile);

                    //Return true if it exists
                    return  ignoreFileString.contains(IGNORE_STRING);
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
            else {
                if (createIgnore(ignoreFile)){
                    return true;
                }
            }
        }

        return false;
    }
 
Example 15
Source File: BlazeIcons.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static Icon loadForBuildSystem(String basename) {
  // Guessing the build system name may result in an NPE if (e.g.) it was called from a
  // unit-testing scenario where there aren't Application/ProjectManager instances yet.
  if (ApplicationManager.getApplication() == null || ProjectManager.getInstance() == null) {
    // Default to the blaze icons.
    return load("blaze/" + basename);
  } else {
    return load(Ascii.toLowerCase(Blaze.guessBuildSystemName()) + "/" + basename);
  }
}
 
Example 16
Source File: DefaultProjectLocator.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public Collection<Project> getProjectsForFile(VirtualFile file) {
  final ProjectManager projectManager = ProjectManager.getInstance();
  if (projectManager == null || file == null) { return new HashSet<Project>(); }
  final Project[] openProjects = projectManager.getOpenProjects();
  return Arrays.asList(openProjects);
}
 
Example 17
Source File: SettingsManager.java    From Plugin with MIT License 4 votes vote down vote up
private Boolean removeIgnoreEntry(){
    ProjectManager pm = ProjectManager.getInstance();
    targetProjects = pm.getOpenProjects();

    if (targetProjects.length > 0){
        Project project = targetProjects[0];

        //Get ignore file
        File gitignore = new File(project.getBasePath()+ Utils.pathSeparator()+".gitignore");
        File hgignoreFile = new File(project.getBasePath()+ Utils.pathSeparator()+".hgignore");
        File ignoreFile =null;

        //Find valid ignore file
        if (gitignore.exists()){
            ignoreFile = gitignore;
        }
        else if (hgignoreFile.exists()){
            ignoreFile = hgignoreFile;
        }

        if (ignoreFile != null){
            try {
                //Read back ignore file
                String ignoreFileString = FileUtils.readFileToString(ignoreFile);

                //remove entry
                ignoreFileString = ignoreFileString.replace(IGNORE_COMMENT_STRING, "");
                ignoreFileString = ignoreFileString.replace(IGNORE_CONTENT_STRING, "");

                //Write back changes to gitignore
                FileUtils.forceDelete(ignoreFile);
                FileUtils.write(ignoreFile, ignoreFileString);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        else {
            if (createIgnore(ignoreFile)){
                return true;
            }
        }
    }

    return true;
}
 
Example 18
Source File: SettingsManager.java    From Plugin with MIT License 4 votes vote down vote up
private Boolean addIgnoreEntry(){
    ProjectManager pm = ProjectManager.getInstance();
    targetProjects = pm.getOpenProjects();

    if (targetProjects.length > 0){
        Project project = targetProjects[0];

        //Get ignore file
        File gitignore = new File(project.getBasePath()+ Utils.pathSeparator()+".gitignore");
        File hgignoreFile = new File(project.getBasePath()+ Utils.pathSeparator()+".hgignore");
        File ignoreFile =null;

        //Find valid ignore file
        if (gitignore.exists()){
            ignoreFile = gitignore;
        }
        else if (hgignoreFile.exists()){
            ignoreFile = hgignoreFile;
        }

        if (ignoreFile.exists()){
            try {
                //Read back ignore file
                String ignoreFileString = FileUtils.readFileToString(ignoreFile);

                //Check if it exists already. If it does, we're good
                if (!ignoreFileString.contains(IGNORE_COMMENT_STRING) && !ignoreFileString.contains(IGNORE_CONTENT_STRING)){
                    //Add entry
                    ignoreFileString = ignoreFileString+IGNORE_STRING;

                    //Write back changes to gitignore
                    FileUtils.forceDelete(ignoreFile);
                    FileUtils.write(ignoreFile, ignoreFileString);
                }

                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        else {
            //If no gitignore exists, create one!
            if (createIgnore(gitignore)){
                return true;
            }
        }
    }

    return true;
}
 
Example 19
Source File: ProjectCloseScope.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Override
public void onScopeEnd(BlazeContext context) {
  ProjectManager projectManager = ProjectManager.getInstance();
  projectManager.removeProjectManagerListener(project, this);
}
 
Example 20
Source File: ProjectExecutor.java    From tmc-intellij with MIT License 4 votes vote down vote up
private boolean noProjectsAreOpen() {
    logger.info("Checking that at least one project is open.");
    ProjectManager projectManager = ProjectManager.getInstance();
    Project[] openProjects = projectManager.getOpenProjects();
    return openProjects.length == 0;
}