Skip to content

Commit

Permalink
ColorBehavior can accept IModel<Color>
Browse files Browse the repository at this point in the history
  • Loading branch information
vrozkovec committed Nov 25, 2023
1 parent 18f8588 commit 84d4ff4
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import org.apache.wicket.Component;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import de.agilecoders.wicket.core.markup.html.bootstrap.behavior.BootstrapBaseBehavior;
import de.agilecoders.wicket.core.markup.html.bootstrap.behavior.ICssClassNameProvider;
import de.agilecoders.wicket.core.util.Attributes;

/**
Expand All @@ -18,7 +21,7 @@ public class ColorBehavior extends BootstrapBaseBehavior {
/**
* Enum of available text colors.
*/
public enum Color {
public enum Color implements ICssClassNameProvider {
Primary("primary"),
Primary_emphasis("primary-emphasis"),
Secondary("secondary"),
Expand All @@ -44,39 +47,48 @@ public enum Color {
Black50("black-50"),
White50("white-50");

private final String value;
private final String cssClassName;

Color(String value) {
this.value = value;
this.cssClassName = "text-" + value;
}

/**
* Css class associated with this color.
* @return Css class associated with this color.
*/
public String cssClassName() {
return String.format("text-%s", this.value);
return cssClassName;
}
}

/**
* Color that should be added to component.
*/
private final Color color;
private final IModel<Color> colorModel;

/**
* Constructs new instance for given color.
* @param color the color that should be added to component.
*/
public ColorBehavior(Color color) {
this.color = color;
this(Model.of(color));
}

/**
* @param colorModel
*/
public ColorBehavior(IModel<Color> colorModel)
{
this.colorModel = colorModel;
}


@Override
@Override
public void onComponentTag(Component component, ComponentTag tag) {
super.onComponentTag(component, tag);

Attributes.addClass(tag, color.cssClassName());
Attributes.addClass(tag, colorModel.getObject());
}

/**
Expand Down

0 comments on commit 84d4ff4

Please sign in to comment.