package org.apache.wicket.examples.library;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.Localizer;
import org.apache.wicket.examples.library.Book.WritingStyle;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringList;
import org.apache.wicket.util.string.StringValueConversionException;
@author
public final class BookDetails extends AuthenticatedWebPage
{
@param
@throws
public BookDetails(final PageParameters parameters) throws StringValueConversionException
{
this(Book.get(parameters.get("id").toLong()));
}
@param
public BookDetails(final Book book)
{
add(new Label("title", book.getTitle()));
add(new Label("author", book.getAuthor()));
add(new Label("fiction", Boolean.toString(book.getFiction())));
Localizer localizer = getLocalizer();
add(BookDetails.link("companion", book.getCompanionBook(),
localizer.getString("noBookTitle", this)));
add(BookDetails.link("related", book.getRelatedBook(),
localizer.getString("noBookTitle", this)));
String writingStyles;
final boolean hasStyles = (book.getWritingStyles() != null) &&
(book.getWritingStyles().size() > 0);
if (hasStyles)
{
StringList styles = new StringList();
for (WritingStyle style : book.getWritingStyles())
{
styles.add(localizer.getString(style.toString(), this));
}
writingStyles = styles.toString();
}
else
{
writingStyles = localizer.getString("noWritingStyles", this);
}
Label writingStylesLabel = new Label("writingStyles", writingStyles);
final AttributeModifier italic = new AttributeModifier("class", new Model<>("italic"))
{
@Override
public boolean isEnabled(Component component)
{
return !hasStyles;
}
};
add(writingStylesLabel.add(italic));
add(EditBook.link("edit", book.getId()));
}
@param
@param
@param
@return
public static BookmarkablePageLink<Void> link(final String name, final Book book,
final String noBookTitle)
{
final BookmarkablePageLink<Void> link = new BookmarkablePageLink<>(name,
BookDetails.class);
if (book != null)
{
link.getPageParameters().add("id", book.getId());
link.add(new Label("title", new Model<>(book)));
}
else
{
link.add(new Label("title", noBookTitle));
link.setEnabled(false);
}
return link;
}
}