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

The following examples show how to use com.intellij.openapi.project.ProjectManager#getOpenProjects() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 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: 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;
}
 
Example 13
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 14
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 15
Source File: ProjectSettingsComponent.java    From Plugin with MIT License 4 votes vote down vote up
private void syncGears(){
    //Load settings
    SettingsManager.getInstance().loadSettings();

    //Get all projects
    ProjectManager pm = ProjectManager.getInstance();
    Project[] targetProjects = pm.getOpenProjects();
    Project p = targetProjects[0];

    //Load project settings
    SettingsManager.getInstance().loadProjectSettings(p);

    //Get all modules
    ModuleManager mm = ModuleManager.getInstance(p);
    Module[] targetModules = mm.getModules();

    //Get main module
    String mainModule = SettingsManager.getInstance().getMainModule();

    if (!mainModule.equals("")){
        //Find the module object from module name
        for (int ii = 0; ii < targetModules.length; ii++){
            //If module name matches target module, sync
            if (targetModules[ii].getName().equals(mainModule)){

                //Sync gears
                if (SettingsManager.getInstance().getAutoSync()){
                    SyncGears syncWorker = new SyncGears(p, targetModules[ii]){
                        @Override
                        protected void done() {
                            super.done();

                            //Possibly show toast here...
                        }
                    };
                    syncWorker.execute();
                }

                break;
            }
        }
    }
}