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 7.6KB

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