KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > chat > ui > conversation > ChatMediator


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.chat.ui.conversation;
19
20 import java.awt.Dimension JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23
24 import javax.swing.JButton JavaDoc;
25 import javax.swing.JPanel JavaDoc;
26 import javax.swing.JScrollPane JavaDoc;
27
28 import org.columba.chat.model.BuddyList;
29 import org.columba.chat.model.api.IBuddyStatus;
30 import org.columba.chat.ui.conversation.api.IChatMediator;
31 import org.jivesoftware.smack.Chat;
32 import org.jivesoftware.smack.XMPPException;
33 import org.jivesoftware.smack.packet.Message;
34
35 import com.jgoodies.forms.builder.ButtonBarBuilder;
36 import com.jgoodies.forms.layout.CellConstraints;
37 import com.jgoodies.forms.layout.FormLayout;
38
39 /**
40  * @author fdietz
41  *
42  */

43
44 public class ChatMediator extends JPanel JavaDoc implements IChatMediator,
45         ActionListener JavaDoc {
46
47     private Chat chat;
48
49     private ReceivingMessageController receiving;
50
51     private SendingMessageController sending;
52
53     // private SendButtonController sendButton;
54

55     private JButton JavaDoc sendButton;
56
57     private JButton JavaDoc closeButton;
58
59     public ChatMediator(Chat chat) {
60         super();
61
62         this.chat = chat;
63
64         receiving = new ReceivingMessageController(this);
65         sending = new SendingMessageController(this);
66         sendButton = new JButton JavaDoc("Send");
67         sendButton.setActionCommand("SEND");
68         sendButton.addActionListener(this);
69
70         layoutComponents();
71     }
72
73     public void registerCloseActionListener(ActionListener JavaDoc actionListener) {
74         closeButton.addActionListener(actionListener);
75     }
76
77     public void layoutComponents() {
78         FormLayout mainLayout = new FormLayout("fill:pref:grow",
79                 "fill:default:grow, 3dlu, fill:default, 3dlu, fill:default");
80         setLayout(mainLayout);
81         // JPanel mainPanel = new JPanel(mainLayout);
82
// mainPanel.setBorder(Borders.DIALOG_BORDER);
83

84         CellConstraints cc = new CellConstraints();
85
86         JScrollPane JavaDoc receivingPane = new JScrollPane JavaDoc(getReceiving());
87
88         receivingPane.setPreferredSize(new Dimension JavaDoc(300, 250));
89
90         add(receivingPane, cc.xy(1, 1));
91
92         JScrollPane JavaDoc sendingPane = new JScrollPane JavaDoc(getSending());
93
94         sendingPane.setPreferredSize(new Dimension JavaDoc(300, 100));
95
96         add(sendingPane, cc.xy(1, 3));
97
98         closeButton = new JButton JavaDoc("Close");
99         closeButton.setActionCommand("CLOSE");
100
101         ButtonBarBuilder builder = new ButtonBarBuilder();
102         builder.addGlue();
103         builder.addGridded(closeButton);
104         builder.addRelatedGap();
105         builder.addGridded(sendButton);
106
107         add(builder.getPanel(), cc.xy(1, 5));
108
109     }
110
111     /**
112      * @return Returns the chat.
113      */

114     public Chat getChat() {
115         return chat;
116     }
117
118     /**
119      * @return Returns the receiving.
120      */

121     public ReceivingMessageController getReceiving() {
122         return receiving;
123     }
124
125     /**
126      * @return Returns the sendButton.
127      */

128     public JButton JavaDoc getSendButton() {
129         return sendButton;
130     }
131
132     /**
133      * @return Returns the sending.
134      */

135     public SendingMessageController getSending() {
136         return sending;
137     }
138
139     /**
140      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
141      */

142     public void actionPerformed(ActionEvent JavaDoc arg0) {
143         String JavaDoc action = arg0.getActionCommand();
144
145         if (action.equals("SEND")) {
146
147             // create message object
148
Message message = getChat().createMessage();
149
150             // set message body
151
message.setBody(getSending().getText());
152
153             String JavaDoc to = message.getTo();
154             // example: fdietz@jabber.org/Jabber-client
155
// -> remove "/Jabber-client"
156
String JavaDoc normalizedId = to.replaceAll("\\/.*", "");
157
158             try {
159                 // send message
160
getChat().sendMessage(message);
161
162                 // clear text box
163
getSending().setText("");
164
165                 IBuddyStatus buddyStatus = BuddyList.getInstance().getBuddy(
166                         normalizedId);
167
168                 displaySendMessage(message, buddyStatus);
169
170             } catch (XMPPException e) {
171                 // TODO Auto-generated catch block
172
e.printStackTrace();
173             }
174         }
175
176     }
177
178     public void displayReceivedMessage(Message message, IBuddyStatus buddyStatus) {
179         getReceiving().displayReceivedMessage(message, buddyStatus);
180
181     }
182
183     public void displaySendMessage(Message message, IBuddyStatus buddyStatus) {
184         getReceiving().displaySendMessage(message, buddyStatus);
185
186     }
187
188     public void sendTextFieldRequestFocus() {
189         getSending().requestFocus();
190     }
191
192 }
Popular Tags