This documentation is for an old version of fiftyfive-wicket (2.0.9).


fiftyfive.wicket.model
Class CountMessageModel

java.lang.Object
  extended by org.apache.wicket.model.AbstractReadOnlyModel<String>
      extended by fiftyfive.wicket.model.CountMessageModel
All Implemented Interfaces:
Serializable, IClusterable, IDetachable, IModel<String>

public class CountMessageModel
extends AbstractReadOnlyModel<String>

Selects an appropriate localized string based on whether a count value is zero, one or greater than one. This makes it easier to produce singluar and plural phrases.

For example, consider the case where we would like to set one of these three feedback messages. Notice how the phrasing is slightly different depending on the number of items. Furthermore in the latter two examples we would like to embed a hyperlink to a bookmarkable page.

Normally in Wicket you would accomplish this using tedious if/else statements and string concatenation, conditionally visible fragments/containers, or the incredibly arcane syntax of Java's ChoiceFormat (and note that you'd have to carefully escape the "<" and ">" if you want to include HTML markup as we need for the hyperlinks in this example). All of these approaches are clumsy, and it would be hard for a non-programmer to localize or edit the strings.

With CountMessageModel there is a much easier way: simply declare three separate localizable strings in the properties file of your page or panel.

 friends.zero = There are no friends in your network.
 friends.one = There is <a href="${href}">one friend</a> in your network.
 friends.many = There are <a href="${href}">${count} friends</a> in your network.

And your Java would be:

 new CountMessageModel("friends", pageOrPanelObject, numFriends)
     setLink(FriendsPage.class);

CountMessageModel selects the appropriate localized string depending on the value of numFriends, and subtitutes values for ${href} and ${count} automatically.

You can now use this model in a label (also consider CountLabel as a shortcut for this common use case), or call getObject() to get the interpolated string value for use in a feedback message.

Since:
2.0
See Also:
Serialized Form

Constructor Summary
CountMessageModel(String messageKey, Component component, IModel<? extends Number> count)
          Constructs a CountMessageModel that will choose the appropriate localized string from a properties file.
 
Method Summary
 void detach()
          Detaches the count model passed into the constructor and other internal state.
 int getCount()
          Returns the current value of the count model.
 CharSequence getHref()
          Returns the URL to the bookmarkable page specified in a previous call to setLink().
 String getObject()
          Returns the message after all interpolation has been performed.
 String getResourceSuffix()
          Returns either ".zero", ".one", or ".many" depending on the current value of the count model.
 CountMessageModel setLink(Class<? extends Page> page)
          Sets the bookmarkable page that will be used to generate a link within the message.
 CountMessageModel setLink(Class<? extends Page> page, PageParameters params)
          Sets the bookmarkable page class and page parameters that will be used to generate a link within the message.
 
Methods inherited from class org.apache.wicket.model.AbstractReadOnlyModel
getObject, setObject, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CountMessageModel

public CountMessageModel(String messageKey,
                         Component component,
                         IModel<? extends Number> count)
Constructs a CountMessageModel that will choose the appropriate localized string from a properties file.

Parameters:
messageKey - The key that will be consulted in the properties file. The key will have ".zero", ".one" or ".many" appended to it depending on the value of the count.
component - The Wicket component that will be used for finding the properties file. Usually this is the page or panel where you plan to use the model.
count - The number that will be used in the message. This will be used to substitute any ${count} expressions in the message.
Method Detail

detach

public void detach()
Detaches the count model passed into the constructor and other internal state.

Specified by:
detach in interface IDetachable
Overrides:
detach in class AbstractReadOnlyModel<String>

getObject

public String getObject()
Returns the message after all interpolation has been performed.

Specified by:
getObject in interface IModel<String>
Specified by:
getObject in class AbstractReadOnlyModel<String>

setLink

public CountMessageModel setLink(Class<? extends Page> page)
Sets the bookmarkable page that will be used to generate a link within the message. A special ${href} variable will be exposed for use within the message.

Returns:
this to allow chaining

setLink

public CountMessageModel setLink(Class<? extends Page> page,
                                 PageParameters params)
Sets the bookmarkable page class and page parameters that will be used to generate a link within the message. A special ${href} variable will be exposed for use within the message.

Returns:
this to allow chaining

getCount

public int getCount()
Returns the current value of the count model. This is exposed as ${count} within the message.


getHref

public CharSequence getHref()
Returns the URL to the bookmarkable page specified in a previous call to setLink(). This is exposed as ${href} within the message. If setLink() was not called, then this value will be null.


getResourceSuffix

public String getResourceSuffix()
Returns either ".zero", ".one", or ".many" depending on the current value of the count model.



Copyright © 2011 55 Minutes. All Rights Reserved.