KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > client > DHTMLSessionScreen


1 /*
2  * DHTMLSession.java
3  *
4  * Created on March 20, 2004, 9:06 AM
5  */

6
7 package com.quikj.application.web.talk.client;
8
9 import netscape.javascript.*;
10 import java.applet.*;
11 import java.awt.*;
12 import java.util.*;
13
14 /**
15  *
16  * @author amit
17  */

18 public class DHTMLSessionScreen implements ChatScreenInterface
19 {
20     /** Holds value of property window. */
21     private JSObject window;
22     
23     /** Holds value of property applet. */
24     private Applet applet;
25     
26     /** Holds value of property chat. */
27     private JSObject chat;
28     
29     private StringBuffer JavaDoc transcript = new StringBuffer JavaDoc();
30     
31     /** Holds value of property locale. */
32     private Locale locale;
33     
34     private TalkSession parent;
35     
36     /** Creates a new instance of DHTMLSession */
37     public DHTMLSessionScreen()
38     {
39     }
40     
41     /** Getter for property window.
42      * @return Value of property window.
43      *
44      */

45     public JSObject getWindow()
46     {
47         return this.window;
48     }
49     
50     /** Getter for property applet.
51      * @return Value of property applet.
52      *
53      */

54     public Applet getApplet()
55     {
56         return this.applet;
57     }
58     
59     /** Setter for property applet.
60      * @param applet New value of property applet.
61      *
62      */

63     public void setApplet(Applet applet)
64     {
65         this.applet = applet;
66         
67         // initialize the other objects
68
window = JSObject.getWindow(applet);
69         chat = (JSObject)window.getMember("chat");
70     }
71     
72     /** Getter for property chat.
73      * @return Value of property chat.
74      *
75      */

76     public JSObject getChat()
77     {
78         return this.chat;
79     }
80     
81     public void initScreen(TalkSession parent, Locale locale, Applet applet)
82     {
83         setLocale(locale);
84         setApplet(applet);
85         setParent(parent);
86         setTitleText(java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.client.language",
87         locale).getString("Talk_Session"));
88     }
89     
90     public void setStatusBarText(String JavaDoc text)
91     {
92         chat.call("setStatusBarText", new Object JavaDoc[]
93         {text});
94     }
95     
96     public void alert(String JavaDoc text)
97     {
98         window.call("alert", new Object JavaDoc[]
99         {text});
100     }
101     
102     public void setActionButtonLabel(String JavaDoc label, Color color)
103     {
104         chat.call("setActionButtonLabel", new Object JavaDoc[]
105         {label});
106     }
107     
108     private void addTextSegment(String JavaDoc label, String JavaDoc color, String JavaDoc weight)
109     {
110         chat.call("writeText", new Object JavaDoc[]
111         {label, color, weight});
112         transcript.append(label);
113     }
114     
115     private void endTextSegment()
116     {
117         chat.call("writeHR", null);
118         chat.call("scrollDown", null);
119         transcript.append("\n");
120     }
121     
122     public void mediaFieldsSetEnabled(boolean enabled)
123     {
124         String JavaDoc val = enabled == true ? "false": "true";
125         
126         chat.call("disableActionButton", new Object JavaDoc[]
127         {val});
128         chat.call("disableSend", new Object JavaDoc[]
129         {val});
130         
131         if (enabled == true)
132         {
133             chat.call("enableMediaBar", new Object JavaDoc[]
134             {"visible"});
135         }
136         else
137         {
138             chat.call("enableMediaBar", new Object JavaDoc[]
139             {"hidden"});
140         }
141     }
142     
143     public void setTitleText(String JavaDoc title)
144     {
145         chat.call("setTitle", new Object JavaDoc[]
146         {title});
147     }
148     
149     public String JavaDoc getTranscript()
150     {
151         return transcript.toString();
152     }
153     
154     public void focus()
155     {
156         window.call("focus", null);
157     }
158     
159     public void showWebPage(String JavaDoc location)
160     {
161         chat.call("openWebPage", new Object JavaDoc[]
162         {location});
163     }
164     
165     public void informationDialog(String JavaDoc title, String JavaDoc text, String JavaDoc button, int timeout)
166     {
167         chat.call("informationDialog", new Object JavaDoc[]
168         {title, text, button, new Integer JavaDoc(timeout)});
169     }
170     
171     public void appendToConversation(String JavaDoc text, java.awt.Color JavaDoc color, int style)
172     {
173         String JavaDoc color_str = "";
174         if (color != null)
175         {
176             color_str = "RGB("
177             + color.getRed()
178             + "," + color.getGreen()
179             + "," + color.getBlue() + ")";
180         }
181         
182         String JavaDoc font_str = "";
183         switch (style)
184         {
185             case Font.BOLD:
186                 font_str = "bold";
187                 break;
188                 
189             case Font.ITALIC:
190                 font_str = "italic";
191                 break;
192         }
193         
194         
195         // remove all carriage returns (\r)
196
char[] text_array = text.toCharArray();
197         char[] new_text_array = new char[text_array.length];
198         int count = 0;
199         for (int i = 0; i < text_array.length; i++)
200         {
201             if (text_array[i] == '\r')
202             {
203                 continue;
204             }
205             
206             new_text_array[count++] = text_array[i];
207         }
208         
209         String JavaDoc cr_removed_str = new String JavaDoc(new_text_array, 0, count);
210         StringTokenizer tokens = new StringTokenizer(cr_removed_str, "\n");
211         int ct = tokens.countTokens();
212         boolean ends_with_nl = cr_removed_str.endsWith("\n");
213         
214         for (int i = 0; i < ct; i++)
215         {
216             if (i > 0)
217             {
218                 endTextSegment();
219             }
220             
221             addTextSegment(tokens.nextToken(),
222             color_str, font_str);
223             
224             if (i == ct -1) // if this is the last token
225
{
226                 if (ends_with_nl == true)
227                 {
228                     endTextSegment();
229                 }
230             }
231         }
232     }
233     
234     
235     public void disposeScreen()
236     {
237         mediaFieldsSetEnabled(false);
238     }
239     
240     /** Getter for property locale.
241      * @return Value of property locale.
242      *
243      */

