KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > calculator > presentation > CalculatorPresentation


1 package calculator.presentation;
2
3 // Standard imports
4
import java.math.BigDecimal JavaDoc;
5 import com.lutris.appserver.server.session.*;
6 import calculator.CalcApp;
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 import org.w3c.dom.html.HTMLImageElement;
15 import org.w3c.dom.Node JavaDoc;
16
17 import calculator.spec.*;
18
19
20 public class CalculatorPresentation implements HttpPresentation {
21
22   private static CalculatorHTML calculator;
23
24   public void run(HttpPresentationComms comms) throws HttpPresentationException, IOException JavaDoc {
25
26     try{
27       processButton(comms);
28     
29       getState(comms);
30       
31     
32     }catch(Exception JavaDoc e){
33       e.printStackTrace();
34     }
35   }
36
37   /**
38    * A sample Enhydra application. A simple calculator is simulated using
39    * a single presentation object. Every time a button is pressed this
40    * presentation object is notified via the querry string
41    * "?button=...". <P>
42    *
43    * The responsability of this object is to emit an HTML string that
44    * shows the current state of the calculator. If a button is being
45    * passed to us in the query string, we process that button first,
46    * then show the current state of the calculator. <P>
47    *
48    * Because the SessionData object is being used to store the state of
49    * the calculator, multiple users may use the calculator simultaneously
50    * "for free". <P>
51    *
52    * This method is called to process a button push. If no button
53    * was sent in the URL, do nothing.
54    *
55    * @author Andy John andy@enhydra.org
56    */

57
58   static public void processButton(HttpPresentationComms comms) throws Exception JavaDoc {
59     // Get the session data for the user who issued this request.
60

61     
62     SessionData sd = comms.session.getSessionData();
63     
64     
65     String JavaDoc button = (String JavaDoc) comms.request.getParameter("button");
66
67     CalculatorManager calculatorManager = CalculatorManagerFactory.getCalculatorManager("calculator.business.CalculatorManagerImpl");
68
69
70     if (button != null) {
71       // Increment the counter stored in the application object.
72
CalcApp myApplication = (calculator.CalcApp) comms.application;
73       myApplication.buttonsPushed++;
74
75
76
77 /*
78  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run calculator_pres )
79  * We need to allow calculator_pres to be functional
80  */

81       if (button.equals("0") || button.equals("1") ||
82           button.equals("2") || button.equals("3") ||
83           button.equals("4") || button.equals("5") ||
84           button.equals("6") || button.equals("7") ||
85           button.equals("8") || button.equals("9"))
86       {
87         try{
88                calculatorManager.addDigit(sd, button);
89         
90         }catch(NullPointerException JavaDoc ex){
91         }catch(Exception JavaDoc e){
92           e.printStackTrace();
93         }
94       }
95       else if (button.equals("point"))
96       {
97         try{
98             calculatorManager.addPoint(sd);
99         }catch(NullPointerException JavaDoc ex){
100         }catch(Exception JavaDoc e){
101           e.printStackTrace();
102         }
103       }
104       else if (button.equals("negate"))
105       {
106         try{
107             calculatorManager.negate(sd);
108         }catch(NullPointerException JavaDoc ex){
109         }catch(Exception JavaDoc e){
110           e.printStackTrace();
111         }
112       }
113       else if (button.equals("clear")) {
114         try{
115             calculatorManager.clear(sd);
116         }catch(NullPointerException JavaDoc ex){
117         }catch(Exception JavaDoc e){
118           e.printStackTrace();
119         }
120         } else if (button.equals("equals"))
121         {
122           try{
123               calculatorManager.doEquals(sd);
124         }catch(NullPointerException JavaDoc ex){
125         }catch(Exception JavaDoc e){
126             e.printStackTrace();
127           }
128         }
129         else if (button.equals("plus"))
130         {
131           try{
132               calculatorManager.doFunction(sd, "+");
133         }catch(NullPointerException JavaDoc ex){
134         }catch(Exception JavaDoc e){
135             e.printStackTrace();
136         }
137         }
138         else if (button.equals("minus"))
139         {
140           try{
141               calculatorManager.doFunction(sd, "-");
142         }catch(NullPointerException JavaDoc ex){
143         }catch(Exception JavaDoc e){
144             e.printStackTrace();
145           }
146         }
147         else if (button.equals("times"))
148         {
149           try{
150               calculatorManager.doFunction(sd, "*");
151         }catch(NullPointerException JavaDoc ex){
152         }catch(Exception JavaDoc e){
153             e.printStackTrace();
154           }
155         }
156         else if (button.equals("divide"))
157         {
158           try{
159               calculatorManager.doFunction(sd, "/");
160         }catch(NullPointerException JavaDoc ex){
161         }catch(Exception JavaDoc e){
162             e.printStackTrace();
163           }
164         }
165     }
166   }
167
168    
169   /**
170    * This method initializes an XMLC field with the contents of
171    * the current number in the display. The string of digits is converted
172    * into a sequence of HTML image tags.
173    *
174    * This is only a demo program. It does not handle exceptional cases
175    * well, like dividing by zero, overflow, or infinties. The point is to
176    * show off one way to use Enhydra, not be a full-on HP calculator.
177    */

178   
179   
180   
181   public static void getState(HttpPresentationComms comms) throws Exception JavaDoc {
182     // Convert current state to HTML
183
SessionData sd = comms.session.getSessionData();
184     
185     
186     State state = StateFactory.getState("calculator.business.StateImpl");
187     calculator = (CalculatorHTML)comms.xmlcFactory.create(CalculatorHTML.class);
188
189
190
191 /*
192  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run calculator_pres )
193  * We need to allow calculator_pres to be functional , so if the requested url is /calculator_pres/..... the response
194  * will be default HTML page
195  */

196
197 try{
198 //DigitsChack
199
String JavaDoc digits = state.checkDigits(sd);
200     
201 // Convert the string of digits to HTML.
202
String JavaDoc digitHtml = state.convertDigitsToHTML(digits);
203
204     // Make the result available to the HTML page.
205

206     HTMLImageElement digitsDummy = calculator.getElementDigits();
207     Node JavaDoc parent = digitsDummy.getParentNode();
208
209     int resultStart = 0;
210     int resultEnd = 0;
211     while((resultStart=digitHtml.indexOf("<")) != -1){
212       resultEnd = digitHtml.indexOf(">");
213       String JavaDoc txt = digitHtml.substring(resultStart+9, resultEnd);
214       digitHtml = digitHtml.substring(resultEnd+1);
215
216       HTMLImageElement image = (HTMLImageElement) digitsDummy.cloneNode(false);
217       image.setSrc(txt);
218       parent.appendChild(image);
219     }
220     parent.removeChild(digitsDummy);
221 }catch(NullPointerException JavaDoc ex){
222 }
223     
224     comms.response.writeDOM(calculator);
225   }
226 }
227
228
229
230
231
Popular Tags