Skip to content

Commit 31cd1c9

Browse files
authored
Refactoring AWS settings dialog to UI DSL (#3848)
* refactoring settings dialog * fixed tests * addressed feedback * fixed tests failing * fixed detekt * feedback changes
1 parent ad1c5a0 commit 31cd1c9

File tree

3 files changed

+51
-159
lines changed

3 files changed

+51
-159
lines changed

jetbrains-core/src/software/aws/toolkits/jetbrains/settings/AwsSettingsConfigurable.form

Lines changed: 0 additions & 112 deletions
This file was deleted.

jetbrains-core/src/software/aws/toolkits/jetbrains/settings/AwsSettingsConfigurable.kt

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
1+
// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3+
34
package software.aws.toolkits.jetbrains.settings
45

56
import com.intellij.ide.BrowserUtil
@@ -10,15 +11,12 @@ import com.intellij.openapi.options.ConfigurationException
1011
import com.intellij.openapi.options.SearchableConfigurable
1112
import com.intellij.openapi.ui.ComboBox
1213
import com.intellij.openapi.ui.TextFieldWithBrowseButton
13-
import com.intellij.openapi.util.Comparing
1414
import com.intellij.openapi.util.text.StringUtil
15-
import com.intellij.ui.IdeBorderFactory
16-
import com.intellij.ui.components.ActionLink
1715
import com.intellij.ui.components.JBCheckBox
1816
import com.intellij.ui.components.JBTextField
19-
import com.intellij.util.ui.SwingHelper
17+
import com.intellij.ui.dsl.builder.AlignX
18+
import com.intellij.ui.dsl.builder.panel
2019
import software.aws.toolkits.jetbrains.core.executables.ExecutableInstance
21-
import software.aws.toolkits.jetbrains.core.executables.ExecutableInstance.BadExecutable
2220
import software.aws.toolkits.jetbrains.core.executables.ExecutableInstance.ExecutableWithPath
2321
import software.aws.toolkits.jetbrains.core.executables.ExecutableManager
2422
import software.aws.toolkits.jetbrains.core.executables.ExecutableType
@@ -30,58 +28,60 @@ import java.nio.file.Path
3028
import java.nio.file.Paths
3129
import java.util.concurrent.CompletionException
3230
import javax.swing.JComponent
33-
import javax.swing.JPanel
3431

3532
class AwsSettingsConfigurable : SearchableConfigurable {
36-
private lateinit var panel: JPanel
37-
private lateinit var samHelp: JComponent
38-
private lateinit var serverlessSettings: JPanel
39-
private lateinit var applicationLevelSettings: JPanel
40-
private lateinit var defaultRegionHandling: ComboBox<UseAwsCredentialRegion>
41-
private lateinit var profilesNotification: ComboBox<ProfilesNotification>
42-
lateinit var samExecutablePath: TextFieldWithBrowseButton
43-
private set
44-
lateinit var enableTelemetry: JBCheckBox
45-
private set
4633
private val samExecutableInstance: SamExecutable
4734
get() = ExecutableType.getExecutable(SamExecutable::class.java)
48-
private val samTextboxInput: String?
49-
get() = StringUtil.nullize(samExecutablePath.text.trim { it <= ' ' })
50-
51-
override fun createComponent(): JComponent = panel
52-
53-
private fun createUIComponents() {
54-
samHelp = createHelpLink(HelpIds.SAM_CLI_INSTALL)
55-
samExecutablePath = createCliConfigurationElement(samExecutableInstance, SAM)
56-
defaultRegionHandling = ComboBox(UseAwsCredentialRegion.values())
57-
profilesNotification = ComboBox(ProfilesNotification.values())
58-
}
35+
val samExecutablePath: TextFieldWithBrowseButton = createCliConfigurationElement(samExecutableInstance, SAM)
36+
37+
private val defaultRegionHandling: ComboBox<UseAwsCredentialRegion> = ComboBox(UseAwsCredentialRegion.values())
38+
private val profilesNotification: ComboBox<ProfilesNotification> = ComboBox(ProfilesNotification.values())
39+
40+
val enableTelemetry: JBCheckBox = JBCheckBox()
41+
override fun createComponent(): JComponent = panel {
42+
group(message("aws.settings.serverless_label")) {
43+
row {
44+
label(message("aws.settings.sam.location"))
45+
// samExecutablePath = createCliConfigurationElement(samExecutableInstance, SAM)
46+
cell(samExecutablePath).align(AlignX.FILL).resizableColumn()
47+
browserLink(message("aws.settings.learn_more"), HelpIds.SAM_CLI_INSTALL.url)
48+
}
49+
}
50+
group(message("aws.settings.global_label")) {
51+
row {
52+
label(message("settings.credentials.prompt_for_default_region_switch.setting_label"))
53+
cell(defaultRegionHandling).resizableColumn().align(AlignX.FILL).applyToComponent {
54+
this.selectedItem = AwsSettings.getInstance().useDefaultCredentialRegion ?: UseAwsCredentialRegion.Never
55+
}
56+
}
57+
row {
58+
label(message("settings.profiles.label"))
59+
cell(profilesNotification).resizableColumn().align(AlignX.FILL).applyToComponent {
60+
this.selectedItem = AwsSettings.getInstance().profilesNotification ?: ProfilesNotification.Always
61+
}
62+
}
5963

60-
init {
61-
applicationLevelSettings.border = IdeBorderFactory.createTitledBorder(message("aws.settings.global_label"))
62-
serverlessSettings.border = IdeBorderFactory.createTitledBorder(message("aws.settings.serverless_label"))
63-
SwingHelper.setPreferredWidth(samExecutablePath, panel.width)
64+
row {
65+
cell(enableTelemetry).applyToComponent { this.isSelected = AwsSettings.getInstance().isTelemetryEnabled }
66+
text(message("aws.settings.telemetry.option") + " <a>${message("general.details")}</a>") {
67+
BrowserUtil.open("https://docs.aws.amazon.com/sdkref/latest/guide/support-maint-idetoolkits.html")
68+
}
69+
}
70+
}
6471
}
6572

66-
override fun getId(): String = "aws"
67-
override fun getDisplayName(): String = message("aws.settings.title")
68-
69-
override fun isModified(): Boolean {
70-
val awsSettings = AwsSettings.getInstance()
71-
return samTextboxInput != getSavedExecutablePath(samExecutableInstance, false) ||
72-
isModified(enableTelemetry, awsSettings.isTelemetryEnabled) ||
73-
// isModified for ComboBoxes is removed from 2021.3
74-
!Comparing.equal(defaultRegionHandling.selectedItem, awsSettings.useDefaultCredentialRegion) ||
75-
!Comparing.equal(profilesNotification.selectedItem, awsSettings.profilesNotification)
76-
}
73+
override fun isModified(): Boolean = getSamPathWithoutSpaces() != getSavedExecutablePath(samExecutableInstance, false) ||
74+
defaultRegionHandling.selectedItem != AwsSettings.getInstance().useDefaultCredentialRegion ||
75+
profilesNotification.selectedItem != AwsSettings.getInstance().profilesNotification ||
76+
enableTelemetry.isSelected != AwsSettings.getInstance().isTelemetryEnabled
7777

7878
override fun apply() {
7979
validateAndSaveCliSettings(
8080
samExecutablePath.textField as JBTextField,
8181
"sam",
8282
samExecutableInstance,
8383
getSavedExecutablePath(samExecutableInstance, false),
84-
samTextboxInput
84+
getSamPathWithoutSpaces()
8585
)
8686
saveAwsSettings()
8787
}
@@ -94,7 +94,9 @@ class AwsSettingsConfigurable : SearchableConfigurable {
9494
profilesNotification.selectedItem = awsSettings.profilesNotification
9595
}
9696

97-
private fun createHelpLink(helpId: HelpIds): JComponent = ActionLink(message("aws.settings.learn_more")) { BrowserUtil.browse(helpId.url) }
97+
override fun getDisplayName(): String = message("aws.settings.title")
98+
99+
override fun getId(): String = "aws"
98100

99101
private fun createCliConfigurationElement(executableType: ExecutableType<*>, cliName: String): TextFieldWithBrowseButton {
100102
val autoDetectPath = getSavedExecutablePath(executableType, true)
@@ -176,21 +178,22 @@ class AwsSettingsConfigurable : SearchableConfigurable {
176178
throw ConfigurationException(message("aws.settings.executables.executable_invalid", executableName, currentInput))
177179
}
178180
val instance = ExecutableManager.getInstance().validateExecutablePath(executableType, path)
179-
if (instance is BadExecutable) {
181+
if (instance is ExecutableInstance.BadExecutable) {
180182
throw ConfigurationException(instance.validationError)
181183
}
182184

183185
// We have validated so now we can set
184186
ExecutableManager.getInstance().setExecutablePath(executableType, path)
185187
}
186-
187188
private fun saveAwsSettings() {
188189
val awsSettings = AwsSettings.getInstance()
189190
awsSettings.isTelemetryEnabled = enableTelemetry.isSelected
190191
awsSettings.useDefaultCredentialRegion = defaultRegionHandling.selectedItem as? UseAwsCredentialRegion ?: UseAwsCredentialRegion.Never
191192
awsSettings.profilesNotification = profilesNotification.selectedItem as? ProfilesNotification ?: ProfilesNotification.Always
192193
}
193194

195+
private fun getSamPathWithoutSpaces() = StringUtil.nullize(samExecutablePath.text.trim { it <= ' ' })
196+
194197
companion object {
195198
private const val SAM = "sam"
196199
}

resources/resources/software/aws/toolkits/resources/MessagesBundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ general.create_in_progress=Creating...
827827
general.default=Default
828828
general.delete=Delete
829829
general.delete_accessible_name=Delete confirmation box
830+
general.details=(details)
830831
general.execute_button=Execute
831832
general.execution.canceled=canceled
832833
general.execution.cli_error=Command did not exit successfully, exit code: {0}\n

0 commit comments

Comments
 (0)