244     public Locale getLocale()
245     {
246         return this.locale;
247     }
248     
249     /** Setter for property locale.
250      * @param locale New value of property locale.
251      *
252      */

253     public void setLocale(Locale locale)
254     {
255         this.locale = locale;
256     }
257     
258     public void setChatText(String JavaDoc text)
259     {
260         // TODO
261
}
262     
263     public void addMediaMenuItem(String JavaDoc name, String JavaDoc className)
264     {
265         // not supported for DHTML
266
}
267     
268     public void menuItemSetEnabled(String JavaDoc name, boolean enabled)
269     {
270         // not supported for DHTML
271
}
272     
273     public boolean yesNoDialog(String JavaDoc title, String JavaDoc text, String JavaDoc yes, String JavaDoc no)
274     {
275         // not supported for DHTML
276
return false;
277     }
278     
279     public void setParent(TalkSession parent)
280     {
281         this.parent = parent;
282     }
283     
284     public Rectangle getBounds()
285     {
286         String JavaDoc bounds_s = (String JavaDoc)chat.call("getBounds", null);
287         StringTokenizer toks = new StringTokenizer(bounds_s, ",");
288         
289         try
290         {
291             Rectangle r = new Rectangle(Integer.parseInt(toks.nextToken()),
292             Integer.parseInt(toks.nextToken()),
293             Integer.parseInt(toks.nextToken()),
294             Integer.parseInt(toks.nextToken()));
295             return r;
296         }
297         catch (NumberFormatException JavaDoc ex)
298         {
299             return null;
300         }
301     }
302     
303 }
304
Popular Tags