/** * Copyright 2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.bricket.web; import java.io.Serializable; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.apache.wicket.Component; import org.apache.wicket.feedback.FeedbackMessage; import org.apache.wicket.feedback.FeedbackMessagesModel; import org.apache.wicket.feedback.IFeedback; import org.apache.wicket.feedback.IFeedbackMessageFilter; import org.apache.wicket.markup.html.CSSPackageResource; import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.list.ListItem; import org.apache.wicket.markup.html.list.ListView; import org.apache.wicket.markup.html.panel.Panel; import org.odlabs.wiquery.ui.themes.ThemeUiHelper; import org.odlabs.wiquery.ui.themes.UiIcon; public class BricketFeedbackPanel extends Panel implements IFeedback { /** * List for messages. */ private final class MessageListView extends ListView<FeedbackMessage> { private static final long serialVersionUID = 1L; /** * @see org.apache.wicket.Component#Component(String) */ public MessageListView(final String id) { super(id); setDefaultModel(newFeedbackMessagesModel()); } /** * @see org.apache.wicket.markup.html.list.ListView#populateItem(org.apache.wicket.markup.html.list.ListItem) */ @Override protected void populateItem(final ListItem<FeedbackMessage> listItem) { final FeedbackMessage message = listItem.getModelObject(); message.markRendered(); WebMarkupContainer conatiner = getMessageContainer(message); conatiner.add(getIcon(message)); conatiner.add(newMessageDisplayComponent("message", message)); listItem.add(conatiner); } } /** Message view */ private final MessageListView messageListView; /** * @see org.apache.wicket.Component#Component(String) */ public BricketFeedbackPanel(String id) { this(id, null); } /** * @see org.apache.wicket.Component#Component(String) * * @param id * @param filter */ public BricketFeedbackPanel(String id, IFeedbackMessageFilter filter) { super(id); add(CSSPackageResource.getHeaderContribution(BricketFeedbackPanel.class, "BricketFeedbackPanel.css")); WebMarkupContainer messagesContainer = new WebMarkupContainer("feedbackul") { @Override public boolean isVisible() { return anyMessage(); } }; add(messagesContainer); messageListView = new MessageListView("messages"); messageListView.setVersioned(false); messagesContainer.add(messageListView); if (filter != null) { setFilter(filter); } } /** * Search messages that this panel will render, and see if there is any * message of level ERROR or up. This is a convenience method; same as * calling 'anyMessage(FeedbackMessage.ERROR)'. * * @return whether there is any message for this panel of level ERROR or up */ public final boolean anyErrorMessage() { return anyMessage(FeedbackMessage.ERROR); } /** * Search messages that this panel will render, and see if there is any * message. * * @return whether there is any message for this panel */ public final boolean anyMessage() { return anyMessage(FeedbackMessage.UNDEFINED); } /** * Search messages that this panel will render, and see if there is any * message of the given level. * * @param level * the level, see FeedbackMessage * @return whether there is any message for this panel of the given level */ public final boolean anyMessage(int level) { List<FeedbackMessage> msgs = getCurrentMessages(); for (FeedbackMessage msg : msgs) { if (msg.isLevel(level)) { return true; } } return false; } /** * @return Model for feedback messages on which you can install filters and * other properties */ public final FeedbackMessagesModel getFeedbackMessagesModel() { return (FeedbackMessagesModel) messageListView.getDefaultModel(); } /** * @return The current message filter */ public final IFeedbackMessageFilter getFilter() { return getFeedbackMessagesModel().getFilter(); } /** * @return The current sorting comparator */ public final Comparator<FeedbackMessage> getSortingComparator() { return getFeedbackMessagesModel().getSortingComparator(); } /** * @see org.apache.wicket.Component#isVersioned() */ @Override public boolean isVersioned() { return false; } /** * Sets a filter to use on the feedback messages model * * @param filter * The message filter to install on the feedback messages model * * @return FeedbackPanel this. */ public final BricketFeedbackPanel setFilter(IFeedbackMessageFilter filter) { getFeedbackMessagesModel().setFilter(filter); return this; } /** * @param maxMessages * The maximum number of feedback messages that this feedback * panel should show at one time * * @return FeedbackPanel this. */ public final BricketFeedbackPanel setMaxMessages(int maxMessages) { messageListView.setViewSize(maxMessages); return this; } /** * Sets the comparator used for sorting the messages. * * @param sortingComparator * comparator used for sorting the messages. * * @return FeedbackPanel this. */ public final BricketFeedbackPanel setSortingComparator(Comparator<FeedbackMessage> sortingComparator) { getFeedbackMessagesModel().setSortingComparator(sortingComparator); return this; } protected WebMarkupContainer getMessageContainer(final FeedbackMessage message) { WebMarkupContainer container = new WebMarkupContainer("container"); if (message.getLevel() == FeedbackMessage.ERROR || message.getLevel() == FeedbackMessage.FATAL) { ThemeUiHelper.errorText(container); } else { ThemeUiHelper.highlightedText(container); } return container; } protected Component getIcon(final FeedbackMessage message) { WebMarkupContainer icon = new WebMarkupContainer("icon"); if (message.getLevel() == FeedbackMessage.ERROR || message.getLevel() == FeedbackMessage.FATAL) { ThemeUiHelper.iconComponent(icon, UiIcon.ALERT); } else { ThemeUiHelper.iconComponent(icon, UiIcon.INFO); } return icon; } /** * Gets the currently collected messages for this panel. * * @return the currently collected messages for this panel, possibly empty */ protected final List<FeedbackMessage> getCurrentMessages() { final List<FeedbackMessage> messages = messageListView.getModelObject(); return Collections.unmodifiableList(messages); } /** * Gets a new instance of FeedbackMessagesModel to use. * * @return Instance of FeedbackMessagesModel to use */ protected FeedbackMessagesModel newFeedbackMessagesModel() { return new FeedbackMessagesModel(this); } /** * Generates a component that is used to display the message inside the * feedback panel. This component must handle being attached to * <code>span</code> tags. * * By default a {@link Label} is used. * * Note that the created component is expected to respect feedback panel's * {@link #getEscapeModelStrings()} value * * @param id * parent id * @param message * feedback message * @return component used to display the message */ protected Label newMessageDisplayComponent(String id, FeedbackMessage message) { Serializable serializable = message.getMessage(); Label label = new Label(id, (serializable == null) ? "" : serializable.toString()); label.setEscapeModelStrings(BricketFeedbackPanel.this.getEscapeModelStrings()); return label; } }