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


fiftyfive.wicket.util
Class Shortcuts

java.lang.Object
  extended by fiftyfive.wicket.util.Shortcuts

public class Shortcuts
extends Object

Helper methods that greatly reduce the amount of boilerplate code needed for common Wicket tasks. Consider adding this in the Java file of your wicket page or component:

 import static fiftyfive.wicket.util.Shortcuts.*;

Author:
Matt Brictson

Method Summary
static IBehavior afterTag(String textToAppend)
          Appends the specified text after the closing tag of the component it decorates.
static IBehavior cssClass(IModel<String> cssClass)
          Adds a CSS class to the component it decorates.
static IBehavior cssClass(String cssClass)
          Adds a CSS class to the component it decorates.
static IBehavior cssConditionalResource(String cond, String filename)
          Creates a header contributor that adds a <link> to an Internet Explorer conditional stylesheet CSS file with the specified name, relative to the current application class.
static IBehavior cssPrintResource(String filename)
          Creates a header contributor that adds a <link> to a print stylesheet CSS file with the specified name, relative to the current application class.
static IBehavior cssResource(Class<?> cls)
          Creates a header contributor that adds a <link> to a CSS file with the same name as the specified class.
static IBehavior cssResource(Class<?> scope, String filename)
          Creates a header contributor that adds a <link> to a CSS file with the specified name, relative to the given class.
static IBehavior cssResource(String filename)
          Creates a header contributor that adds a <link> to a CSS file with the specified name, relative to the current application class.
static boolean empty(Object obj)
          Return true if the object is null, zero-length (if array or collection) false (if a boolean) or blank (if a string).
static LabelWithPlaceholder label(String id)
          Shortcut for creating a LabelWithPlaceholder with an implied model.
static LabelWithPlaceholder label(String id, Object bean, String propertyExpr)
          Shortcut for creating a LabelWithPlaceholder with a PropertyModel.
static LabelWithPlaceholder label(String id, Serializable valueOrIModel)
          Shortcut for creating a LabelWithPlaceholder with a hardcoded value or a custom model.
static LoadableDetachableModel loadedModel(Object bean, String loadMethod)
          Creates a model that loads itself by exeucting the specified method on a bean.
static PropertyModel prop(Object bean, String propertyExpr)
          Shortcut for creating a PropertyModel.
static IBehavior toggledCssClass(String classIfTrue, IModel<Boolean> toggle)
          Adds classIfTrue to the HTML class attribute of the component if the toggle value is true.
static IBehavior toggledCssClass(String classIfTrue, String classIfFalse, IModel<Boolean> toggle)
          Adds classIfTrue to the HTML class attribute of the component if the toggle value is true.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

prop

public static PropertyModel prop(Object bean,
                                 String propertyExpr)
Shortcut for creating a PropertyModel. Equivalent to:
 new PropertyModel(bean, propertyExpr)

See Also:
PropertyModel

loadedModel

public static LoadableDetachableModel loadedModel(Object bean,
                                                  String loadMethod)
Creates a model that loads itself by exeucting the specified method on a bean. The return value of that method will be cached as the value of the model. When the model is detached, the cache will be discarded. This is useful for connecting your Wicket page to your back-end:
 public PersonDetailPage(PageParameters params)
 {
     super(params);
     setDefaultModel(loadedModel(this, "loadPerson"));
 }
 private Person loadPerson()
 {
     return _personService.loadPerson(getPageParameters().get("id"));
 }

Throws:
IllegalArgumentException - if the loadMethod does not exist, or takes more than zero arguments

label

public static LabelWithPlaceholder label(String id)
Shortcut for creating a LabelWithPlaceholder with an implied model. Equivalent to:
 new LabelWithPlaceholder("name")

See Also:
LabelWithPlaceholder

label

public static LabelWithPlaceholder label(String id,
                                         Object bean,
                                         String propertyExpr)
Shortcut for creating a LabelWithPlaceholder with a PropertyModel. Equivalent to:
 new LabelWithPlaceholder("name", new PropertyModel(person, "fullName"))

See Also:
LabelWithPlaceholder

label

public static LabelWithPlaceholder label(String id,
                                         Serializable valueOrIModel)
Shortcut for creating a LabelWithPlaceholder with a hardcoded value or a custom model. Equivalent to:
 new LabelWithPlaceholder("name", "Hardcoded string value")

See Also:
LabelWithPlaceholder

afterTag

public static IBehavior afterTag(String textToAppend)
Appends the specified text after the closing tag of the component it decorates. The text will not be escaped to be safe HTML, so take care to escape the string first if necessary.

This shortcut is very useful for doing comma separated lists. For example:

 add(new ListView("list", myList) {
     @Override protected void populateItem(ListItem item)
     {
         if(item.getIndex() < getList().size() - 1)
         {
             item.add(afterTag(", "));
         }
     }
 });


cssClass

