KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > RecievedMessagePanel


1 import java.awt.*;
2 import java.awt.event.*;
3 import java.awt.image.ImageObserver JavaDoc;
4 import java.io.*;
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 when a message is recieved.*/
14 public final class RecievedMessagePanel extends JPanel implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, Serializable, WMP{
15     
16     private static final int GAP=10;
17     private String JavaDoc FromID;
18     private String JavaDoc FromName;
19         
20     private boolean replyBtnWasEnabled=true;
21     
22     JPanel fromPanel=new JPanel(new GridLayout(2,1,GAP,GAP));
23     Box fromBox=Box.createHorizontalBox();
24         JLabel fromLbl=new JLabel();
25         JLabel timeLbl=new JLabel();
26     JLabel statLbl=new JLabel(Lang.gs("encrypted"),Icons.PADLOCK,SwingConstants.LEFT);
27     
28     JEditorPane pane=new JEditorPane();
29     JScrollPane paneSP=new JScrollPane(pane);
30     
31     JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP));
32     JButton saveBtn=new JButton(Lang.gs("save"),Icons.SAVE);
33     JButton replyBtn=new JButton(Lang.gs("reply"),Icons.MESSAGE);
34     JButton closeBtn=new JButton(Lang.gs("close"),Icons.CLOSE);
35     
36     BtnClick click=new BtnClick();
37     
38     JPanel thisPanel=this;
39     
40     
41     public RecievedMessagePanel(String JavaDoc text,String JavaDoc fromName,String JavaDoc fromID,boolean encrypted, String JavaDoc time){
42         super(new BorderLayout(GAP,GAP));
43         this.FromID=fromID;
44         this.FromName=fromName;
45         
46         fromLbl.setText(Lang.gs("from")+fromName);
47         fromLbl.setToolTipText(fromID);
48         timeLbl.setText(time);
49         fromBox.add(fromLbl);
50         fromBox.add(Box.createGlue());
51         fromBox.add(timeLbl);
52         if(!encrypted){
53             statLbl.setText(Lang.gs("nonencrypted"));
54             statLbl.setIcon(Icons.WARNING);
55             statLbl.setToolTipText(Lang.gs("nonencrypted"));
56         }
57         fromPanel.add(fromBox);
58         fromPanel.add(statLbl);
59         add(fromPanel,BorderLayout.NORTH);
60         
61         pane.setEditable(false);
62         pane.setContentType("text/html");
63         paneSP.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
64         if(WhisperIM.isMac){
65             paneSP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
66         }
67         else{
68             paneSP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
69         }
70         
71         java.net.URL JavaDoc base=null;
72         try{
73             base=(new File(".")).toURL();
74             ((javax.swing.text.html.HTMLDocument JavaDoc)pane.getDocument()).setBase(base);
75         }
76         catch (Exception JavaDoc e){
77             // swallow
78
}
79         pane.setText(GUI.getHeader()+"<p>"+Convert.text(text)+"</p>"+GUI.FOOTER);
80         add(paneSP,BorderLayout.CENTER);
81         
82         saveBtn.setVisible(false); //FUTURE FEATURE
83
saveBtn.setToolTipText(Lang.gs("save_tt"));
84         saveBtn.setMnemonic(Lang.s2k("save_mn"));
85         saveBtn.addActionListener(click);
86         replyBtn.setToolTipText(Lang.gs("reply_tt"));
87         replyBtn.setMnemonic(Lang.s2k("reply_mn"));
88         replyBtn.addActionListener(click);
89         closeBtn.setMnemonic(Lang.s2k("close_mn"));
90         closeBtn.addActionListener(click);
91         btnPanel.add(saveBtn);
92         btnPanel.add(replyBtn);
93         btnPanel.add(closeBtn);
94         add(btnPanel,BorderLayout.SOUTH);
95         replyBtn.setDefaultCapable(true);
96         closeBtn.setDefaultCapable(true);
97         //help key
98
getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); //put in help key for action help
99
getActionMap().put("help",new HelpAction(this,"messages.html#messages"));
100     }
101     
102     public void doClose(){
103         WhisperIM.MainWindow.chatPane.remove(thisPanel);
104     }
105     
106     public void goneOffline(){
107         if(replyBtn.isEnabled()){
108             replyBtnWasEnabled=true;
109             replyBtn.setEnabled(false);
110         }
111     }
112         
113     public void goneOnline(){
114         if(replyBtnWasEnabled){
115             replyBtn.setEnabled(true);
116         }
117     }
118     
119     public void doFocus(){
120         if(replyBtn.isEnabled()){
121             replyBtn.requestFocusInWindow();
122         }
123         else{
124             closeBtn.requestFocusInWindow();
125         }
126     }
127     
128     public JButton getDefaultButton(){
129         if(replyBtn.isEnabled()){
130             return replyBtn;
131         }
132         else{
133             return closeBtn;
134         }
135     }
136     
137     class BtnClick implements ActionListener{
138         public void actionPerformed(ActionEvent ae){
139             //get button
140
String JavaDoc b=ae.getActionCommand();
141             // call method depending on button clicked
142
if(b.equals(closeBtn.getText())){
143                 doClose();
144                 return;
145             }
146             if(b.equals(replyBtn.getText())){
147                 ComposeMessagePanel cmp=new ComposeMessagePanel();
148                 ContactListItem cli=(ContactListItem) WhisperIM.MainWindow.Contacts.get(FromName);
149                 if(cli==null){
150                     
151                     WhisperIM.MainWindow.chatPane.addTab("",Icons.MESSAGE,cmp);
152                     
153                     WhisperIM.MainWindow.chatPane.setSelectedComponent(cmp);
154                     cmp.setTo(FromID);
155                     replyBtn.setEnabled(false);
156                     cmp.doFocus();
157                     return;
158                 }
159                 WhisperIM.MainWindow.chatPane.addTab("",Icons.MESSAGE,cmp);
160                 cmp.setTo(cli);
161                 WhisperIM.MainWindow.chatPane.setSelectedComponent(cmp);
162                 replyBtn.setEnabled(false);
163                 cmp.doFocus();
164                 return;
165             }
166         }
167     }
168 }
Popular Tags