Skip to content
Closed
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 @@ -21,21 +21,10 @@
/**
* Interface for a label
*
* @since com.adobe.cq.forms.core.components.models.form 0.0.1
* @since com.adobe.cq.forms.core.components.models.form 4.8.0
*/
@ConsumerType
public interface Label {

/**
* Returns {@code true} if label is rich text, otherwise {@code false}.
*
* @return {@code true} if label is rich text, otherwise {@code false}
* @since com.adobe.cq.forms.core.components.models.form 0.0.1
*/
@Nullable
default Boolean isRichText() {
return null;
}
public interface Label extends TextContent {

/**
* Returns {@code true} if label should be visible, otherwise {@code false}.
Expand All @@ -48,15 +37,4 @@ default Boolean isVisible() {
return null;
}

/**
* Returns the value of this label.
*
* @return the value of this label
* @since com.adobe.cq.forms.core.components.models.form 0.0.1
*/
@Nullable
default String getValue() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.osgi.annotation.versioning.ProviderType;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
Expand Down Expand Up @@ -57,5 +58,17 @@ default boolean isEnforceEnum() {
* @return the list of enum names
* @since com.adobe.cq.forms.core.components.models.form 0.0.1
*/
@Deprecated
@JsonIgnore
String[] getEnumNames();

/**
* Returns a list of RichText to be displayed to the end user.
* The length of enum and enumNames array must match
*
* @return the list of enum names
* @since com.adobe.cq.forms.core.components.models.form 4.8.0
*/
@JsonProperty("enumNames")
TextContent[] getEnumNamesAsTextContent();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2023 Adobe
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.cq.forms.core.components.models.form;

import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ProviderType;

/**
* Interface to represent text as rich content
*
* @since com.adobe.cq.forms.core.components.models.form 4.8.0
*/
@ProviderType
public interface TextContent {

/**
* Returns {@code true} if text is rich, otherwise {@code false}.
*
* @return {@code true} if text is rich, otherwise {@code false}
* @since com.adobe.cq.forms.core.components.models.form 4.8.0
*/
@Nullable
default Boolean isRichText() {
return null;
}

/**
* Returns a user friendly text to display for the possible options to be shown to the end user.
*
* @return the content of this text
* @since com.adobe.cq.forms.core.components.models.form 4.8.0
*/
@Nullable
default String getValue() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

import com.adobe.cq.forms.core.components.models.form.Field;
import com.adobe.cq.forms.core.components.models.form.OptionsConstraint;
import com.adobe.cq.forms.core.components.models.form.TextContent;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

/**
* Abstract class which can be used as base class for options {@link Field} implementations.
Expand All @@ -49,6 +52,10 @@ public abstract class AbstractOptionsFieldImpl extends AbstractFieldImpl impleme
@Nullable
protected String[] enumNames;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = "areOptionsRichText")
@Nullable
private Boolean areOptionsRichText;

@Override
public boolean isEnforceEnum() {
return enforceEnum;
Expand Down Expand Up @@ -95,6 +102,8 @@ public Object[] getEnums() {
}

@Override
@Deprecated
@JsonIgnore
public String[] getEnumNames() {
if (enumNames != null) {
Map<Object, String> map = removeDuplicates();
Expand All @@ -108,6 +117,29 @@ public String[] getEnumNames() {
return null;
}

@Override
public TextContent[] getEnumNamesAsTextContent() {
if (enumNames != null) {
Map<Object, String> map = removeDuplicates();
String[] enumName = map.values().toArray(new String[0]);
return Arrays.stream(enumName)
.map(p -> new TextContent() {
@Override
@JsonInclude(JsonInclude.Include.NON_NULL)
public @Nullable Boolean isRichText() {
return areOptionsRichText;
}

@Override
public @Nullable String getValue() {
return translate("enumNames", p);
}
})
.toArray(TextContent[]::new);
}
return null;
}

@Override
public Object[] getDefault() {
Object[] typedDefaultValue = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -296,6 +297,44 @@ void testGetType() {
void testGetEnumNames() {
CheckBoxGroup checkboxGroup = getCheckBoxGroupUnderTest(PATH_CHECKBOX_GROUP);
assertArrayEquals(new String[] { "m", "f", "o" }, checkboxGroup.getEnumNames());
TextContent textContent1 = new TextContent() {
@Override
public @Nullable Boolean isRichText() {
return null;
}

@Override
public @Nullable String getValue() {
return "m";
}
};
TextContent textContent2 = new TextContent() {
@Override
public @Nullable Boolean isRichText() {
return null;
}

@Override
public @Nullable String getValue() {
return "f";
}
};
TextContent textContent3 = new TextContent() {
@Override
public @Nullable Boolean isRichText() {
return null;
}

@Override
public @Nullable String getValue() {
return "o";
}
};
TextContent[] textContent = new TextContent[] { textContent1, textContent2, textContent3 };
for (int i = 0; i < checkboxGroup.getEnumNamesAsTextContent().length; i++) {
assertEquals(textContent[i].getValue(), checkboxGroup.getEnumNamesAsTextContent()[i].getValue());
assertEquals(textContent[i].isRichText(), checkboxGroup.getEnumNamesAsTextContent()[i].isRichText());
}
}

@Test
Expand All @@ -306,6 +345,9 @@ void testGetEnumNamesWithDuplicateEnumValues() {
map.put("1", "Item 2");
map.put("0", "Item 3");
assertArrayEquals(map.values().toArray(new String[0]), checkboxGroup.getEnumNames());
String[] checkboxGroupValues = Arrays.stream(checkboxGroup.getEnumNamesAsTextContent()).map(d -> d.getValue()).toArray(
size -> new String[checkboxGroup.getEnumNamesAsTextContent().length]);
assertArrayEquals(map.values().toArray(new String[0]), checkboxGroupValues);
}

@Test
Expand Down Expand Up @@ -386,5 +428,8 @@ void testInsertionOrderForEnumNames() {
Set<String> set = new LinkedHashSet<>(Arrays.asList("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"));
assertArrayEquals(set.toArray(new String[0]), checkboxGroup.getEnumNames());
String[] checkboxGroupValues = Arrays.stream(checkboxGroup.getEnumNamesAsTextContent()).map(d -> d.getValue()).toArray(
size -> new String[checkboxGroup.getEnumNamesAsTextContent().length]);
assertArrayEquals(set.toArray(new String[0]), checkboxGroupValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@

import com.adobe.cq.forms.core.Utils;
import com.adobe.cq.forms.core.components.internal.form.FormConstants;
import com.adobe.cq.forms.core.components.models.form.Base;
import com.adobe.cq.forms.core.components.models.form.CheckBox;
import com.adobe.cq.forms.core.components.models.form.*;
import com.adobe.cq.forms.core.components.models.form.CheckBox.Orientation;
import com.adobe.cq.forms.core.components.models.form.ConstraintType;
import com.adobe.cq.forms.core.components.models.form.FieldType;
import com.adobe.cq.forms.core.components.models.form.Label;
import com.adobe.cq.forms.core.context.FormsCoreComponentTestContext;
import com.adobe.cq.wcm.style.ComponentStyleInfo;
import io.wcm.testing.mock.aem.junit5.AemContext;
Expand Down Expand Up @@ -309,6 +305,16 @@ void testGetEnum() {

}

@Test
void testGetNullEnumNames() {
CheckBox checkbox = getCheckBoxUnderTest(PATH_CHECKBOX);
assertNull(checkbox.getEnumNamesAsTextContent());
assertNull(checkbox.getEnumNames());
CheckBox noEnumCheckbox = getCheckBoxUnderTest(PATH_CHECKBOX_NOENUM);
assertNull(noEnumCheckbox.getEnumNamesAsTextContent());
assertNull(noEnumCheckbox.getEnumNames());
}

@Test
void testGetNullEnum() {
CheckBox noEnumCheckbox = getCheckBoxUnderTest(PATH_CHECKBOX_NOENUM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -379,6 +380,44 @@ void testGetMultiSelectMaxItems() {
void testGetEnumNames() {
DropDown dropdown = Utils.getComponentUnderTest(PATH_DROPDOWN_1, DropDown.class, context);
assertArrayEquals(new String[] { "m", "f", "o" }, dropdown.getEnumNames());
TextContent textContent1 = new TextContent() {
@Override
public @Nullable Boolean isRichText() {
return null;
}

@Override
public @Nullable String getValue() {
return "m";
}
};
TextContent textContent2 = new TextContent() {
@Override
public @Nullable Boolean isRichText() {
return null;
}

@Override
public @Nullable String getValue() {
return "f";
}
};
TextContent textContent3 = new TextContent() {
@Override
public @Nullable Boolean isRichText() {
return null;
}

@Override
public @Nullable String getValue() {
return "o";
}
};
TextContent[] textContent = new TextContent[] { textContent1, textContent2, textContent3 };
for (int i = 0; i < dropdown.getEnumNamesAsTextContent().length; i++) {
assertEquals(textContent[i].getValue(), dropdown.getEnumNamesAsTextContent()[i].getValue());
assertEquals(textContent[i].isRichText(), dropdown.getEnumNamesAsTextContent()[i].isRichText());
}
}

@Test
Expand All @@ -389,6 +428,10 @@ void testGetEnumNamesWithDuplicateEnumValues() {
map.put("1", "Item 2");
map.put("0", "Item 3");
assertArrayEquals(map.values().toArray(new String[0]), dropdown.getEnumNames());
String[] dropdownValues = Arrays.stream(dropdown.getEnumNamesAsTextContent()).map(d -> d.getValue()).toArray(
size -> new String[dropdown
.getEnumNamesAsTextContent().length]);
assertArrayEquals(map.values().toArray(new String[0]), dropdownValues);
}

@Test
Expand Down Expand Up @@ -438,5 +481,9 @@ void testInsertionOrderForEnumNames() {
Set<String> set = new LinkedHashSet<>(Arrays.asList("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"));
assertArrayEquals(set.toArray(new String[0]), dropdown.getEnumNames());
String[] dropdownValues = Arrays.stream(dropdown.getEnumNamesAsTextContent()).map(d -> d.getValue()).toArray(
size -> new String[dropdown
.getEnumNamesAsTextContent().length]);
assertArrayEquals(set.toArray(new String[0]), dropdownValues);
}
}
Loading