<commit_before>package fr.openwide.core.wicket.more.markup.html.image;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;

import fr.openwide.core.wicket.behavior.ClassAttributeAppender;

/**
 * Container affichant une icône selon que le model soit true ou false.
 * Par défaut un carré coché ou non avec possibilité de personnaliser.
 */
public class BooleanIcon extends WebMarkupContainer {

	private static final long serialVersionUID = 6413843086031282399L;
	
	private static final String DEFAULT_TRUE_ICON_CLASS = "fa fa-check-square-o fa-fw";
	private static final String DEFAULT_FALSE_ICON_CLASS = "fa fa-square-o fa-fw";
	
	private static final String DEFAULT_TRUE_HIDE_IF_NULL_OR_FALSE_ICON_CLASS = "fa fa-check fa-fw";
	
	private static final String BOOLEAN_ICON_BASE_CLASS = "boolean-icon";
	
	private IModel<Boolean> booleanModel;
	
	private IModel<String> trueIconClassModel;
	
	private IModel<String> falseIconClassModel;
	
	private boolean hideIfNullOrFalse = false;
	
	public BooleanIcon(String id, IModel<Boolean> booleanModel) {
		this(id, booleanModel, null, null);
	}
	
	public BooleanIcon(String id, IModel<Boolean> booleanModel,
			IModel<String> trueIconClassModel, IModel<String> falseIconClassModel) {
		super(id);
		this.booleanModel = booleanModel;
		this.trueIconClassModel = trueIconClassModel;
		this.falseIconClassModel = falseIconClassModel;
		
		add(new ClassAttributeAppender(BOOLEAN_ICON_BASE_CLASS));
		add(new ClassAttributeAppender(new LoadableDetachableModel<String>() {
			private static final long serialVersionUID = 1L;
			
			@Override
			protected String load() {
				return Boolean.TRUE.equals(getBooleanModelObject()) ? getTrueIconClass() : getFalseIconClass();
			}
		}));
	}
	
	private Boolean getBooleanModelObject() {
		return booleanModel != null ? booleanModel.getObject() : null;
	}
	
	private String getTrueIconClass() {
		if (trueIconClassModel == null) {
			if (hideIfNullOrFalse) {
				trueIconClassModel = new Model<String>(DEFAULT_TRUE_HIDE_IF_NULL_OR_FALSE_ICON_CLASS);
			} else {
				trueIconClassModel = new Model<String>(DEFAULT_TRUE_ICON_CLASS);
			}
		}
		return trueIconClassModel.getObject();
	}
	<commit_msg>Note à moi-même : un model ça se détache...<commit_after>package fr.openwide.core.wicket.more.markup.html.image;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;

import fr.openwide.core.wicket.behavior.ClassAttributeAppender;

/**
 * Container affichant une icône selon que le model soit true ou false.
 * Par défaut un carré coché ou non avec possibilité de personnaliser.
 */
public class BooleanIcon extends WebMarkupContainer {

	private static final long serialVersionUID = 6413843086031282399L;
	
	private static final String DEFAULT_TRUE_ICON_CLASS = "fa fa-check-square-o fa-fw";
	private static final String DEFAULT_FALSE_ICON_CLASS = "fa fa-square-o fa-fw";
	
	private static final String DEFAULT_TRUE_HIDE_IF_NULL_OR_FALSE_ICON_CLASS = "fa fa-check fa-fw";
	
	private static final String BOOLEAN_ICON_BASE_CLASS = "boolean-icon";
	
	private IModel<Boolean> booleanModel;
	
	private IModel<String> trueIconClassModel;
	
	private IModel<String> falseIconClassModel;
	
	private boolean hideIfNullOrFalse = false;
	
	public BooleanIcon(String id, IModel<Boolean> booleanModel) {
		this(id, booleanModel, null, null);
	}
	
	public BooleanIcon(String id, IModel<Boolean> booleanModel,
			IModel<String> trueIconClassModel, IModel<String> falseIconClassModel) {
		super(id, booleanModel);
		this.booleanModel = booleanModel;
		this.trueIconClassModel = trueIconClassModel;
		this.falseIconClassModel = falseIconClassModel;
		
		add(new ClassAttributeAppender(BOOLEAN_ICON_BASE_CLASS));
		add(new ClassAttributeAppender(new LoadableDetachableModel<String>() {
			private static final long serialVersionUID = 1L;
			
			@Override
			protected String load() {
				return Boolean.TRUE.equals(getBooleanModelObject()) ? getTrueIconClass() : getFalseIconClass();
			}
		}));
	}
	
	private Boolean getBooleanModelObject() {
		return booleanModel != null ? booleanModel.getObject() : null;
	}
	
	private String getTrueIconClass() {
		if (trueIconClassModel == null) {
			if (hideIfNullOrFalse) {
				trueIconClassModel = new Model<String>(DEFAULT_TRUE_HIDE_IF_NULL_OR_FALSE_ICON_CLASS);
			} else {
				trueIconClassModel = new Model<String>(DEFAULT_TRUE_ICON_CLASS);
			}
		}
		return trueIconClassModel.getObject();
	}
	
{'fraction_non_alphanumeric': 0.08892689183411714, 'fraction_numerical': 0.008550662676357419, 'mean_word_length': 5.322972972972973, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6006165', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1448, 'n_words': 324}