KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > chatclient > MessagePane


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package echo2example.chatclient;
31
32 import java.text.DateFormat JavaDoc;
33
34 import nextapp.echo2.app.Column;
35 import nextapp.echo2.app.ContentPane;
36 import nextapp.echo2.app.Extent;
37 import nextapp.echo2.app.Label;
38 import nextapp.echo2.app.Row;
39
40 /**
41  * A <code>ContentPane</code> which displays the messages which have been posted
42  * in the chat.
43  */

44 public class MessagePane extends ContentPane {
45     
46     /**
47      * <code>DateFormat</code> object to render post times.
48      */

49     private static final DateFormat JavaDoc dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
50     
51     /**
52      * Maximum number of messages to display simultaneously.
53      */

54     private static final int MAX_MESSAGE_COUNT = 100;
55     
56     /**
57      * Extent specifying the 'scroll-to-bottom' position (-1px).
58      */

59     private static final Extent BOTTOM = new Extent(-1, Extent.PX);
60     
61     private Column listColumn;
62     private String JavaDoc userName;
63     
64     /**
65      * Creates a new <code>MessagePane</code>.
66      */

67     public MessagePane() {
68         super();
69         listColumn = new Column();
70         listColumn.setStyleName("MessagePane.ListColumn");
71         add(listColumn);
72         userName = ChatApp.getApp().getUserName();
73     }
74
75     /**
76      * Updates the state of the <code>MessagePane</code> based on new messages
77      * by querying the <code>ChatApp</code> for new messages.
78      */

79     public void update() {
80         ChatApp app = ChatApp.getApp();
81         ChatSession.Message[] messages = app.getNewMessages();
82         Label label;
83         
84         for (int i = 0; i < messages.length; ++i) {
85             boolean announcement = messages[i].getUserName() == null;
86             Row row = new Row();
87             
88             row.setStyleName(announcement ? "MessagePane.MessageRow.Announcement" : "MessagePane.MessageRow");
89             
90             label = new Label(dateFormat.format(messages[i].getDate()));
91             label.setStyleName("MessagePane.DateLabel");
92             row.add(label);
93
94             if (announcement) {
95                 label = new Label(messages[i].getContent());
96                 label.setStyleName("MessagePane.SystemMessageLabel");
97                 row.add(label);
98             } else {
99                 label = new Label(messages[i].getUserName());
100                 label.setStyleName(userName.equals(messages[i].getUserName())
101                         ? "MessagePane.UserNameLabel.Yourself" : "MessagePane.UserNameLabel.Other");
102                 row.add(label);
103                 
104                 label = new Label(messages[i].getContent());
105                 row.add(label);
106             }
107             
108             listColumn.add(row);
109         }
110         
111         // Remove leading messages greater than MAX_MESSAGE_COUNT.
112
int componentCount = listColumn.getComponentCount();
113         for (int i = MAX_MESSAGE_COUNT; i < componentCount; ++i) {
114             listColumn.remove(0);
115         }
116         
117         setVerticalScroll(BOTTOM);
118     }
119 }
120
Popular Tags