Skip to content

Commit

Permalink
Populating the update models in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
codemakerai-dev committed Apr 8, 2024
1 parent 60d1337 commit 36cc39a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import ai.codemaker.jetbrains.service.CodeMakerService
import com.intellij.ide.BrowserUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.components.*
import com.intellij.ui.components.ActionLink
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBTextField
import com.intellij.util.ui.FormBuilder
import java.awt.FlowLayout
import javax.swing.DefaultComboBoxModel
Expand All @@ -19,7 +22,7 @@ class AppSettingsComponent(project: Project) {

val panel: JPanel
private val apiKeyText = JBTextField()
private val modelCombo = ComboBox(arrayOf(defaultModel))
private val modelCombo = ComboBox<String>()
private val updateModelsButton = JButton("Update")
private val codeActionsEnabledCheck = JBCheckBox()
private val autocompletionEnabledCheck = JBCheckBox()
Expand All @@ -36,16 +39,13 @@ class AppSettingsComponent(project: Project) {
}
createAccountLabel.setExternalLinkIcon()

updateModelsButton.addActionListener({
updateModels()
updateModelsButton.addActionListener {
val service: CodeMakerService = project.getService(CodeMakerService::class.java)
val models = service.listModels()
models = service.listModels().map { it.name }

val allModels = mutableListOf<String>()
allModels.add(defaultModel)
allModels.addAll(models.map { it.name })

modelCombo.model = DefaultComboBoxModel(allModels.toTypedArray())
})
updateModels()
}

val modelPanel = JPanel(FlowLayout(FlowLayout.LEFT))
modelPanel.add(modelCombo)
Expand Down Expand Up @@ -73,6 +73,21 @@ class AppSettingsComponent(project: Project) {
.panel
}

private fun updateModels() {
val userModels = models ?: ArrayList()

val allModels = mutableListOf<String>()
allModels.add(defaultModel)
allModels.addAll(userModels)
modelCombo.model = DefaultComboBoxModel(allModels.toTypedArray())
}

var models: List<String>? = ArrayList()
set(value) {
field = value ?: ArrayList()
updateModels()
}

var apiKey: String?
get() = apiKeyText.text.trim()
set(apiKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class AppSettingsConfigurable(private val project: Project) : Configurable {
override fun isModified(): Boolean {
val settings = AppSettingsState.instance
return settingsComponent!!.apiKey != settings.apiKey
|| settingsComponent!!.models != settings.models
|| settingsComponent!!.model != settings.model
|| settingsComponent!!.codeActionsEnabled != settings.codeActionsEnabled
|| settingsComponent!!.autocompletionEnabled != settings.autocompletionEnabled
Expand All @@ -41,6 +42,7 @@ class AppSettingsConfigurable(private val project: Project) : Configurable {
override fun apply() {
val settings = AppSettingsState.instance
settings.apiKey = settingsComponent!!.apiKey
settings.models = settingsComponent!!.models
settings.model = settingsComponent!!.model
settings.codeActionsEnabled = settingsComponent!!.codeActionsEnabled
settings.autocompletionEnabled = settingsComponent!!.autocompletionEnabled
Expand All @@ -55,6 +57,7 @@ class AppSettingsConfigurable(private val project: Project) : Configurable {
override fun reset() {
val settings = AppSettingsState.instance
settingsComponent!!.apiKey = settings.apiKey
settingsComponent!!.models = settings.models
settingsComponent!!.model = settings.model
settingsComponent!!.codeActionsEnabled = settings.codeActionsEnabled
settingsComponent!!.autocompletionEnabled = settings.autocompletionEnabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class AppSettingsState : PersistentStateComponent<AppSettingsState> {
@JvmField
var apiKey: String? = null

@JvmField
var models: List<String>? = ArrayList()

@JvmField
var model: String? = null

Expand Down

0 comments on commit 36cc39a

Please sign in to comment.