Source Code Cross Referenced for CalculatorPresentation.java in  » J2EE » Enhydra-Demos » calculator » presentation » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Enhydra Demos » calculator.presentation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.