KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chat > presentation > EntryFormPresentation


1 package chat.presentation;
2
3 import chat.ChatApplication;
4 import chat.spec.*;
5
6 // Standard imports
7
import java.io.IOException JavaDoc;
8
9 // Enhydra SuperServlet imports
10
import com.lutris.appserver.server.httpPresentation.HttpPresentation;
11 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
12 import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
13 import com.lutris.util.KeywordValueException;
14
15 import org.w3c.dom.html.HTMLBodyElement;
16 import org.w3c.dom.html.HTMLElement;
17 import org.w3c.dom.Node JavaDoc;
18 import org.w3c.dom.html.HTMLFontElement;
19
20 public class EntryFormPresentation implements HttpPresentation {
21
22   private static EntryFormHTML entryForm;
23
24   public void run(HttpPresentationComms comms) throws HttpPresentationException, IOException JavaDoc {
25     try{
26       maybeAddMsg(comms);
27       fillInFields(comms);
28     }catch(Exception JavaDoc e){
29       e.printStackTrace();
30     }
31
32   }
33
34   /**
35    * If is not the first request from a user (or a refresh), then
36    * we are being called by the form we emitted last time. In this case
37    * we should process the message being sent in. If it's the clear
38    * command (optional), then clear the message list. Most of the time
39    * we just add the message to the current discussion. Note how the
40    * HTML/HTTP and the logic of keeping messages is kept separate.
41    * If there is no message, do nothing.
42    */

43   static public void maybeAddMsg(HttpPresentationComms comms) throws Exception JavaDoc {
44     ChatApplication myApp = (ChatApplication) comms.application;
45     String JavaDoc name = comms.request.getParameter("userName");
46
47     if (name == null)
48       return;
49     String JavaDoc text = comms.request.getParameter("text");
50     if (text == null)
51       return;
52     String JavaDoc clearCmd = myApp.getClearCommand();
53    
54     DiscussionManager discussionManager = DiscussionManagerFactory.getDiscussionManager("chat.business.DiscussionManagerImpl");
55
56 /*
57  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run calculator_pres )
58  * We need to allow calculator_pres to be functional , so if the requested url is /calculator_pres/..... the response
59  * will be default HTML page
60  */

61 try{
62     if ((clearCmd.length() != 0) && text.equals(clearCmd))
63       discussionManager.clear();
64     else
65      discussionManager.addMessage(name, text);
66 }catch(NullPointerException JavaDoc ex){}
67   }
68
69   /**
70    * Set up the fields.
71    */

72   static public void fillInFields(HttpPresentationComms comms) throws Exception JavaDoc {
73     ChatApplication myApp = (ChatApplication) comms.application;
74     String JavaDoc name = comms.request.getParameter("userName");
75
76     // Background color.
77
entryForm = (EntryFormHTML)comms.xmlcFactory.create(EntryFormHTML.class);
78     HTMLBodyElement color = entryForm.getElementColor();
79     color.setBgColor(myApp.getBgColor());
80     comms.response.writeDOM(entryForm);
81
82     /*
83     * If it's IE, they won't see the spinning icon while the page loads
84     * forever. So if it's not IE, then add a little explanation.
85      */

86     String JavaDoc message = "Reload the page if your messages stop appearing.";
87     String JavaDoc ua = comms.request.getHeader("User-Agent");
88     if ((ua == null) || (ua.indexOf("MSIE") == -1))
89       message += "<br>It is normal for the page to " +
90       "keep loading indefinitely.";
91     message += "<br>This is a demonstration <a HREF=http://www.enhydra.org " +
92                "target=enhydra>Enhydra</a> application.";
93     entryForm.setTextMessage(message);
94     comms.response.writeDOM(entryForm);
95
96   }
97
98 }
99
100
101
102
Popular Tags