public static IBehavior cssClass(IModel<String> cssClass)
Adds a CSS class to the component it decorates. Equivalent to:
 new AttributeAppender("class", true, cssClass, " ");

Since:
2.0

cssClass

public static IBehavior cssClass(String cssClass)
Adds a CSS class to the component it decorates. Equivalent to:
 new AttributeAppender("class", true, new Model(cssClass), " ");


toggledCssClass

public static IBehavior toggledCssClass(String classIfTrue,
                                        IModel<Boolean> toggle)
Adds classIfTrue to the HTML class attribute of the component if the toggle value is true. If the toggle value is false, ensure that the class is removed (in case it was already present in the markup).

For example, let's say your HTML file contains the following markup:

 <span wicket:id="message" class="severe">blah blah</span>
You've added a behavior like so:
 add(new MyMessageComponent("message")
     .add(toggledCssClass("severe", isSevereModel)));
If the isSevereModel model returns true, the component will render as:
 <span class="severe">...
If false, then:
 <span class="">...

Since:
2.0.4

toggledCssClass

public static IBehavior toggledCssClass(String classIfTrue,
                                        String classIfFalse,
                                        IModel<Boolean> toggle)
Adds classIfTrue to the HTML class attribute of the component if the toggle value is true. If the toggle value is false, adds classIfFalse to the HTML instead. In both cases, if these classes already exist in the markup, they will first be removed.

For example, let's say your HTML file contains the following markup:

 <span wicket:id="message" class="high-priority">blah blah</span>
You've added a behavior like so:
 add(new MyMessageComponent("message")
     .add(toggledCssClass("high-priority", "low-priority", isHighPriorityModel)));
If the isHighPriorityModel model returns true, the component will render as:
 <span class="high-priority">...
If false, then:
 <span class="low-priority">...

Since:
2.0.4

cssResource

public static IBehavior cssResource(Class<?> cls)
Creates a header contributor that adds a <link> to a CSS file with the same name as the specified class. For example:
 add(cssResource(MyPanel.class));

will add a <link> to the <head> for MyPanel.css, found in the same classpath location as MyPanel.class.

This is equivalent to:

 CSSPackageResource.getHeaderContribution(
     new CompressedResourceReference(
         cls, Classes.simpleName(cls) + ".css"
     )
 );

Since:
2.0

cssResource

public static IBehavior cssResource(String filename)
Creates a header contributor that adds a <link> to a CSS file with the specified name, relative to the current application class. For example:
 add(cssResource("screen.css"));

will add a <link> to the <head> for screen.css, found in the same classpath location as your wicket application class.

This is equivalent to:

 CSSPackageResource.getHeaderContribution(
     new CompressedResourceReference(
         Application.get().getClass(), "screen.css"
     )
 );

Since:
2.0

cssResource

public static IBehavior cssResource(Class<?> scope,
                                    String filename)
Creates a header contributor that adds a <link> to a CSS file with the specified name, relative to the given class. For example:
 add(cssResource(BasePage.class, "screen.css"));

will add a <link> to the <head> for screen.css, found in the same classpath location as BasePage.

This is equivalent to:

 CSSPackageResource.getHeaderContribution(
     new CompressedResourceReference(
         BasePage.class, "screen.css"
     )
 );

Since:
2.0

cssPrintResource

public static IBehavior cssPrintResource(String filename)
Creates a header contributor that adds a <link> to a print stylesheet CSS file with the specified name, relative to the current application class. For example:
 add(cssPrintResource("print.css"));

will add a <link> to the <head> for print.css, found in the same classpath location as your wicket application class. The <link> will have a print media type.

This is equivalent to:

 CSSPackageResource.getHeaderContribution(
     new CompressedResourceReference(
         Application.get().getClass(), "print.css"
     ),
     "print"
 );

Since:
2.0

cssConditionalResource

public static IBehavior cssConditionalResource(String cond,
                                               String filename)
Creates a header contributor that adds a <link> to an Internet Explorer conditional stylesheet CSS file with the specified name, relative to the current application class. The stylesheet will apply based on the IE condition argument. For example:
 add(cssConditionalResource("IE 7", "ie-7.css"));

will add a <link> to the <head> for ie-7.css, found in the same classpath location as your wicket application class. The stylesheet will only be loaded in IE 7 browsers.

This is equivalent to:

 InternetExplorerCss.getConditionalHeaderContribution(
     "IE 7"
     new CompressedResourceReference(
         Application.get().getClass(), "ie-7.css")
     )
 );

Since:
2.0

empty

public static boolean empty(Object obj)
Return true if the object is null, zero-length (if array or collection) false (if a boolean) or blank (if a string). Examples:
ObjectEmpty?
"foo"false
""true
" "true
nulltrue
Collections.EMPTY_LISTtrue
Collections.singletonList("foo")false
new String[0]true
new String[] { "foo" }false
truefalse
falsetrue



Copyright © 2011 55 Minutes. All Rights Reserved.