1 package org.columba.chat.ui.conversation; 19 20 import java.awt.Dimension ; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 24 import javax.swing.JButton ; 25 import javax.swing.JPanel ; 26 import javax.swing.JScrollPane ; 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 43 44 public class ChatMediator extends JPanel implements IChatMediator, 45 ActionListener { 46 47 private Chat chat; 48 49 private ReceivingMessageController receiving; 50 51 private SendingMessageController sending; 52 53 55 private JButton sendButton; 56 57 private JButton 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 ("Send"); 67 sendButton.setActionCommand("SEND"); 68 sendButton.addActionListener(this); 69 70 layoutComponents(); 71 } 72 73 public void registerCloseActionListener(ActionListener 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 84 CellConstraints cc = new CellConstraints(); 85 86 JScrollPane receivingPane = new JScrollPane (getReceiving()); 87 88 receivingPane.setPreferredSize(new Dimension (300, 250)); 89 90 add(receivingPane, cc.xy(1, 1)); 91 92 JScrollPane sendingPane = new JScrollPane (getSending()); 93 94 sendingPane.setPreferredSize(new Dimension (300, 100)); 95 96 add(sendingPane, cc.xy(1, 3)); 97 98 closeButton = new JButton ("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 114 public Chat getChat() { 115 return chat; 116 } 117 118 121 public ReceivingMessageController getReceiving() { 122 return receiving; 123 } 124 125 128 public JButton getSendButton() { 129 return sendButton; 130 } 131 132 135 public SendingMessageController getSending() { 136 return sending; 137 } 138 139 142 public void actionPerformed(ActionEvent arg0) { 143 String action = arg0.getActionCommand(); 144 145 if (action.equals("SEND")) { 146 147 Message message = getChat().createMessage(); 149 150 message.setBody(getSending().getText()); 152 153 String to = message.getTo(); 154 String normalizedId = to.replaceAll("\\/.*", ""); 157 158 try { 159 getChat().sendMessage(message); 161 162 getSending().setText(""); 164 165 IBuddyStatus buddyStatus = BuddyList.getInstance().getBuddy( 166 normalizedId); 167 168 displaySendMessage(message, buddyStatus); 169 170 } catch (XMPPException e) { 171 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 |