1 29 30 package echo2example.chatclient; 31 32 import java.text.DateFormat ; 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 44 public class MessagePane extends ContentPane { 45 46 49 private static final DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT); 50 51 54 private static final int MAX_MESSAGE_COUNT = 100; 55 56 59 private static final Extent BOTTOM = new Extent(-1, Extent.PX); 60 61 private Column listColumn; 62 private String userName; 63 64 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 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 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 |