001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.feedback;
018:
019: import java.io.Serializable;
020: import java.util.Collections;
021: import java.util.Comparator;
022: import java.util.List;
023:
024: import org.apache.wicket.Component;
025: import org.apache.wicket.Page;
026: import org.apache.wicket.Session;
027: import org.apache.wicket.model.IModel;
028:
029: /**
030: * Model for extracting feedback messages.
031: *
032: * @author Eelco Hillenius
033: */
034: public class FeedbackMessagesModel implements IModel {
035: private static final long serialVersionUID = 1L;
036:
037: /** Message filter */
038: private IFeedbackMessageFilter filter;
039:
040: /** Lazy loaded, temporary list. */
041: private transient List messages;
042:
043: /** Comparator used for sorting the messages. */
044: private Comparator sortingComparator;
045:
046: /**
047: * Constructor. Creates a model for all feedback messages on the page.
048: *
049: * @param component
050: * The component where the page will be get from for which
051: * messages will be displayed usually the same page as the one
052: * feedbackpanel is attached to
053: */
054: public FeedbackMessagesModel(Component component) {
055: if (component == null) {
056: throw new IllegalArgumentException(
057: "Argument [[page]] cannot be null");
058: }
059: }
060:
061: /**
062: * Constructor. Creates a model for all feedback messags accepted by the
063: * given filter.
064: *
065: * @param filter
066: * The filter to apply
067: * @param page
068: * Page for which messages will be displayed - usually the same
069: * page as the one feedbackpanel is attached to
070: *
071: */
072: public FeedbackMessagesModel(Page page,
073: IFeedbackMessageFilter filter) {
074: this (page);
075: setFilter(filter);
076: }
077:
078: /**
079: * @return The current message filter
080: */
081: public final IFeedbackMessageFilter getFilter() {
082: return filter;
083: }
084:
085: /**
086: * @return The current sorting comparator
087: */
088: public final Comparator getSortingComparator() {
089: return sortingComparator;
090: }
091:
092: /**
093: * @see org.apache.wicket.model.IModel#getObject()
094: */
095: public final Object getObject() {
096: if (messages == null) {
097: // Get filtered messages from page where component lives
098: messages = Session.get().getFeedbackMessages().messages(
099: filter);
100:
101: // Sort the list before returning it
102: if (sortingComparator != null) {
103: Collections.sort(messages, sortingComparator);
104: }
105:
106: // Let subclass do any extra processing it wants to on the messages.
107: // It may want to do something special, such as removing a given
108: // message under some special condition or perhaps eliminate
109: // duplicate messages. It could even add a message under certain
110: // conditions.
111: messages = processMessages(messages);
112: }
113: return messages;
114: }
115:
116: /**
117: * @param filter
118: * Filter to apply to model
119: * @return this
120: */
121: public final FeedbackMessagesModel setFilter(
122: IFeedbackMessageFilter filter) {
123: this .filter = filter;
124: return this ;
125: }
126:
127: /**
128: * Sets the comparator used for sorting the messages.
129: *
130: * @param sortingComparator
131: * comparator used for sorting the messages
132: * @return this
133: */
134: public final FeedbackMessagesModel setSortingComparator(
135: Comparator sortingComparator) {
136: if (!(sortingComparator instanceof Serializable)) {
137: throw new IllegalArgumentException(
138: "sortingComparator must be serializable");
139: }
140: this .sortingComparator = sortingComparator;
141: return this ;
142: }
143:
144: /**
145: * Override this method to post process to the FeedbackMessage list.
146: *
147: * @param messages
148: * List of sorted and filtered FeedbackMessages for further
149: * processing
150: * @return The processed FeedbackMessage list
151: */
152: protected List processMessages(final List messages) {
153: return messages;
154: }
155:
156: /**
157: *
158: * @see org.apache.wicket.model.IModel#setObject(java.lang.Object)
159: */
160: public void setObject(Object object) {
161: }
162:
163: /**
164: *
165: * @see org.apache.wicket.model.IDetachable#detach()
166: */
167: public void detach() {
168: messages = null;
169: }
170: }
|