From bd3073a4bb5cbaaf644258551cc714d98a505506 Mon Sep 17 00:00:00 2001 From: Pjiesco <55349095+Pjiesco@users.noreply.github.com> Date: Mon, 18 Nov 2024 18:28:07 +0100 Subject: [PATCH] [API 7] SubCategories (#68) * Feat: SubCategory * cleanup * Feat: Event SubCategory * Feat: Connector SubCategory * Rename iconRelativePath to imagepath * SubCategory directly inside Parent Category --- .../touchportal/annotations/Action.java | 7 +++ .../touchportal/annotations/Category.java | 8 +++ .../touchportal/annotations/Connector.java | 7 +++ .../touchportal/annotations/Event.java | 7 +++ .../processor/ActionProcessor.java | 8 ++- .../processor/CategoryProcessor.java | 8 +++ .../processor/ConnectorProcessor.java | 7 ++- .../annotations/processor/EventProcessor.java | 9 +++- .../processor/SubCategoryProcessor.java | 31 ++++++++++++ .../processor/utils/SpecUtils.java | 41 ++++++++++++++-- .../touchportal/helpers/ActionHelper.java | 16 ++++++ .../touchportal/helpers/CategoryHelper.java | 1 + .../touchportal/helpers/ConnectorHelper.java | 16 ++++++ .../touchportal/helpers/EventHelper.java | 16 +++++- .../touchportal/helpers/GenericHelper.java | 1 + .../helpers/SubCategoryHelper.java | 49 +++++++++++++++++++ .../TouchPortalSampleJavaPlugin.java | 17 +++++-- .../connector/ExampleClassConnector.java | 5 +- 18 files changed, 242 insertions(+), 12 deletions(-) create mode 100644 AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/SubCategoryProcessor.java create mode 100644 Helpers/src/main/java/com/christophecvb/touchportal/helpers/SubCategoryHelper.java diff --git a/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Action.java b/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Action.java index 31c279b..7579120 100644 --- a/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Action.java +++ b/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Action.java @@ -120,6 +120,13 @@ */ String categoryId(); + /** + * Action subCategoryId + * + * @return String subCategoryId + */ + String subCategoryId() default ""; + /** * Action hasHoldFunctionality *

diff --git a/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Category.java b/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Category.java index e6b622b..fce2d0c 100644 --- a/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Category.java +++ b/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Category.java @@ -65,4 +65,12 @@ * @return String imagePath */ String imagePath() default ""; + + SubCategory[] subCategories() default {}; + + @interface SubCategory { + String id(); + String name(); + String imagePath() default ""; + } } diff --git a/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Connector.java b/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Connector.java index 5aafdd0..5979286 100644 --- a/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Connector.java +++ b/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Connector.java @@ -86,4 +86,11 @@ * @return String categoryId */ String categoryId(); + + /** + * Connector subConnectorId + * + * @return String subConnectorId + */ + String subCategoryId() default ""; } diff --git a/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Event.java b/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Event.java index 0105afe..8945f6f 100644 --- a/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Event.java +++ b/Annotations/src/main/java/com/christophecvb/touchportal/annotations/Event.java @@ -75,4 +75,11 @@ * @return String[] valueChoices */ String[] valueChoices() default {}; + + /** + * Event subCategoryId + * + * @return String subCategoryId + */ + String subCategoryId() default ""; } diff --git a/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/ActionProcessor.java b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/ActionProcessor.java index 5eb5970..2a3b522 100644 --- a/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/ActionProcessor.java +++ b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/ActionProcessor.java @@ -5,6 +5,7 @@ import com.christophecvb.touchportal.annotations.processor.utils.SpecUtils; import com.christophecvb.touchportal.helpers.ActionHelper; import com.christophecvb.touchportal.helpers.GenericHelper; +import com.christophecvb.touchportal.helpers.SubCategoryHelper; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.squareup.javapoet.TypeSpec; @@ -35,7 +36,12 @@ public static Pair process(TouchPortalPluginAnnota TypeSpec.Builder actionTypeSpecBuilder = SpecUtils.createActionTypeSpecBuilder(pluginElement, categoryElement, category, actionElement, action); JsonObject jsonAction = new JsonObject(); - jsonAction.addProperty(ActionHelper.ID, ActionHelper.getActionId(pluginElement, categoryElement, category, actionElement, action)); + if (!action.subCategoryId().isEmpty()) { + jsonAction.addProperty(ActionHelper.ID, ActionHelper.getActionId(pluginElement, categoryElement, category, action.subCategoryId(), actionElement, action)); + jsonAction.addProperty(ActionHelper.SUB_CATEGORY_ID, SubCategoryHelper.getSubCategoryId(pluginElement, categoryElement, category, action.subCategoryId())); + } else { + jsonAction.addProperty(ActionHelper.ID, ActionHelper.getActionId(pluginElement, categoryElement, category, actionElement, action)); + } jsonAction.addProperty(ActionHelper.NAME, ActionHelper.getActionName(actionElement, action)); jsonAction.addProperty(ActionHelper.PREFIX, action.prefix()); jsonAction.addProperty(ActionHelper.TYPE, action.type()); diff --git a/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/CategoryProcessor.java b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/CategoryProcessor.java index acd990f..a2d7804 100644 --- a/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/CategoryProcessor.java +++ b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/CategoryProcessor.java @@ -40,6 +40,14 @@ public static Pair process(TouchPortalPluginAnnota jsonCategory.addProperty(CategoryHelper.NAME, CategoryHelper.getCategoryName(categoryElement, category)); jsonCategory.addProperty(CategoryHelper.IMAGE_PATH, PluginHelper.TP_PLUGIN_FOLDER + pluginElement.getSimpleName() + "/" + category.imagePath()); + JsonArray jsonSubCategories = new JsonArray(); + for (Category.SubCategory subCategory : category.subCategories()) { + Pair subCategoriesResult = SubCategoryProcessor.process(processor, pluginElement, category, categoryElement, subCategory); + jsonSubCategories.add(subCategoriesResult.first); + categoryTypeSpecBuilder.addType(subCategoriesResult.second.build()); + } + jsonCategory.add(CategoryHelper.SUB_CATEGORIES, jsonSubCategories); + TypeSpec.Builder actionsTypeSpecBuilder = TypeSpec.classBuilder("Actions").addModifiers(Modifier.PUBLIC, Modifier.STATIC); JsonArray jsonActions = new JsonArray(); Set actionElements = roundEnv.getElementsAnnotatedWith(Action.class); diff --git a/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/ConnectorProcessor.java b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/ConnectorProcessor.java index 45388fb..4619f50 100644 --- a/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/ConnectorProcessor.java +++ b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/ConnectorProcessor.java @@ -37,7 +37,12 @@ public static Pair process(TouchPortalPluginAnnota TypeSpec.Builder connectorTypeSpecBuilder = SpecUtils.createConnectorTypeSpecBuilder(pluginElement, categoryElement, category, connectorElement, connector); JsonObject jsonConnector = new JsonObject(); - jsonConnector.addProperty(ConnectorHelper.ID, ConnectorHelper.getConnectorId(pluginElement, categoryElement, category, connectorElement, connector)); + if (!connector.subCategoryId().isEmpty()){ + jsonConnector.addProperty(ConnectorHelper.ID, ConnectorHelper.getConnectorId(pluginElement, categoryElement, category, connectorElement, connector)); + jsonConnector.addProperty(ConnectorHelper.SUB_CATEGORY_ID, ConnectorHelper.getConnectorId(pluginElement, categoryElement, category, connector.subCategoryId(), connectorElement, connector)); + } else { + jsonConnector.addProperty(ConnectorHelper.ID, ConnectorHelper.getConnectorId(pluginElement, categoryElement, category, connectorElement, connector)); + } jsonConnector.addProperty(ConnectorHelper.NAME, ConnectorHelper.getConnectorName(connectorElement, connector)); jsonConnector.addProperty(ConnectorHelper.FORMAT, connector.format()); 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 f5d7a44..a2ec880 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 @@ -9,6 +9,7 @@ import com.christophecvb.touchportal.helpers.EventHelper; import com.christophecvb.touchportal.helpers.GenericHelper; import com.christophecvb.touchportal.helpers.StateHelper; +import com.christophecvb.touchportal.helpers.SubCategoryHelper; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.squareup.javapoet.TypeSpec; @@ -46,10 +47,16 @@ public static Pair process(TouchPortalPluginAnnota TypeSpec.Builder eventTypeSpecBuilder = SpecUtils.createEventTypeSpecBuilder(pluginElement, categoryElement, category, eventElement, event); JsonObject jsonEvent = new JsonObject(); - jsonEvent.addProperty(EventHelper.ID, EventHelper.getEventId(pluginElement, categoryElement, category, eventElement, event)); + if (!event.subCategoryId().isEmpty()) { + jsonEvent.addProperty(EventHelper.ID, EventHelper.getEventId(pluginElement, categoryElement, category, event.subCategoryId(), eventElement, event)); + jsonEvent.addProperty(EventHelper.SUB_CATEGORY_ID, SubCategoryHelper.getSubCategoryId(pluginElement, categoryElement, category, event.subCategoryId())); + } else { + jsonEvent.addProperty(EventHelper.ID, EventHelper.getEventId(pluginElement, categoryElement, category, eventElement, event)); + } jsonEvent.addProperty(EventHelper.TYPE, EventHelper.TYPE_COMMUNICATE); jsonEvent.addProperty(EventHelper.NAME, EventHelper.getEventName(eventElement, event)); jsonEvent.addProperty(EventHelper.FORMAT, event.format()); + String desiredTPType = GenericHelper.getTouchPortalType(reference, eventElement); if (desiredTPType.equals(StateHelper.TYPE_TEXT)) { jsonEvent.addProperty(EventHelper.VALUE_TYPE, EventHelper.VALUE_TYPE_CHOICE); diff --git a/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/SubCategoryProcessor.java b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/SubCategoryProcessor.java new file mode 100644 index 0000000..9dd26b9 --- /dev/null +++ b/AnnotationsProcessor/src/main/java/com/christophecvb/touchportal/annotations/processor/SubCategoryProcessor.java @@ -0,0 +1,31 @@ +package com.christophecvb.touchportal.annotations.processor; + +import com.christophecvb.touchportal.annotations.Category; +import com.christophecvb.touchportal.annotations.processor.utils.Pair; +import com.christophecvb.touchportal.annotations.processor.utils.SpecUtils; +import com.christophecvb.touchportal.helpers.SubCategoryHelper; +import com.google.gson.JsonObject; +import com.squareup.javapoet.TypeSpec; + +import javax.lang.model.element.Element; +import javax.lang.model.element.Modifier; +import javax.tools.Diagnostic; + +public class SubCategoryProcessor { + + public static Pair process(TouchPortalPluginAnnotationsProcessor processor, Element pluginElement, Category category, Element categoryElement, Category.SubCategory subCategory) { + processor.getMessager().printMessage(Diagnostic.Kind.NOTE, "Process SubCategory: " + subCategory.id()); + + TypeSpec.Builder categoryTypeSpecBuilder = SpecUtils.createSubCategoryTypeSpecBuilder(pluginElement, categoryElement, category, subCategory).addModifiers(Modifier.PUBLIC, Modifier.STATIC); + + JsonObject jsonSubCategory = new JsonObject(); + + jsonSubCategory.addProperty(SubCategoryHelper.ID, SubCategoryHelper.getSubCategoryId(pluginElement, categoryElement, category, subCategory)); + jsonSubCategory.addProperty(SubCategoryHelper.NAME, subCategory.name()); + if (!subCategory.imagePath().isEmpty()) { + jsonSubCategory.addProperty(SubCategoryHelper.IMAGE_PATH, subCategory.imagePath()); + } + + return Pair.create(jsonSubCategory, categoryTypeSpecBuilder); + } +} 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 044022f..3f2b2b7 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 @@ -59,6 +59,26 @@ public static TypeSpec.Builder createCategoryTypeSpecBuilder(Element pluginEleme return categoryTypeSpecBuilder; } + + /** + * Generates a TypeSpec.Builder with Constants for the {@link Category.SubCategory} + * + * @param pluginElement Element + * @param categoryElement Element + * @param category {@link Category} + * @param subCategory {@link Category.SubCategory} + * @return TypeSpec.Builder subCategoryTypeSpecBuilder + */ + public static TypeSpec.Builder createSubCategoryTypeSpecBuilder(Element pluginElement, Element categoryElement, Category category, Category.SubCategory subCategory) { + TypeSpec.Builder subCategoryTypeSpecBuilder = TypeSpec.classBuilder(SpecUtils.capitalize(subCategory.id())).addModifiers(Modifier.PUBLIC, Modifier.STATIC); + + subCategoryTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("id", SubCategoryHelper.getSubCategoryId(pluginElement, categoryElement, category, subCategory))); + subCategoryTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("name", subCategory.name())); + subCategoryTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("imagepath", subCategory.imagePath())); + + return subCategoryTypeSpecBuilder; + } + /** * Generates a TypeSpec.Builder with Constants for the {@link Action} * @@ -75,7 +95,12 @@ public static TypeSpec.Builder createActionTypeSpecBuilder(Element pluginElement TypeSpec.Builder actionTypeSpecBuilder = TypeSpec.classBuilder(SpecUtils.capitalize(simpleClassName)).addModifiers(Modifier.PUBLIC, Modifier.STATIC); actionTypeSpecBuilder.addModifiers(Modifier.PUBLIC); - actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("id", ActionHelper.getActionId(pluginElement, categoryElement, category, actionElement, action))); + if (!action.subCategoryId().isEmpty()) { + actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("id", ActionHelper.getActionId(pluginElement, categoryElement, category, action.subCategoryId(), actionElement, action))); + actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("sub_category_id", SubCategoryHelper.getSubCategoryId(pluginElement, categoryElement, category, action.subCategoryId()))); + } else { + actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("id", ActionHelper.getActionId(pluginElement, categoryElement, category, actionElement, action))); + } actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("name", ActionHelper.getActionName(actionElement, action))); actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("prefix", action.prefix())); actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("description", action.description())); @@ -102,7 +127,12 @@ public static TypeSpec.Builder createConnectorTypeSpecBuilder(Element pluginElem TypeSpec.Builder actionTypeSpecBuilder = TypeSpec.classBuilder(SpecUtils.capitalize(simpleClassName)).addModifiers(Modifier.PUBLIC, Modifier.STATIC); actionTypeSpecBuilder.addModifiers(Modifier.PUBLIC); - actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("id", ConnectorHelper.getConnectorId(pluginElement, categoryElement, category, connectorElement, connector))); + if (!connector.subCategoryId().isEmpty()) { + actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("id", ConnectorHelper.getConnectorId(pluginElement, categoryElement, category, connector.subCategoryId(), connectorElement, connector))); + actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("sub_category_id", SubCategoryHelper.getSubCategoryId(pluginElement, categoryElement, category, connector.subCategoryId()))); + } else { + actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("id", ConnectorHelper.getConnectorId(pluginElement, categoryElement, category, connectorElement, connector))); + } actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("name", ConnectorHelper.getConnectorName(connectorElement, connector))); actionTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("format", connector.format())); @@ -258,7 +288,12 @@ public static TypeSpec.Builder createEventTypeSpecBuilder(Element pluginElement, String simpleClassName = event.id().isEmpty() ? eventElement.getSimpleName().toString() : event.id(); TypeSpec.Builder eventTypeSpecBuilder = TypeSpec.classBuilder(SpecUtils.capitalize(simpleClassName)).addModifiers(Modifier.PUBLIC, Modifier.STATIC); - eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("id", EventHelper.getEventId(pluginElement, categoryElement, category, eventElement, event))); + if (!event.subCategoryId().isEmpty()) { + eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("id", EventHelper.getEventId(pluginElement, categoryElement, category, event.subCategoryId(), eventElement, event))); + eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("sub_category_id", SubCategoryHelper.getSubCategoryId(pluginElement, categoryElement, category, event.subCategoryId()))); + } else { + eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("id", EventHelper.getEventId(pluginElement, categoryElement, category, eventElement, event))); + } eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("name", EventHelper.getEventName(eventElement, event))); eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("format", event.format())); eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringArrayFieldSpec("value_choices", event.valueChoices())); diff --git a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/ActionHelper.java b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/ActionHelper.java index 4ded488..6e8b253 100644 --- a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/ActionHelper.java +++ b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/ActionHelper.java @@ -44,6 +44,7 @@ public class ActionHelper { public static final String TRY_INLINE = "tryInline"; public static final String FORMAT = "format"; public static final String HAS_HOLD_FUNCTIONALITY = "hasHoldFunctionality"; + public static final String SUB_CATEGORY_ID = GenericHelper.SUB_CATEGORY_ID; protected static final String KEY_ACTION = "action"; @@ -61,6 +62,21 @@ public static String getActionId(Element pluginElement, Element categoryElement, return ActionHelper._getActionId(CategoryHelper.getCategoryId(pluginElement, categoryElement, category), action.id().isEmpty() ? actionElement.getSimpleName().toString() : action.id()); } + /** + * Get the generated Action ID + * + * @param pluginElement Element + * @param categoryElement Element + * @param category {@link Category} + * @param subCategoryId String + * @param actionElement Element + * @param action {@link Action} + * @return String actionId + */ + public static String getActionId(Element pluginElement, Element categoryElement, Category category, String subCategoryId, Element actionElement, Action action) { + return ActionHelper._getActionId(SubCategoryHelper.getSubCategoryId(pluginElement, categoryElement, category, subCategoryId), action.id().isEmpty() ? actionElement.getSimpleName().toString() : action.id()); + } + /** * Get the generated Action Name * diff --git a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/CategoryHelper.java b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/CategoryHelper.java index c4eba79..c40d64c 100644 --- a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/CategoryHelper.java +++ b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/CategoryHelper.java @@ -36,6 +36,7 @@ public class CategoryHelper { public static final String EVENTS = "events"; public static final String STATES = "states"; public static final String CONNECTORS = "connectors"; + public static final String SUB_CATEGORIES = "subCategories"; /** * Get the generated Category ID diff --git a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/ConnectorHelper.java b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/ConnectorHelper.java index 993075f..82261ff 100644 --- a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/ConnectorHelper.java +++ b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/ConnectorHelper.java @@ -37,6 +37,7 @@ public class ConnectorHelper { public static final String TYPE = GenericHelper.TYPE; public static final String DATA = "data"; public static final String FORMAT = "format"; + public static final String SUB_CATEGORY_ID = GenericHelper.SUB_CATEGORY_ID; public static final String UPDATE_PREFIX = "pc"; public static final String UPDATE_ID_SEPARATOR = "_"; public static final String UPDATE_DATA_SEPARATOR = "|"; @@ -57,6 +58,21 @@ public static String getConnectorId(Element pluginElement, Element categoryEleme return ConnectorHelper._getConnectorId(CategoryHelper.getCategoryId(pluginElement, categoryElement, category), connector.id().isEmpty() ? connectorElement.getSimpleName().toString() : connector.id()); } + /** + * Get the generated Connector ID + * + * @param pluginElement Element + * @param categoryElement Element + * @param category {@link Category} + * @param subCategoryId String + * @param connectorElement Element + * @param connector {@link Connector} + * @return String connectorId + */ + public static String getConnectorId(Element pluginElement, Element categoryElement, Category category, String subCategoryId, Element connectorElement, Connector connector) { + return ConnectorHelper._getConnectorId(SubCategoryHelper.getSubCategoryId(pluginElement, categoryElement, category, subCategoryId), connector.id().isEmpty() ? connectorElement.getSimpleName().toString() : connector.id()); + } + /** * Get the generated Connector Name * diff --git a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/EventHelper.java b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/EventHelper.java index 0e9f695..d43087e 100644 --- a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/EventHelper.java +++ b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/EventHelper.java @@ -38,7 +38,7 @@ public class EventHelper { public static final String VALUE_TYPE_CHOICE = GenericHelper.TP_TYPE_CHOICE; public static final String VALUE_CHOICES = GenericHelper.VALUE_CHOICES; public static final String VALUE_STATE_ID = "valueStateId"; - + public static final String SUB_CATEGORY_ID = GenericHelper.SUB_CATEGORY_ID; private static final String KEY_EVENT = "event"; /** @@ -55,6 +55,20 @@ public static String getEventId(Element pluginElement, Element categoryElement, return EventHelper._getEventId(CategoryHelper.getCategoryId(pluginElement, categoryElement, category), event.id().isEmpty() ? eventElement.getSimpleName().toString() : event.id()); } + /** + * Get the generated Event ID + * + * @param pluginElement Element + * @param categoryElement Element + * @param category {@link Category} + * @param eventElement Element + * @param event {@link Event} + * @return String eventId + */ + public static String getEventId(Element pluginElement, Element categoryElement, Category category, String subCategoryId, Element eventElement, Event event) { + return EventHelper._getEventId(SubCategoryHelper.getSubCategoryId(pluginElement, categoryElement, category, subCategoryId), event.id().isEmpty() ? eventElement.getSimpleName().toString() : event.id()); + } + /** * Get the generated Event Name * diff --git a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/GenericHelper.java b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/GenericHelper.java index ada4f28..b9d275d 100644 --- a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/GenericHelper.java +++ b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/GenericHelper.java @@ -41,6 +41,7 @@ public class GenericHelper { protected static final String VALUE = "value"; protected static final String DEFAULT = "default"; protected static final String VALUE_CHOICES = "valueChoices"; + protected static final String SUB_CATEGORY_ID = "subCategoryId"; /** * Retrieve the Touch Portal type according to the Java's element type diff --git a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/SubCategoryHelper.java b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/SubCategoryHelper.java new file mode 100644 index 0000000..66580bc --- /dev/null +++ b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/SubCategoryHelper.java @@ -0,0 +1,49 @@ +package com.christophecvb.touchportal.helpers; + +import com.christophecvb.touchportal.annotations.Category; + +import javax.lang.model.element.Element; + +public class SubCategoryHelper { + public static final String ID = GenericHelper.ID; + public static final String NAME = GenericHelper.NAME; + public static final String IMAGE_PATH = "imagepath"; + + + /** + * Get the generated SubCategory ID + * + * @param pluginElement Element + * @param categoryElement Element + * @param category {@link Category} + * @param subCategory {@link Category.SubCategory} + * @return String subCategoryId + */ + public static String getSubCategoryId(Element pluginElement, Element categoryElement, Category category, Category.SubCategory subCategory) { + return SubCategoryHelper._getSubCategoryId(CategoryHelper.getCategoryId(pluginElement, categoryElement, category), subCategory.id()); + } + + /** + * Get the generated SubCategory ID + * + * @param pluginElement Element + * @param categoryElement Element + * @param category {@link Category} + * @param subCategoryId String + * @return String subCategoryId + */ + public static String getSubCategoryId(Element pluginElement, Element categoryElement, Category category, String subCategoryId) { + return SubCategoryHelper._getSubCategoryId(CategoryHelper.getCategoryId(pluginElement, categoryElement, category), subCategoryId); + } + + /** + * Internal - Get the formatted SubCategory ID + * + * @param categoryId String + * @param subCategoryId String + * @return String subCategoryId + */ + private static String _getSubCategoryId(String categoryId, String subCategoryId) { + return categoryId + "." + subCategoryId; + } +} 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 0f96873..11d6ed3 100644 --- a/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/TouchPortalSampleJavaPlugin.java +++ b/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/TouchPortalSampleJavaPlugin.java @@ -54,7 +54,11 @@ private enum Categories { @Category(name = "Touch Portal Plugin Example Base Category", imagePath = "images/icon-24.png") BaseCategory, @Category(name = "Touch Portal Plugin Example Second Category", imagePath = "images/icon-24.png") - SecondCategory + SecondCategory, + @Category(name = "Touch Portal Plugin Example Category With Subcategories", imagePath = "images/icon-24.png", subCategories = { + @Category.SubCategory(id = "SubCat1", name = "SubCategory 1", imagePath = "%TP_PLUGIN_FOLDER%/images/icon-24.png") + }) + CategoryWithSubs } /** @@ -64,6 +68,13 @@ private enum Categories { @Event(valueChoices = {"1", "2"}, format = "When customStateWithEvent becomes $val") private String customStateWithEvent; + /** + * State and Event in Subcategory definition example + */ + @State(defaultValue = "1", categoryId = "CategoryWithSubs") + @Event(valueChoices = {"1", "2"}, format = "When customStateWithEventInSubCat becomes $val", subCategoryId = "Cat1") + private String customStateWithEventInSubCat; + /** * State of type choice definition example */ @@ -113,7 +124,7 @@ public static void main(String... args) { // Register Invokable touchPortalSampleJavaPlugin.registerInvokable(TouchPortalSampleJavaPluginConstants.BaseCategory.Actions.ExampleClassAction.ID, ExampleClassAction.class); - touchPortalSampleJavaPlugin.registerInvokable(TouchPortalSampleJavaPluginConstants.BaseCategory.Connectors.ExampleClassConnector.ID, ExampleClassConnector.class); + touchPortalSampleJavaPlugin.registerInvokable(TouchPortalSampleJavaPluginConstants.CategoryWithSubs.SubCat1.ID, ExampleClassConnector.class); // Load a properties File touchPortalSampleJavaPlugin.loadProperties("plugin.config"); @@ -282,7 +293,7 @@ private void actionHoldable() { } } - @Action(format = "Do Action with Choice {$choices$}", categoryId = "SecondCategory") + @Action(format = "Do Action with Choice {$choices$}", categoryId = "CategoryWithSubs", subCategoryId = "SubCat1") private void actionWithDataStateId(@Data(stateId = "customStateChoice") String[] choices) { LOGGER.log(Level.INFO, "Action with Data State Id received: " + choices[0]); } diff --git a/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/invokable/connector/ExampleClassConnector.java b/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/invokable/connector/ExampleClassConnector.java index 8ffc387..4162f4f 100644 --- a/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/invokable/connector/ExampleClassConnector.java +++ b/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/invokable/connector/ExampleClassConnector.java @@ -12,8 +12,9 @@ @Connector( name = "Example Class Connector", - categoryId = "BaseCategory", - format = "Connect Example Class Connector with param {$param$}" + categoryId = "CategoryWithSubs", + format = "Connect Example Class Connector with param {$param$}", + subCategoryId = "Cat1" ) public class ExampleClassConnector extends TPConnector { private final static Logger LOGGER = Logger.getLogger(TouchPortalPlugin.class.getName());