From 120b7e7c32b2cd617aa8f077932da1579337f7a8 Mon Sep 17 00:00:00 2001 From: tangcent Date: Thu, 23 Dec 2021 20:07:07 +0800 Subject: [PATCH] feat: improve Settings UI --- .../AbstractEasyApiConfigurable.kt | 28 +- .../configurable/AbstractEasyApiSettingGUI.kt | 14 +- .../plugin/configurable/EasyApiSettingGUI.kt | 12 +- .../dialog/EasyApiSettingBuiltInConfigGUI.kt | 15 +- .../idea/plugin/dialog/EasyApiSettingGUI.kt | 317 ++++++------------ .../plugin/dialog/EasyApiSettingOtherGUI.kt | 16 +- .../dialog/EasyApiSettingRecommendGUI.kt | 37 +- .../com/itangcent/idea/swing/ListenerKit.kt | 55 +++ .../AbstractEasyApiConfigurableTest.kt | 10 +- 9 files changed, 199 insertions(+), 305 deletions(-) create mode 100644 idea-plugin/src/main/kotlin/com/itangcent/idea/swing/ListenerKit.kt diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiConfigurable.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiConfigurable.kt index e7d080e5d..27c1734a9 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiConfigurable.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiConfigurable.kt @@ -35,7 +35,7 @@ abstract class AbstractEasyApiConfigurable(private var myProject: Project?) : Se return false } if (throttle.acquire(5000L)) { - LOG.info("settings is modified:\npre:----\n${pre.toJson()}\ninUI:----\n${inUI.toJson()}\n----\n") + LOG.info("settings is modified at ${this::class.qualifiedName}:\npre:----\n${pre.toJson()}\ninUI:----\n${inUI.toJson()}\n----\n") } return true } @@ -72,33 +72,11 @@ abstract class AbstractEasyApiConfigurable(private var myProject: Project?) : Se easyApiConfigurableGUI = createGUI() context.init(easyApiConfigurableGUI) - context.runAsync { - context.runInSwingUI { - easyApiConfigurableGUI.onCreate() - easyApiConfigurableGUI.setSettings(settingBinder.read().copy()) - checkUI() - } - } + easyApiConfigurableGUI.onCreate() + easyApiConfigurableGUI.setSettings(settingBinder.read().copy()) return easyApiConfigurableGUI.getRootPanel() } - private fun checkUI() { - context!!.runAsync { - for (i in 0..8) { - Thread.sleep(250) - if (easyApiConfigurableGUI.checkUI()) { - continue - } - LOG.error("checkUI failed.Try refresh ui again.") - context!!.runInSwingUI { - easyApiConfigurableGUI.setSettings(easyApiConfigurableGUI.getSettings()) - checkUI() - } - break - } - } - } - open fun afterBuildActionContext(builder: ActionContext.ActionContextBuilder) {} protected abstract fun createGUI(): EasyApiSettingGUI diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiSettingGUI.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiSettingGUI.kt index 7d4b83b0d..28d7aa941 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiSettingGUI.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiSettingGUI.kt @@ -1,29 +1,23 @@ package com.itangcent.idea.plugin.configurable import com.itangcent.idea.plugin.settings.Settings -import com.itangcent.intellij.extend.rx.AutoComputer abstract class AbstractEasyApiSettingGUI : EasyApiSettingGUI { protected var settingsInstance: Settings? = null - protected var autoComputer: AutoComputer = AutoComputer() - - override fun checkUI(): Boolean { - return true + override fun onCreate() { + //NOP } override fun getSettings(): Settings { - return (settingsInstance ?: Settings()).copy() + return (settingsInstance ?: Settings()).copy().also { readSettings((it)) } } override fun setSettings(settings: Settings) { - autoComputer.value(this::settingsInstance, settings.copy()) + this.settingsInstance = settings } override fun readSettings(settings: Settings) { - readSettings(settings, getSettings()) } - - protected abstract fun readSettings(settings: Settings, from: Settings) } \ No newline at end of file diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/EasyApiSettingGUI.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/EasyApiSettingGUI.kt index 7b7b49bb8..69c085260 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/EasyApiSettingGUI.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/configurable/EasyApiSettingGUI.kt @@ -4,15 +4,23 @@ import com.itangcent.idea.plugin.settings.Settings import javax.swing.JComponent interface EasyApiSettingGUI { + fun getRootPanel(): JComponent? fun onCreate() - fun checkUI(): Boolean - + /** + * get settings in UI + */ fun getSettings(): Settings + /** + * set settings to UI + */ fun setSettings(settings: Settings) + /** + * read settings from UI + */ fun readSettings(settings: Settings) } \ No newline at end of file diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingBuiltInConfigGUI.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingBuiltInConfigGUI.kt index 98edb34e8..34a945e58 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingBuiltInConfigGUI.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingBuiltInConfigGUI.kt @@ -17,17 +17,12 @@ class EasyApiSettingBuiltInConfigGUI : AbstractEasyApiSettingGUI() { return rootPanel } - override fun onCreate() { - autoComputer.bind(this.builtInConfigTextArea!!) - .with(this, "settingsInstance.builtInConfig") - .eval { it.takeIf { it.notNullOrBlank() } ?: EasyApiSettingGUI.DEFAULT_BUILT_IN_CONFIG } - - autoComputer.bind(this, "settingsInstance.builtInConfig") - .with(builtInConfigTextArea!!) - .eval { it.takeIf { it != EasyApiSettingGUI.DEFAULT_BUILT_IN_CONFIG } ?: "" } + override fun setSettings(settings: Settings) { + super.setSettings(settings) + this.builtInConfigTextArea!!.text = settings.builtInConfig.takeIf { it.notNullOrBlank() } ?: EasyApiSettingGUI.DEFAULT_BUILT_IN_CONFIG } - override fun readSettings(settings: Settings, from: Settings) { - settings.builtInConfig = from.builtInConfig + override fun readSettings(settings: Settings) { + settings.builtInConfig = this.builtInConfigTextArea!!.text.takeIf { it != EasyApiSettingGUI.DEFAULT_BUILT_IN_CONFIG } ?: "" } } \ No newline at end of file diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingGUI.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingGUI.kt index 4897b9bdc..eb8579b9d 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingGUI.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingGUI.kt @@ -22,12 +22,14 @@ import com.itangcent.idea.plugin.settings.helper.* import com.itangcent.idea.plugin.settings.xml.postmanCollectionsAsPairs import com.itangcent.idea.plugin.settings.xml.setPostmanCollectionsPairs import com.itangcent.idea.plugin.support.IdeaSupport +import com.itangcent.idea.swing.onSelect +import com.itangcent.idea.swing.onTextChange +import com.itangcent.idea.swing.selected import com.itangcent.idea.utils.Charsets import com.itangcent.idea.utils.SwingUtils import com.itangcent.idea.utils.isDoubleClick import com.itangcent.intellij.context.ActionContext import com.itangcent.intellij.extend.rx.ThrottleHelper -import com.itangcent.intellij.extend.rx.mutual import com.itangcent.intellij.logger.Logger import com.itangcent.suv.http.ConfigurableHttpClientProvider import com.itangcent.utils.ResourceUtils @@ -49,12 +51,6 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() { private var rootPanel: JPanel? = null - //region import&export----------------------------------------------------- - private var importButton: JButton? = null - - private var exportButton: JButton? = null - //endregion import&export----------------------------------------------------- - //region postman----------------------------------------------------- private var postmanTokenLabel: JLabel? = null @@ -121,12 +117,8 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() { private var projectCacheSizeLabel: JLabel? = null - private var globalCacheSize: String = "0M" - private var clearGlobalCacheButton: JButton? = null - private var projectCacheSize: String = "0M" - private var clearProjectCacheButton: JButton? = null private var outputDemoCheckBox: JCheckBox? = null @@ -196,28 +188,16 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() { postmanJson5FormatTypeComboBox!!.model = DefaultComboBoxModel(PostmanJson5FormatType.values().mapToTypedArray { it.name }) - autoComputer.bind(this, "settingsInstance.postmanJson5FormatType") - .with(this.postmanJson5FormatTypeComboBox!!) - .filter { throttleHelper.acquire("settingsInstance.postmanJson5FormatType", 300) } - .eval { (it ?: PostmanJson5FormatType.EXAMPLE_ONLY.name) } - postmanExportModeComboBox!!.model = DefaultComboBoxModel(PostmanExportMode.values().mapToTypedArray { it.name }) - autoComputer.bind(this, "settingsInstance.postmanExportMode") - .with(this.postmanExportModeComboBox!!) - .filter { throttleHelper.acquire("settingsInstance.postmanExportMode", 300) } - .eval { (it ?: PostmanExportMode.COPY.name) } - - autoComputer.bindVisible(postmanWorkSpaceRefreshButton!!) - .with(this.postmanTokenTextField!!) - .eval { it.notNullOrBlank() } + this.postmanTokenTextField!!.onTextChange { + postmanWorkSpaceRefreshButton!!.isVisible = it.notNullOrBlank() + } - autoComputer.bindVisible(postmanExportCollectionPanel!!) - .with(this, "settingsInstance.postmanExportMode") - .eval { - it == PostmanExportMode.UPDATE.name - } + this.postmanExportModeComboBox!!.onSelect { + postmanExportCollectionPanel!!.isVisible = it == PostmanExportMode.UPDATE.name + } postmanWorkSpaceRefreshButton!!.addActionListener { refreshPostmanWorkSpaces(false) @@ -241,164 +221,35 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() { recommendedCheckBox!!.toolTipText = RecommendConfigLoader.plaint() - autoComputer.bind(pullNewestDataBeforeCheckBox!!) - .mutual(this, "settingsInstance.pullNewestDataBefore") - - autoComputer.bind(postmanTokenTextField!!) - .mutual(this, "settingsInstance.postmanToken") - - autoComputer.bind(wrapCollectionCheckBox!!) - .mutual(this, "settingsInstance.wrapCollection") - - autoComputer.bind(autoMergeScriptCheckBox!!) - .mutual(this, "settingsInstance.autoMergeScript") - - autoComputer.bind(this.globalCacheSizeLabel!!) - .with(this::globalCacheSize) - .eval { it } - this.clearGlobalCacheButton!!.addActionListener { clearGlobalCache() } - autoComputer.bind(this.projectCacheSizeLabel!!) - .with(this::projectCacheSize) - .eval { it } - this.clearProjectCacheButton!!.addActionListener { clearProjectCache() } - autoComputer.bind(methodDocEnableCheckBox!!) - .mutual(this, "settingsInstance.methodDocEnable") - - autoComputer.bind(genericEnableCheckBox!!) - .mutual(this, "settingsInstance.genericEnable") - - autoComputer.bind(feignEnableCheckBox!!) - .mutual(this, "settingsInstance.feignEnable") - - autoComputer.bind(quarkusEnableCheckBox!!) - .mutual(this, "settingsInstance.quarkusEnable") - - autoComputer.bind(inferEnableCheckBox!!) - .mutual(this, "settingsInstance.inferEnable") - - autoComputer.bind(readGetterCheckBox!!) - .mutual(this, "settingsInstance.readGetter") - - autoComputer.bind(readSetterCheckBox!!) - .mutual(this, "settingsInstance.readSetter") - - autoComputer.bind(formExpandedCheckBox!!) - .mutual(this, "settingsInstance.formExpanded") - - autoComputer.bind(queryExpandedCheckBox!!) - .mutual(this, "settingsInstance.queryExpanded") - - autoComputer.bind(recommendedCheckBox!!) - .mutual(this, "settingsInstance.useRecommendConfig") - - autoComputer.bind(outputDemoCheckBox!!) - .mutual(this, "settingsInstance.outputDemo") - - autoComputer.bind(this.maxDeepTextField!!) - .with(this, "settingsInstance.inferMaxDeep") - .eval { (it ?: Settings.DEFAULT_INFER_MAX_DEEP).toString() } - - autoComputer.bind(this, "settingsInstance.inferMaxDeep") - .with(this.maxDeepTextField!!) - .eval { - try { - it?.toInt() ?: Settings.DEFAULT_INFER_MAX_DEEP - } catch (e: Exception) { - Settings.DEFAULT_INFER_MAX_DEEP - } - } - - autoComputer.bind(yapiServerTextField!!) - .mutual(this, "settingsInstance.yapiServer") - - autoComputer.bind(yapiTokenTextArea!!) - .mutual(this, "settingsInstance.yapiTokens") - - autoComputer.bind(enableUrlTemplatingCheckBox!!) - .mutual(this, "settingsInstance.enableUrlTemplating") - - autoComputer.bind(switchNoticeCheckBox!!) - .mutual(this, "settingsInstance.switchNotice") - - autoComputer.bind(loginModeCheckBox!!) - .mutual(this, "settingsInstance.loginMode") - - autoComputer.bind(yapiReqBodyJson5CheckBox!!) - .mutual(this, "settingsInstance.yapiReqBodyJson5") - - autoComputer.bind(yapiResBodyJson5CheckBox!!) - .mutual(this, "settingsInstance.yapiResBodyJson5") - - autoComputer.bind(yapiTokenLabel!!) - .with(this, "settingsInstance.loginMode") - .eval { - if (it == true) { - "projectIds:" - } else { - "tokens:" - } - } - - autoComputer.bind(this.httpTimeOutTextField!!) - .with(this, "settingsInstance.httpTimeOut") - .eval { (it ?: ConfigurableHttpClientProvider.defaultHttpTimeOut).toString() } - - autoComputer.bind(this, "settingsInstance.httpTimeOut") - .with(this.httpTimeOutTextField!!) - .eval { - try { - it?.toInt() ?: ConfigurableHttpClientProvider.defaultHttpTimeOut - } catch (e: Exception) { - ConfigurableHttpClientProvider.defaultHttpTimeOut - } - } + this.loginModeCheckBox!!.onSelect { + this.yapiTokenLabel!!.text = if (it) "projectIds:" else "tokens:" + } logLevelComboBox!!.model = DefaultComboBoxModel(CommonSettingsHelper.CoarseLogLevel.editableValues()) - autoComputer.bind(this, "settingsInstance.logLevel") - .with(this.logLevelComboBox!!) - .filter { throttleHelper.acquire("settingsInstance.logLevel", 300) } - .eval { (it ?: CommonSettingsHelper.CoarseLogLevel.LOW).getLevel() } - outputCharsetComboBox!!.model = DefaultComboBoxModel(Charsets.SUPPORTED_CHARSETS) markdownFormatTypeComboBox!!.model = DefaultComboBoxModel(MarkdownFormatType.values().mapToTypedArray { it.name }) - autoComputer.bind(this, "settingsInstance.outputCharset") - .with(this.outputCharsetComboBox!!) - .filter { throttleHelper.acquire("settingsInstance.outputCharset", 300) } - .eval { (it ?: Charsets.UTF_8).displayName() } - - autoComputer.bind(this, "settingsInstance.markdownFormatType") - .with(this.markdownFormatTypeComboBox!!) - .filter { throttleHelper.acquire("settingsInstance.markdownFormatType", 300) } - .eval { (it ?: MarkdownFormatType.SIMPLE.name) } - - autoComputer.bind>(this, "settingsInstance.trustHosts") - .with(trustHostsTextArea!!) - .eval { trustHosts -> trustHosts?.lines()?.toTypedArray() ?: emptyArray() } - - autoComputer.bind(trustHostsTextArea!!) - .with>(this, "settingsInstance.trustHosts") - .eval { trustHosts -> trustHosts.joinToString(separator = "\n") } - //endregion general----------------------------------------------------- } override fun setSettings(settings: Settings) { - val snapshot = settings.copy() super.setSettings(settings) - throttleHelper.refresh("throttleHelper") + this.pullNewestDataBeforeCheckBox!!.isSelected = settings.pullNewestDataBefore + this.postmanTokenTextField!!.text = settings.postmanToken ?: "" + this.wrapCollectionCheckBox!!.isSelected = settings.wrapCollection + this.autoMergeScriptCheckBox!!.isSelected = settings.autoMergeScript this.postmanWorkspaceComboBoxModel?.selectedItem = this.selectedPostmanWorkspace this.logLevelComboBox!!.selectedItem = CommonSettingsHelper.CoarseLogLevel.toLevel(settings.logLevel) @@ -407,13 +258,37 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() { this.postmanExportModeComboBox!!.selectedItem = settings.postmanExportMode this.markdownFormatTypeComboBox!!.selectedItem = settings.markdownFormatType - refresh(snapshot) + this.methodDocEnableCheckBox!!.isSelected = settings.methodDocEnable + this.genericEnableCheckBox!!.isSelected = settings.genericEnable + this.feignEnableCheckBox!!.isSelected = settings.feignEnable + this.quarkusEnableCheckBox!!.isSelected = settings.quarkusEnable + this.inferEnableCheckBox!!.isSelected = settings.inferEnable + this.readGetterCheckBox!!.isSelected = settings.readGetter + this.readSetterCheckBox!!.isSelected = settings.readSetter + this.formExpandedCheckBox!!.isSelected = settings.formExpanded + this.queryExpandedCheckBox!!.isSelected = settings.queryExpanded + this.recommendedCheckBox!!.isSelected = settings.useRecommendConfig + this.outputDemoCheckBox!!.isSelected = settings.outputDemo + + + this.yapiServerTextField!!.text = settings.yapiServer ?: "" + this.yapiTokenTextArea!!.text = settings.yapiTokens ?: "" + this.enableUrlTemplatingCheckBox!!.isSelected = settings.enableUrlTemplating + this.switchNoticeCheckBox!!.isSelected = settings.switchNotice + this.loginModeCheckBox!!.isSelected = settings.loginMode + this.yapiReqBodyJson5CheckBox!!.isSelected = settings.yapiReqBodyJson5 + this.yapiResBodyJson5CheckBox!!.isSelected = settings.yapiResBodyJson5 + + this.trustHostsTextArea!!.text = settings.trustHosts.joinToString(separator = "\n") + this.maxDeepTextField!!.text = settings.inferMaxDeep.toString() + + this.httpTimeOutTextField!!.text = settings.httpTimeOut.toString() + + refresh() } - private fun refresh(settings: Settings) { + private fun refresh() { actionContext.runAsync { - //fix - this.settingsInstance?.postmanExportMode = settings.postmanExportMode refreshCache() refreshPostmanWorkSpaces() refreshPostmanCollections() @@ -451,10 +326,11 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() { this.allWorkspaces = allWorkspacesData if (postmanWorkspaceComboBoxModel == null) { postmanWorkspaceComboBoxModel = - MutableCollectionComboBoxModel(allWorkspacesData ?: emptyList(), selectedPostmanWorkspace) + MutableCollectionComboBoxModel(allWorkspacesData, selectedPostmanWorkspace) postmanWorkspaceComboBox.model = postmanWorkspaceComboBoxModel - autoComputer.bind(postmanWorkspaceComboBox) - .mutual(this::selectedPostmanWorkspace) + postmanWorkspaceComboBox.onSelect { + this.selectedPostmanWorkspace = it + } } else { val selected = this.selectedPostmanWorkspace postmanWorkspaceComboBoxModel!!.replaceAll(allWorkspacesData.toMutableList()) @@ -554,8 +430,9 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() { val projectBasePath = currentProject.basePath val cachePath = "$projectBasePath${File.separator}.idea${File.separator}.cache${File.separator}$basePath" val cacheSize = computeFolderSize(cachePath) - val readableCacheSize = StringUtil.formatFileSize(cacheSize) - autoComputer.value(this::projectCacheSize, readableCacheSize) + actionContext.runInSwingUI { + this.projectCacheSizeLabel!!.text = StringUtil.formatFileSize(cacheSize) + } } private fun clearProjectCache() { @@ -569,8 +446,9 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() { private fun computeGlobalCacheSize() { val cachePath = "${globalBasePath()}${File.separator}$basePath" val cacheSize = computeFolderSize(cachePath) - val readableCacheSize = StringUtil.formatFileSize(cacheSize) - autoComputer.value(this::globalCacheSize, readableCacheSize) + actionContext.runInSwingUI { + this.globalCacheSizeLabel!!.text = StringUtil.formatFileSize(cacheSize) + } } private fun clearGlobalCache() { @@ -591,12 +469,6 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() { return FileUtils.deleteDirectory(File(path)) } - override fun getSettings(): Settings { - return super.getSettings().also { - readPostmanCollections(it) - } - } - private fun readPostmanCollections(settings: Settings) { if (!postmanCollectionInit) { return @@ -687,47 +559,46 @@ class EasyApiSettingGUI : AbstractEasyApiSettingGUI() { } } - override fun readSettings(settings: Settings, from: Settings) { - settings.postmanToken = from.postmanToken - settings.wrapCollection = from.wrapCollection - settings.autoMergeScript = from.autoMergeScript - settings.postmanJson5FormatType = from.postmanJson5FormatType - settings.pullNewestDataBefore = from.pullNewestDataBefore - settings.methodDocEnable = from.methodDocEnable - settings.genericEnable = from.genericEnable - settings.feignEnable = from.feignEnable - settings.quarkusEnable = from.quarkusEnable - settings.queryExpanded = from.queryExpanded - settings.formExpanded = from.formExpanded - settings.readGetter = from.readGetter - settings.readSetter = from.readSetter - settings.inferEnable = from.inferEnable - settings.inferMaxDeep = from.inferMaxDeep - settings.yapiServer = from.yapiServer - settings.yapiTokens = from.yapiTokens - settings.enableUrlTemplating = from.enableUrlTemplating - settings.switchNotice = from.switchNotice - settings.loginMode = from.loginMode - settings.yapiReqBodyJson5 = from.yapiReqBodyJson5 - settings.yapiResBodyJson5 = from.yapiResBodyJson5 - settings.httpTimeOut = from.httpTimeOut - settings.useRecommendConfig = from.useRecommendConfig - settings.logLevel = from.logLevel - settings.outputDemo = from.outputDemo - settings.outputCharset = from.outputCharset - settings.markdownFormatType = from.markdownFormatType - settings.trustHosts = from.trustHosts - settings.postmanWorkspace = from.postmanWorkspace - settings.postmanExportMode = from.postmanExportMode - settings.postmanCollections = from.postmanCollections - settings.yapiTokens = from.yapiTokens - } - - override fun checkUI(): Boolean { - return this.yapiServerTextField?.text == this.settingsInstance?.yapiServer - && this.postmanTokenTextField?.text == this.settingsInstance?.postmanToken - && this.trustHostsTextArea?.text == this.settingsInstance?.trustHosts?.joinToString(separator = "\n") - && (this.postmanExportCollectionPanel!!.isVisible == (this.settingsInstance?.postmanExportMode == PostmanExportMode.UPDATE.name)) + override fun readSettings(settings: Settings) { + settings.postmanToken = postmanTokenTextField!!.text + settings.wrapCollection = wrapCollectionCheckBox!!.isSelected + settings.autoMergeScript = autoMergeScriptCheckBox!!.isSelected + postmanJson5FormatTypeComboBox!!.selected()?.let { + settings.postmanJson5FormatType = it + } + settings.pullNewestDataBefore = pullNewestDataBeforeCheckBox!!.isSelected + settings.methodDocEnable = methodDocEnableCheckBox!!.isSelected + settings.genericEnable = genericEnableCheckBox!!.isSelected + settings.feignEnable = feignEnableCheckBox!!.isSelected + settings.quarkusEnable = quarkusEnableCheckBox!!.isSelected + settings.queryExpanded = queryExpandedCheckBox!!.isSelected + settings.formExpanded = formExpandedCheckBox!!.isSelected + settings.readGetter = readGetterCheckBox!!.isSelected + settings.readSetter = readSetterCheckBox!!.isSelected + settings.inferEnable = inferEnableCheckBox!!.isSelected + settings.inferMaxDeep = maxDeepTextField!!.text.toIntOrNull() ?: Settings.DEFAULT_INFER_MAX_DEEP + settings.yapiServer = yapiServerTextField!!.text + settings.yapiTokens = yapiTokenTextArea!!.text + settings.enableUrlTemplating = enableUrlTemplatingCheckBox!!.isSelected + settings.switchNotice = switchNoticeCheckBox!!.isSelected + settings.loginMode = loginModeCheckBox!!.isSelected + settings.yapiReqBodyJson5 = yapiReqBodyJson5CheckBox!!.isSelected + settings.yapiResBodyJson5 = yapiResBodyJson5CheckBox!!.isSelected + settings.httpTimeOut = + httpTimeOutTextField!!.text.toIntOrNull() ?: ConfigurableHttpClientProvider.defaultHttpTimeOut + settings.useRecommendConfig = recommendedCheckBox!!.isSelected + settings.logLevel = + (logLevelComboBox!!.selected() ?: CommonSettingsHelper.CoarseLogLevel.LOW).getLevel() + settings.outputDemo = outputDemoCheckBox!!.isSelected + settings.outputCharset = (outputCharsetComboBox!!.selectedItem as? Charsets ?: Charsets.UTF_8).displayName() + settings.markdownFormatType = + markdownFormatTypeComboBox!!.selected() ?: MarkdownFormatType.SIMPLE.name + settings.trustHosts = trustHostsTextArea!!.text?.lines()?.toTypedArray() ?: emptyArray() + settings.postmanWorkspace = settingsInstance?.postmanWorkspace + settings.postmanExportMode = postmanExportModeComboBox!!.selected() ?: PostmanExportMode.COPY.name + settings.yapiTokens = this.yapiTokenTextArea!!.text + + readPostmanCollections(settings) } companion object { diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingOtherGUI.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingOtherGUI.kt index 9045df1e3..258c3ab0c 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingOtherGUI.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingOtherGUI.kt @@ -1,5 +1,6 @@ package com.itangcent.idea.plugin.dialog +import com.google.inject.Inject import com.intellij.ide.util.PropertiesComponent import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory import com.intellij.openapi.fileChooser.FileChooserFactory @@ -12,6 +13,7 @@ import com.itangcent.common.utils.notNullOrEmpty import com.itangcent.idea.icons.EasyIcons import com.itangcent.idea.icons.iconOnly import com.itangcent.idea.plugin.configurable.AbstractEasyApiSettingGUI +import com.itangcent.idea.plugin.settings.SettingBinder import com.itangcent.idea.plugin.settings.Settings import com.itangcent.idea.utils.SwingUtils import java.io.File @@ -27,6 +29,9 @@ class EasyApiSettingOtherGUI : AbstractEasyApiSettingGUI() { private var exportButton: JButton? = null + @Inject + private lateinit var settingBinder: SettingBinder + override fun getRootPanel(): JComponent? { return rootPanel } @@ -45,9 +50,6 @@ class EasyApiSettingOtherGUI : AbstractEasyApiSettingGUI() { } } - override fun readSettings(settings: Settings, from: Settings) { - } - private fun export() { val descriptor = FileSaverDescriptor( "Export Setting", @@ -65,7 +67,7 @@ class EasyApiSettingOtherGUI : AbstractEasyApiSettingGUI() { if (fileWrapper != null) { com.itangcent.intellij.util.FileUtils.forceSave( fileWrapper.file.path, - GsonUtils.toJson(settingsInstance).toByteArray(kotlin.text.Charsets.UTF_8) + GsonUtils.toJson(settingsInstance).toByteArray(Charsets.UTF_8) ) } } @@ -85,9 +87,11 @@ class EasyApiSettingOtherGUI : AbstractEasyApiSettingGUI() { val files = chooser.choose(null, toSelect) if (files.notNullOrEmpty()) { val virtualFile = files[0] - val read = FileUtils.read(File(virtualFile.path), kotlin.text.Charsets.UTF_8) + val read = FileUtils.read(File(virtualFile.path), Charsets.UTF_8) if (read.notNullOrEmpty()) { - setSettings(GsonUtils.fromJson(read!!, Settings::class)) + val settings = GsonUtils.fromJson(read!!, Settings::class) + settingBinder.save(settings) + setSettings(settings) } } } diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingRecommendGUI.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingRecommendGUI.kt index b011a84cf..9ecc7e1eb 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingRecommendGUI.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/EasyApiSettingRecommendGUI.kt @@ -22,27 +22,6 @@ class EasyApiSettingRecommendGUI : AbstractEasyApiSettingGUI() { } override fun onCreate() { - initRecommendConfig() - } - - private fun initRecommendConfig() { - autoComputer.bind(this.previewTextArea!!) - .with(this, "settingsInstance.recommendConfigs") - .eval { configs -> - RecommendConfigLoader.buildRecommendConfig( - configs, - "\n#${"-".repeat(20)}\n" - ) - } - - autoComputer.bind(this.previewTextArea!!) - .with(this, "settingsInstance.recommendConfigs") - .eval { configs -> - RecommendConfigLoader.buildRecommendConfig( - configs, - "\n#${"-".repeat(20)}\n" - ) - } recommendConfigList!!.setItems(RecommendConfigLoader.codes().toList()) { @@ -59,7 +38,7 @@ class EasyApiSettingRecommendGUI : AbstractEasyApiSettingGUI() { } else { settings.recommendConfigs = RecommendConfigLoader.removeSelectedConfig(settings.recommendConfigs, code) } - autoComputer.value(this, "settingsInstance.recommendConfigs", settings.recommendConfigs) + computePreviewTextArea() } } @@ -70,9 +49,19 @@ class EasyApiSettingRecommendGUI : AbstractEasyApiSettingGUI() { RecommendConfigLoader.selectedCodes(settings.recommendConfigs).forEach { this.recommendConfigList!!.setItemSelected(it, true) } + + computePreviewTextArea() + } + + private fun computePreviewTextArea() { + this.previewTextArea!!.text = + RecommendConfigLoader.buildRecommendConfig( + settingsInstance?.recommendConfigs ?: "", + "\n#${"-".repeat(20)}\n" + ) } - override fun readSettings(settings: Settings, from: Settings) { - settings.recommendConfigs = from.recommendConfigs + override fun readSettings(settings: Settings) { + settings.recommendConfigs = settingsInstance?.recommendConfigs ?: "" } } diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/swing/ListenerKit.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/swing/ListenerKit.kt new file mode 100644 index 000000000..09679d49a --- /dev/null +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/swing/ListenerKit.kt @@ -0,0 +1,55 @@ +@file:Suppress("UNCHECKED_CAST") + +package com.itangcent.idea.swing + +import javax.swing.JCheckBox +import javax.swing.JComboBox +import javax.swing.event.DocumentEvent +import javax.swing.event.DocumentListener +import javax.swing.event.ListDataEvent +import javax.swing.event.ListDataListener +import javax.swing.text.JTextComponent + +fun JTextComponent.onTextChange(action: (String?) -> Unit) { + val jTextComponent = this + jTextComponent.document.addDocumentListener(object : DocumentListener { + override fun insertUpdate(e: DocumentEvent) { + action(jTextComponent.text) + } + + override fun removeUpdate(e: DocumentEvent) { + action(jTextComponent.text) + } + + override fun changedUpdate(e: DocumentEvent) { + action(jTextComponent.text) + } + }) +} + +fun JComboBox.selected(): T? { + return this.selectedItem as? T +} + +fun JComboBox.onSelect(action: (T?) -> Unit) { + val jComboBox = this + jComboBox.model.addListDataListener(object : ListDataListener { + override fun contentsChanged(e: ListDataEvent?) { + action(jComboBox.selected()) + } + + override fun intervalRemoved(e: ListDataEvent?) { + action(jComboBox.selected()) + } + + override fun intervalAdded(e: ListDataEvent?) { + action(jComboBox.selected()) + } + }) +} + +fun JCheckBox.onSelect(action: (Boolean) -> Unit) { + this.addActionListener { + action(this.isSelected) + } +} \ No newline at end of file diff --git a/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiConfigurableTest.kt b/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiConfigurableTest.kt index 9940a5859..edab8e024 100644 --- a/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiConfigurableTest.kt +++ b/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/configurable/AbstractEasyApiConfigurableTest.kt @@ -92,11 +92,6 @@ class FakeEasyApiSettingGUI : AbstractEasyApiSettingGUI() { this.settingsInstance?.let { action(it) } } - override fun readSettings(settings: Settings, from: Settings) { - from.copyTo(settings as ProjectSettingsSupport) - from.copyTo(settings as ApplicationSettingsSupport) - } - override fun getRootPanel(): JComponent { return mock() } @@ -114,4 +109,9 @@ class FakeEasyApiSettingGUI : AbstractEasyApiSettingGUI() { fun onCreateCalled(): Boolean { return this.onCreateCalled } + + override fun readSettings(settings: Settings) { + settingsInstance!!.copyTo(settings as ProjectSettingsSupport) + settingsInstance!!.copyTo(settings as ApplicationSettingsSupport) + } } \ No newline at end of file