You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Calc.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.application.calculator;
  17. import com.vaadin.server.VaadinRequest;
  18. import com.vaadin.tests.components.AbstractTestUI;
  19. import com.vaadin.ui.Alignment;
  20. import com.vaadin.ui.Button;
  21. import com.vaadin.ui.Button.ClickEvent;
  22. import com.vaadin.ui.Button.ClickListener;
  23. import com.vaadin.ui.GridLayout;
  24. import com.vaadin.ui.HorizontalLayout;
  25. import com.vaadin.ui.Label;
  26. import com.vaadin.ui.Table;
  27. import com.vaadin.ui.Table.ColumnHeaderMode;
  28. import com.vaadin.ui.TextField;
  29. import com.vaadin.ui.VerticalLayout;
  30. import com.vaadin.ui.Window;
  31. @SuppressWarnings("serial")
  32. public class Calc extends AbstractTestUI {
  33. private class Log extends VerticalLayout {
  34. private Table table;
  35. private Button addCommentButton;
  36. private int line = 0;
  37. public Log() {
  38. super();
  39. table = new Table();
  40. table.setSizeFull();
  41. setWidth("200px");
  42. setHeight("100%");
  43. table.setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
  44. table.addContainerProperty("Operation", String.class, "");
  45. addComponent(table);
  46. addCommentButton = new Button("Add Comment");
  47. addCommentButton.setWidth("100%");
  48. addCommentButton.addClickListener(new ClickListener() {
  49. @Override
  50. public void buttonClick(ClickEvent event) {
  51. final Window w = new Window("Add comment");
  52. VerticalLayout vl = new VerticalLayout();
  53. vl.setMargin(true);
  54. final TextField tf = new TextField();
  55. tf.setSizeFull();
  56. vl.addComponent(tf);
  57. HorizontalLayout hl = new HorizontalLayout();
  58. Button okButton = new Button("OK");
  59. okButton.setWidth("100%");
  60. okButton.addClickListener(new ClickListener() {
  61. @Override
  62. public void buttonClick(ClickEvent event) {
  63. addRow("[ " + tf.getValue() + " ]");
  64. tf.setValue("");
  65. w.close();
  66. removeWindow(w);
  67. }
  68. });
  69. Button cancelButton = new Button("Cancel");
  70. cancelButton.setWidth("100%");
  71. cancelButton.addClickListener(new ClickListener() {
  72. @Override
  73. public void buttonClick(ClickEvent event) {
  74. tf.setValue("");
  75. w.close();
  76. removeWindow(w);
  77. }
  78. });
  79. hl.addComponent(cancelButton);
  80. hl.addComponent(okButton);
  81. hl.setSpacing(true);
  82. hl.setWidth("100%");
  83. vl.addComponent(hl);
  84. vl.setSpacing(true);
  85. w.setContent(vl);
  86. addWindow(w);
  87. }
  88. });
  89. addComponent(addCommentButton);
  90. setExpandRatio(table, 1);
  91. setSpacing(true);
  92. }
  93. public void addRow(String row) {
  94. Integer id = ++line;
  95. table.addItem(new Object[] { row }, id);
  96. table.setCurrentPageFirstItemIndex(line + 1);
  97. }
  98. }
  99. // All variables are automatically stored in the session.
  100. private Double current = 0.0;
  101. private double stored = 0.0;
  102. private char lastOperationRequested = 'C';
  103. private VerticalLayout topLayout = new VerticalLayout();
  104. // User interface components
  105. private final TextField display = new TextField();
  106. private final Log log = new Log();
  107. // Calculator "business logic" implemented here to keep the example
  108. // minimal
  109. private double calculate(char requestedOperation) {
  110. if ('0' <= requestedOperation && requestedOperation <= '9') {
  111. if (current == null) {
  112. current = 0.0;
  113. }
  114. current = current * 10
  115. + Double.parseDouble("" + requestedOperation);
  116. return current;
  117. }
  118. if (current == null) {
  119. current = stored;
  120. }
  121. switch (lastOperationRequested) {
  122. case '+':
  123. stored += current;
  124. break;
  125. case '-':
  126. stored -= current;
  127. break;
  128. case '/':
  129. stored /= current;
  130. break;
  131. case '*':
  132. stored *= current;
  133. break;
  134. default:
  135. stored = current;
  136. break;
  137. }
  138. switch (requestedOperation) {
  139. case '+':
  140. log.addRow(current + " +");
  141. break;
  142. case '-':
  143. log.addRow(current + " -");
  144. break;
  145. case '/':
  146. log.addRow(current + " /");
  147. break;
  148. case '*':
  149. log.addRow(current + " x");
  150. break;
  151. case '=':
  152. log.addRow(current + " =");
  153. log.addRow("------------");
  154. log.addRow("" + stored);
  155. break;
  156. }
  157. lastOperationRequested = requestedOperation;
  158. current = null;
  159. if (requestedOperation == 'C') {
  160. log.addRow("0.0");
  161. stored = 0.0;
  162. }
  163. return stored;
  164. }
  165. @Override
  166. protected void setup(VaadinRequest request) {
  167. setContent(topLayout);
  168. // Create the main layout for our application (4 columns, 5 rows)
  169. final GridLayout layout = new GridLayout(4, 5);
  170. topLayout.setMargin(true);
  171. topLayout.setSpacing(true);
  172. Label title = new Label("Calculator");
  173. topLayout.addComponent(title);
  174. topLayout.addComponent(log);
  175. HorizontalLayout horizontalLayout = new HorizontalLayout();
  176. horizontalLayout.setSpacing(true);
  177. horizontalLayout.addComponent(layout);
  178. horizontalLayout.addComponent(log);
  179. topLayout.addComponent(horizontalLayout);
  180. // Create a result label that over all 4 columns in the first row
  181. layout.setSpacing(true);
  182. layout.addComponent(display, 0, 0, 3, 0);
  183. layout.setComponentAlignment(display, Alignment.MIDDLE_RIGHT);
  184. display.setSizeFull();
  185. display.setId("display");
  186. display.setValue("0.0");
  187. // The operations for the calculator in the order they appear on the
  188. // screen (left to right, top to bottom)
  189. String[] operations = new String[] { "7", "8", "9", "/", "4", "5", "6",
  190. "*", "1", "2", "3", "-", "0", "=", "C", "+" };
  191. for (String caption : operations) {
  192. // Create a button and use this application for event handling
  193. Button button = new Button(caption);
  194. button.setWidth("40px");
  195. button.addClickListener(new ClickListener() {
  196. @Override
  197. public void buttonClick(ClickEvent event) {
  198. // Get the button that was clicked
  199. Button button = event.getButton();
  200. // Get the requested operation from the button caption
  201. char requestedOperation = button.getCaption().charAt(0);
  202. // Calculate the new value
  203. double newValue = calculate(requestedOperation);
  204. // Update the result label with the new value
  205. display.setValue("" + newValue);
  206. }
  207. });
  208. button.setId("button_" + caption);
  209. // Add the button to our main layout
  210. layout.addComponent(button);
  211. }
  212. }
  213. @Override
  214. protected String getTestDescription() {
  215. return "Provide test application for generic testing purposes";
  216. }
  217. @Override
  218. protected Integer getTicketNumber() {
  219. return 12444;
  220. }
  221. }