Skip to content

Commit 04a568b

Browse files
authored
Migrate to UI DSL v2 (#24)
1 parent 6968b2c commit 04a568b

File tree

8 files changed

+326
-267
lines changed

8 files changed

+326
-267
lines changed

plugin/src/main/kotlin/com/mitteloupe/cag/cleanarchitecturegenerator/CreateCleanArchitectureFeatureDialog.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.mitteloupe.cag.cleanarchitecturegenerator
22

33
import com.intellij.openapi.project.Project
4-
import com.intellij.openapi.ui.ComboBox
54
import com.intellij.openapi.ui.DialogWrapper
65
import com.intellij.openapi.ui.ValidationInfo
76
import com.intellij.ui.components.JBTextField
7+
import com.intellij.ui.dsl.builder.bindItem
8+
import com.intellij.ui.dsl.builder.bindSelected
89
import com.intellij.ui.dsl.builder.panel
910
import com.mitteloupe.cag.cleanarchitecturegenerator.form.OnChangeDocumentListener
1011
import com.mitteloupe.cag.cleanarchitecturegenerator.form.PredicateDocumentFilter
1112
import java.io.File
12-
import javax.swing.JCheckBox
1313
import javax.swing.JComponent
1414
import javax.swing.text.AbstractDocument
1515

@@ -23,34 +23,34 @@ class CreateCleanArchitectureFeatureDialog(
2323
private val featureNameTextField = JBTextField()
2424
private val featurePackageTextField = JBTextField()
2525
private var lastFeatureName: String = PLACEHOLDER
26-
private val appModuleComboBox = ComboBox(appModuleDirectories.map { it.name }.toTypedArray())
27-
private val ktlintCheckBox = JCheckBox("ktlint")
28-
private val detektCheckBox = JCheckBox("detekt")
2926

3027
val featureName: String
3128
get() = featureNameTextField.text
3229

3330
val featurePackageName: String
3431
get() = featurePackageTextField.text.trim()
3532

33+
private var appModuleSelectedIndex: Int = 0
34+
3635
val selectedAppModuleDirectory: File?
3736
get() =
3837
if (appModuleDirectories.isEmpty()) {
3938
null
4039
} else {
41-
val selectedIndex = appModuleComboBox.selectedIndex
42-
if (selectedIndex in appModuleDirectories.indices) {
43-
appModuleDirectories[selectedIndex]
40+
if (appModuleSelectedIndex in appModuleDirectories.indices) {
41+
appModuleDirectories[appModuleSelectedIndex]
4442
} else {
4543
null
4644
}
4745
}
4846

47+
private var enableKtlintInternal: Boolean = false
4948
val enableKtlint: Boolean
50-
get() = ktlintCheckBox.isSelected
49+
get() = enableKtlintInternal
5150

51+
private var enableDetektInternal: Boolean = false
5252
val enableDetekt: Boolean
53-
get() = detektCheckBox.isSelected
53+
get() = enableDetektInternal
5454

5555
init {
5656
title = CleanArchitectureGeneratorBundle.message("info.feature.generator.title")
@@ -101,7 +101,12 @@ class CreateCleanArchitectureFeatureDialog(
101101
panel {
102102
if (appModuleDirectories.size >= 2) {
103103
row(CleanArchitectureGeneratorBundle.message("dialog.feature.app.module.label")) {
104-
cell(appModuleComboBox)
104+
val appModules = appModuleDirectories.map { it.name }
105+
comboBox(appModules, null)
106+
.bindItem(
107+
getter = { appModules[appModuleSelectedIndex] },
108+
setter = { appModuleSelectedIndex = it?.let(appModules::indexOf) ?: 0 }
109+
)
105110
}
106111
}
107112
row(CleanArchitectureGeneratorBundle.message("dialog.feature.name.label")) {
@@ -111,8 +116,8 @@ class CreateCleanArchitectureFeatureDialog(
111116
cell(featurePackageTextField)
112117
}
113118
row(CleanArchitectureGeneratorBundle.message("dialog.feature.code.quality.label")) {
114-
cell(ktlintCheckBox)
115-
cell(detektCheckBox)
119+
checkBox("ktlint").bindSelected(::enableKtlintInternal)
120+
checkBox("detekt").bindSelected(::enableDetektInternal)
116121
}
117122
}
118123

plugin/src/main/kotlin/com/mitteloupe/cag/cleanarchitecturegenerator/CreateCleanArchitecturePackageDialog.kt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package com.mitteloupe.cag.cleanarchitecturegenerator
33
import com.intellij.openapi.project.Project
44
import com.intellij.openapi.ui.DialogWrapper
55
import com.intellij.openapi.ui.ValidationInfo
6+
import com.intellij.ui.dsl.builder.bindSelected
67
import com.intellij.ui.dsl.builder.panel
7-
import javax.swing.JCheckBox
88
import javax.swing.JComponent
99

1010
class CreateCleanArchitecturePackageDialog(
1111
project: Project
1212
) : DialogWrapper(project) {
13-
private val enableComposeCheckBox = JCheckBox(CleanArchitectureGeneratorBundle.message("dialog.architecture.compose.label"), true)
14-
private val enableKtlintCheckBox = JCheckBox(CleanArchitectureGeneratorBundle.message("dialog.architecture.ktlint.label"), false)
15-
private val enableDetektCheckBox = JCheckBox(CleanArchitectureGeneratorBundle.message("dialog.architecture.detekt.label"), false)
13+
private var enableCompose: Boolean = true
14+
private var enableKtlint: Boolean = false
15+
private var enableDetekt: Boolean = false
1616

1717
init {
1818
title = CleanArchitectureGeneratorBundle.message("info.architecture.generator.title")
@@ -22,21 +22,24 @@ class CreateCleanArchitecturePackageDialog(
2222
override fun createCenterPanel(): JComponent =
2323
panel {
2424
row {
25-
cell(enableComposeCheckBox)
25+
checkBox(CleanArchitectureGeneratorBundle.message("dialog.architecture.compose.label"))
26+
.bindSelected(::enableCompose)
2627
}
2728
row {
28-
cell(enableKtlintCheckBox)
29+
checkBox(CleanArchitectureGeneratorBundle.message("dialog.architecture.ktlint.label"))
30+
.bindSelected(::enableKtlint)
2931
}
3032
row {
31-
cell(enableDetektCheckBox)
33+
checkBox(CleanArchitectureGeneratorBundle.message("dialog.architecture.detekt.label"))
34+
.bindSelected(::enableDetekt)
3235
}
3336
}
3437

3538
override fun doValidate(): ValidationInfo? = null
3639

37-
fun isComposeEnabled(): Boolean = enableComposeCheckBox.isSelected
40+
fun isComposeEnabled(): Boolean = enableCompose
3841

39-
fun isKtlintEnabled(): Boolean = enableKtlintCheckBox.isSelected
42+
fun isKtlintEnabled(): Boolean = enableKtlint
4043

41-
fun isDetektEnabled(): Boolean = enableDetektCheckBox.isSelected
44+
fun isDetektEnabled(): Boolean = enableDetekt
4245
}

plugin/src/main/kotlin/com/mitteloupe/cag/cleanarchitecturegenerator/CreateDataSourceDialog.kt

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,66 @@ package com.mitteloupe.cag.cleanarchitecturegenerator
33
import com.intellij.openapi.project.Project
44
import com.intellij.openapi.ui.DialogWrapper
55
import com.intellij.openapi.ui.ValidationInfo
6-
import com.intellij.ui.components.JBCheckBox
7-
import com.intellij.ui.components.JBLabel
86
import com.intellij.ui.components.JBTextField
7+
import com.intellij.ui.dsl.builder.bindSelected
8+
import com.intellij.ui.dsl.builder.bindText
99
import com.intellij.ui.dsl.builder.panel
1010
import com.intellij.util.ui.UIUtil
1111
import com.mitteloupe.cag.cleanarchitecturegenerator.form.PredicateDocumentFilter
12-
import javax.swing.Box
13-
import javax.swing.BoxLayout
14-
import javax.swing.JComponent
15-
import javax.swing.JPanel
12+
import java.awt.EventQueue.invokeLater
1613
import javax.swing.text.AbstractDocument
1714

1815
private const val DATA_SOURCE_SUFFIX = "DataSource"
1916

2017
class CreateDataSourceDialog(
2118
project: Project?
2219
) : DialogWrapper(project) {
23-
private val dataSourceNameTextField = JBTextField()
24-
private val ktorCheckBox = JBCheckBox("Add Ktor dependencies")
25-
private val retrofitCheckBox = JBCheckBox("Add Retrofit dependencies")
20+
private lateinit var dataSourceNameTextField: JBTextField
2621

2722
val dataSourceNameWithSuffix: String
2823
get() = "$dataSourceName$DATA_SOURCE_SUFFIX"
2924

30-
private val dataSourceName: String
31-
get() = dataSourceNameTextField.text.trim()
25+
private var dataSourceName: String = ""
3226

27+
private var useKtorInternal: Boolean = false
3328
val useKtor: Boolean
34-
get() = ktorCheckBox.isSelected
29+
get() = useKtorInternal
3530

31+
private var useRetrofitInternal: Boolean = false
3632
val useRetrofit: Boolean
37-
get() = retrofitCheckBox.isSelected
33+
get() = useRetrofitInternal
3834

3935
init {
4036
title = CleanArchitectureGeneratorBundle.message("info.datasource.generator.title")
4137
init()
42-
43-
dataSourceNameTextField.columns = 20
44-
45-
(dataSourceNameTextField.document as AbstractDocument).documentFilter =
46-
PredicateDocumentFilter { !it.isWhitespace() }
4738
}
4839

49-
override fun getPreferredFocusedComponent(): JComponent = dataSourceNameTextField
50-
51-
override fun createCenterPanel(): JComponent {
52-
val suffixLabel =
53-
JBLabel(DATA_SOURCE_SUFFIX).apply {
54-
foreground = UIUtil.getLabelDisabledForeground()
55-
}
56-
57-
val nameWithSuffixPanel =
58-
JPanel().apply {
59-
layout = BoxLayout(this, BoxLayout.X_AXIS)
60-
add(dataSourceNameTextField)
61-
add(Box.createHorizontalStrut(4))
62-
add(suffixLabel)
63-
}
64-
65-
return panel {
40+
override fun createCenterPanel() =
41+
panel {
6642
row(CleanArchitectureGeneratorBundle.message("dialog.datasource.name.label")) {
67-
cell(nameWithSuffixPanel)
43+
textField()
44+
.bindText({ dataSourceName }, { dataSourceName = it })
45+
.applyToComponent {
46+
(document as AbstractDocument).documentFilter =
47+
PredicateDocumentFilter { !it.isWhitespace() }
48+
dataSourceNameTextField = this
49+
}
50+
label(DATA_SOURCE_SUFFIX)
51+
.applyToComponent {
52+
foreground = UIUtil.getLabelDisabledForeground()
53+
}
6854
}
6955
row {
70-
cell(ktorCheckBox)
56+
checkBox("Add Ktor dependencies")
57+
.bindSelected(::useKtorInternal)
7158
}
7259
row {
73-
cell(retrofitCheckBox)
60+
checkBox("Add Retrofit dependencies")
61+
.bindSelected(::useRetrofitInternal)
7462
}
63+
}.apply {
64+
invokeLater { dataSourceNameTextField.requestFocusInWindow() }
7565
}
76-
}
7766

7867
override fun doValidate(): ValidationInfo? =
7968
if (dataSourceName.isEmpty()) {

0 commit comments

Comments
 (0)