8000 Slider, range and color arguments by kamil-orwat-vmltech · Pull Request #82 · wttech/acm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Slider, range and color arguments #82

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

Merged
merged 17 commits into from
May 15, 2025
Merged
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 @@ -8,5 +8,7 @@ public enum ArgumentType {
STRING,
TEXT,
SELECT,
MULTISELECT
MULTISELECT,
COLOR,
NUMBER_RANGE
}
31 changes: 31 additions & 0 deletions core/src/main/java/com/vml/es/aem/acm/core/code/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public ValueMap getValues() {
return new ArgumentsValueMap(props);
}

@JsonIgnore
public ValueMap values() {
return getValues();
}

public <T> T getValue(String name, Class<T> type) {
return getValues().get(name, type);
}
Expand Down Expand Up @@ -145,6 +150,32 @@ public void integerNumber(String name, Closure<IntegerArgument> options) {
add(argument);
}

public void color(String name) {
color(name, null);
}

public void color(String name, Closure<ColorArgument> options) {
ColorArgument argument = new ColorArgument(name);
GroovyUtils.with(argument, options);
add(argument);
}

public void integerRange(String name, Closure<IntegerRangeArgument> options) {
IntegerRangeArgument argument = new IntegerRangeArgument(name);
GroovyUtils.with(argument, options);
add(argument);
}

public void decimalRange(String name) {
decimalRange(name, null);
}

public void decimalRange(String name, Closure<DecimalRangeArgument> options) {
DecimalRangeArgument argument = new DecimalRangeArgument(name);
GroovyUtils.with(argument, options);
add(argument);
}

public void decimalNumber(String name) {
decimalNumber(name, null);
}
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,64 @@
import org.apache.sling.api.wrappers.ValueMapDecorator;

