Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
* development:
  Update readme
  Add color picker
  Fix group margins
  Add stepper param design
  Add stepper param without final designs
  Read only param description added to readme
  Add read only param
  • Loading branch information
Marcin Kornacki committed Aug 28, 2018
2 parents e8d15f8 + 90b8fec commit 1503326
Show file tree
Hide file tree
Showing 42 changed files with 692 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ internal class MapperConsts {
const val KEY_LIST_VALUES = "values"
const val KEY_LIST_SELECTION_INDEX = "selectedItemIndex"

const val KEY_RANGE_MIN = "min"
const val KEY_RANGE_MAX = "max"
const val KEY_RANGE_STEP = "step"
const val KEY_RANGE_VALUE = "value"
const val KEY_MIN = "min"
const val KEY_MAX = "max"
const val KEY_STEP = "step"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package com.polidea.cockpit.core.mapper

import com.polidea.cockpit.core.CockpitParam
import com.polidea.cockpit.core.isSimpleParam
import com.polidea.cockpit.core.type.CockpitAction
import com.polidea.cockpit.core.type.CockpitColor
import com.polidea.cockpit.core.type.CockpitListType
import com.polidea.cockpit.core.type.CockpitRange
import com.polidea.cockpit.core.type.*

internal class ParamToYamlMapper {

Expand Down Expand Up @@ -40,10 +37,19 @@ internal class ParamToYamlMapper {
}
is CockpitRange<*> -> {
map[MapperConsts.KEY_TYPE] = YamlParamType.RANGE.value
map[MapperConsts.KEY_RANGE_MIN] = value.min
map[MapperConsts.KEY_RANGE_MAX] = value.max
map[MapperConsts.KEY_RANGE_STEP] = value.step
map[MapperConsts.KEY_RANGE_VALUE] = value.value
map[MapperConsts.KEY_MIN] = value.min
map[MapperConsts.KEY_MAX] = value.max
map[MapperConsts.KEY_STEP] = value.step
map[MapperConsts.KEY_VALUE] = value.value
}
is CockpitStep<*> -> {
value.min?.let { map[MapperConsts.KEY_MIN] = it }
value.max?.let { map[MapperConsts.KEY_MAX] = it }
map[MapperConsts.KEY_STEP] = value.step
map[MapperConsts.KEY_VALUE] = value.value
}
is CockpitReadOnly -> {
map[MapperConsts.KEY_TYPE] = YamlParamType.READ_ONLY.value
}

else -> value.let { map[MapperConsts.KEY_VALUE] = it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ internal enum class YamlParamType(val value: String) {
LIST("list"),
COLOR("color"),
RANGE("range"),
READ_ONLY("read_only"),
DEFAULT("");

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package com.polidea.cockpit.core.mapper

import com.polidea.cockpit.core.CockpitParam
import com.polidea.cockpit.core.exception.CockpitParseException
import com.polidea.cockpit.core.type.CockpitAction
import com.polidea.cockpit.core.type.CockpitColor
import com.polidea.cockpit.core.type.CockpitListType
import com.polidea.cockpit.core.type.CockpitRange
import com.polidea.cockpit.core.type.*

internal class YamlToParamMapper {

Expand Down Expand Up @@ -35,7 +32,8 @@ internal class YamlToParamMapper {
YamlParamType.LIST -> createCockpitListType(paramName, valueMap)
YamlParamType.COLOR -> createCockpitColor(paramName, valueMap)
YamlParamType.RANGE -> createCockpitRange(paramName, valueMap)
YamlParamType.DEFAULT -> valueMap[MapperConsts.KEY_VALUE] as Any
YamlParamType.READ_ONLY -> CockpitReadOnly()
YamlParamType.DEFAULT -> createCockpitStepOrSimpleType(paramName, valueMap)
}
val description = valueMap[MapperConsts.KEY_DESCRIPTION] as String?
val group = valueMap[MapperConsts.KEY_GROUP] as String?
Expand Down Expand Up @@ -69,12 +67,12 @@ internal class YamlToParamMapper {
}

private fun createCockpitRange(paramName: String, valueMap: Map<*, *>): CockpitRange<*> {
val min = valueMap[MapperConsts.KEY_RANGE_MIN] as? Number
val min = valueMap[MapperConsts.KEY_MIN] as? Number
?: throw CockpitParseException("$paramName parameter must contain min and max fields")
val max = valueMap[MapperConsts.KEY_RANGE_MAX] as? Number
val max = valueMap[MapperConsts.KEY_MAX] as? Number
?: throw throw CockpitParseException("$paramName parameter must contain min and max fields")
val step = valueMap[MapperConsts.KEY_RANGE_STEP] as? Number ?: 1
val selectedValue = valueMap[MapperConsts.KEY_RANGE_VALUE] as? Number ?: min
val step = valueMap[MapperConsts.KEY_STEP] as? Number ?: 1
val selectedValue = valueMap[MapperConsts.KEY_VALUE] as? Number ?: min

try {
if (min is Int && max is Int && step is Int && selectedValue is Int)
Expand All @@ -85,4 +83,32 @@ internal class YamlToParamMapper {
throw CockpitParseException("$paramName`s ${e.message}")
}
}

private fun createCockpitStepOrSimpleType(paramName: String, valueMap: Map<*, *>): Any {

val value = valueMap[MapperConsts.KEY_VALUE] as Any

val step = valueMap[MapperConsts.KEY_STEP] as? Number
val min = valueMap[MapperConsts.KEY_MIN] as? Number
val max = valueMap[MapperConsts.KEY_MAX] as? Number

if (step == null && (min != null || max != null))
throw CockpitParseException("$paramName`s step is not defined")

if (step != null && value is Number)
return createCockpitStep(paramName, value, step, min, max)

return value
}

private fun createCockpitStep(paramName: String, value: Number, step: Number, min: Number?, max: Number?): CockpitStep<*> {
try {
if (value is Int && step is Int && min is Int? && max is Int?)
return CockpitStep(min, max, step, value)

return CockpitStep(min?.toDouble(), max?.toDouble(), step.toDouble(), value.toDouble())
} catch (e: IllegalArgumentException) {
throw CockpitParseException("$paramName`s ${e.message}")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.polidea.cockpit.core.type

data class CockpitReadOnly(val text: String = "")
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.polidea.cockpit.core.type

data class CockpitStep<T : Number>(val min: T?, val max: T?, val step: T, val value: T) {

init {
if (min != null && max != null && min.toDouble() > max.toDouble())
throw IllegalArgumentException("Max value must be greater than min value")
if (min != null && value.toDouble() < min.toDouble())
throw IllegalArgumentException("Selected value must be greater than min value")
if (max != null && value.toDouble() > max.toDouble())
throw IllegalArgumentException("Selected value must be less than max value")
if (step.toDouble() <= 0)
throw IllegalArgumentException("Step value must be positive")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ internal abstract class BaseCockpitGenerator {
private val selectionChangeListener = "SelectionChangeListener"
private val cockpitColor = "CockpitColor"
private val cockpitRange = "CockpitRange"
private val cockpitStep = "CockpitStep"
private val cockpitReadOnlyMapper = "CockpitReadOnlyMapper"
private val cockpitColorMapper = "CockpitColorMapper"
private val cockpitRangeMapper = "CockpitRangeMapper"
private val cockpitStepMapper = "CockpitStepMapper"
private val mappingPropertyChangeListener = "MappingPropertyChangeListener"

protected val map = "Map"
Expand All @@ -45,8 +48,11 @@ internal abstract class BaseCockpitGenerator {
protected val selectionChangeListenerClassName = ClassName.get(cockpitEventPackage, selectionChangeListener)
protected val cockpitColorClassName = ClassName.get(cockpitCoreTypePackage, cockpitColor)
protected val cockpitRangeClassName = ClassName.get(cockpitCoreTypePackage, cockpitRange)
protected val cockpitStepClassName = ClassName.get(cockpitCoreTypePackage, cockpitStep)
protected val cockpitReadOnlyMapperClassName = ClassName.get(cockpitMapperPackage, cockpitReadOnlyMapper)
protected val cockpitColorMapperClassName = ClassName.get(cockpitMapperPackage, cockpitColorMapper)
protected val cockpitRangeMapperClassName = ClassName.get(cockpitMapperPackage, cockpitRangeMapper)
protected val cockpitStepMapperClassName = ClassName.get(cockpitMapperPackage, cockpitStepMapper)
protected val mappingPropertyChangeListenerClassName = ClassName.get(cockpitMapperPackage, mappingPropertyChangeListener)

protected val mapClassName = ClassName.get(javaUtilPackage, map)
Expand All @@ -67,6 +73,12 @@ internal abstract class BaseCockpitGenerator {
protected fun getParametrizedCockpitRangeMapperClassName(clazz: Class<*>) =
ParameterizedTypeName.get(cockpitRangeMapperClassName, TypeName.get(clazz))

protected fun getParametrizedCockpitStepClassName(clazz: Class<*>) =
ParameterizedTypeName.get(cockpitStepClassName, TypeName.get(clazz))

protected fun getParametrizedCockpitStepMapperClassName(clazz: Class<*>) =
ParameterizedTypeName.get(cockpitStepMapperClassName, TypeName.get(clazz))

protected fun generate(file: File?, configurator: (TypeSpec.Builder) -> TypeSpec.Builder) {

val cockpitClass = configurator(TypeSpec.classBuilder(cockpit)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.polidea.cockpitplugin.generator

import com.polidea.cockpit.core.CockpitParam
import com.polidea.cockpit.core.type.CockpitAction
import com.polidea.cockpit.core.type.CockpitColor
import com.polidea.cockpit.core.type.CockpitListType
import com.polidea.cockpit.core.type.CockpitRange
import com.polidea.cockpit.core.type.*
import com.squareup.javapoet.*
import java.io.File
import javax.lang.model.element.Modifier
Expand Down Expand Up @@ -39,6 +36,14 @@ internal class DebugCockpitGenerator : BaseCockpitGenerator() {
add(createAddPropertyChangeListenerMethodSpecForRangeParam(paramName, paramValue.value))
add(createRemovePropertyChangeListenerMethodSpecForRangeParam(paramName, paramValue.value))
}
is CockpitStep<*> -> {
add(createGetterMethodSpecForStepParam(paramName, paramValue.value))
add(createSetterMethodSpecForStepParam(paramName, paramValue.value))
add(createAddPropertyChangeListenerMethodSpecForStepParam(paramName, paramValue.value))
add(createRemovePropertyChangeListenerMethodSpecForStepParam(paramName, paramValue.value))
}
is CockpitReadOnly ->
add(createSetterMethodSpecForReadOnlyParam(paramName, paramValue))
else -> {
add(createGetterMethodSpecForParam(paramName, paramValue))
add(createSetterMethodSpecForParam(paramName, paramValue))
Expand Down Expand Up @@ -69,6 +74,17 @@ internal class DebugCockpitGenerator : BaseCockpitGenerator() {
add(createRangeListenerMapFieldSpec(it))
add(createRangeMapperFieldSpec(it))
}

val cockpitStepTypes = params.mapNotNull { it.value as? CockpitStep<*> }.map { mapToJavaObjectTypeClass(it.value) }.distinct()
cockpitStepTypes.forEach {
add(createStepListenerMapFieldSpec(it))
add(createStepMapperFieldSpec(it))
}

val readOnlyMapperNeeded = params.find { it.value is CockpitReadOnly } != null
if (readOnlyMapperNeeded) {
add(createReadOnlyMapperFieldSpec())
}
}
}

Expand All @@ -82,25 +98,41 @@ internal class DebugCockpitGenerator : BaseCockpitGenerator() {
getParametrizedCockpitPropertyChangeListenerClassName(getParametrizedCockpitRangeClassName(paramClass)),
getParametrizedCockpitPropertyChangeListenerClassName(paramClass))

internal fun createStepListenerMapFieldSpec(paramClass: Class<*>) =
createListenerMapFieldSpec(getStepListenerMapName(paramClass),
getParametrizedCockpitPropertyChangeListenerClassName(getParametrizedCockpitStepClassName(paramClass)),
getParametrizedCockpitPropertyChangeListenerClassName(paramClass))

private fun createListenerMapFieldSpec(mapName: String, wrappablePropertyChangeListener: TypeName, paramPropertyChangeListener: TypeName): FieldSpec {
return FieldSpec.builder(ParameterizedTypeName.get(mapClassName, paramPropertyChangeListener, wrappablePropertyChangeListener),
mapName, Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
.initializer("new \$T<>()", hashMapClassName)
.build()
}

internal fun createReadOnlyMapperFieldSpec(): FieldSpec =
createMapperFieldSpec(cockpitReadOnlyMapperClassName, COCKPIT_READ_ONLY_MAPPER)

internal fun createColorMapperFieldSpec(): FieldSpec =
createMapperFieldSpec(cockpitColorMapperClassName, COCKPIT_COLOR_MAPPER)

internal fun createRangeMapperFieldSpec(paramClass: Class<*>): FieldSpec =
createMapperFieldSpec(getParametrizedCockpitRangeMapperClassName(paramClass), getCockpitRangeMapperName(paramClass))

internal fun createStepMapperFieldSpec(paramClass: Class<*>): FieldSpec =
createMapperFieldSpec(getParametrizedCockpitStepMapperClassName(paramClass), getCockpitStepMapperName(paramClass))

internal fun createMapperFieldSpec(mapperClassName: TypeName, mapperFieldName: String): FieldSpec =
FieldSpec.builder(mapperClassName, mapperFieldName,
Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
.initializer("new \$T()", mapperClassName)
.build()

internal fun createGetterMethodSpecForStepParam(paramName: String, stepValue: Number): MethodSpec {
val stepValueClass = mapToJavaObjectTypeClass(stepValue)
return createGetterMethodSpecForWrappableParam(paramName, stepValue, getParametrizedCockpitStepClassName(stepValueClass), getCockpitStepMapperName(stepValueClass))
}

internal fun createGetterMethodSpecForRangeParam(paramName: String, rangeValue: Number): MethodSpec {
val rangeValueClass = mapToJavaObjectTypeClass(rangeValue)
return createGetterMethodSpecForWrappableParam(paramName, rangeValue, getParametrizedCockpitRangeClassName(rangeValueClass), getCockpitRangeMapperName(rangeValueClass))
Expand Down Expand Up @@ -128,6 +160,12 @@ internal class DebugCockpitGenerator : BaseCockpitGenerator() {
internal fun createSetterMethodSpecForRangeParam(paramName: String, rangeValue: Number) =
createSetterMethodSpecForWrappableComplexParam(paramName, getParametrizedCockpitRangeClassName(mapToJavaObjectTypeClass(rangeValue)), rangeValue, getCockpitRangeMapperName(mapToJavaObjectTypeClass(rangeValue)))

internal fun createSetterMethodSpecForStepParam(paramName: String, stepValue: Number) =
createSetterMethodSpecForWrappableComplexParam(paramName, getParametrizedCockpitStepClassName(mapToJavaObjectTypeClass(stepValue)), stepValue, getCockpitStepMapperName(mapToJavaObjectTypeClass(stepValue)))

internal fun createSetterMethodSpecForReadOnlyParam(paramName: String, readOnlyValue: CockpitReadOnly) =
createSetterMethodSpecForWrappableParam(paramName, mapToTypeClass(readOnlyValue), readOnlyValue.text, COCKPIT_READ_ONLY_MAPPER)

internal fun createSetterMethodSpecForWrappableParam(paramName: String, wrappableClass: Class<*>, param: Any, mapperName: String): MethodSpec =
createSetterMethodSpecForParam(paramName, param, "value", listOf<MethodSpec.Builder.() -> MethodSpec.Builder>({ addStatement("\$T value = $mapperName.wrap($paramName)", wrappableClass) }))

Expand Down Expand Up @@ -156,11 +194,21 @@ internal class DebugCockpitGenerator : BaseCockpitGenerator() {
return createAddPropertyChangeListenerMethodSpecForWrappableParam(paramName, rangeValueClass, getCockpitRangeMapperName(rangeValueClass), getRangeListenerMapName(rangeValueClass), getParametrizedCockpitRangeClassName(rangeValueClass))
}

internal fun createAddPropertyChangeListenerMethodSpecForStepParam(paramName: String, stepValue: Number): MethodSpec {
val stepValueClass = mapToJavaObjectTypeClass(stepValue)
return createAddPropertyChangeListenerMethodSpecForWrappableParam(paramName, stepValueClass, getCockpitStepMapperName(stepValueClass), getStepListenerMapName(stepValueClass), getParametrizedCockpitStepClassName(stepValueClass))
}

internal fun createRemovePropertyChangeListenerMethodSpecForRangeParam(paramName: String, rangeValue: Number): MethodSpec {
val rangeValueClass = mapToJavaObjectTypeClass(rangeValue)
return createRemovePropertyChangeListenerMethodSpecForWrappableParam(paramName, rangeValueClass, getRangeListenerMapName(rangeValueClass), getParametrizedCockpitRangeClassName(rangeValueClass))
}

internal fun createRemovePropertyChangeListenerMethodSpecForStepParam(paramName: String, stepValue: Number): MethodSpec {
val stepValueClass = mapToJavaObjectTypeClass(stepValue)
return createRemovePropertyChangeListenerMethodSpecForWrappableParam(paramName, stepValueClass, getStepListenerMapName(stepValueClass), getParametrizedCockpitStepClassName(stepValueClass))
}

internal fun createAddPropertyChangeListenerMethodSpecForColorParam(paramName: String, color: CockpitColor) =
createAddPropertyChangeListenerMethodSpecForWrappableParam(paramName, mapToTypeClass(color.value), COCKPIT_COLOR_MAPPER, COLOR_LISTENER_MAP, cockpitColorClassName)

Expand Down Expand Up @@ -268,10 +316,13 @@ internal class DebugCockpitGenerator : BaseCockpitGenerator() {
private const val REMOVE = "remove"

private const val COCKPIT_COLOR_MAPPER = "cockpitColorMapper"
private const val COCKPIT_READ_ONLY_MAPPER = "cockpitReadOnlyMapper"
private fun getCockpitRangeMapperName(paramClass: Class<*>) = "cockpitRange${paramClass.simpleName}Mapper"
private fun getCockpitStepMapperName(paramClass: Class<*>) = "cockpitStep${paramClass.simpleName}Mapper"

private const val COLOR_LISTENER_MAP = "colorListenerMap"
private fun getRangeListenerMapName(paramClass: Class<*>) = "range${paramClass.simpleName}ListenerMap"
private fun getStepListenerMapName(paramClass: Class<*>) = "step${paramClass.simpleName}ListenerMap"

private const val LISTENER_ARGUMENT_NAME = "listener"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.polidea.cockpitplugin.generator

import com.polidea.cockpit.core.CockpitParam
import com.polidea.cockpit.core.type.CockpitAction
import com.polidea.cockpit.core.type.CockpitColor
import com.polidea.cockpit.core.type.CockpitListType
import com.polidea.cockpit.core.type.CockpitRange
import com.polidea.cockpit.core.type.*
import com.squareup.javapoet.MethodSpec
import java.io.File
import javax.lang.model.element.Modifier
Expand All @@ -20,6 +17,8 @@ internal class ReleaseCockpitGenerator : BaseCockpitGenerator() {
is CockpitAction -> Unit // we don't need getter for action
is CockpitListType<*> -> add(createSelectedValueGetterMethodSpecForParam(param as CockpitParam<CockpitListType<*>>))
is CockpitRange<*> -> add(createGetterMethodSpecForParam(paramName, paramValue.value))
is CockpitStep<*> -> add(createGetterMethodSpecForParam(paramName, paramValue.value))
is CockpitReadOnly -> Unit
else -> add(createGetterMethodSpecForParam(paramName, paramValue))
}
}
Expand Down
Loading

0 comments on commit 1503326

Please sign in to comment.