001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messageboards.model.impl;
022:
023: import com.liferay.portlet.messageboards.model.MBCategory;
024: import com.liferay.portlet.messageboards.model.MBMessage;
025: import com.liferay.portlet.messageboards.model.MBMessageDisplay;
026: import com.liferay.portlet.messageboards.model.MBThread;
027: import com.liferay.portlet.messageboards.model.MBTreeWalker;
028:
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: /**
033: * <a href="MBMessageDisplayImpl.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Brian Wing Shun Chan
036: *
037: */
038: public class MBMessageDisplayImpl implements MBMessageDisplay {
039:
040: public MBMessageDisplayImpl(MBMessage message,
041: MBMessage parentMessage, MBCategory category,
042: MBThread thread, MBTreeWalker treeWalker,
043: MBThread previousThread, MBThread nextThread,
044: MBThread firstThread, MBThread lastThread) {
045:
046: _message = message;
047: _parentMessage = parentMessage;
048: _category = category;
049: _thread = thread;
050: _treeWalker = new MBTreeWalkerImpl(message);
051: _previousThread = previousThread;
052: _nextThread = nextThread;
053: _firstThread = firstThread;
054: _lastThread = lastThread;
055:
056: List orderedMessages = new ArrayList();
057:
058: _orderMessages(_treeWalker, treeWalker.getRoot(),
059: orderedMessages);
060:
061: for (int i = 0; i < orderedMessages.size(); i++) {
062: MBMessage curMessage = (MBMessage) orderedMessages.get(i);
063:
064: if (i == 0) {
065: _firstMessage = curMessage;
066: }
067:
068: if (curMessage.equals(message)) {
069: if ((i - 1) >= 0) {
070: _previousMessage = (MBMessage) orderedMessages
071: .get(i - 1);
072: }
073:
074: if ((i + 1) < orderedMessages.size()) {
075: _nextMessage = (MBMessage) orderedMessages
076: .get(i + 1);
077: }
078: }
079:
080: if ((i + 1) == orderedMessages.size()) {
081: _lastMessage = curMessage;
082: }
083: }
084: }
085:
086: public MBMessage getMessage() {
087: return _message;
088: }
089:
090: public MBMessage getParentMessage() {
091: return _parentMessage;
092: }
093:
094: public MBCategory getCategory() {
095: return _category;
096: }
097:
098: public MBThread getThread() {
099: return _thread;
100: }
101:
102: public MBTreeWalker getTreeWalker() {
103: return _treeWalker;
104: }
105:
106: public MBThread getPreviousThread() {
107: return _previousThread;
108: }
109:
110: public MBThread getNextThread() {
111: return _nextThread;
112: }
113:
114: public MBThread getFirstThread() {
115: return _firstThread;
116: }
117:
118: public MBThread getLastThread() {
119: return _lastThread;
120: }
121:
122: public boolean isFirstThread() {
123: if (_firstThread == null) {
124: return false;
125: } else if (_firstThread.equals(_thread)) {
126: return true;
127: } else {
128: return false;
129: }
130: }
131:
132: public boolean isLastThread() {
133: if (_lastThread == null) {
134: return false;
135: } else if (_lastThread.equals(_thread)) {
136: return true;
137: } else {
138: return false;
139: }
140: }
141:
142: public MBMessage getPreviousMessage() {
143: return _previousMessage;
144: }
145:
146: public MBMessage getNextMessage() {
147: return _nextMessage;
148: }
149:
150: public MBMessage getFirstMessage() {
151: return _firstMessage;
152: }
153:
154: public MBMessage getLastMessage() {
155: return _lastMessage;
156: }
157:
158: public boolean isFirstMessage() {
159: if (_firstMessage == null) {
160: return false;
161: } else if (_firstMessage.equals(_message)) {
162: return true;
163: } else {
164: return false;
165: }
166: }
167:
168: public boolean isLastMessage() {
169: if (_lastMessage == null) {
170: return false;
171: } else if (_lastMessage.equals(_message)) {
172: return true;
173: } else {
174: return false;
175: }
176: }
177:
178: private void _orderMessages(MBTreeWalker treeWalker,
179: MBMessage message, List orderedMessages) {
180:
181: orderedMessages.add(message);
182:
183: List messages = treeWalker.getMessages();
184: int[] range = treeWalker.getChildrenRange(message);
185:
186: for (int i = range[0]; i < range[1]; i++) {
187: MBMessage curMessage = (MBMessage) messages.get(i);
188:
189: _orderMessages(treeWalker, curMessage, orderedMessages);
190: }
191: }
192:
193: private MBMessage _message;
194: private MBMessage _parentMessage;
195: private MBCategory _category;
196: private MBThread _thread;
197: private MBTreeWalker _treeWalker;
198: private MBThread _previousThread;
199: private MBThread _nextThread;
200: private MBThread _firstThread;
201: private MBThread _lastThread;
202: private MBMessage _previousMessage;
203: private MBMessage _nextMessage;
204: private MBMessage _firstMessage;
205: private MBMessage _lastMessage;
206:
207: }
|