public class ArgumentsValueMap extends ValueMapDecorator {
/**
* Creates a new wrapper around a given map.
*
* @param base wrapped object
*/

public ArgumentsValueMap(Map<String, Object> base) {
super(base);
}

/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public <T> T get(String name, Class<T> type) {
Object obj = get(name);
Object value = get(name);

// LocalDateTime conversion
if (obj instanceof Date && type == LocalDateTime.class) {
if (value instanceof Date && type == LocalDateTime.class) {
// Convert Date to LocalDateTime
return (T) DateUtils.toLocalDateTime((Date) obj);
} else if (obj instanceof Calendar && type == LocalDateTime.class) {
return (T) DateUtils.toLocalDateTime((Date) value);
} else if (value instanceof Calendar && type == LocalDateTime.class) {
// Convert Calendar to LocalDateTime
return (T) DateUtils.toLocalDateTime((Calendar) obj);
} else if (obj instanceof LocalDateTime && type == Date.class) {
return (T) DateUtils.toLocalDateTime((Calendar) value);
} else if (value instanceof LocalDateTime && type == Date.class) {
// Convert LocalDateTime to Date
return (T) DateUtils.toDate((LocalDateTime) obj);
} else if (obj instanceof LocalDateTime && type == Calendar.class) {
return (T) DateUtils.toDate((LocalDateTime) value);
} else if (value instanceof LocalDateTime && type == Calendar.class) {
// Convert LocalDateTime to Calendar
return (T) DateUtils.toCalendar((LocalDateTime) obj);
} else if (obj instanceof LocalDateTime && type == LocalDate.class) {
return (T) DateUtils.toCalendar((LocalDateTime) value);
} else if (value instanceof LocalDateTime && type == LocalDate.class) {
// Convert LocalDateTime to LocalDate
return (T) ((LocalDateTime) obj).toLocalDate();
return (T) ((LocalDateTime) value).toLocalDate();
}

// LocalDate conversion
if (obj instanceof Date && type == LocalDate.class) {
if (value instanceof Date && type == LocalDate.class) {
// Convert Date to LocalDate
return (T) DateUtils.toLocalDateTime((Date) obj).toLocalDate();
} else if (obj instanceof Calendar && type == LocalDate.class) {
return (T) DateUtils.toLocalDateTime((Date) value).toLocalDate();
} else if (value instanceof Calendar && type == LocalDate.class) {
// Convert Calendar to LocalDate
return (T) DateUtils.toLocalDateTime((Calendar) obj).toLocalDate();
} else if (obj instanceof LocalDate && type == Date.class) {
return (T) DateUtils.toLocalDateTime((Calendar) value).toLocalDate();
} else if (value instanceof LocalDate && type == Date.class) {
// Convert LocalDate to Date
return (T) DateUtils.toDate(((LocalDate) obj).atTime(LocalTime.MIN));
} else if (obj instanceof LocalDate && type == Calendar.class) {
return (T) DateUtils.toDate(((LocalDate) value).atTime(LocalTime.MIN));
} else if (value instanceof LocalDate && type == Calendar.class) {
// Convert LocalDate to Calendar
return (T) DateUtils.toCalendar(((LocalDate) obj).atTime(LocalTime.MIN));
} else if (obj instanceof LocalDate && type == LocalDateTime.class) {
return (T) DateUtils.toCalendar(((LocalDate) value).atTime(LocalTime.MIN));
} else if (value instanceof LocalDate && type == LocalDateTime.class) {
// Convert LocalDate to LocalDateTime
return (T) ((LocalDate) obj).atTime(LocalTime.MIN);
return (T) ((LocalDate) value).atTime(LocalTime.MIN);
}

// String conversion
if (obj instanceof String && type == LocalDateTime.class) {
if (value instanceof String && type == LocalDateTime.class) {
// Convert String to LocalDateTime
return (T) DateUtils.toLocalDateTime((String) obj);
} else if (obj instanceof String && type == Date.class) {
return (T) DateUtils.toLocalDateTime((String) value);
} else if (value instanceof String && type == Date.class) {
// Convert String to Date
return (T) DateUtils.fromString((String) obj);
} else if (obj instanceof String && type == Calendar.class) {
return (T) DateUtils.fromString((String) value);
} else if (value instanceof String && type == Calendar.class) {
// Convert String to Calendar
return (T) DateUtils.toCalendar((String) obj);
} else if (obj instanceof String && type == LocalDate.class) {
return (T) DateUtils.toCalendar((String) value);
} else if (value instanceof String && type == LocalDate.class) {
// Convert String to LocalDate
return (T) DateUtils.toLocalDate((String) obj);
return (T) DateUtils.toLocalDate((String) value);
}

return super.get(name, type);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.vml.es.aem.acm.core.code.arg;

import com.vml.es.aem.acm.core.code.Argument;
import com.vml.es.aem.acm.core.code.ArgumentType;
import java.util.Arrays;

public class ColorArgument extends Argument<String> {
private ColorArgument.Format format = ColorArgument.Format.RGBA;

public ColorArgument(String name) {
super(name, ArgumentType.COLOR);
}

public Format getFormat() {
return format;
}

public void setFormat(String format) {
this.format = ColorArgument.Format.of(format);
}

public void setFormat(ColorArgument.Format format) {
this.format = format;
}

public void hex() {
this.format = ColorArgument.Format.HEX;
}

public void hsl() {
this.format = ColorArgument.Format.HSL;
}

public void rgba() {
this.format = ColorArgument.Format.RGBA;
}

public void hsb() {
this.format = ColorArgument.Format.HSB;
}

public enum Format {
HEX,
HSL,
RGBA,
HSB;

public static ColorArgument.Format of(String name) {
return Arrays.stream(ColorArgument.Format.values())
.filter(r -> r.name().equalsIgnoreCase(name))
.findFirst()
.orElseThrow(() ->
new IllegalArgumentException(String.format("Color format '%s' is not supported!", name)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import com.vml.es.aem.acm.core.code.Argument;
import com.vml.es.aem.acm.core.code.ArgumentType;
import java.math.BigDecimal;
import java.util.Arrays;

public class DecimalArgument extends Argument<BigDecimal> {

private BigDecimal min;

private BigDecimal max;

private Display display = Display.INPUT;

private BigDecimal step;

public DecimalArgument(String name) {
super(name, ArgumentType.DECIMAL);
}
Expand All @@ -29,4 +34,43 @@ public BigDecimal getMax() {
public void setMax(BigDecimal max) {
this.max = max;
}

public void input() {
this.display = Display.INPUT;
}

public void slider() {
this.display = Display.SLIDER;
}

public void slider(BigDecimal min, BigDecimal max) {
Copy link
Collaborator
@krystian-panek-vmltech krystian-panek-vmltech May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will it work with ints, doubles, strings when passed from groovy?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, only decimals, but I think it's expected if we want type safety?

this.display = Display.SLIDER;
this.min = min;
this.max = max;
}

public Display getDisplay() {
return display;
}

public BigDecimal getStep() {
return step;
}

public void setStep(BigDecimal step) {
this.step = step;
}

public enum Display {
INPUT,
SLIDER;

public static DecimalArgument.Display of(String name) {
return Arrays.stream(DecimalArgument.Display.values())
.filter(r -> r.name().equalsIgnoreCase(name))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(
String.format("Decimal argument cannot be displayed as '%s'!", name)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.vml.es.aem.acm.core.code.arg;

import com.vml.es.aem.acm.core.code.Argument;
import com.vml.es.aem.acm.core.code.ArgumentType;
import com.vml.es.aem.acm.core.util.Range;
import java.math.BigDecimal;
import java.util.List;

public class DecimalRangeArgument extends Argument<Range<BigDecimal>> {
private BigDecimal min;

private BigDecimal max;

private BigDecimal step;

public DecimalRangeArgument(String name) {
super(name, ArgumentType.NUMBER_RANGE);
}

public void setValue(List<BigDecimal> value) {
if (value.size() != 2) {
throw new IllegalArgumentException(
String.format("Range value must be a list of two elements but specified '%s'!", value));
}
super.setValue(new Range<>(value.get(0), value.get(1)));
}

public BigDecimal getMin() {
return min;
}

public void setMin(BigDecimal min) {
this.min = min;
}

public BigDecimal getStep() {
return step;
}

public void setStep(BigDecimal step) {
this.step = step;
}

public BigDecimal getMax() {
return max;
}

public void setMax(BigDecimal max) {
this.max = max;
}
}
96C7
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import com.vml.es.aem.acm.core.code.Argument;
import com.vml.es.aem.acm.core.code.ArgumentType;
import java.util.Arrays;

public class IntegerArgument extends Argument<Integer> {

private Integer min;

private Integer max;

private Display display = Display.INPUT;

private Integer step;

public IntegerArgument(String name) {
super(name, ArgumentType.INTEGER);
}
Expand All @@ -28,4 +33,43 @@ public Integer getMax() {
public void setMax(Integer max) {
this.max = max;
}

public void input() {
this.display = Display.INPUT;
}

public void slider() {
this.display = Display.SLIDER;
}

public void slider(Integer min, Integer max) {
this.display = Display.SLIDER;
this.min = min;
this.max = max;
}

public Display getDisplay() {
return display;
}

public Integer getStep() {
return step;
}

public void setStep(Integer step) {
this.step = step;
}

public enum Display {
INPUT,
SLIDER;

public static IntegerArgument.Display of(String name) {
return Arrays.stream(IntegerArgument.Display.values())
.filter(r -> r.name().equalsIgnoreCase(name))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(
String.format("Decimal argument cannot be displayed as '%s'!", name)));
}
}
}
Loading
0