package com.worldturner.commons.wicket.googleanalytics; import org.apache.wicket.Component; import org.apache.wicket.MarkupContainer; import org.apache.wicket.Page; import org.apache.wicket.markup.html.panel.Panel; import org.apache.wicket.model.AbstractReadOnlyModel; import org.apache.wicket.model.IModel; import org.apache.wicket.model.Model; /** * An implementation of IModel that returns the class name of a component as its object. It will look up that * class name each time it is asked for it, and it can be different each time. It will query a MarkupContainer to find a * child component with a certain component id, and return the class name of that component. Any wicket component that * can hold other components is a MarkupContainer, including {@link Panel} and {@link Page}. *

* By using models to resolve both the markup container and the component id, there is a lot of flexibility and the * decision to ask which container for which comnponent id can be dynamically made just before the value of this model * is needed. * * @author Erwin Bolwidt (ebolwidt@worldturner.nl) */ public class ComponentClassNameModel extends AbstractReadOnlyModel { /** * A model that, when queried, will return the markup container that will be asked for a child component. */ private IModel containerModel; /** * A model that, when queried, will return the component id of the child within the markup container, that we are * interested in. */ private IModel componentIdModel; /** * Constructor. * * @param container * The markup container that will be asked for a child component. * @param componentId * The component id of the child within the markup container, that we are interested in. */ public ComponentClassNameModel(MarkupContainer container, String componentId) { this(new Model(container), new Model(componentId)); } /** * Constructor. * * @param container * A model that, when queried, will return the markup container that will be asked for a child component. * @param componentId * A model that, when queried, will return the component id of the child within the markup container, * that we are interested in. */ public ComponentClassNameModel(IModel containerModel, IModel componentIdModel) { this.containerModel = containerModel; this.componentIdModel = componentIdModel; } @Override public void detach() { super.detach(); if (containerModel != null) { containerModel.detach(); } if (componentIdModel != null) { componentIdModel.detach(); } } @Override public String getObject() { MarkupContainer container = containerModel.getObject(); String componentId = componentIdModel.getObject(); if (container == null || componentId == null) { return null; } Component component = container.get(componentId); if (component == null) { return null; } else { return component.getClass().getName(); } } }