diff --git a/Annotations/src/main/java/com/christophecvb/touchportal/annotations/ValueType.java b/Annotations/src/main/java/com/christophecvb/touchportal/annotations/ValueType.java new file mode 100644 index 0000000..297f26b --- /dev/null +++ b/Annotations/src/main/java/com/christophecvb/touchportal/annotations/ValueType.java @@ -0,0 +1,17 @@ +package com.christophecvb.touchportal.annotations; + +public enum ValueType { + TEXT("text"), + CHOICE("choice") + ; + + private final String key; + + ValueType(String key) { + this.key = key; + } + + public String getKey() { + return this.key; + } +} diff --git a/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/EventProcessor.java b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/EventProcessor.java index a2ec880..a8c102d 100644 --- a/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/EventProcessor.java +++ b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/EventProcessor.java @@ -1,9 +1,6 @@ package com.christophecvb.touchportal.annotations.processor; -import com.christophecvb.touchportal.annotations.Category; -import com.christophecvb.touchportal.annotations.Event; -import com.christophecvb.touchportal.annotations.Plugin; -import com.christophecvb.touchportal.annotations.State; +import com.christophecvb.touchportal.annotations.*; import com.christophecvb.touchportal.annotations.processor.utils.Pair; import com.christophecvb.touchportal.annotations.processor.utils.SpecUtils; import com.christophecvb.touchportal.helpers.EventHelper; @@ -38,12 +35,12 @@ public static Pair process(TouchPortalPluginAnnota State state = eventElement.getAnnotation(State.class); Event event = eventElement.getAnnotation(Event.class); - String reference = eventElement.getEnclosingElement().getSimpleName() + "." + eventElement.getSimpleName(); - if (state == null) { throw new TPAnnotationException.Builder(State.class).isMissing(true).forElement(eventElement).build(); } + String reference = eventElement.getEnclosingElement().getSimpleName() + "." + eventElement.getSimpleName(); + TypeSpec.Builder eventTypeSpecBuilder = SpecUtils.createEventTypeSpecBuilder(pluginElement, categoryElement, category, eventElement, event); JsonObject jsonEvent = new JsonObject(); @@ -56,19 +53,21 @@ public static Pair process(TouchPortalPluginAnnota jsonEvent.addProperty(EventHelper.TYPE, EventHelper.TYPE_COMMUNICATE); jsonEvent.addProperty(EventHelper.NAME, EventHelper.getEventName(eventElement, event)); jsonEvent.addProperty(EventHelper.FORMAT, event.format()); + jsonEvent.addProperty(EventHelper.VALUE_STATE_ID, StateHelper.getStateId(pluginElement, categoryElement, category, eventElement, state)); String desiredTPType = GenericHelper.getTouchPortalType(reference, eventElement); - if (desiredTPType.equals(StateHelper.TYPE_TEXT)) { - jsonEvent.addProperty(EventHelper.VALUE_TYPE, EventHelper.VALUE_TYPE_CHOICE); - JsonArray eventValueChoices = new JsonArray(); - for (String valueChoice : event.valueChoices()) { - eventValueChoices.add(valueChoice); + + if (desiredTPType.equals(EventHelper.VALUE_TYPE)) { + if (event.valueChoices().length > 0) { + jsonEvent.addProperty(EventHelper.VALUE_TYPE, StateHelper.TYPE_CHOICE); + JsonArray eventValueChoices = new JsonArray(); + for (String valueChoice : event.valueChoices()) { + eventValueChoices.add(valueChoice); + } + jsonEvent.add(EventHelper.VALUE_CHOICES, eventValueChoices); + } else { + jsonEvent.addProperty(EventHelper.VALUE_TYPE, StateHelper.TYPE_TEXT); } - jsonEvent.add(EventHelper.VALUE_CHOICES, eventValueChoices); - jsonEvent.addProperty(EventHelper.VALUE_STATE_ID, StateHelper.getStateId(pluginElement, categoryElement, category, eventElement, state)); - } - else { - throw new GenericHelper.TPTypeException.Builder(reference).typeUnsupported(desiredTPType).forAnnotation(GenericHelper.TPTypeException.ForAnnotation.EVENT).build(); } return Pair.create(jsonEvent, eventTypeSpecBuilder); diff --git a/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/utils/SpecUtils.java b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/utils/SpecUtils.java index 3f2b2b7..5e2b97b 100644 --- a/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/utils/SpecUtils.java +++ b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/utils/SpecUtils.java @@ -297,6 +297,7 @@ public static TypeSpec.Builder createEventTypeSpecBuilder(Element pluginElement, eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("name", EventHelper.getEventName(eventElement, event))); eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("format", event.format())); eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringArrayFieldSpec("value_choices", event.valueChoices())); + eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("value_type", event.valueType().getKey())); return eventTypeSpecBuilder; } diff --git a/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/TouchPortalSampleJavaPlugin.java b/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/TouchPortalSampleJavaPlugin.java index 11d6ed3..cd402a8 100644 --- a/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/TouchPortalSampleJavaPlugin.java +++ b/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/TouchPortalSampleJavaPlugin.java @@ -65,10 +65,15 @@ private enum Categories { * State and Event definition example */ @State(defaultValue = "1", categoryId = "BaseCategory") - @Event(valueChoices = {"1", "2"}, format = "When customStateWithEvent becomes $val") + @Event(valueChoices = {"1", "2"}, valueType = ValueType.CHOICE, format = "When customStateWithEvent becomes $val") private String customStateWithEvent; /** + * State and Event definition example + */ + @State(defaultValue = "1", categoryId = "BaseCategory") + @Event(valueType = ValueType.TEXT, format = "When customStateWithEvent becomes $val") + private String stateWithEventTypeText; * State and Event in Subcategory definition example */ @State(defaultValue = "1", categoryId = "CategoryWithSubs")