KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ComposeMessagePanel


1 import java.awt.*;
2 import java.awt.event.*;
3 import java.awt.image.ImageObserver JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import javax.swing.*;
6 import javax.accessibility.Accessible JavaDoc;
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 /** Panel that appears as a tab in the main window for composing a one-off message.*/
14 public final class ComposeMessagePanel extends JPanel implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, Serializable JavaDoc,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 JavaDoc data=new java.util.Vector JavaDoc(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()); // make sure we dont show empty space
44
}
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         //help key
75
getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); //put in help key for action help
76
getActionMap().put("help",new HelpAction(this,"messages.html#messages"));
77         // enter key
78
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 JavaDoc 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         // validate first
121
String JavaDoc 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         // remove aangled brackets if present
134
address=address.replace('<',' ');
135         address=address.replace('>',' ');
136         address=address.trim();
137         // check is valid JID
138
// Disabled so we can send messages to hosts
139
//~ if(StringUtils.parseName(address).equals("") || StringUtils.parseServer(address).equals("") ){
140
//~ GUI.showInfo(WhisperIM.MainWindow,"compose","invalid address","'"+address+"'");
141
//~ toList.requestFocus();
142
//~ return;
143
//~ }
144
// check we have a message to send
145
if(text.getText().equals("")){
146             GUI.showInfo(WhisperIM.MainWindow,"compose","no message");
147             text.requestFocus();
148             return;
149         }
150         // create the message
151
Message message=new Message(address);
152          // We need a key
153
String JavaDoc bareID=StringUtils.parseBareAddress(address.toLowerCase());
154         // check the recipient is known already no to have key
155
if(WhisperIM.MainWindow.hasNoKey("jabber",bareID)){
156             // see if we need to warn
157
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             // if ok, send unencrypted
164
message.setBody(text.getText());
165             WhisperIM.MainWindow.Conn.sendPacket(message);
166             WhisperIM.MainWindow.chatPane.remove(thisPanel);
167             return;
168         }
169         // get key from cache
170
whisper.PublicKey key=(whisper.PublicKey) WhisperIM.MainWindow.Keys.get("jabber"+bareID);
171         if(key==null){
172             // try and fetch it
173
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){ //no key for user
184
// Offer to send unencrypted
185
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                 // if ok, send unencrypted
192
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 JavaDoc 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 JavaDoc 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             //get button
221
String JavaDoc b=ae.getActionCommand();
222             // call method depending on button clicked
223
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 JavaDoc 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