Class Behavior

java.lang.Object
org.apache.wicket.behavior.Behavior
All Implemented Interfaces:
Serializable, IComponentAwareEventSink, IComponentAwareHeaderContributor, IClusterable
Direct Known Subclasses:
AbstractAjaxBehavior, AbstractFormValidator, AbstractRangeValidator, AbstractTransformerBehavior, AjaxFormValidatingBehavior, AjaxIndicatorAppender, AttributeModifier, BaseWebSocketBehavior, BorderBehavior, CompoundValidator, ContextPathGenerator, DefaultTheme, DefaultTheme, DisabledAttributeLinkBehavior, FormComponentUpdatingBehavior, FormValidatorAdapter, HTML5Attributes, HumanTheme, NodeBorder, OutputMarkupContainerClassNameBehavior, PropertyValidator, TrapFocusBehavior, ValidatorAdapter, VelocityContributor, WicketMessageTagHandler.AttributeLocalizer, WindowsTheme

Behaviors are kind of plug-ins for Components. They allow functionality to be added to a component and get essential events forwarded by the component. They can be bound to a concrete component (using the bind method which is called when the behavior is attached), but they don't need to. They can modify the components markup by changing the rendered ComponentTag. Behaviors can have their own models as well, and they are notified when these are to be detached by the component.

You also cannot modify a components model with a behavior.

Author:
Ralf Ebert, Eelco Hillenius, Igor Vaynberg (ivaynberg)
See Also:
  • Constructor Details

    • Behavior

      public Behavior()
      Constructor
  • Method Details

    • beforeRender

      public void beforeRender(Component component)
      Called when a component is about to render.
      Parameters:
      component - the component that has this behavior coupled
    • afterRender

      public void afterRender(Component component)
      Called when a component that has this behavior coupled was rendered.
      Parameters:
      component - the component that has this behavior coupled
    • bind

      public void bind(Component component)
      Bind this handler to the given component. This method is called by the host component immediately after this behavior is added to it. This method is useful if you need to do initialization based on the component it is attached and you can't wait to do it at render time. Keep in mind that if you decide to keep a reference to the host component, it is not thread safe anymore, and should thus only be used in situations where you do not reuse the behavior for multiple components.
      Parameters:
      component - the component to bind to
    • unbind

      public void unbind(Component component)
      Notifies the behavior it is removed from the specified component
      Parameters:
      component - the component this behavior is unbound from
    • detach

      public void detach(Component component)
      Allows the behavior to detach any state it has attached during request processing.
      Parameters:
      component - the component that initiates the detachment of this behavior
    • onException

      public void onException(Component component, RuntimeException exception)
      In case an unexpected exception happened anywhere between onComponentTag(org.apache.wicket.Component, org.apache.wicket.markup.ComponentTag) and afterRender(org.apache.wicket.Component), onException() will be called for any behavior. Typically, if you clean up resources in afterRender(Component), you should do the same in the implementation of this method.
      Parameters:
      component - the component that has a reference to this behavior and during which processing the exception occurred
      exception - the unexpected exception
    • getStatelessHint

      public boolean getStatelessHint(Component component)
      This method returns false if the behavior generates a callback url (for example ajax behaviors)
      Parameters:
      component - the component that has this behavior coupled.
      Returns:
      boolean true or false.
    • isEnabled

      public boolean isEnabled(Component component)
      Called when a components is rendering and wants to render this behavior. If false is returned this behavior will be ignored.
      Parameters:
      component - the component that has this behavior coupled
      Returns:
      true if this behavior must be executed/rendered
    • onComponentTag

      public void onComponentTag(Component component, ComponentTag tag)
      Called any time a component that has this behavior registered is rendering the component tag.
      Parameters:
      component - the component that renders this tag currently
      tag - the tag that is rendered
    • isTemporary

      public boolean isTemporary(Component component)
      Specifies whether or not this behavior is temporary. Temporary behaviors are removed at the end of request and never reattached. Such behaviors are useful for modifying component rendering only when it renders next. Usecases include javascript effects, initial clientside dom setup, etc.
      Parameters:
      component -
      Returns:
      true if this behavior is temporary
    • canCallListener

      public boolean canCallListener(Component component)
      Checks whether or not an IRequestListener can be invoked on this behavior. For further information please read the javadoc on Component.canCallListener(), this method has the same semantics. WARNING: Read the javadoc of Component.canCallListener() for important security-related information.
      Parameters:
      component - component this behavior is attached to
      Returns:
      true iff the listener method can be invoked
    • renderHead

      public void renderHead(Component component, IHeaderResponse response)
      Render to the web response whatever the component wants to contribute to the head section.
      Specified by:
      renderHead in interface IComponentAwareHeaderContributor
      Parameters:
      component -
      response - Response object
    • onConfigure

      public void onConfigure(Component component)
      Called immediately after the onConfigure method in a component. Since this is before the rendering cycle has begun, the behavior can modify the configuration of the component (i.e. setVisible(false))
      Parameters:
      component - the component being configured
    • onEvent

      public void onEvent(Component component, IEvent<?> event)
      Called to notify the behavior about any events sent to the component
      Specified by:
      onEvent in interface IComponentAwareEventSink
      Parameters:
      component - component that owns this sink. For example, if the implementation of this interface is a Behavior then component parameter will contain the component to which the behavior is attached.
      See Also:
    • onRemove

      public void onRemove(Component component)
      Called to notify that the component is being removed from its parent
      Parameters:
      component - the removed component
    • onTag

      public static Behavior onTag(org.danekja.java.util.function.serializable.SerializableBiConsumer<Component,ComponentTag> onTagConsumer)
      Creates a Behavior that uses the given SerializableConsumer consumer to do something with the component's tag.

      Usage:
      component.add(onTag(tag -> tag.put(key, value)));

      Parameters:
      onTagConsumer - the SerializableConsumer that accepts the ComponentTag
      Returns:
      The created behavior
    • onAttribute

      public static Behavior onAttribute(String name, org.danekja.java.util.function.serializable.SerializableFunction<String,CharSequence> onAttribute)
      Creates a Behavior that uses the given SerializableFunction function to do something with a component's attribute.

      Usage:
      component.add(onAttribute("class", currentValue -> condition(currentValue) ? "positive" : "negative"));

      Parameters:
      name - the name of the attribute to manipulate
      onAttribute - the SerializableFunction that accepts the old value of the attribute and returns a new value
      Returns:
      The created behavior