001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.chat.ui.conversation;
019:
020: import java.awt.Dimension;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023:
024: import javax.swing.JButton;
025: import javax.swing.JPanel;
026: import javax.swing.JScrollPane;
027:
028: import org.columba.chat.model.BuddyList;
029: import org.columba.chat.model.api.IBuddyStatus;
030: import org.columba.chat.ui.conversation.api.IChatMediator;
031: import org.jivesoftware.smack.Chat;
032: import org.jivesoftware.smack.XMPPException;
033: import org.jivesoftware.smack.packet.Message;
034:
035: import com.jgoodies.forms.builder.ButtonBarBuilder;
036: import com.jgoodies.forms.layout.CellConstraints;
037: import com.jgoodies.forms.layout.FormLayout;
038:
039: /**
040: * @author fdietz
041: *
042: */
043:
044: public class ChatMediator extends JPanel implements IChatMediator,
045: ActionListener {
046:
047: private Chat chat;
048:
049: private ReceivingMessageController receiving;
050:
051: private SendingMessageController sending;
052:
053: // private SendButtonController sendButton;
054:
055: private JButton sendButton;
056:
057: private JButton closeButton;
058:
059: public ChatMediator(Chat chat) {
060: super ();
061:
062: this .chat = chat;
063:
064: receiving = new ReceivingMessageController(this );
065: sending = new SendingMessageController(this );
066: sendButton = new JButton("Send");
067: sendButton.setActionCommand("SEND");
068: sendButton.addActionListener(this );
069:
070: layoutComponents();
071: }
072:
073: public void registerCloseActionListener(
074: ActionListener actionListener) {
075: closeButton.addActionListener(actionListener);
076: }
077:
078: public void layoutComponents() {
079: FormLayout mainLayout = new FormLayout("fill:pref:grow",
080: "fill:default:grow, 3dlu, fill:default, 3dlu, fill:default");
081: setLayout(mainLayout);
082: // JPanel mainPanel = new JPanel(mainLayout);
083: // mainPanel.setBorder(Borders.DIALOG_BORDER);
084:
085: CellConstraints cc = new CellConstraints();
086:
087: JScrollPane receivingPane = new JScrollPane(getReceiving());
088:
089: receivingPane.setPreferredSize(new Dimension(300, 250));
090:
091: add(receivingPane, cc.xy(1, 1));
092:
093: JScrollPane sendingPane = new JScrollPane(getSending());
094:
095: sendingPane.setPreferredSize(new Dimension(300, 100));
096:
097: add(sendingPane, cc.xy(1, 3));
098:
099: closeButton = new JButton("Close");
100: closeButton.setActionCommand("CLOSE");
101:
102: ButtonBarBuilder builder = new ButtonBarBuilder();
103: builder.addGlue();
104: builder.addGridded(closeButton);
105: builder.addRelatedGap();
106: builder.addGridded(sendButton);
107:
108: add(builder.getPanel(), cc.xy(1, 5));
109:
110: }
111:
112: /**
113: * @return Returns the chat.
114: */
115: public Chat getChat() {
116: return chat;
117: }
118:
119: /**
120: * @return Returns the receiving.
121: */
122: public ReceivingMessageController getReceiving() {
123: return receiving;
124: }
125:
126: /**
127: * @return Returns the sendButton.
128: */
129: public JButton getSendButton() {
130: return sendButton;
131: }
132:
133: /**
134: * @return Returns the sending.
135: */
136: public SendingMessageController getSending() {
137: return sending;
138: }
139:
140: /**
141: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
142: */
143: public void actionPerformed(ActionEvent arg0) {
144: String action = arg0.getActionCommand();
145:
146: if (action.equals("SEND")) {
147:
148: // create message object
149: Message message = getChat().createMessage();
150:
151: // set message body
152: message.setBody(getSending().getText());
153:
154: String to = message.getTo();
155: // example: fdietz@jabber.org/Jabber-client
156: // -> remove "/Jabber-client"
157: String normalizedId = to.replaceAll("\\/.*", "");
158:
159: try {
160: // send message
161: getChat().sendMessage(message);
162:
163: // clear text box
164: getSending().setText("");
165:
166: IBuddyStatus buddyStatus = BuddyList.getInstance()
167: .getBuddy(normalizedId);
168:
169: displaySendMessage(message, buddyStatus);
170:
171: } catch (XMPPException e) {
172: // TODO Auto-generated catch block
173: e.printStackTrace();
174: }
175: }
176:
177: }
178:
179: public void displayReceivedMessage(Message message,
180: IBuddyStatus buddyStatus) {
181: getReceiving().displayReceivedMessage(message, buddyStatus);
182:
183: }
184:
185: public void displaySendMessage(Message message,
186: IBuddyStatus buddyStatus) {
187: getReceiving().displaySendMessage(message, buddyStatus);
188:
189: }
190:
191: public void sendTextFieldRequestFocus() {
192: getSending().requestFocus();
193: }
194:
195: }
|