Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,58 @@
*/
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

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<out Project> = 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down