Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API 7] SubCategories #68

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@
*/
String categoryId();

/**
* Action subCategoryId
*
* @return String subCategoryId
*/
String subCategoryId() default "";

/**
* Action hasHoldFunctionality
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@
* @return String imagePath
*/
String imagePath() default "";

SubCategory[] subCategories() default {};

@interface SubCategory {
String id();
String name();
String imagePath() default "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,11 @@
* @return String categoryId
*/
String categoryId();

/**
* Connector subConnectorId
*
* @return String subConnectorId
*/
String subCategoryId() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,11 @@
* @return String[] valueChoices
*/
String[] valueChoices() default {};

/**
* Event subCategoryId
*
* @return String subCategoryId
*/
String subCategoryId() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,7 +36,12 @@ public static Pair<JsonObject, TypeSpec.Builder> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public static Pair<JsonObject, TypeSpec.Builder> 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<JsonObject, TypeSpec.Builder> 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<? extends Element> actionElements = roundEnv.getElementsAnnotatedWith(Action.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ public static Pair<JsonObject, TypeSpec.Builder> 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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,10 +47,16 @@ public static Pair<JsonObject, TypeSpec.Builder> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<JsonObject, TypeSpec.Builder> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*
Expand All @@ -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()));
Expand All @@ -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()));

Expand Down Expand Up @@ -236,7 +266,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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "|";
Expand All @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand All @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading