Skip to content

Commit 75c20e6

Browse files
authored
Merge pull request #397 from FWDekker/v3-major-features
Two major features for v3
2 parents c27259c + 32d8e82 commit 75c20e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1329
-395
lines changed

src/main/kotlin/com/fwdekker/randomness/PopupAction.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.intellij.openapi.actionSystem.ActionGroup
88
import com.intellij.openapi.actionSystem.AnAction
99
import com.intellij.openapi.actionSystem.AnActionEvent
1010
import com.intellij.openapi.actionSystem.CommonDataKeys
11+
import com.intellij.openapi.actionSystem.Separator
1112
import com.intellij.openapi.ui.popup.JBPopupFactory
1213
import com.intellij.ui.popup.list.ListPopupImpl
1314
import icons.RandomnessIcons
@@ -100,7 +101,9 @@ class PopupAction : AnAction() {
100101
* @param event carries information on the invocation place
101102
*/
102103
override fun getChildren(event: AnActionEvent?) =
103-
TemplateSettings.default.state.templates.map { TemplateGroupAction(it) }.toTypedArray()
104+
TemplateSettings.default.state.templates.map { TemplateGroupAction(it) }.toTypedArray<AnAction>() +
105+
Separator() +
106+
TemplateSettingsAction()
104107
}
105108

106109
/**
@@ -113,7 +116,9 @@ class PopupAction : AnAction() {
113116
* @param event carries information on the invocation place
114117
*/
115118
override fun getChildren(event: AnActionEvent?) =
116-
arrayOf(TemplateSettingsAction())
119+
TemplateSettings.default.state.templates.map { TemplateSettingsAction(it) }.toTypedArray<AnAction>() +
120+
Separator() +
121+
TemplateSettingsAction()
117122
}
118123

119124

src/main/kotlin/com/fwdekker/randomness/Scheme.kt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.fwdekker.randomness
22

33
import com.fwdekker.randomness.array.ArraySchemeDecorator
44
import com.intellij.util.xmlb.annotations.Transient
5+
import icons.RandomnessIcons
56
import javax.swing.Icon
67
import kotlin.random.Random
78

