Skip to content

Commit

Permalink
Move graph and properties map into a CreatorContext
Browse files Browse the repository at this point in the history
Also add WizardContext to it so we can access it anytime
This is required for more flexibility and a future change to migrate
 away from Dispatchers.Swing and possibly remove our bundled coroutines lib
  • Loading branch information
RedNesto committed Jul 16, 2024
1 parent 69241a6 commit 1579723
Show file tree
Hide file tree
Showing 21 changed files with 153 additions and 167 deletions.
31 changes: 31 additions & 0 deletions src/main/kotlin/creator/custom/CreatorContext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Minecraft Development for IntelliJ
*
* https://mcdev.io/
*
* Copyright (C) 2024 minecraft-dev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, version 3.0 only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.demonwav.mcdev.creator.custom

import com.demonwav.mcdev.creator.custom.types.CreatorProperty
import com.intellij.ide.util.projectWizard.WizardContext
import com.intellij.openapi.observable.properties.PropertyGraph

data class CreatorContext(
val graph: PropertyGraph,
val properties: Map<String, CreatorProperty<*>>,
val wizardContext: WizardContext,
)
9 changes: 5 additions & 4 deletions src/main/kotlin/creator/custom/CustomPlatformStep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class CustomPlatformStep(
private var hasTemplateErrors: Boolean = true

private var properties = mutableMapOf<String, CreatorProperty<*>>()
private var creatorContext = CreatorContext(propertyGraph, properties, context)

override fun setupUI(builder: Panel) {
lateinit var templatePropertyPlaceholder: Placeholder
Expand Down Expand Up @@ -311,6 +312,7 @@ class CustomPlatformStep(

private fun createOptionsPanelInBackground(template: LoadedTemplate, placeholder: Placeholder) {
properties = mutableMapOf()
creatorContext = creatorContext.copy(properties = properties)

if (!template.isValid) {
return
Expand All @@ -320,8 +322,7 @@ class CustomPlatformStep(
?: return thisLogger().error("Could not find wizard base data")

properties["PROJECT_NAME"] = ExternalCreatorProperty(
graph = propertyGraph,
properties = properties,
context = creatorContext,
graphProperty = baseData.nameProperty,
valueType = String::class.java
)
Expand Down Expand Up @@ -421,7 +422,7 @@ class CustomPlatformStep(
reporter.fatal("Duplicate property name ${descriptor.name}")
}

val prop = CreatorPropertyFactory.createFromType(descriptor.type, descriptor, propertyGraph, properties)
val prop = CreatorPropertyFactory.createFromType(descriptor.type, descriptor, creatorContext)
if (prop == null) {
reporter.fatal("Unknown template property type ${descriptor.type}")
}
Expand All @@ -434,7 +435,7 @@ class CustomPlatformStep(
return null
}

val factory = Consumer<Panel> { panel -> prop.buildUi(panel, context) }
val factory = Consumer<Panel> { panel -> prop.buildUi(panel) }
val order = descriptor.order ?: 0
return factory to order
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.demonwav.mcdev.asset.MCDevBundle
import com.demonwav.mcdev.asset.MCDevBundle.invoke
import com.demonwav.mcdev.creator.collectMavenVersions
import com.demonwav.mcdev.creator.custom.BuiltinValidations
import com.demonwav.mcdev.creator.custom.CreatorContext
import com.demonwav.mcdev.creator.custom.TemplateEvaluator
import com.demonwav.mcdev.creator.custom.TemplatePropertyDescriptor
import com.demonwav.mcdev.creator.custom.TemplateValidationReporter
Expand All @@ -35,9 +36,7 @@ import com.demonwav.mcdev.platform.forge.version.ForgeVersion
import com.demonwav.mcdev.platform.neoforge.version.NeoForgeVersion
import com.demonwav.mcdev.util.SemanticVersion
import com.demonwav.mcdev.util.asyncIO
import com.intellij.ide.util.projectWizard.WizardContext
import com.intellij.openapi.observable.properties.GraphProperty
import com.intellij.openapi.observable.properties.PropertyGraph
import com.intellij.openapi.observable.util.not
import com.intellij.openapi.observable.util.transform
import com.intellij.ui.ComboboxSpeedSearch
Expand All @@ -56,9 +55,8 @@ import kotlinx.coroutines.withContext

class ArchitecturyVersionsCreatorProperty(
descriptor: TemplatePropertyDescriptor,
graph: PropertyGraph,
properties: Map<String, CreatorProperty<*>>
) : CreatorProperty<ArchitecturyVersionsModel>(descriptor, graph, properties, ArchitecturyVersionsModel::class.java) {
context: CreatorContext
) : CreatorProperty<ArchitecturyVersionsModel>(descriptor, context, ArchitecturyVersionsModel::class.java) {

private val emptyVersion = SemanticVersion.release()
private val emptyValue = ArchitecturyVersionsModel(
Expand Down Expand Up @@ -164,7 +162,7 @@ class ArchitecturyVersionsCreatorProperty(
)
}

override fun buildUi(panel: Panel, context: WizardContext) {
override fun buildUi(panel: Panel) {
panel.row("") {
cell(AsyncProcessIcon("ArchitecturyVersions download"))
label(MCDevBundle("creator.ui.versions_download.label"))
Expand Down Expand Up @@ -474,8 +472,7 @@ class ArchitecturyVersionsCreatorProperty(

override fun create(
descriptor: TemplatePropertyDescriptor,
graph: PropertyGraph,
properties: Map<String, CreatorProperty<*>>
): CreatorProperty<*> = ArchitecturyVersionsCreatorProperty(descriptor, graph, properties)
context: CreatorContext
): CreatorProperty<*> = ArchitecturyVersionsCreatorProperty(descriptor, context)
}
}
15 changes: 6 additions & 9 deletions src/main/kotlin/creator/custom/types/BooleanCreatorProperty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,26 @@

package com.demonwav.mcdev.creator.custom.types

import com.demonwav.mcdev.creator.custom.CreatorContext
import com.demonwav.mcdev.creator.custom.TemplatePropertyDescriptor
import com.intellij.icons.AllIcons
import com.intellij.ide.util.projectWizard.WizardContext
import com.intellij.openapi.observable.properties.PropertyGraph
import com.intellij.ui.content.AlertIcon
import com.intellij.ui.dsl.builder.Panel
import com.intellij.ui.dsl.builder.RightGap
import com.intellij.ui.dsl.builder.bindSelected

class BooleanCreatorProperty(
descriptor: TemplatePropertyDescriptor,
graph: PropertyGraph,
properties: Map<String, CreatorProperty<*>>
) : SimpleCreatorProperty<Boolean>(descriptor, graph, properties, Boolean::class.java) {
context: CreatorContext
) : SimpleCreatorProperty<Boolean>(descriptor, context, Boolean::class.java) {

override fun createDefaultValue(raw: Any?): Boolean = raw as? Boolean ?: false

override fun serialize(value: Boolean): String = value.toString()

override fun deserialize(string: String): Boolean = string.toBoolean()

override fun buildSimpleUi(panel: Panel, context: WizardContext) {
override fun buildSimpleUi(panel: Panel) {
val label = descriptor.translatedLabel
panel.row(label) {
val warning = descriptor.translatedWarning
Expand All @@ -60,8 +58,7 @@ class BooleanCreatorProperty(
class Factory : CreatorPropertyFactory {
override fun create(
descriptor: TemplatePropertyDescriptor,
graph: PropertyGraph,
properties: Map<String, CreatorProperty<*>>
): CreatorProperty<*> = BooleanCreatorProperty(descriptor, graph, properties)
context: CreatorContext
): CreatorProperty<*> = BooleanCreatorProperty(descriptor, context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ package com.demonwav.mcdev.creator.custom.types

import com.demonwav.mcdev.asset.MCDevBundle
import com.demonwav.mcdev.creator.custom.BuiltinValidations
import com.demonwav.mcdev.creator.custom.CreatorContext
import com.demonwav.mcdev.creator.custom.TemplatePropertyDescriptor
import com.demonwav.mcdev.creator.custom.TemplateValidationReporter
import com.demonwav.mcdev.creator.custom.model.BuildSystemCoordinates
import com.intellij.ide.util.projectWizard.WizardContext
import com.intellij.openapi.observable.properties.GraphProperty
import com.intellij.openapi.observable.properties.PropertyGraph
import com.intellij.openapi.observable.util.transform
import com.intellij.openapi.ui.validation.CHECK_ARTIFACT_ID
import com.intellij.openapi.ui.validation.CHECK_GROUP_ID
Expand All @@ -46,9 +45,8 @@ private val nonExampleValidation = validationErrorIf<String>(MCDevBundle("creato

class BuildSystemCoordinatesCreatorProperty(
descriptor: TemplatePropertyDescriptor,
graph: PropertyGraph,
properties: Map<String, CreatorProperty<*>>
) : CreatorProperty<BuildSystemCoordinates>(descriptor, graph, properties, BuildSystemCoordinates::class.java) {
context: CreatorContext
) : CreatorProperty<BuildSystemCoordinates>(descriptor, context, BuildSystemCoordinates::class.java) {

private val default = createDefaultValue(descriptor.default)

Expand Down Expand Up @@ -99,7 +97,7 @@ class BuildSystemCoordinatesCreatorProperty(
}
}

override fun buildUi(panel: Panel, context: WizardContext) {
override fun buildUi(panel: Panel) {
panel.collapsibleGroup(MCDevBundle("creator.ui.group.title")) {
this.row(MCDevBundle("creator.ui.group.group_id")) {
this.textField()
Expand Down Expand Up @@ -128,8 +126,7 @@ class BuildSystemCoordinatesCreatorProperty(
class Factory : CreatorPropertyFactory {
override fun create(
descriptor: TemplatePropertyDescriptor,
graph: PropertyGraph,
properties: Map<String, CreatorProperty<*>>
): CreatorProperty<*> = BuildSystemCoordinatesCreatorProperty(descriptor, graph, properties)
context: CreatorContext
): CreatorProperty<*> = BuildSystemCoordinatesCreatorProperty(descriptor, context)
}
}
15 changes: 6 additions & 9 deletions src/main/kotlin/creator/custom/types/ClassFqnCreatorProperty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
package com.demonwav.mcdev.creator.custom.types

import com.demonwav.mcdev.creator.custom.BuiltinValidations
import com.demonwav.mcdev.creator.custom.CreatorContext
import com.demonwav.mcdev.creator.custom.PropertyDerivation
import com.demonwav.mcdev.creator.custom.TemplatePropertyDescriptor
import com.demonwav.mcdev.creator.custom.TemplateValidationReporter
import com.demonwav.mcdev.creator.custom.derivation.PreparedDerivation
import com.demonwav.mcdev.creator.custom.derivation.SuggestClassNamePropertyDerivation
import com.demonwav.mcdev.creator.custom.model.ClassFqn
import com.intellij.ide.util.projectWizard.WizardContext
import com.intellij.openapi.observable.properties.PropertyGraph
import com.intellij.ui.dsl.builder.COLUMNS_LARGE
import com.intellij.ui.dsl.builder.Panel
import com.intellij.ui.dsl.builder.bindText
Expand All @@ -37,17 +36,16 @@ import com.intellij.ui.dsl.builder.textValidation

class ClassFqnCreatorProperty(
descriptor: TemplatePropertyDescriptor,
graph: PropertyGraph,
properties: Map<String, CreatorProperty<*>>
) : SimpleCreatorProperty<ClassFqn>(descriptor, graph, properties, ClassFqn::class.java) {
context: CreatorContext
) : SimpleCreatorProperty<ClassFqn>(descriptor, context, ClassFqn::class.java) {

override fun createDefaultValue(raw: Any?): ClassFqn = ClassFqn(raw as? String ?: "")

override fun serialize(value: ClassFqn): String = value.toString()

override fun deserialize(string: String): ClassFqn = ClassFqn(string)

override fun buildSimpleUi(panel: Panel, context: WizardContext) {
override fun buildSimpleUi(panel: Panel) {
panel.row(descriptor.translatedLabel) {
this.textField().bindText(this@ClassFqnCreatorProperty.toStringProperty(graphProperty))
.columns(COLUMNS_LARGE)
Expand All @@ -71,8 +69,7 @@ class ClassFqnCreatorProperty(
class Factory : CreatorPropertyFactory {
override fun create(
descriptor: TemplatePropertyDescriptor,
graph: PropertyGraph,
properties: Map<String, CreatorProperty<*>>
): CreatorProperty<*> = ClassFqnCreatorProperty(descriptor, graph, properties)
context: CreatorContext
): CreatorProperty<*> = ClassFqnCreatorProperty(descriptor, context)
}
}
15 changes: 12 additions & 3 deletions src/main/kotlin/creator/custom/types/CreatorProperty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.demonwav.mcdev.creator.custom.types

import com.demonwav.mcdev.creator.custom.CreatorContext
import com.demonwav.mcdev.creator.custom.PropertyDerivation
import com.demonwav.mcdev.creator.custom.TemplateEvaluator
import com.demonwav.mcdev.creator.custom.TemplatePropertyDescriptor
Expand All @@ -38,10 +39,18 @@ import com.intellij.ui.dsl.builder.Row

abstract class CreatorProperty<T>(
val descriptor: TemplatePropertyDescriptor,
val graph: PropertyGraph,
protected val properties: Map<String, CreatorProperty<*>>,
protected val context: CreatorContext,
val valueType: Class<T>
) {
protected val graph: PropertyGraph
get() = context.graph

protected val properties
get() = context.properties

protected val wizardContext: WizardContext
get() = context.wizardContext

private var derivation: PreparedDerivation? = null
private lateinit var visibleProperty: GraphProperty<Boolean>

Expand Down Expand Up @@ -95,7 +104,7 @@ abstract class CreatorProperty<T>(

protected open fun convertSelectDerivationResult(original: Any?): Any? = original

abstract fun buildUi(panel: Panel, context: WizardContext)
abstract fun buildUi(panel: Panel)

/**
* Prepares everything this property needs, like calling [GraphProperty]'s [GraphProperty.afterChange] and
Expand Down
13 changes: 4 additions & 9 deletions src/main/kotlin/creator/custom/types/CreatorPropertyFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

package com.demonwav.mcdev.creator.custom.types

import com.demonwav.mcdev.creator.custom.CreatorContext
import com.demonwav.mcdev.creator.custom.TemplatePropertyDescriptor
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.extensions.RequiredElement
import com.intellij.openapi.observable.properties.PropertyGraph
import com.intellij.openapi.util.KeyedExtensionCollector
import com.intellij.serviceContainer.BaseKeyedLazyInstance
import com.intellij.util.KeyedLazyInstance
Expand All @@ -42,18 +42,13 @@ interface CreatorPropertyFactory {
fun createFromType(
type: String,
descriptor: TemplatePropertyDescriptor,
graph: PropertyGraph,
properties: Map<String, CreatorProperty<*>>
context: CreatorContext
): CreatorProperty<*>? {
return COLLECTOR.findSingle(type)?.create(descriptor, graph, properties)
return COLLECTOR.findSingle(type)?.create(descriptor, context)
}
}

fun create(
descriptor: TemplatePropertyDescriptor,
graph: PropertyGraph,
properties: Map<String, CreatorProperty<*>>
): CreatorProperty<*>
fun create(descriptor: TemplatePropertyDescriptor, context: CreatorContext): CreatorProperty<*>
}

class CreatorPropertyFactoryBean :
Expand Down
10 changes: 4 additions & 6 deletions src/main/kotlin/creator/custom/types/ExternalCreatorProperty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@

package com.demonwav.mcdev.creator.custom.types

import com.demonwav.mcdev.creator.custom.CreatorContext
import com.demonwav.mcdev.creator.custom.TemplatePropertyDescriptor
import com.demonwav.mcdev.creator.custom.TemplateValidationReporter
import com.intellij.ide.util.projectWizard.WizardContext
import com.intellij.openapi.observable.properties.GraphProperty
import com.intellij.openapi.observable.properties.PropertyGraph
import com.intellij.ui.dsl.builder.Panel

class ExternalCreatorProperty<T>(
descriptor: TemplatePropertyDescriptor = TemplatePropertyDescriptor("", "", "", default = ""),
graph: PropertyGraph,
properties: Map<String, CreatorProperty<*>>,
context: CreatorContext,
override val graphProperty: GraphProperty<T>,
valueType: Class<T>,
) : CreatorProperty<T>(descriptor, graph, properties, valueType) {
) : CreatorProperty<T>(descriptor, context, valueType) {

override fun setupProperty(reporter: TemplateValidationReporter) = Unit

Expand All @@ -46,5 +44,5 @@ class ExternalCreatorProperty<T>(
override fun deserialize(string: String): T =
throw UnsupportedOperationException("Unsupported for external properties")

override fun buildUi(panel: Panel, context: WizardContext) = Unit
override fun buildUi(panel: Panel) = Unit
}
Loading

0 comments on commit 1579723

Please sign in to comment.