修复打开多项目时获取当前项目问题

This commit is contained in:
tangcent 2020-02-14 18:45:37 +08:00
parent a498217262
commit cc0a6e1d25
4 changed files with 56 additions and 11 deletions

View File

@ -0,0 +1,49 @@
package com.sjhy.plugin.tool;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.wm.WindowManager;
import java.awt.*;
/**
* IDEA项目相关工具
*
* @author tangcent
* @version 1.0.0
* @since 2020/02/14 18:35
*/
public class ProjectUtils {
/**
* 获取当前项目对象
*
* @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();
}
}

View File

@ -12,6 +12,7 @@ import com.sjhy.plugin.entity.GlobalConfig;
import com.sjhy.plugin.entity.GlobalConfigGroup;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.config.Settings;
import com.sjhy.plugin.tool.ProjectUtils;
import com.sjhy.plugin.ui.base.BaseGroupPanel;
import com.sjhy.plugin.ui.base.BaseItemSelectPanel;
import com.sjhy.plugin.ui.base.TemplateEditor;
@ -88,11 +89,8 @@ public class GlobalConfigSettingPanel implements Configurable {
* 默认构造方法
*/
GlobalConfigSettingPanel() {
// 存在打开的项目则使用打开的项目否则使用默认项目
ProjectManager projectManager = ProjectManager.getInstance();
Project[] openProjects = projectManager.getOpenProjects();
// 项目对象
this.project = openProjects.length > 0 ? openProjects[0] : projectManager.getDefaultProject();
this.project = ProjectUtils.getCurrProject();
// 配置服务实例化
this.settings = Settings.getInstance();
// 克隆对象

View File

@ -7,7 +7,6 @@ import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.UnnamedConfigurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.ui.*;
import com.intellij.openapi.ui.ex.MultiLineLabel;
import com.intellij.util.ExceptionUtil;
@ -17,6 +16,7 @@ import com.sjhy.plugin.constants.StrState;
import com.sjhy.plugin.entity.*;
import com.sjhy.plugin.tool.CollectionUtil;
import com.sjhy.plugin.tool.HttpUtils;
import com.sjhy.plugin.tool.ProjectUtils;
import com.sjhy.plugin.tool.StringUtils;
import com.sjhy.plugin.ui.base.ListCheckboxPanel;
import org.jetbrains.annotations.Nls;
@ -91,8 +91,8 @@ public class MainSetting implements Configurable, Configurable.Composite {
* 默认构造方法
*/
public MainSetting() {
// 获取默认项目
Project project = ProjectManager.getInstance().getDefaultProject();
// 获取当前项目
Project project = ProjectUtils.getCurrProject();
init();
//初始化事件

View File

@ -41,6 +41,7 @@ import com.sjhy.plugin.service.CodeGenerateService;
import com.sjhy.plugin.service.TableInfoService;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.tool.CollectionUtil;
import com.sjhy.plugin.tool.ProjectUtils;
import com.sjhy.plugin.ui.base.BaseGroupPanel;
import com.sjhy.plugin.ui.base.BaseItemSelectPanel;
import com.sjhy.plugin.ui.base.TemplateEditor;
@ -117,11 +118,8 @@ public class TemplateSettingPanel implements Configurable {
private Project project;
TemplateSettingPanel() {
// 存在打开的项目则使用打开的项目否则使用默认项目
ProjectManager projectManager = ProjectManager.getInstance();
Project[] openProjects = projectManager.getOpenProjects();
// 项目对象
this.project = openProjects.length > 0 ? openProjects[0] : projectManager.getDefaultProject();
this.project = ProjectUtils.getCurrProject();
// 配置服务实例化
this.settings = Settings.getInstance();
// 克隆对象