001: package chat.presentation;
002:
003: import chat.ChatApplication;
004: import chat.spec.*;
005:
006: // Standard imports
007: import java.io.IOException;
008:
009: // Enhydra SuperServlet imports
010: import com.lutris.appserver.server.httpPresentation.HttpPresentation;
011: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
012: import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
013: import com.lutris.util.KeywordValueException;
014:
015: import org.w3c.dom.html.HTMLBodyElement;
016: import org.w3c.dom.html.HTMLElement;
017: import org.w3c.dom.Node;
018: import org.w3c.dom.html.HTMLFontElement;
019:
020: public class EntryFormPresentation implements HttpPresentation {
021:
022: private static EntryFormHTML entryForm;
023:
024: public void run(HttpPresentationComms comms)
025: throws HttpPresentationException, IOException {
026: try {
027: maybeAddMsg(comms);
028: fillInFields(comms);
029: } catch (Exception e) {
030: e.printStackTrace();
031: }
032:
033: }
034:
035: /**
036: * If is not the first request from a user (or a refresh), then
037: * we are being called by the form we emitted last time. In this case
038: * we should process the message being sent in. If it's the clear
039: * command (optional), then clear the message list. Most of the time
040: * we just add the message to the current discussion. Note how the
041: * HTML/HTTP and the logic of keeping messages is kept separate.
042: * If there is no message, do nothing.
043: */
044: static public void maybeAddMsg(HttpPresentationComms comms)
045: throws Exception {
046: ChatApplication myApp = (ChatApplication) comms.application;
047: String name = comms.request.getParameter("userName");
048:
049: if (name == null)
050: return;
051: String text = comms.request.getParameter("text");
052: if (text == null)
053: return;
054: String clearCmd = myApp.getClearCommand();
055:
056: DiscussionManager discussionManager = DiscussionManagerFactory
057: .getDiscussionManager("chat.business.DiscussionManagerImpl");
058:
059: /*
060: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run calculator_pres )
061: * We need to allow calculator_pres to be functional , so if the requested url is /calculator_pres/..... the response
062: * will be default HTML page
063: */
064: try {
065: if ((clearCmd.length() != 0) && text.equals(clearCmd))
066: discussionManager.clear();
067: else
068: discussionManager.addMessage(name, text);
069: } catch (NullPointerException ex) {
070: }
071: }
072:
073: /**
074: * Set up the fields.
075: */
076: static public void fillInFields(HttpPresentationComms comms)
077: throws Exception {
078: ChatApplication myApp = (ChatApplication) comms.application;
079: String name = comms.request.getParameter("userName");
080:
081: // Background color.
082: entryForm = (EntryFormHTML) comms.xmlcFactory
083: .create(EntryFormHTML.class);
084: HTMLBodyElement color = entryForm.getElementColor();
085: color.setBgColor(myApp.getBgColor());
086: comms.response.writeDOM(entryForm);
087:
088: /*
089: * If it's IE, they won't see the spinning icon while the page loads
090: * forever. So if it's not IE, then add a little explanation.
091: */
092: String message = "Reload the page if your messages stop appearing.";
093: String ua = comms.request.getHeader("User-Agent");
094: if ((ua == null) || (ua.indexOf("MSIE") == -1))
095: message += "<br>It is normal for the page to "
096: + "keep loading indefinitely.";
097: message += "<br>This is a demonstration <a href=http://www.enhydra.org "
098: + "target=enhydra>Enhydra</a> application.";
099: entryForm.setTextMessage(message);
100: comms.response.writeDOM(entryForm);
101:
102: }
103:
104: }
|