001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package echo2example.chatclient;
031:
032: import java.text.DateFormat;
033:
034: import nextapp.echo2.app.Column;
035: import nextapp.echo2.app.ContentPane;
036: import nextapp.echo2.app.Extent;
037: import nextapp.echo2.app.Label;
038: import nextapp.echo2.app.Row;
039:
040: /**
041: * A <code>ContentPane</code> which displays the messages which have been posted
042: * in the chat.
043: */
044: public class MessagePane extends ContentPane {
045:
046: /**
047: * <code>DateFormat</code> object to render post times.
048: */
049: private static final DateFormat dateFormat = DateFormat
050: .getTimeInstance(DateFormat.SHORT);
051:
052: /**
053: * Maximum number of messages to display simultaneously.
054: */
055: private static final int MAX_MESSAGE_COUNT = 100;
056:
057: /**
058: * Extent specifying the 'scroll-to-bottom' position (-1px).
059: */
060: private static final Extent BOTTOM = new Extent(-1, Extent.PX);
061:
062: private Column listColumn;
063: private String userName;
064:
065: /**
066: * Creates a new <code>MessagePane</code>.
067: */
068: public MessagePane() {
069: super ();
070: listColumn = new Column();
071: listColumn.setStyleName("MessagePane.ListColumn");
072: add(listColumn);
073: userName = ChatApp.getApp().getUserName();
074: }
075:
076: /**
077: * Updates the state of the <code>MessagePane</code> based on new messages
078: * by querying the <code>ChatApp</code> for new messages.
079: */
080: public void update() {
081: ChatApp app = ChatApp.getApp();
082: ChatSession.Message[] messages = app.getNewMessages();
083: Label label;
084:
085: for (int i = 0; i < messages.length; ++i) {
086: boolean announcement = messages[i].getUserName() == null;
087: Row row = new Row();
088:
089: row
090: .setStyleName(announcement ? "MessagePane.MessageRow.Announcement"
091: : "MessagePane.MessageRow");
092:
093: label = new Label(dateFormat.format(messages[i].getDate()));
094: label.setStyleName("MessagePane.DateLabel");
095: row.add(label);
096:
097: if (announcement) {
098: label = new Label(messages[i].getContent());
099: label.setStyleName("MessagePane.SystemMessageLabel");
100: row.add(label);
101: } else {
102: label = new Label(messages[i].getUserName());
103: label
104: .setStyleName(userName.equals(messages[i]
105: .getUserName()) ? "MessagePane.UserNameLabel.Yourself"
106: : "MessagePane.UserNameLabel.Other");
107: row.add(label);
108:
109: label = new Label(messages[i].getContent());
110: row.add(label);
111: }
112:
113: listColumn.add(row);
114: }
115:
116: // Remove leading messages greater than MAX_MESSAGE_COUNT.
117: int componentCount = listColumn.getComponentCount();
118: for (int i = MAX_MESSAGE_COUNT; i < componentCount; ++i) {
119: listColumn.remove(0);
120: }
121:
122: setVerticalScroll(BOTTOM);
123: }
124: }
|