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 nextapp.echo2.app.Alignment;
033: import nextapp.echo2.app.Button;
034: import nextapp.echo2.app.ContentPane;
035: import nextapp.echo2.app.Extent;
036: import nextapp.echo2.app.Label;
037: import nextapp.echo2.app.Row;
038: import nextapp.echo2.app.SplitPane;
039: import nextapp.echo2.app.TextArea;
040: import nextapp.echo2.app.event.ActionEvent;
041: import nextapp.echo2.app.event.ActionListener;
042:
043: /**
044: * A screen which displays the current state of the chat room and allow the
045: * user to submit chat input.
046: */
047: public class ChatScreen extends ContentPane {
048:
049: private TextArea postField;
050: private MessagePane messagePane;
051:
052: /**
053: * Creates a new <code>ChatScreen</code>.
054: */
055: public ChatScreen() {
056: super ();
057:
058: SplitPane outerSplitPane = new SplitPane(
059: SplitPane.ORIENTATION_VERTICAL_TOP_BOTTOM, new Extent(
060: 32));
061: outerSplitPane.setStyleName("ChatScreen.SplitPane");
062: add(outerSplitPane);
063:
064: Row controlsRow = new Row();
065: controlsRow.setStyleName("ControlPane");
066: outerSplitPane.add(controlsRow);
067:
068: Button logoutButton = new Button(Messages
069: .getString("Generic.Exit"), Styles.ICON_24_EXIT);
070: logoutButton.setStyleName("ControlPane.Button");
071: logoutButton.addActionListener(new ActionListener() {
072: public void actionPerformed(ActionEvent e) {
073: ChatApp.getApp().disconnect();
074: }
075: });
076: controlsRow.add(logoutButton);
077:
078: SplitPane mainSplitPane = new SplitPane(
079: SplitPane.ORIENTATION_VERTICAL_BOTTOM_TOP, new Extent(
080: 90));
081: mainSplitPane.setStyleName("ChatScreen.SplitPane");
082: outerSplitPane.add(mainSplitPane);
083:
084: SplitPane inputAreaSplitPane = new SplitPane(
085: SplitPane.ORIENTATION_HORIZONTAL_LEADING_TRAILING,
086: new Extent(150));
087: mainSplitPane.add(inputAreaSplitPane);
088:
089: Label currentUserLabel = new Label(ChatApp.getApp()
090: .getUserName()
091: + ":");
092: currentUserLabel.setStyleName("ChatScreen.CurrentUserLabel");
093: inputAreaSplitPane.add(currentUserLabel);
094:
095: SplitPane postSplitPane = new SplitPane(
096: SplitPane.ORIENTATION_HORIZONTAL_TRAILING_LEADING,
097: new Extent(100));
098: inputAreaSplitPane.add(postSplitPane);
099:
100: Button submitPostButton = new Button(Messages
101: .getString("ChatScreen.SubmitPostButton"),
102: Styles.ICON_24_RIGHT_ARROW);
103: submitPostButton.setStyleName("ChatScreen.SubmitPostButton");
104: submitPostButton.setTextPosition(new Alignment(Alignment.LEFT,
105: Alignment.DEFAULT));
106: submitPostButton.addActionListener(new ActionListener() {
107: public void actionPerformed(ActionEvent e) {
108: postMessage();
109: }
110: });
111: postSplitPane.add(submitPostButton);
112:
113: postField = new TextArea();
114: postField.setStyleName("ChatScreen.PostField");
115: postField.setWidth(new Extent(97, Extent.PERCENT));
116: postField.setHeight(new Extent(70, Extent.PX));
117: postField.addActionListener(new ActionListener() {
118: public void actionPerformed(ActionEvent e) {
119: postMessage();
120: }
121: });
122: postSplitPane.add(postField);
123:
124: messagePane = new MessagePane();
125: mainSplitPane.add(messagePane);
126:
127: ChatApp app = ChatApp.getApp();
128: app.setFocusedComponent(postField);
129: }
130:
131: /**
132: * Updates the list of messages.
133: */
134: public void updateMessageList() {
135: messagePane.update();
136: }
137:
138: /**
139: * Posts the text currently entered as a new message.
140: */
141: private void postMessage() {
142: ChatApp app = ChatApp.getApp();
143: if (postField.getText().trim().length() != 0) {
144: app.postMessage(postField.getText());
145: }
146: postField.setText("");
147: app.setFocusedComponent(postField);
148: messagePane.update();
149: }
150: }
|