Skip to content

Commit

Permalink
Allow BorderBehavior to use IModel, add Width enum
Browse files Browse the repository at this point in the history
  • Loading branch information
vrozkovec committed Nov 25, 2023
1 parent 8584a42 commit 18f8588
Showing 1 changed file with 102 additions and 18 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 @@ -17,7 +20,7 @@ public class BorderBehavior extends BootstrapBaseBehavior {
/**
* Border colors built on our theme colors.
*/
public enum Color {
public enum Color implements ICssClassNameProvider {
Primary("primary"),
Primary_subtle("primary-subtle"),
Secondary("secondary"),
Expand All @@ -37,24 +40,24 @@ public enum Color {
Black("black"),
White("white");

private final String value;
private final String cssClassName;

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

/**
* Returns css class name of given border color
*/
public String cssClassName() {
return String.format("border-%s", value);
return cssClassName;
}
}

/**
* Classes to easily round its corners.
*/
public enum Radius {
public enum Radius implements ICssClassNameProvider {
All("rounded"),
Top("rounded-top"),
Right("rounded-end"),
Expand All @@ -81,7 +84,7 @@ public String cssClassName() {
/**
* Enum of available border types.
*/
public enum Type {
public enum Type implements ICssClassNameProvider {
All("border"),
Top("border-top"),
Right("border-end"),
Expand All @@ -106,46 +109,89 @@ public String cssClassName() {
return value;
}
}

/**
* Enum of available border types.
*/
public enum Width implements ICssClassNameProvider {
Width_1(1),
Width_2(2),
Width_3(3),
Width_4(4),
Width_5(5)
;

private final String cssClassName;

Width(int width) {
this.cssClassName = "border-" + width;
}

/**
* Returns css class of given border type.
*/
public String cssClassName() {
return cssClassName;
}
}

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

/**
* Border rounding type.
*/
private BorderBehavior.Radius radius;
private IModel<BorderBehavior.Radius> radiusModel;

/**
* Type of border.
*/
private BorderBehavior.Type type;
private IModel<BorderBehavior.Type> typeModel;

/**
* Width of border.
*/
private IModel<BorderBehavior.Width> widthModel;

/**
* Constructs new instance of default border.
*/
public BorderBehavior() {
this.color = Color.Secondary;
this.radius = Radius.None;
this.type = Type.None;
this.colorModel = Model.of(Color.Secondary);
this.radiusModel = Model.of(Radius.None);
this.typeModel = Model.of(Type.None);
this.widthModel = Model.of();
}

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

Attributes.addClass(tag, type.cssClassName(), color.cssClassName(), radius.cssClassName());
Attributes.addClass(tag, colorModel.getObject());
Attributes.addClass(tag, radiusModel.getObject());
Attributes.addClass(tag, typeModel.getObject());
Attributes.addClass(tag, widthModel.getObject());
}

/**
* Sets new border color.
*
* @param color the new border color
*/
public BorderBehavior color(Color color) {
this.color = color;

colorModel.setObject(color);
return this;
}

/**
* Sets new border color.
*
* @param colorModel the new border color
*/
public BorderBehavior color(IModel<Color> colorModel) {
this.colorModel = colorModel;
return this;
}

Expand All @@ -155,8 +201,17 @@ public BorderBehavior color(Color color) {
* @param radius the new rounding border corners type.
*/
public BorderBehavior radius(Radius radius) {
this.radius = radius;
radiusModel.setObject(radius);
return this;
}

/**
* Sets new type of rounding border corners.
*
* @param radiusModel the model for the rounding border corners type
*/
public BorderBehavior radius(IModel<Radius> radiusModel) {
this.radiusModel = radiusModel;
return this;
}

Expand All @@ -166,8 +221,37 @@ public BorderBehavior radius(Radius radius) {
* @param type the type of border
*/
public BorderBehavior type(Type type) {
this.type = type;
typeModel.setObject(type);
return this;
}

/**
* Sets new border type.
*
* @param typeModel the model for the type of border
*/
public BorderBehavior type(IModel<Type> typeModel) {
this.typeModel = typeModel;
return this;
}

/**
* Sets new border width.
*
* @param width the width of the border
*/
public BorderBehavior width(Width width) {
widthModel.setObject(width);
return this;
}

/**
* Sets new border width.
*
* @param widthModel the model for the width of the border
*/
public BorderBehavior width(IModel<Width> widthModel) {
this.widthModel = widthModel;
return this;
}
}

0 comments on commit 18f8588

Please sign in to comment.