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