1 import java.awt.*; 2 import java.awt.event.*; 3 import java.awt.image.ImageObserver ; 4 import java.io.Serializable ; 5 import javax.swing.*; 6 import javax.accessibility.Accessible ; 7 import java.util.*; 8 import org.jivesoftware.smack.*; 9 import org.jivesoftware.smack.packet.*; 10 import org.jivesoftware.smack.util.StringUtils; 11 12 13 14 public final class ComposeMessagePanel extends JPanel implements Accessible , ImageObserver , MenuContainer, Serializable ,WMP{ 15 16 private static final int GAP=10; 17 18 final JPanel toPanel=new JPanel(new BorderLayout(GAP,GAP)); 19 final JLabel toLbl=new JLabel(Lang.gs("to")); 20 final java.util.Vector data=new java.util.Vector (WhisperIM.MainWindow.Contacts.values()); 21 final JComboBox toList=new JComboBox(data); 22 23 24 final JTextArea text=new JTextArea(); 25 final JScrollPane textSP=new JScrollPane(text); 26 27 final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP)); 28 final JButton sendBtn=new JButton(Lang.gs("send"),Icons.SEND); 29 final JButton cancelBtn=new JButton(Lang.gs("cancel"),Icons.CANCEL); 30 31 final BtnClick click=new BtnClick(); 32 33 final JPanel thisPanel=this; 34 35 private boolean toSet=false; 36 37 public ComposeMessagePanel(){ 38 super(new BorderLayout(GAP,GAP)); 39 40 GUI.connect(toLbl,toList,"to_mn","to_tt"); 41 toList.setEditable(true); 42 if(toList.getMaximumRowCount()>toList.getItemCount() ){ 43 toList.setMaximumRowCount(toList.getItemCount()); } 45 java.util.Collections.sort(data,new ContactComparator()); 46 toList.setSelectedItem(""); 47 toList.addActionListener(new ToChangeListener()); 48 49 toPanel.add(toLbl,BorderLayout.WEST); 50 toPanel.add(toList,BorderLayout.CENTER); 51 add(toPanel,BorderLayout.NORTH); 52 53 text.setLineWrap(true); 54 text.setWrapStyleWord(true); 55 textSP.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 56 if(WhisperIM.isMac){ 57 textSP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 58 } 59 else{ 60 textSP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 61 } 62 63 add(textSP,BorderLayout.CENTER); 64 65 sendBtn.addActionListener(click); 66 cancelBtn.addActionListener(click); 67 sendBtn.setDefaultCapable(true); 68 sendBtn.setMnemonic(Lang.s2k("send_mn")); 69 sendBtn.setToolTipText(Lang.gs("send_tt")); 70 cancelBtn.setMnemonic(Lang.s2k("cancel_mn")); 71 btnPanel.add(sendBtn); 72 btnPanel.add(cancelBtn); 73 add(btnPanel,BorderLayout.SOUTH); 74 getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); getActionMap().put("help",new HelpAction(this,"messages.html#messages")); 77 text.getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),"send_messge"); 79 text.getActionMap().put("send_messge", new EnterAction()); 80 } 81 82 public void setTo(String id){ 83 toList.setSelectedItem(id); 84 WhisperIM.MainWindow.chatPane.setToolTipTextAt(WhisperIM.MainWindow.chatPane.indexOfComponent(thisPanel),Lang.gs("compose_tt")+" "+id); 85 toSet=true; 86 } 87 88 public void setTo(ContactListItem cli){ 89 toList.setSelectedItem(cli); 90 WhisperIM.MainWindow.chatPane.setToolTipTextAt(WhisperIM.MainWindow.chatPane.indexOfComponent(thisPanel),Lang.gs("compose_tt")+" "+cli.toString() ); 91 toSet=true; 92 } 93 94 public void doFocus(){ 95 if(!toSet){ 96 toList.requestFocusInWindow(); 97 return; 98 } 99 text.requestFocusInWindow(); 100 } 101 102 public void doClose(){ 103 WhisperIM.MainWindow.chatPane.remove(thisPanel); 104 } 105 106 public void goneOffline(){ 107 sendBtn.setEnabled(false); 108 } 109 110 public void goneOnline(){ 111 sendBtn.setEnabled(true); 112 } 113 114 public JButton getDefaultButton(){ 115 return sendBtn; 116 } 117 118 public void doSend(){ 119 try{ 120 String address; 122 if(toList.getSelectedIndex()==-1){ 123 address=toList.getSelectedItem().toString(); 124 } 125 else{ 126 address=( (ContactListItem) toList.getSelectedItem()).getID(); 127 } 128 if(address==null || address.equals("")){ 129 GUI.showInfo(WhisperIM.MainWindow,"compose","no address"); 130 toList.requestFocus(); 131 return; 132 } 133 address=address.replace('<',' '); 135 address=address.replace('>',' '); 136 address=address.trim(); 137 if(text.getText().equals("")){ 146 GUI.showInfo(WhisperIM.MainWindow,"compose","no message"); 147 text.requestFocus(); 148 return; 149 } 150 Message message=new Message(address); 152 String bareID=StringUtils.parseBareAddress(address.toLowerCase()); 154 if(WhisperIM.MainWindow.hasNoKey("jabber",bareID)){ 156 if(WhisperIM.UserPref.getProperty("warn_unencrypted").equals("true")){ 158 if(JOptionPane.showConfirmDialog(thisPanel,Lang.gs("no key"),Lang.gs("compose"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE)!=JOptionPane.YES_OPTION){ 159 cancelBtn.requestFocus(); 160 return; 161 } 162 } 163 message.setBody(text.getText()); 165 WhisperIM.MainWindow.Conn.sendPacket(message); 166 WhisperIM.MainWindow.chatPane.remove(thisPanel); 167 return; 168 } 169 whisper.PublicKey key=(whisper.PublicKey) WhisperIM.MainWindow.Keys.get("jabber"+bareID); 171 if(key==null){ 172 JWaitDialog wait=new JWaitDialog(WhisperIM.MainWindow,"getting key"); 174 wait.show(); 175 try{ 176 key=WhisperIM.MainWindow.getKey("jabber",bareID); 177 } 178 catch (XMPPException xe){ 179 wait.dispose(); 180 GUI.showWarning(this,"no key","no public key",xe.getMessage()); 181 } 182 wait.dispose(); 183 if(key==null){ if(WhisperIM.UserPref.getProperty("warn_unencrypted").equals("true")){ 186 if(JOptionPane.showConfirmDialog(thisPanel,Lang.gs("no key"),Lang.gs("compose"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE)!=JOptionPane.YES_OPTION){ 187 cancelBtn.requestFocus(); 188 return; 189 } 190 } 191 message.setBody(text.getText()); 193 WhisperIM.MainWindow.Conn.sendPacket(message); 194 WhisperIM.MainWindow.chatPane.remove(thisPanel); 195 return; 196 } 197 } 198 while(WhisperIM.KEYPAIR==null){ 199 try{ 200 Thread.currentThread().sleep(3000); 201 } 202 catch(InterruptedException ie){ 203 return; 204 } 205 } 206 whisper.Message wm=new whisper.Message(text.getText(),key,WhisperIM.KEYPAIR,WhisperIM.SR); 207 message.setBody(StringUtils.escapeForXML(wm.getText())); 208 WhisperIM.MainWindow.Conn.sendPacket(message); 209 WhisperIM.MainWindow.chatPane.remove(thisPanel); 210 } 211 catch (Exception e){ 212 e.printStackTrace(); 213 GUI.showError(thisPanel,"error","error",e.getMessage()); 214 } 215 } 216 217 218 final class BtnClick implements ActionListener{ 219 public void actionPerformed(ActionEvent ae){ 220 String b=ae.getActionCommand(); 222 if(b.equals(cancelBtn.getText())){ 224 WhisperIM.MainWindow.chatPane.remove(thisPanel); 225 return; 226 } 227 if(b.equals(sendBtn.getText())){ 228 doSend(); 229 return; 230 } 231 } 232 } 233 234 final class ToChangeListener implements ActionListener{ 235 public void actionPerformed(ActionEvent ae){ 236 String tt=toList.getSelectedItem().toString(); 237 if(tt==null){return;} 238 WhisperIM.MainWindow.chatPane.setToolTipTextAt(WhisperIM.MainWindow.chatPane.indexOfComponent(thisPanel),Lang.gs("compose_tt")+" "+tt ); 239 } 240 } 241 242 final class EnterAction extends AbstractAction{ 243 public void actionPerformed(ActionEvent e) { 244 doSend(); 245 } 246 } 247 } | Popular Tags |