MPP wizard step refactoring: use UI layout DSL
This commit is contained in:
committed by
Mikhail Glukhikh
parent
ea4b04780c
commit
71a6827b1c
+43
-132
@@ -26,14 +26,11 @@ import com.intellij.openapi.projectRoots.JavaSdk
|
|||||||
import com.intellij.openapi.projectRoots.Sdk
|
import com.intellij.openapi.projectRoots.Sdk
|
||||||
import com.intellij.openapi.roots.ui.configuration.JdkComboBox
|
import com.intellij.openapi.roots.ui.configuration.JdkComboBox
|
||||||
import com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel
|
import com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel
|
||||||
import com.intellij.openapi.ui.LabeledComponent
|
import com.intellij.openapi.ui.ComboBox
|
||||||
import com.intellij.ui.DocumentAdapter
|
import com.intellij.ui.DocumentAdapter
|
||||||
import com.intellij.ui.PanelWithAnchor
|
import com.intellij.ui.layout.CCFlags
|
||||||
import com.intellij.util.ui.JBUI
|
import com.intellij.ui.layout.LCFlags
|
||||||
import com.intellij.util.ui.UIUtil
|
import com.intellij.ui.layout.panel
|
||||||
import java.awt.BorderLayout
|
|
||||||
import java.awt.GridBagConstraints
|
|
||||||
import java.awt.GridBagLayout
|
|
||||||
import javax.swing.*
|
import javax.swing.*
|
||||||
import javax.swing.event.DocumentEvent
|
import javax.swing.event.DocumentEvent
|
||||||
|
|
||||||
@@ -42,61 +39,37 @@ class KotlinGradleMultiplatformWizardStep(
|
|||||||
private val wizardContext: WizardContext
|
private val wizardContext: WizardContext
|
||||||
) : ModuleWizardStep() {
|
) : ModuleWizardStep() {
|
||||||
|
|
||||||
private val hierarchyKindComponent: LabeledComponent<JComboBox<String>> =
|
private val hierarchyKindComponent = ComboBox(
|
||||||
LabeledComponent.create(
|
arrayOf(
|
||||||
JComboBox(
|
"Root empty module with common & platform children",
|
||||||
arrayOf(
|
"Root common module with children platform modules"
|
||||||
"Root empty module with common and platform modules as children",
|
), 400
|
||||||
"Root common module with children platform modules"
|
)
|
||||||
)
|
private val rootModuleNameComponent = JTextField()
|
||||||
), "Hierarchy kind", BorderLayout.WEST
|
private val commonModuleNameComponent = JTextField()
|
||||||
)
|
private val jvmCheckBox = JCheckBox("", true)
|
||||||
private val rootModuleNameComponent: LabeledComponent<JTextField> =
|
|
||||||
LabeledComponent.create(JTextField(), "Root module name:", BorderLayout.WEST)
|
|
||||||
private val commonModuleNameComponent: LabeledComponent<JTextField> =
|
|
||||||
LabeledComponent.create(JTextField(), "Common module name:", BorderLayout.WEST)
|
|
||||||
private val jvmCheckBox: JCheckBox =
|
|
||||||
JCheckBox("Create JVM module", true)
|
|
||||||
private val jdkModel = ProjectSdksModel().also {
|
private val jdkModel = ProjectSdksModel().also {
|
||||||
it.reset(ProjectManager.getInstance().defaultProject)
|
it.reset(ProjectManager.getInstance().defaultProject)
|
||||||
}
|
}
|
||||||
private val jdkComboBox: JdkComboBox =
|
private val jdkComboBox = JdkComboBox(jdkModel) { it is JavaSdk }
|
||||||
JdkComboBox(jdkModel) { it is JavaSdk }
|
private val jvmModuleNameComponent = JTextField()
|
||||||
private val jvmModuleNameComponent: LabeledComponent<JTextField> =
|
private val jsCheckBox = JCheckBox("", true)
|
||||||
LabeledComponent.create(JTextField(), "JVM module name:", BorderLayout.WEST)
|
private val jsModuleNameComponent = JTextField()
|
||||||
private val jsCheckBox: JCheckBox =
|
|
||||||
JCheckBox("Create JS module", true)
|
|
||||||
private val jsModuleNameComponent: LabeledComponent<JTextField> =
|
|
||||||
LabeledComponent.create(JTextField(), "JS module name:", BorderLayout.WEST)
|
|
||||||
|
|
||||||
private val panel: JPanel
|
private val panel: JPanel
|
||||||
private var syncEditing: Boolean = true
|
private var syncEditing: Boolean = true
|
||||||
private var inSyncUpdate: Boolean = false
|
private var inSyncUpdate: Boolean = false
|
||||||
|
|
||||||
init {
|
init {
|
||||||
panel = object : JPanel(GridBagLayout()), PanelWithAnchor {
|
|
||||||
private var anchor: JComponent? = hierarchyKindComponent.anchor
|
|
||||||
|
|
||||||
override fun getAnchor(): JComponent? = anchor
|
|
||||||
|
|
||||||
override fun setAnchor(anchor: JComponent?) {
|
|
||||||
this.anchor = anchor
|
|
||||||
hierarchyKindComponent.anchor = anchor
|
|
||||||
rootModuleNameComponent.anchor = anchor
|
|
||||||
commonModuleNameComponent.anchor = anchor
|
|
||||||
jvmModuleNameComponent.anchor = anchor
|
|
||||||
jsModuleNameComponent.anchor = anchor
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val baseDir = wizardContext.projectFileDirectory
|
val baseDir = wizardContext.projectFileDirectory
|
||||||
val projectName = wizardContext.projectName
|
val projectName = wizardContext.projectName
|
||||||
val initialProjectName = projectName ?: ProjectWizardUtil.findNonExistingFileName(baseDir, "untitled", "")
|
val initialProjectName = projectName ?: ProjectWizardUtil.findNonExistingFileName(baseDir, "untitled", "")
|
||||||
rootModuleNameComponent.component.text = initialProjectName
|
rootModuleNameComponent.text = initialProjectName
|
||||||
rootModuleNameComponent.component.select(0, initialProjectName.length)
|
rootModuleNameComponent.select(0, initialProjectName.length)
|
||||||
|
|
||||||
updateDerivedModuleNames()
|
updateDerivedModuleNames()
|
||||||
|
|
||||||
rootModuleNameComponent.component.document.addDocumentListener(object : DocumentAdapter() {
|
rootModuleNameComponent.document.addDocumentListener(object : DocumentAdapter() {
|
||||||
override fun textChanged(e: DocumentEvent?) {
|
override fun textChanged(e: DocumentEvent?) {
|
||||||
if (syncEditing) {
|
if (syncEditing) {
|
||||||
updateDerivedModuleNames()
|
updateDerivedModuleNames()
|
||||||
@@ -111,13 +84,13 @@ class KotlinGradleMultiplatformWizardStep(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
commonModuleNameComponent.component.document.addDocumentListener(stopSyncEditingListener)
|
commonModuleNameComponent.document.addDocumentListener(stopSyncEditingListener)
|
||||||
jvmModuleNameComponent.component.document.addDocumentListener(stopSyncEditingListener)
|
jvmModuleNameComponent.document.addDocumentListener(stopSyncEditingListener)
|
||||||
jsModuleNameComponent.component.document.addDocumentListener(stopSyncEditingListener)
|
jsModuleNameComponent.document.addDocumentListener(stopSyncEditingListener)
|
||||||
|
|
||||||
jdkComboBox.selectedJdk = jdkModel.projectSdk
|
jdkComboBox.selectedJdk = jdkModel.projectSdk
|
||||||
|
|
||||||
hierarchyKindComponent.component.addActionListener {
|
hierarchyKindComponent.addActionListener {
|
||||||
commonModuleNameComponent.isEnabled = !commonModuleIsRoot
|
commonModuleNameComponent.isEnabled = !commonModuleIsRoot
|
||||||
}
|
}
|
||||||
jvmCheckBox.addItemListener {
|
jvmCheckBox.addItemListener {
|
||||||
@@ -128,88 +101,26 @@ class KotlinGradleMultiplatformWizardStep(
|
|||||||
jsModuleNameComponent.isEnabled = jsCheckBox.isSelected
|
jsModuleNameComponent.isEnabled = jsCheckBox.isSelected
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.panel = panel(LCFlags.fillY) {
|
||||||
|
row("Hierarchy kind:") { hierarchyKindComponent() }
|
||||||
|
row("Root module name:") { rootModuleNameComponent() }
|
||||||
|
row("Common module name:") { commonModuleNameComponent() }
|
||||||
|
row("Create JVM module:") { jvmCheckBox() }
|
||||||
|
row(" JVM module JDK:") { jdkComboBox() }
|
||||||
|
row(" JVM module name:") { jvmModuleNameComponent() }
|
||||||
|
row("Create JS module:") { jsCheckBox() }
|
||||||
|
row(" JS module name:") { jsModuleNameComponent() }
|
||||||
|
row { JLabel("")(CCFlags.pushY, CCFlags.growY) }
|
||||||
|
}
|
||||||
panel.border = BorderFactory.createEmptyBorder(10, 10, 10, 10)
|
panel.border = BorderFactory.createEmptyBorder(10, 10, 10, 10)
|
||||||
panel.add(
|
|
||||||
hierarchyKindComponent,
|
|
||||||
GridBagConstraints(
|
|
||||||
0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0,
|
|
||||||
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
|
|
||||||
JBUI.emptyInsets(), 0, 0
|
|
||||||
)
|
|
||||||
)
|
|
||||||
panel.add(
|
|
||||||
rootModuleNameComponent,
|
|
||||||
GridBagConstraints(
|
|
||||||
0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0,
|
|
||||||
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
|
|
||||||
JBUI.insets(10, 0, 0, 0), 0, 0
|
|
||||||
)
|
|
||||||
)
|
|
||||||
panel.add(
|
|
||||||
commonModuleNameComponent,
|
|
||||||
GridBagConstraints(
|
|
||||||
0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0,
|
|
||||||
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
|
|
||||||
JBUI.emptyInsets(), 0, 0
|
|
||||||
)
|
|
||||||
)
|
|
||||||
panel.add(
|
|
||||||
jvmCheckBox,
|
|
||||||
GridBagConstraints(
|
|
||||||
0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0,
|
|
||||||
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
|
|
||||||
JBUI.emptyInsets(), 0, 0
|
|
||||||
)
|
|
||||||
)
|
|
||||||
panel.add(
|
|
||||||
jdkComboBox,
|
|
||||||
GridBagConstraints(
|
|
||||||
0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0,
|
|
||||||
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
|
|
||||||
JBUI.emptyInsets(), 0, 0
|
|
||||||
)
|
|
||||||
)
|
|
||||||
panel.add(
|
|
||||||
jvmModuleNameComponent,
|
|
||||||
GridBagConstraints(
|
|
||||||
0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0,
|
|
||||||
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
|
|
||||||
JBUI.emptyInsets(), 0, 0
|
|
||||||
)
|
|
||||||
)
|
|
||||||
panel.add(
|
|
||||||
jsCheckBox,
|
|
||||||
GridBagConstraints(
|
|
||||||
0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0,
|
|
||||||
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
|
|
||||||
JBUI.emptyInsets(), 0, 0
|
|
||||||
)
|
|
||||||
)
|
|
||||||
panel.add(
|
|
||||||
jsModuleNameComponent,
|
|
||||||
GridBagConstraints(
|
|
||||||
0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0,
|
|
||||||
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
|
|
||||||
JBUI.emptyInsets(), 0, 0
|
|
||||||
)
|
|
||||||
)
|
|
||||||
panel.add(
|
|
||||||
JLabel(""),
|
|
||||||
GridBagConstraints(
|
|
||||||
0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 1.0,
|
|
||||||
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
|
|
||||||
JBUI.emptyInsets(), 0, 0
|
|
||||||
)
|
|
||||||
)
|
|
||||||
UIUtil.mergeComponentsWithAnchor(panel)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateDerivedModuleNames() {
|
private fun updateDerivedModuleNames() {
|
||||||
inSyncUpdate = true
|
inSyncUpdate = true
|
||||||
try {
|
try {
|
||||||
commonModuleNameComponent.component.text = "$rootModuleName-common"
|
commonModuleNameComponent.text = "$rootModuleName-common"
|
||||||
jvmModuleNameComponent.component.text = "$rootModuleName-jvm"
|
jvmModuleNameComponent.text = "$rootModuleName-jvm"
|
||||||
jsModuleNameComponent.component.text = "$rootModuleName-js"
|
jsModuleNameComponent.text = "$rootModuleName-js"
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
inSyncUpdate = false
|
inSyncUpdate = false
|
||||||
@@ -230,17 +141,17 @@ class KotlinGradleMultiplatformWizardStep(
|
|||||||
override fun getComponent() = panel
|
override fun getComponent() = panel
|
||||||
|
|
||||||
private val commonModuleIsRoot: Boolean
|
private val commonModuleIsRoot: Boolean
|
||||||
get() = hierarchyKindComponent.component.selectedIndex != 0
|
get() = hierarchyKindComponent.selectedIndex != 0
|
||||||
private val rootModuleName: String
|
private val rootModuleName: String
|
||||||
get() = rootModuleNameComponent.component.text
|
get() = rootModuleNameComponent.text
|
||||||
private val commonModuleName: String
|
private val commonModuleName: String
|
||||||
get() = if (commonModuleIsRoot) "" else commonModuleNameComponent.component.text
|
get() = if (commonModuleIsRoot) "" else commonModuleNameComponent.text
|
||||||
private val jvmModuleName: String
|
private val jvmModuleName: String
|
||||||
get() = if (jvmCheckBox.isSelected) jvmModuleNameComponent.component.text else ""
|
get() = if (jvmCheckBox.isSelected) jvmModuleNameComponent.text else ""
|
||||||
private val jdk: Sdk?
|
private val jdk: Sdk?
|
||||||
get() = if (jvmCheckBox.isSelected) jdkComboBox.selectedJdk else null
|
get() = if (jvmCheckBox.isSelected) jdkComboBox.selectedJdk else null
|
||||||
private val jsModuleName: String
|
private val jsModuleName: String
|
||||||
get() = if (jsCheckBox.isSelected) jsModuleNameComponent.component.text else ""
|
get() = if (jsCheckBox.isSelected) jsModuleNameComponent.text else ""
|
||||||
|
|
||||||
override fun validate(): Boolean {
|
override fun validate(): Boolean {
|
||||||
if (rootModuleName.isEmpty()) {
|
if (rootModuleName.isEmpty()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user