1 package calculator.presentation; 2 3 import java.math.BigDecimal ; 5 import com.lutris.appserver.server.session.*; 6 import calculator.CalcApp; 7 import java.io.IOException ; 8 9 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 ; 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 { 25 26 try{ 27 processButton(comms); 28 29 getState(comms); 30 31 32 }catch(Exception e){ 33 e.printStackTrace(); 34 } 35 } 36 37 57 58 static public void processButton(HttpPresentationComms comms) throws Exception { 59 61 62 SessionData sd = comms.session.getSessionData(); 63 64 65 String button = (String ) comms.request.getParameter("button"); 66 67 CalculatorManager calculatorManager = CalculatorManagerFactory.getCalculatorManager("calculator.business.CalculatorManagerImpl"); 68 69 70 if (button != null) { 71 CalcApp myApplication = (calculator.CalcApp) comms.application; 73 myApplication.buttonsPushed++; 74 75 76 77 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 ex){ 91 }catch(Exception e){ 92 e.printStackTrace(); 93 } 94 } 95 else if (button.equals("point")) 96 { 97 try{ 98 calculatorManager.addPoint(sd); 99 }catch(NullPointerException ex){ 100 }catch(Exception e){ 101 e.printStackTrace(); 102 } 103 } 104 else if (button.equals("negate")) 105 { 106 try{ 107 calculatorManager.negate(sd); 108 }catch(NullPointerException ex){ 109 }catch(Exception e){ 110 e.printStackTrace(); 111 } 112 } 113 else if (button.equals("clear")) { 114 try{ 115 calculatorManager.clear(sd); 116 }catch(NullPointerException ex){ 117 }catch(Exception e){ 118 e.printStackTrace(); 119 } 120 } else if (button.equals("equals")) 121 { 122 try{ 123 calculatorManager.doEquals(sd); 124 }catch(NullPointerException ex){ 125 }catch(Exception e){ 126 e.printStackTrace(); 127 } 128 } 129 else if (button.equals("plus")) 130 { 131 try{ 132 calculatorManager.doFunction(sd, "+"); 133 }catch(NullPointerException ex){ 134 }catch(Exception e){ 135 e.printStackTrace(); 136 } 137 } 138 else if (button.equals("minus")) 139 { 140 try{ 141 calculatorManager.doFunction(sd, "-"); 142 }catch(NullPointerException ex){ 143 }catch(Exception e){ 144 e.printStackTrace(); 145 } 146 } 147 else if (button.equals("times")) 148 { 149 try{ 150 calculatorManager.doFunction(sd, "*"); 151 }catch(NullPointerException ex){ 152 }catch(Exception e){ 153 e.printStackTrace(); 154 } 155 } 156 else if (button.equals("divide")) 157 { 158 try{ 159 calculatorManager.doFunction(sd, "/"); 160 }catch(NullPointerException ex){ 161 }catch(Exception e){ 162 e.printStackTrace(); 163 } 164 } 165 } 166 } 167 168 169 178 179 180 181 public static void getState(HttpPresentationComms comms) throws Exception { 182 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 196 197 try{ 198 String digits = state.checkDigits(sd); 200 201 String digitHtml = state.convertDigitsToHTML(digits); 203 204 206 HTMLImageElement digitsDummy = calculator.getElementDigits(); 207 Node parent = digitsDummy.getParentNode(); 208 209 int resultStart = 0; 210 int resultEnd = 0; 211 while((resultStart=digitHtml.indexOf("<")) != -1){ 212 resultEnd = digitHtml.indexOf(">"); 213 String 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 ex){ 222 } 223 224 comms.response.writeDOM(calculator); 225 } 226 } 227 228 229 230 231 | Popular Tags |