diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/helper/ActiveProjectHelper.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/helper/ActiveProjectHelper.kt index e3b92342..ddb8761f 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/helper/ActiveProjectHelper.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/helper/ActiveProjectHelper.kt @@ -15,6 +15,7 @@ */ package org.domaframework.doma.intellij.common.helper +import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.project.Project import com.intellij.openapi.project.ProjectManager import com.intellij.openapi.wm.IdeFocusManager @@ -22,27 +23,50 @@ import com.intellij.openapi.wm.IdeFocusManager object ActiveProjectHelper { private var activeProject: Project? = null + /** + * Sets the current active project. + * + * @param value the project to set as active, or null to clear + */ fun setCurrentActiveProject(value: Project?) { activeProject = value } + /** + * Returns the current active project. + * + * If called from the EDT, tries to get the project with UI focus. + * Otherwise, returns the first open project or the last set active project. + * + * @return the current active [Project], or null if none is available + */ fun getCurrentActiveProject(): Project? { - val initProject = activeProject - val active = getActiveUIProject() - return active ?: initProject + // If EDT, get from focus + if (ApplicationManager.getApplication().isDispatchThread) { + getActiveUIProject()?.let { return it } + } + // If not EDT, the top of openProjects + ProjectManager + .getInstance() + .openProjects + .firstOrNull() + ?.let { return it } + + return activeProject } + /** + * Returns the project that currently has UI focus, if any. + * + * @return the [Project] with UI focus, or null if none + */ private fun getActiveUIProject(): Project? { val openProjects: Array = ProjectManager.getInstance().openProjects - var active: Project? = null - for (project in openProjects) { if (IdeFocusManager.getInstance(project).focusOwner != null) { - active = project - break + return project } } - - return active + return null } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/setting/DomaToolsConfigurable.kt b/src/main/kotlin/org/domaframework/doma/intellij/setting/DomaToolsConfigurable.kt index 5b277da0..62b14464 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/setting/DomaToolsConfigurable.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/setting/DomaToolsConfigurable.kt @@ -17,25 +17,37 @@ package org.domaframework.doma.intellij.setting import com.intellij.openapi.options.Configurable import com.intellij.openapi.options.ConfigurationException +import com.intellij.util.ui.UIUtil.setEnabledRecursively import org.domaframework.doma.intellij.common.helper.ActiveProjectHelper import org.domaframework.doma.intellij.setting.state.DomaToolsFormatEnableSettings import javax.swing.JComponent class DomaToolsConfigurable : Configurable { private var mySettingsComponent: SettingComponent? = SettingComponent() - private val project = ActiveProjectHelper.getCurrentActiveProject() private var formatSettings: DomaToolsFormatEnableSettings? = null - init { - project?.let { - formatSettings = DomaToolsFormatEnableSettings.getInstance(it) - } - } - override fun getDisplayName(): String = "Doma Tools" - override fun createComponent(): JComponent? = mySettingsComponent?.panel + /** + * Creates and returns the settings panel component. + * + * Gets the current active project and initializes the settings panel. + * If no project is active, the panel will be disabled. + * Loads both format settings and custom function settings for the active project. + * + * @return The settings panel component, or null if creation fails + */ + override fun createComponent(): JComponent? { + val project = ActiveProjectHelper.getCurrentActiveProject() + val panel = mySettingsComponent?.panel ?: return null + if (project == null) { + setEnabledRecursively(panel, false) + return null + } + formatSettings = DomaToolsFormatEnableSettings.getInstance(project) + return panel + } override fun isModified(): Boolean { val enableFormatModified = formatSettings?.isModified(mySettingsComponent) != false