Skip to content

Commit

Permalink
Consistify insertion popup
Browse files Browse the repository at this point in the history
Fixes #463.
  • Loading branch information
FWDekker committed Sep 22, 2023
1 parent 8ff7277 commit 5700fc9
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/main/kotlin/com/fwdekker/randomness/template/TemplateJTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,7 @@ class TemplateJTree(
*/
override fun actionPerformed(event: AnActionEvent) =
JBPopupFactory.getInstance()
.createListPopup(
if (selectedNodeNotRoot == null) DefaultTemplatesPopupStep()
else MainPopupStep()
)
.createListPopup(MainPopupStep(templatesOnly = selectedNodeNotRoot == null))
.show(preferredPopupPoint)


Expand All @@ -422,8 +419,9 @@ class TemplateJTree(
*
* @param schemes the schemes that can be inserted
*/
private abstract inner class AddSchemePopupStep(schemes: List<Scheme>) :
BaseListPopupStep<Scheme>(null, schemes) {
private abstract inner class AbstractPopupStep(
schemes: List<Scheme>,
) : BaseListPopupStep<Scheme>(null, schemes) {
/**
* Returns [value]'s icon.
*/
Expand Down Expand Up @@ -451,6 +449,7 @@ class TemplateJTree(
return null
}


/**
* Returns `true`.
*/
Expand All @@ -464,15 +463,22 @@ class TemplateJTree(

/**
* The top-level [PopupStep], which includes the default templates and various reference types.
*
* @property templatesOnly `true` if and only if non-[Template] schemes cannot be inserted.
*/
private inner class MainPopupStep : AddSchemePopupStep(POPUP_STEP_SCHEMES) {
private inner class MainPopupStep(private val templatesOnly: Boolean) : AbstractPopupStep(POPUP_STEP_SCHEMES) {
override fun onChosen(value: Scheme?, finalChoice: Boolean) =
when (value) {
POPUP_STEP_SCHEMES[0] -> DefaultTemplatesPopupStep()
POPUP_STEP_SCHEMES[0] -> TemplatesPopupStep()
POPUP_STEP_SCHEMES[POPUP_STEP_SCHEMES.size - 1] -> ReferencesPopupStep()
else -> super.onChosen(value, finalChoice)
}

/**
* Returns `true` if and only if [value] is a [Template] or [templatesOnly] is `false`.
*/
override fun isSelectable(value: Scheme?) = value is Template || !templatesOnly

/**
* Returns `true` if and only if [value] equals the [Template] or [TemplateReference] entry.
*/
Expand All @@ -490,16 +496,16 @@ class TemplateJTree(
/**
* A [PopupStep] that shows only the default templates.
*/
private inner class DefaultTemplatesPopupStep :
AddSchemePopupStep(listOf(Template("Empty")) + TemplateList.DEFAULT_TEMPLATES)
private inner class TemplatesPopupStep :
AbstractPopupStep(listOf(Template("Empty")) + TemplateList.DEFAULT_TEMPLATES)

/**
* A [PopupStep] that contains a [TemplateReference] for each [Template] that can currently be referenced from
* [selectedTemplate].
*
* Ineligible [Template]s are automatically filtered out.
*/
private inner class ReferencesPopupStep : AddSchemePopupStep(
private inner class ReferencesPopupStep : AbstractPopupStep(
currentSettings.templates
.filter { selectedTemplate!!.canReference(it) }
.map { TemplateReference(it.uuid) }
Expand Down

0 comments on commit 5700fc9

Please sign in to comment.