Skip to content

Commit

Permalink
Synchronize border and color classes with BS 5.3.2, add Color and bac…
Browse files Browse the repository at this point in the history
…kground helper, behavior improvements (#1005)

* Fix border classes, add new colors for BS5

* Align colors with BS 5

https://getbootstrap.com/docs/5.3/utilities/colors/

* Javadoc fix

* Add new implementation to accommodate Color and background helper

https://getbootstrap.com/docs/5.3/helpers/color-background/

* Allow BorderBehavior to use IModel, add Width enum

* ColorBehavior can accept IModel<Color>

* BackgroundColorBehavior now accepts IModel<Color>

* ColorAndBackgroundBehavior now accepts IModel<Color>

* Add getters and setters for color

(cherry picked from commit 73aa879)
  • Loading branch information
vrozkovec authored and martin-g committed Nov 27, 2023
1 parent 824617b commit 30bdcdb
Show file tree
Hide file tree
Showing 5 changed files with 513 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package de.agilecoders.wicket.core.markup.html.bootstrap.helpers;

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.markup.html.bootstrap.utilities.BackgroundColorBehavior;
import de.agilecoders.wicket.core.markup.html.bootstrap.utilities.BackgroundColorBehavior.Color;
import de.agilecoders.wicket.core.util.Attributes;

/**
* Color and background helpers combine the power of our .text-* utilities and .bg-* utilities in one class.
*/
public class ColorAndBackgroundBehavior extends BootstrapBaseBehavior {

/**
* Enumeration of all possible background colors.
*/
public enum Color implements ICssClassNameProvider {
Primary("primary"),
Secondary("secondary"),
Success("success"),
Danger("danger"),
Warning("warning"),
Info("info"),
Light("light"),
Dark("dark");

private final String cssClassName;

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

/**
* Css class associated with this background color.
* @return Css class associated with this background color.
*/
public String cssClassName() {
return cssClassName;
}
}

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

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

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

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

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

/**
* Sets color.
* @param color
* @return this for chaining
*/
public ColorAndBackgroundBehavior color(Color color) {
colorModel.setObject(color);
return this;
}

/**
* Sets color model.
* @param colorModel
* @return this for chaining
*/
public ColorAndBackgroundBehavior color(IModel<Color> colorModel) {
this.colorModel = colorModel;
return this;
}

/**
* @return color
*/
public Color getColor() {
return colorModel.getObject();
}

/**
* @return color model
*/
public IModel<Color> getColorModel() {
return colorModel;
}


/**
* Constructs new behavior that adds {@link ColorAndBackgroundBehavior.Color#Primary} to component
*
* @return behavior that adds primary background color to component
*/
public static ColorAndBackgroundBehavior primary() {
return new ColorAndBackgroundBehavior(Color.Primary);
}

/**
* Constructs new behavior that adds {@link ColorAndBackgroundBehavior.Color#Secondary} to component
*
* @return behavior that adds secondary background color to component
*/
public static ColorAndBackgroundBehavior secondary() {
return new ColorAndBackgroundBehavior(Color.Secondary);
}

/**
* Constructs new behavior that adds {@link ColorAndBackgroundBehavior.Color#Success} to component
*
* @return behavior that adds success background color to component
*/
public static ColorAndBackgroundBehavior success() {
return new ColorAndBackgroundBehavior(Color.Success);
}

/**
* Constructs new behavior that adds {@link ColorAndBackgroundBehavior.Color#Danger} to component
*
* @return behavior that adds danger background color to component
*/
public static ColorAndBackgroundBehavior danger() {
return new ColorAndBackgroundBehavior(Color.Danger);
}

/**
* Constructs new behavior that adds {@link ColorAndBackgroundBehavior.Color#Warning} to component
*
* @return behavior that adds warning background color to component
*/
public static ColorAndBackgroundBehavior warning() {
return new ColorAndBackgroundBehavior(Color.Warning);
}

/**
* Constructs new behavior that adds {@link ColorAndBackgroundBehavior.Color#Info} to component
*
* @return behavior that adds info background color to component
*/
public static ColorAndBackgroundBehavior info() {
return new ColorAndBackgroundBehavior(Color.Info);
}

/**
* Constructs new behavior that adds {@link ColorAndBackgroundBehavior.Color#Light} to component
*
* @return behavior that adds light background color to component
*/
public static ColorAndBackgroundBehavior light() {
return new ColorAndBackgroundBehavior(Color.Light);
}

/**
* Constructs new behavior that adds {@link ColorAndBackgroundBehavior.Color#Dark} to component
*
* @return behavior that adds dark background color to component
*/
public static ColorAndBackgroundBehavior dark() {
return new ColorAndBackgroundBehavior(Color.Dark);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
import de.agilecoders.wicket.core.util.Components;

/**
* A convenient implementation of the Bootstrap styles Panel of a Wicket {@link GenericPanel}.
* A convenient implementation of the Bootstrap-styled Card of a Wicket {@link GenericPanel}.
*
* documentation: http://getbootstrap.com/components/#panels
* documentation: https://getbootstrap.com/docs/5.3/components/card/
*
* Implement as you would a standard Wicket {@link GenericPanel}.
*
* Adding components to this panel will insert them below the markup for the body and aove the markup for the
* footer, per the Bootstrap documentation. This is useful for:
*
* panels with tables - http://getbootstrap.com/components/#panels-tables
* panel with list groups - http://getbootstrap.com/components/#panels-list-group
* Adding components to this panel will insert them below the markup for the body and above the markup for the
* footer, per the Bootstrap documentation.
*
* @author Eric Hamel <eric.hamel@me.com>
*
Expand Down
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 @@ -16,7 +19,7 @@ public class BackgroundColorBehavior extends BootstrapBaseBehavior {
/**
* Enumeration of all possible background colors.
*/
public enum Color {
public enum Color implements ICssClassNameProvider {
Primary("primary"),
Secondary("secondary"),
Danger("danger"),
Expand All @@ -28,48 +31,84 @@ public enum Color {
White("white"),
Transparent("transparent");

private final String value;
private final String cssClassName;

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

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

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


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

@Override

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


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

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

public void color(Color color) {
this.color = color;
/**
* Sets color.
* @param color
* @return this for chaining
*/
public BackgroundColorBehavior color(Color color) {
colorModel.setObject(color);
return this;
}

/**
* Sets color model.
* @param colorModel
* @return this for chaining
*/
public BackgroundColorBehavior color(IModel<Color> colorModel) {
this.colorModel = colorModel;
return this;
}

/**
* @return color
*/
public Color getColor() {
return this.color;
return colorModel.getObject();
}

/**
* @return color model
*/
public IModel<Color> getColorModel() {
return colorModel;
}

/**
Expand Down
Loading

0 comments on commit 30bdcdb

Please sign in to comment.