@@ -17,14 +18,27 @@ abstract class Scheme : State() {
1718
*/
1819
abstract val decorator: ArraySchemeDecorator?
1920

21+
/**
22+
* The name of the scheme as shown to the user.
23+
*/
24+
abstract val name: String
25+
26+
/**
27+
* The icons that represent schemes of this type.
28+
*/
29+
@Transient
30+
open val icons: RandomnessIcons? = null
31+
2032
/**
2133
* The icon for this scheme; depends on whether its array decorator is enabled.
2234
*/
23-
override val icon: Icon?
35+
@get:Transient
36+
val icon: Icon?
2437
get() =
2538
if (decorator?.enabled == true) icons?.Array
2639
else icons?.Base
2740

41+
2842
/**
2943
* The random number generator used to generate random values.
3044
*/
@@ -63,8 +77,17 @@ abstract class Scheme : State() {
6377
@Throws(DataGenerationException::class)
6478
abstract fun generateUndecoratedStrings(count: Int = 1): List<String>
6579

80+
/**
81+
* Sets the [SettingsState] that may be used by this scheme.
82+
*
83+
* Useful in case the scheme's behavior depends not only on its own internal state, but also that of other settings.
84+
*/
85+
open fun setSettingsState(settingsState: SettingsState) {
86+
decorator?.setSettingsState(settingsState)
87+
}
88+
6689

67-
abstract override fun deepCopy(): Scheme
90+
abstract override fun deepCopy(retainUuid: Boolean): Scheme
6891
}
6992

7093
/**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.fwdekker.randomness
2+
3+
4+
/**
5+
* A user-configurable persistent collection.
6+
*/
7+
abstract class Settings : State() {
8+
abstract override fun deepCopy(retainUuid: Boolean): Settings
9+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fwdekker.randomness
2+
3+
import com.fwdekker.randomness.string.SymbolSetSettings
4+
import com.fwdekker.randomness.template.TemplateList
5+
import com.fwdekker.randomness.template.TemplateSettings
6+
import com.fwdekker.randomness.word.DictionarySettings
7+
8+
9+
/**
10+
* Contains references to various [Settings] objects.
11+
*
12+
* @property templateList The template list.
13+
* @property symbolSetSettings The symbol set settings.
14+
* @property dictionarySettings The dictionary settings.
15+
*/
16+
data class SettingsState(
17+
var templateList: TemplateList = TemplateList(emptyList()),
18+
var symbolSetSettings: SymbolSetSettings = SymbolSetSettings(emptyMap()),
19+
var dictionarySettings: DictionarySettings = DictionarySettings(emptySet())
20+
) : State() {
21+
override fun doValidate() =
22+
templateList.doValidate() ?: symbolSetSettings.doValidate() ?: dictionarySettings.doValidate()
23+
24+
override fun copyFrom(scheme: State) {
25+
require(scheme is SettingsState) { "Cannot copy from non-SettingsState." }
26+
27+
templateList.copyFrom(scheme.templateList)
28+
symbolSetSettings.copyFrom(scheme.symbolSetSettings)
29+
dictionarySettings.copyFrom(scheme.dictionarySettings)
30+
}
31+
32+
override fun deepCopy(retainUuid: Boolean) =
33+
copy(
34+
templateList = templateList.deepCopy(retainUuid = retainUuid),
35+
symbolSetSettings = symbolSetSettings.deepCopy(retainUuid = retainUuid),
36+
dictionarySettings = dictionarySettings.deepCopy(retainUuid = retainUuid)
37+
).also { if (retainUuid) it.uuid = uuid }
38+
39+
40+
/**
41+
* Holds constants.
42+
*/
43+
companion object {
44+
/**
45+
* The persistent `SettingsState` instance.
46+
*/
47+
val default by lazy {
48+
SettingsState(
49+
TemplateSettings.default.state,
50+
SymbolSetSettings.default,
51+
DictionarySettings.default
52+
)
53+
}
54+
}
55+
}

src/main/kotlin/com/fwdekker/randomness/State.kt

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
package com.fwdekker.randomness
22

3+
import com.fasterxml.uuid.Generators
34
import com.intellij.util.xmlb.XmlSerializerUtil
45
import com.intellij.util.xmlb.annotations.Transient
5-
import icons.RandomnessIcons
6-
import javax.swing.Icon
6+
import java.util.UUID
7+
import kotlin.random.Random
8+
import kotlin.random.asJavaRandom
79

810

911
/**
1012
* A state holds variables that can be configured.
1113
*/
1214
abstract class State {
1315
/**
14-
* The name of the scheme as shown to the user.
15-
*/
16-
abstract val name: String
17-
18-
/**
19-
* The icons that represent schemes of this type.
20-
*/
21-
@Transient
22-
open val icons: RandomnessIcons? = null
23-
24-
/**
25-
* The icon that represents this scheme instance.
16+
* A UUID to uniquely track this scheme even when it is copied.
2617
*/
2718
@get:Transient
28-
open val icon: Icon?
29-
get() = icons?.Base
19+
var uuid: UUID = Generators.randomBasedGenerator(Random.Default.asJavaRandom()).generate()
3020

3121

3222
/**
@@ -44,16 +34,17 @@ abstract class State {
4434
*
4535
* @param scheme the scheme to copy into this scheme; should be a subclass of this scheme
4636
*/
47-
fun copyFrom(scheme: State) = XmlSerializerUtil.copyBean(scheme.deepCopy(), this)
37+
open fun copyFrom(scheme: State) = XmlSerializerUtil.copyBean(scheme.deepCopy(retainUuid = true), this)
4838

4939
/**
5040
* Returns a deep copy of this scheme.
5141
*
5242
* Fields marked with [Transient] will be shallow-copied.
5343
*
44+
* @param retainUuid false if and only if the copy should have a different [uuid]
5445
* @return a deep copy of this scheme
5546
*/
56-
abstract fun deepCopy(): State
47+
abstract fun deepCopy(retainUuid: Boolean = false): State
5748

5849

5950
/**

src/main/kotlin/com/fwdekker/randomness/StateEditor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ abstract class StateEditor<S : State>(val originalState: S) {
4040
*
4141
* Does nothing if and only if [isModified] returns false.
4242
*/
43-
fun applyState() = originalState.copyFrom(readState())
43+
open fun applyState() = originalState.copyFrom(readState())
4444

4545

4646
/**

src/main/kotlin/com/fwdekker/randomness/array/ArraySchemeDecorator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ data class ArraySchemeDecorator(
4242
if (count < MIN_COUNT) "Minimum count should be at least $MIN_COUNT, but is $count."
4343
else null
4444

45-
override fun deepCopy() = copy()
45+
override fun deepCopy(retainUuid: Boolean) = copy().also { if (retainUuid) it.uuid = this.uuid }
4646

4747

4848
/**

src/main/kotlin/com/fwdekker/randomness/array/ArraySchemeDecoratorEditor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class ArraySchemeDecoratorEditor(settings: ArraySchemeDecorator) : StateEditor<A
8080
brackets = bracketsGroup.getValue() ?: DEFAULT_BRACKETS,
8181
separator = separatorGroup.getValue() ?: DEFAULT_SEPARATOR,
8282
isSpaceAfterSeparator = spaceAfterSeparatorCheckBox.isSelected
83-
)
83+
).also { it.uuid = originalState.uuid }
8484

8585

8686
override fun addChangeListener(listener: () -> Unit) =

src/main/kotlin/com/fwdekker/randomness/decimal/DecimalScheme.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ data class DecimalScheme(
7575
maxValue - minValue > MAX_VALUE_DIFFERENCE -> "Value range should not exceed $MAX_VALUE_DIFFERENCE."
7676
decimalCount < MIN_DECIMAL_COUNT -> "Decimal count should be at least $MIN_DECIMAL_COUNT."
7777
decimalSeparator.isEmpty() -> "Select a decimal separator."
78-
else -> null
78+
else -> decorator.doValidate()
7979
}
8080

81-
override fun deepCopy() = copy(decorator = decorator.deepCopy())
81+
override fun deepCopy(retainUuid: Boolean) =
82+
copy(decorator = decorator.deepCopy(retainUuid))
83+
.also { if (retainUuid) it.uuid = this.uuid }
8284

8385

8486
/**

src/main/kotlin/com/fwdekker/randomness/decimal/DecimalSchemeEditor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class DecimalSchemeEditor(scheme: DecimalScheme = DecimalScheme()) : StateEditor
4343
decimalCount.addChangeListener { showTrailingZeroesCheckBox.isEnabled = decimalCount.value > 0 }
4444
decimalCount.changeListeners.forEach { it.stateChanged(ChangeEvent(decimalCount)) }
4545

46-
loadState(scheme)
46+
loadState()
4747
}
4848

4949
/**
@@ -88,7 +88,7 @@ class DecimalSchemeEditor(scheme: DecimalScheme = DecimalScheme()) : StateEditor
8888
prefix = prefixInput.text,
8989
suffix = suffixInput.text,
9090
decorator = arrayDecoratorEditor.readState()
91-
)
91+
).also { it.uuid = originalState.uuid }
9292

9393

9494
override fun addChangeListener(listener: () -> Unit) =

0 commit comments

Comments
 (0)