Skip to content

Commit

Permalink
Adding types for portal themeing
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Jul 20, 2023
1 parent 19fc376 commit 1daff4f
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package de.cuioss.jsf.jqplot.portal.theme;

import static de.cuioss.tools.collect.CollectionLiterals.mutableSet;
import static de.cuioss.tools.string.MoreStrings.emptyToNull;
import static de.cuioss.tools.string.MoreStrings.requireNotEmpty;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import de.cuioss.tools.string.Splitter;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
* Class represent a single Css rule and provide a simplified api to interact
* with
*
* @author i000576
*/
@ToString
@EqualsAndHashCode(of = "selector")
@RequiredArgsConstructor
public class CssRule implements Serializable {

private static final long serialVersionUID = -4809127537364135508L;

@Getter
private final String selector;

private final Map<String, String> declarations;

/**
* @return properties
*/
public Set<String> getProperties() {
return mutableSet(declarations.keySet());
}

/**
* @param propertyName
* @return value for property name
*/
public String getPropertyValue(final String propertyName) {
final String key = emptyToNull(propertyName).toLowerCase();
return declarations.get(key);
}

/**
* @return CssRule declared property names
*/
public Set<String> getPropertyNames() {
return declarations.keySet();
}

/**
* Factory Method create {@linkplain CssRule} from CssText
*
* @param cssText {@linkplain String} must not be {@code null}
* @return {@linkplain CssRule} if possible
*/
public static CssRule createBy(final String cssText) {

final String checked = requireNotEmpty(cssText, "CssText");

// TODO add check for syntax > name { prop1 : val1; ... propN : valN } <

final List<String> splitToList = Splitter.on("{").trimResults().omitEmptyStrings().splitToList(checked);

final String cssSelector = splitToList.get(0).trim();

return new CssRule(cssSelector, parseDeclarations(splitToList.get(1)));
}

private static Map<String, String> parseDeclarations(final String tail) {

final String plain = tail.replace("}", "").trim();

final List<String> pairs = Splitter.on(";").trimResults().omitEmptyStrings().splitToList(plain);

final Map<String, String> result = new HashMap<>(pairs.size());

for (final String pair : pairs) {

final List<String> splittedPair = Splitter.on(":").trimResults().omitEmptyStrings().splitToList(pair);

if (splittedPair.size() == 2) {

final String key = splittedPair.get(0).toLowerCase();
result.putIfAbsent(key, splittedPair.get(1));

}
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package de.cuioss.jsf.jqplot.portal.theme;

import java.io.Serializable;

import de.cuioss.jsf.jqplot.js.types.JsBoolean;
import de.cuioss.jsf.jqplot.js.types.JsString;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
* Provide JqPlot theme specific grid settings
*
* <pre>
* grid: {
drawGridlines: true,
gridLineColor: "#cccccc",
gridLineWidth: 1,
backgroundColor: "#fffdf6",
borderColor: "#999999",
borderWidth: 2,
shadow: true
}
* </pre>
*
* @author i000576
*/
@ToString
@EqualsAndHashCode
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@SuppressWarnings("java:S6548") // owolff: Legacy code, will be removed
public class GridTheme implements Serializable {

private static final long serialVersionUID = -1005187429311999511L;

/**
* default css class name is 'jqplot-grid'
*/
public static final String CSS_CLASS_NAME = "jqplot-grid";

@Getter
private final Boolean drawGridlines;

@Getter
private final String backgroundColor;

private JsBoolean drawGridlinesJs;

/**
* @return drawGridlines setting as JsonValue
*/
public String getDrawGridlinesAsJs() {
if (null == this.drawGridlinesJs) {
this.drawGridlinesJs = JsBoolean.create(this.drawGridlines);
}
return this.drawGridlinesJs.getValueAsString();
}

private JsString backgroundColorJs;

/**
* @return backgroundColor setting as JsonValue
*/
public String getBackgroundColorAsJs() {
if (null == this.backgroundColorJs) {
this.backgroundColorJs = new JsString(this.backgroundColor);
}
return this.backgroundColorJs.getValueAsString();
}

/**
* Factory Method for {@linkplain GridTheme}.<br/>
*
* @param cssRule {@linkplain CssRule} is optional
* @return {@linkplain GridTheme} according property values of cssRule. If
* parameter cssRule is {@code null} default
* {@linkplain GridTheme#DEFAULT_GRID_SETTINGS} will be returned
*/
public static GridTheme createBy(final CssRule cssRule) {

if (null == cssRule) {
return DEFAULT_GRID_SETTINGS;
}

final Boolean drawGridlinesValue = Boolean.valueOf(cssRule.getPropertyValue("-jqplot-drawGridlines"));

final String backgroundColorValue = cssRule.getPropertyValue("-jqplot-backgroundColor");

return new GridTheme(drawGridlinesValue, backgroundColorValue);
}

/**
* Grid with default theme settings
*/
public static final GridTheme DEFAULT_GRID_SETTINGS = new GridTheme(Boolean.TRUE, "#fffdf6");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package de.cuioss.jsf.jqplot.portal.theme;

import static de.cuioss.tools.string.MoreStrings.requireNotEmpty;
import static java.util.stream.Collectors.toList;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import de.cuioss.jsf.jqplot.js.support.JsArray;
import de.cuioss.jsf.jqplot.js.types.JsString;
import de.cuioss.tools.string.MoreStrings;
import de.cuioss.tools.string.Splitter;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
* SeriesColors object that could be applied to all series in the plot.<br/>
* <b>Expected css Rule structure</b>
*
* <pre>
* // color values must be html conform either color names or their hex value
* .some-usefull-class-name{
* -jqplot-seriescolors : "blue, #FF0000, .. colorN"
* }
* </pre>
*
* @author i000576
*/
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@EqualsAndHashCode
@ToString
@SuppressWarnings("java:S6548") // owolff: Legacy code, will be removed
public class SeriesColors implements Serializable {

private static final long serialVersionUID = 7676175049884241143L;

/**
* Default Css class name is 'jqplot-series-styles'
*/
public static final String CSS_CLASS_NAME = "jqplot-series-styles";

@Getter
private final List<String> colors;

private String colorsAsJs;

/**
* @return colors as JsonValue
*/
public String getColorsAsJs() {
if (null == colorsAsJs) {
final JsArray<JsString> colJs = new JsArray<>();
colJs.addAll(colors.stream().map(JsString::new).collect(toList()));
colorsAsJs = colJs.getValueAsString();
}
return colorsAsJs;
}

/**
* Factory method to create {@linkplain SeriesColors} from
* {@linkplain CssRule}.<br/>
* If cssRule is missing {@link SeriesColors#DEFAULT_SERIES_COLORS} will be used
*
* @param cssRule {@linkplain CssRule} could be {@code null}
* @return {@linkplain SeriesColors}
*/
public static SeriesColors createBy(final CssRule cssRule) {

if (null == cssRule) {
return DEFAULT_SERIES_COLORS;
}

final String seriesColors = cssRule.getPropertyValue("-jqplot-seriescolors");

requireNotEmpty(seriesColors, "seriesColors");

return new SeriesColors(extractColors(seriesColors));
}

private static List<String> extractColors(final String value) {

if (MoreStrings.isEmpty(value)) {
return Collections.emptyList();
}

return Splitter.on(",").omitEmptyStrings().trimResults().splitToList(value).stream()
.map(item -> item.replace("'", "")).map(item -> item.replace("\"", "")).collect(toList());

}

/**
* Fall-back SeriesColors use red and green as colors
*/
public static final SeriesColors DEFAULT_SERIES_COLORS = new SeriesColors(Arrays.asList("red", "green"));

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.cuioss.jsf.jqplot.portal.theme;

import static de.cuioss.test.generator.Generators.fixedValues;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import de.cuioss.jsf.jqplot.portal.theme.CssRule;
import de.cuioss.test.generator.TypedGenerator;
import de.cuioss.test.valueobjects.ValueObjectTest;
import de.cuioss.test.valueobjects.api.property.PropertyReflectionConfig;

@PropertyReflectionConfig(of = "selector")
class CssRuleTest extends ValueObjectTest<CssRule> {

private final TypedGenerator<String> validRules = fixedValues("name{prop1:value1}", "name{prop2:value2}",
"name{prop3:value3}");

private CssRule target;

@Test
final void shouldFailOnMissingReqiredParameter() {
assertThrows(IllegalArgumentException.class, () -> {
CssRule.createBy(null);
});
}

@Test
final void shouldFailOnMissingContent() {
assertThrows(IllegalArgumentException.class, () -> {
target = CssRule.createBy("");
});
}

@Test
final void shouldProvideAvailablePropertyValue() {
target = CssRule.createBy("selector-name{-property-Name:propertyValue}");
assertEquals("selector-name", target.getSelector());
assertTrue(target.getProperties().contains("-property-name"));
assertEquals("propertyValue", target.getPropertyValue("-property-name"));
}

@Override
protected CssRule anyValueObject() {
return CssRule.createBy(validRules.next());
}
}
Loading

0 comments on commit 1daff4f

Please sign in to comment.