KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > quickmessage > QuickMessage


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.quickmessage;
20
21 import org.lucane.client.*;
22 import org.lucane.client.widgets.*;
23 import org.lucane.common.*;
24 import org.lucane.common.net.ObjectConnection;
25
26 import java.applet.Applet JavaDoc;
27 import java.applet.AudioClip JavaDoc;
28 import java.awt.*;
29 import java.awt.event.*;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.text.DateFormat JavaDoc;
33 import java.util.Date JavaDoc;
34
35 import javax.swing.*;
36
37
38 public class QuickMessage
39 extends Plugin
40 implements ActionListener, KeyListener
41 {
42     private static AudioClip JavaDoc feedback = null;
43     
44     /* parameters */
45     private ConnectInfo[] friends;
46     private String JavaDoc message;
47     
48     /* widgets */
49     private ManagedWindow dialog;
50     private JTextField txtWho;
51     private HTMLEditor txtMessage;
52     private HTMLEditor txtNew;
53     private JButton btnMain;
54     private boolean ctrl;
55         
56     public QuickMessage()
57     {
58         //nothing
59
}
60     
61     public Plugin newInstance(ConnectInfo[] friends)
62     {
63         Plugin p = new QuickMessage(friends);
64         return p;
65     }
66     
67     public QuickMessage(ConnectInfo[] friends)
68     {
69         this.friends = friends;
70     }
71     
72     public void load(ObjectConnection friend, ConnectInfo who, String JavaDoc data)
73     {
74         this.friends = new ConnectInfo[1];
75         this.friends[0] = who;
76         this.message = data;
77     }
78     
79     public void start()
80     {
81         this.message = "";
82         
83         if(this.friends.length == 0)
84         {
85             DialogBox.info(tr("nodest"));
86             exit();
87             
88             return;
89         }
90         
91         dialog = new ManagedWindow(this, getTitle());
92         dialog.setExitPluginOnClose(true);
93         dialog.setDiscardWidgetState(true);
94         dialog.getContentPane().setLayout(new BorderLayout());
95         txtWho = new JTextField(friends[0].getName());
96         txtWho.setEditable(false);
97         
98         for(int i = 1; i < friends.length; i++)
99             txtWho.setText(txtWho.getText() + ";" + friends[i].getName());
100         
101         txtNew = new HTMLEditor();
102         btnMain = new JButton(tr("send"));
103         btnMain.addActionListener(this);
104         dialog.getContentPane().add(txtWho, BorderLayout.NORTH);
105         dialog.getContentPane().add(txtNew, BorderLayout.CENTER);
106         dialog.getContentPane().add(btnMain, BorderLayout.SOUTH);
107         dialog.setPreferredSize(new Dimension(420, 250));
108         
109         txtNew.addKeyListener(this);
110         txtNew.getEditorPane().addKeyListener(this);
111         txtWho.addKeyListener(this);
112         
113         dialog.show();
114     }
115     
116     public void follow()
117     {
118         playFeedbackSound();
119         DateFormat JavaDoc format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
120         String JavaDoc date = format.format(new Date JavaDoc());
121         SimpleHistory.add(friends[0], friends[0].getName(), date, this.message);
122         
123         dialog = new ManagedWindow(this, getTitle());
124         dialog.setExitPluginOnClose(true);
125         dialog.setDiscardWidgetState(true);
126         dialog.getContentPane().setLayout(new BorderLayout());
127         txtWho = new JTextField(friends[0].getName());
128         txtWho.setEditable(false);
129         txtMessage = new HTMLEditor();
130         txtMessage.setText(SimpleHistory.toHtml(friends[0]));
131         txtMessage.scrollToBottom();
132         txtMessage.setEditable(false);
133         txtMessage.setToolbarVisible(false);
134         txtNew = new HTMLEditor();
135         btnMain = new JButton(tr("answer"));
136         btnMain.addActionListener(this);
137         dialog.getContentPane().add(txtWho, BorderLayout.NORTH);
138         
139         JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, txtMessage, txtNew);
140         jsp.setOneTouchExpandable(true);
141         dialog.getContentPane().add(jsp, BorderLayout.CENTER);
142         dialog.getContentPane().add(btnMain, BorderLayout.SOUTH);
143         dialog.setPreferredSize(new Dimension(420, 400));
144         jsp.setDividerLocation(150);
145         
146         txtNew.addKeyListener(this);
147         txtNew.getEditorPane().addKeyListener(this);
148         txtWho.addKeyListener(this);
149         
150         dialog.show();
151         
152     }
153     
154     public static String JavaDoc formatMessage(String JavaDoc user, String JavaDoc date, String JavaDoc msg)
155     {
156         String JavaDoc color = "blue";
157         if(user.equals(Client.getInstance().getMyInfos().getName()))
158             color = "red";
159         
160         return "<font color='" + color + "'>"
161           + date
162           + " - "
163           + user + "</font>" + msg;
164     }
165     
166     private void playFeedbackSound()
167     {
168         //only play sound if not discarded in localconfig
169
if(getLocalConfig().getInt("play.sound", 1) != 0)
170         {
171             if(feedback == null)
172             {
173                 try {
174                     feedback = Applet.newAudioClip(new URL JavaDoc(getDirectory()+"feedback.wav"));
175                 } catch (MalformedURLException JavaDoc e) {
176                     e.printStackTrace();
177                 }
178             }
179             
180             feedback.play();
181         }
182     }
183     
184     public void actionPerformed(ActionEvent ae)
185     {
186         String JavaDoc myName = Client.getInstance().getMyInfos().getName();
187         DateFormat JavaDoc format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
188         String JavaDoc date = format.format(new Date JavaDoc());
189
190         
191         this.message = this.txtNew.getBodyText();
192         for(int i = 0; i < friends.length; i++)
193         {
194             try
195             {
196                 ObjectConnection sc = Communicator.getInstance().sendMessageTo(friends[i],
197                         this.getName(), this.message);
198                 SimpleHistory.add(friends[i], myName, date, this.message);
199                 sc.close();
200             }
201             catch(Exception JavaDoc e)
202             {
203                 DialogBox.error(tr("err")+ friends[i].getName() + " : " + e);
204             }
205         }
206         
207         dialog.dispose();
208         exit();
209     }
210     
211     public void keyPressed(KeyEvent e)
212     {
213         if(e.getKeyCode() == KeyEvent.VK_CONTROL)
214             ctrl = true;
215         else if(e.getKeyCode() == KeyEvent.VK_ENTER && ctrl)
216         {
217             ctrl = false;
218             actionPerformed(null);
219         }
220         else if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
221         {
222             dialog.dispose();
223         }
224         else if(e.getKeyCode() == KeyEvent.VK_F1)
225         {
226             try {
227                 Plugin help = PluginManager.getInstance().newPluginInstance(
228                         "org.lucane.applications.helpbrowser", new ConnectInfo[0], true);
229                 help.run();
230                 help.invoke("showHelp", new Class JavaDoc[]{Plugin.class}, new Object JavaDoc[]{this});
231                 help.invoke("hidePluginList", new Class JavaDoc[0], new Object JavaDoc[0]);
232             } catch (Exception JavaDoc ex) {
233                 ex.printStackTrace();
234                 ex.getCause().printStackTrace();
235             }
236         }
237         else
238             ctrl = false;
239     }
240     
241     public void keyReleased(KeyEvent e) {}
242     public void keyTyped(KeyEvent e) {}
243 }
244
Popular Tags