KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > feature > proactive > applet > ProactiveApplet


1 /*
2  * ProactiveApplet.java
3  *
4  * Created on April 25, 2003, 2:55 AM
5  */

6
7 package com.quikj.application.web.talk.feature.proactive.applet;
8
9 import java.util.*;
10 import java.net.*;
11 import java.awt.*;
12 import java.awt.event.*;
13
14 import com.quikj.client.framework.*;
15 import com.quikj.client.beans.*;
16
17 /**
18  *
19  * @author amit
20  */

21 public class ProactiveApplet extends java.applet.Applet JavaDoc
22 implements HTTPMessageListenerInterface, HTTPConnectionClosedInterface, ActionListener
23 {
24     private URL codeBase = null;
25     private int port = 80;
26     private int applicationId = 4;
27     private ServerCommunications com = null;
28     private String JavaDoc imageURL = null;
29     private String JavaDoc groupName = "operator";
30     private ImageButton imageButton = null;
31     private URL onContactURL = null;
32     private URL onClickURL = null;
33     private String JavaDoc operator = null;
34     
35     /** Initializes the applet ProactiveApplet */
36     public void init()
37     {
38         initAppletParams();
39         
40         initComponents();
41         
42         if (imageURL != null)
43         {
44             drawImageButton();
45         }
46         
47         //System.out.println("user identifier: " + PersistentObjects.userIdentifier);
48
}
49     
50     private void initAppletParams()
51     {
52         while (PersistentObjects.running == true)
53         {
54             try
55             {
56                 Thread.sleep(10);
57             }
58             catch (InterruptedException JavaDoc ex)
59             {
60                 // do nothing
61
}
62         }
63         
64         PersistentObjects.running = true;
65         
66         // initialize codeBase
67
codeBase = getCodeBase();
68         
69         if (codeBase.getPort() >= 0) // a port is specified
70
{
71             port = codeBase.getPort();
72         }
73         
74         String JavaDoc parm = getParameter("port");
75         if (parm != null)
76         {
77             try
78             {
79                 port = Integer.parseInt(parm);
80             }
81             catch (NumberFormatException JavaDoc ex)
82             {
83                 ;
84             }
85         }
86         
87         parm = getParameter("plugin");
88         if (parm != null)
89         {
90             try
91             {
92                 applicationId = Integer.parseInt(parm);
93             }
94             catch (NumberFormatException JavaDoc ex)
95             {
96                 ;
97             }
98         }
99         
100         parm = getParameter("image");
101         if (parm != null)
102         {
103             imageURL = parm;
104         }
105         
106         parm = getParameter("group");
107         if (parm != null)
108         {
109             groupName = parm;
110         }
111         
112         parm = getParameter("on-contact");
113         if (parm != null)
114         {
115             onContactURL = URLUtils.formatURL(this, parm);
116         }
117         
118         parm = getParameter("on-click");
119         if (parm != null)
120         {
121             onClickURL = URLUtils.formatURL(this, parm);
122         }
123     }
124     
125     /** This method is called from within the init() method to
126      * initialize the form.
127      * WARNING: Do NOT modify this code. The content of this method is
128      * always regenerated by the Form Editor.
129      */

130     private void initComponents()
131     {//GEN-BEGIN:initComponents
132

133         setLayout(new java.awt.BorderLayout JavaDoc());
134         
135     }//GEN-END:initComponents
136

137     public void destroy()
138     {
139         //System.out.println("Applet destroyed");
140
}
141     
142     public void start()
143     {
144         //System.out.println("Applet started");
145

146         disconnect(); // just in case
147
connect();
148         
149         // send a message to the server
150
if (com == null)
151         {
152             return;
153         }
154         
155         if (com.isConnected() == true)
156         {
157             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
158             
159             if (PersistentObjects.userIdentifier != null)
160             {
161                 buffer.append("Identifier:" + PersistentObjects.userIdentifier + "\n");
162             }
163             else
164             {
165                 buffer.append("Identifier:NA\n");
166             }
167             buffer.append("Group:" + groupName + "\n");
168             buffer.append("Document:" + getDocumentBase().toString() + "\n");
169             
170             int req_id = com.sendRequestMessage("text/plain",
171             buffer.toString());
172             if (req_id == -1)
173             {
174                 // print error message
175
}
176         }
177     }
178     
179     public void stop()
180     {
181         //System.out.println("Applet stopped");
182
disconnect();
183         
184         try
185         {
186             Thread.sleep(1000);
187         }
188         catch (InterruptedException JavaDoc ex)
189         {
190             
191         }
192         PersistentObjects.running = false;
193     }
194     
195     private void disconnect()
196     {
197         if (com == null)
198         {
199             return;
200         }
201         
202         if (com.isConnected() == true)
203         {
204             com.disconnect();
205         }
206     }
207     
208     private void connect()
209     {
210         // establish a connection to the server
211
try
212         {
213             com = new ServerCommunications(codeBase.getHost(), port, applicationId);
214         }
215         catch (UnknownHostException ex)
216         {
217             //System.out.println("Unknown host " + codeBase.getHost());
218
return;
219         }
220         
221         if (com.connect() == false)
222         {
223             // TODO print error message
224
return;
225         }
226         
227         com.setRequestListener(this);
228         com.setClosedListener(this);
229         
230         // start the message listener thread
231
com.start();
232     }
233     
234     public void messageReceived(int req_id, int status, String JavaDoc content_type,
235     int http_status, String JavaDoc reason, String JavaDoc message)
236     {
237         if (message != null)
238         {
239             // parse the message line by line
240
parseMessage(message);
241             
242             if (operator != null)
243             {
244                 if (onContactURL != null)
245                 {
246                     try
247                     {
248                         String JavaDoc info = URLEncoder.encode("full-name="
249                         + URLEncoder.encode(PersistentObjects.userIdentifier));
250                         
251                         URL url = new URL(onContactURL.toString()
252                         + "?called="
253                         + operator + "&info=" + info);
254                         
255                         getAppletContext().showDocument(url,
256                         "_blank");
257                     }
258                     catch (MalformedURLException ex)
259                     {
260                         ;
261                     }
262                 }
263             }
264         }
265     }
266     
267     private void parseMessage(String JavaDoc message)
268     {
269         StringTokenizer tokens = new StringTokenizer(message, "\n");
270         int num_lines = tokens.countTokens();
271         
272         for (int i = 0; i < num_lines; i++)
273         {
274             String JavaDoc line = tokens.nextToken().trim();
275             if (line.length() == 0)
276             {
277                 continue;
278             }
279             
280             StringTokenizer t = new StringTokenizer(line, ":");
281             int n = t.countTokens();
282             if (n < 2)
283             {
284                 continue;
285             }
286             
287             String JavaDoc key = t.nextToken().trim();
288             String JavaDoc value = "";
289             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
290             for (int j = 1; j < n; j++)
291             {
292                 buffer.append(t.nextToken());
293             }
294             value = buffer.toString();
295             
296             if ((key.length() == 0) || (value.length() == 0))
297             {
298                 continue;
299             }
300             
301             if (key.equals("Identifier") == true)
302             {
303                 if (value.equals("NA") == false)
304                 {
305                     PersistentObjects.userIdentifier = value;
306                 }
307             }
308             else if (key.equals("Initiate-Call") == true)
309             {
310                 operator = value;
311             }
312         }
313     }
314     
315     public void connectionClosed(String JavaDoc host, int port, int appl_id)
316     {
317     }
318     
319     private void drawImageButton()
320     {
321         URL url = URLUtils.formatURL(this, imageURL);
322         Image image = getImage(url); // download the image
323
if (image == null)
324         {
325             System.err.println("The specified URL does not exist");
326             return;
327         }
328         
329         // next, draw the image
330
imageButton = new ImageButton();
331         imageButton.setImage(image);
332         imageButton.addActionListener(this);
333         //imageButton.setLabel("Live Help");
334
add(imageButton, BorderLayout.CENTER);
335         imageButton.invalidate();
336     }
337     
338     public void actionPerformed(java.awt.event.ActionEvent JavaDoc actionEvent)
339     {
340         if (onClickURL != null)
341         {
342             getAppletContext().showDocument(onClickURL, "_blank");
343         }
344         else if (onContactURL != null)
345         {
346             try
347             {
348                 URL url = new URL(onContactURL.toString()
349                 + "?called="
350                 + groupName);
351                 getAppletContext().showDocument(url, "_blank");
352             }
353             catch (MalformedURLException ex)
354             {
355             }
356         }
357     }
358     
359     // Variables declaration - do not modify//GEN-BEGIN:variables
360
// End of variables declaration//GEN-END:variables
361

362 }
363
Popular Tags