001: package calculator.presentation;
002:
003: // Standard imports
004: import java.math.BigDecimal;
005: import com.lutris.appserver.server.session.*;
006: import calculator.CalcApp;
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.appserver.server.httpPresentation.HttpPresentationIOException;
014: import com.lutris.util.KeywordValueException;
015:
016: import org.enhydra.xml.io.OutputOptions;
017: import org.w3c.dom.html.HTMLImageElement;
018: import org.w3c.dom.Node;
019:
020: import calculator.spec.*;
021:
022: public class CalculatorPresentation implements HttpPresentation {
023:
024: private static CalculatorHTML calculator;
025:
026: public void run(HttpPresentationComms comms)
027: throws HttpPresentationException, IOException {
028:
029: try {
030: processButton(comms);
031:
032: getState(comms);
033:
034: } catch (Exception e) {
035: // ignore exception if it is caused by client
036: if (!HttpPresentationIOException.isClientIOException(e))
037: e.printStackTrace();
038: }
039: }
040:
041: /**
042: * A sample Enhydra application. A simple calculator is simulated using
043: * a single presentation object. Every time a button is pressed this
044: * presentation object is notified via the querry string
045: * "?button=...". <P>
046: *
047: * The responsability of this object is to emit an HTML string that
048: * shows the current state of the calculator. If a button is being
049: * passed to us in the query string, we process that button first,
050: * then show the current state of the calculator. <P>
051: *
052: * Because the SessionData object is being used to store the state of
053: * the calculator, multiple users may use the calculator simultaneously
054: * "for free". <P>
055: *
056: * This method is called to process a button push. If no button
057: * was sent in the URL, do nothing.
058: *
059: * @author Andy John andy@enhydra.org
060: */
061:
062: static public void processButton(HttpPresentationComms comms)
063: throws Exception {
064: // Get the session data for the user who issued this request.
065:
066: SessionData sd = comms.session.getSessionData();
067:
068: String button = (String) comms.request.getParameter("button");
069:
070: CalculatorManager calculatorManager = CalculatorManagerFactory
071: .getCalculatorManager("calculator.business.CalculatorManagerImpl");
072:
073: if (button != null) {
074: // Increment the counter stored in the application object.
075: CalcApp myApplication = (calculator.CalcApp) comms.application;
076: myApplication.buttonsPushed++;
077:
078: /*
079: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run calculator_pres )
080: * We need to allow calculator_pres to be functional
081: */
082: if (button.equals("0") || button.equals("1")
083: || button.equals("2") || button.equals("3")
084: || button.equals("4") || button.equals("5")
085: || button.equals("6") || button.equals("7")
086: || button.equals("8") || button.equals("9")) {
087: try {
088: calculatorManager.addDigit(sd, button);
089:
090: } catch (NullPointerException ex) {
091: } catch (Exception e) {
092: e.printStackTrace();
093: }
094: } else if (button.equals("point")) {
095: try {
096: calculatorManager.addPoint(sd);
097: } catch (NullPointerException ex) {
098: } catch (Exception e) {
099: e.printStackTrace();
100: }
101: } else if (button.equals("negate")) {
102: try {
103: calculatorManager.negate(sd);
104: } catch (NullPointerException ex) {
105: } catch (Exception e) {
106: e.printStackTrace();
107: }
108: } else if (button.equals("clear")) {
109: try {
110: calculatorManager.clear(sd);
111: } catch (NullPointerException ex) {
112: } catch (Exception e) {
113: e.printStackTrace();
114: }
115: } else if (button.equals("equals")) {
116: try {
117: calculatorManager.doEquals(sd);
118: } catch (NullPointerException ex) {
119: } catch (Exception e) {
120: e.printStackTrace();
121: }
122: } else if (button.equals("plus")) {
123: try {
124: calculatorManager.doFunction(sd, "+");
125: } catch (NullPointerException ex) {
126: } catch (Exception e) {
127: e.printStackTrace();
128: }
129: } else if (button.equals("minus")) {
130: try {
131: calculatorManager.doFunction(sd, "-");
132: } catch (NullPointerException ex) {
133: } catch (Exception e) {
134: e.printStackTrace();
135: }
136: } else if (button.equals("times")) {
137: try {
138: calculatorManager.doFunction(sd, "*");
139: } catch (NullPointerException ex) {
140: } catch (Exception e) {
141: e.printStackTrace();
142: }
143: } else if (button.equals("divide")) {
144: try {
145: calculatorManager.doFunction(sd, "/");
146: } catch (NullPointerException ex) {
147: } catch (Exception e) {
148: e.printStackTrace();
149: }
150: }
151: }
152: }
153:
154: /**
155: * This method initializes an XMLC field with the contents of
156: * the current number in the display. The string of digits is converted
157: * into a sequence of HTML image tags.
158: *
159: * This is only a demo program. It does not handle exceptional cases
160: * well, like dividing by zero, overflow, or infinties. The point is to
161: * show off one way to use Enhydra, not be a full-on HP calculator.
162: */
163:
164: public static void getState(HttpPresentationComms comms)
165: throws Exception {
166: // Convert current state to HTML
167: SessionData sd = comms.session.getSessionData();
168:
169: State state = StateFactory
170: .getState("calculator.business.StateImpl");
171: calculator = (CalculatorHTML) comms.xmlcFactory
172: .create(CalculatorHTML.class);
173:
174: /*
175: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run calculator_pres )
176: * We need to allow calculator_pres to be functional , so if the requested url is /calculator_pres/..... the response
177: * will be default HTML page
178: */
179:
180: try {
181: //DigitsChack
182: String digits = state.checkDigits(sd);
183:
184: // Convert the string of digits to HTML.
185: String digitHtml = state.convertDigitsToHTML(digits);
186:
187: // Make the result available to the HTML page.
188:
189: HTMLImageElement digitsDummy = calculator
190: .getElementDigits();
191: Node parent = digitsDummy.getParentNode();
192:
193: int resultStart = 0;
194: int resultEnd = 0;
195: while ((resultStart = digitHtml.indexOf("<")) != -1) {
196: resultEnd = digitHtml.indexOf(">");
197: String txt = digitHtml.substring(resultStart + 9,
198: resultEnd);
199: digitHtml = digitHtml.substring(resultEnd + 1);
200:
201: HTMLImageElement image = (HTMLImageElement) digitsDummy
202: .cloneNode(false);
203: image.setSrc(txt);
204: parent.appendChild(image);
205: }
206: parent.removeChild(digitsDummy);
207: } catch (NullPointerException ex) {
208: }
209:
210: comms.response.writeDOM(calculator);
211: }
212: }
|