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