-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrhino_calculator.js
268 lines (240 loc) · 7.68 KB
/
rhino_calculator.js
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright (C) 2011 by Anton Ivanov [email protected]
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* Simple demo of the Java Script Rhino engine and its integration with Java.
* What is left to do:
* - Calculator should have limitations on the maximum number of digits before/after the dot so that overflowing does not occur
* and inform the user when such an error may potentially occur, plus rounding behavior should be thought out
* - Thorough testing of calculator and maybe writing automated tests for it with Window Licker http://code.google.com/p/windowlicker/
* In general, the quality of code/functionality is worse than what I usually write as no automated tests were written along the way
*
* To launch the demo, download Rhino http://www.mozilla.org/rhino/ and execute the following command line:
* java -jar /PATH_TO_RHINO/rhino1_7R3/js.jar java_swing_calculator.js
*/
importPackage(javax.swing);
importPackage(java.awt);
importClass(javax.swing.border.EmptyBorder);
importClass(java.awt.event.ActionListener);
importClass(java.awt.event.KeyEvent);
importClass(java.lang.Thread);
/*
* Supported arithmetic functions.
*/
function add(x, y) {
return x + y;
}
function minus(x, y) {
return x - y;
}
function multiply(x, y) {
return x * y;
}
function divide(x, y) {
return x / y;
}
/*
* Calculator appearance customization
*/
var font = new Font("Tahoma", Font.BOLD, 18);
var widgetDimension = 60;
var buttonColumns = 4;
var buttonRows = 5;
/*
* Support for keyboard keys
*/
var keyboardMappings = {
"0": KeyEvent.VK_0,
"1": KeyEvent.VK_1,
"2": KeyEvent.VK_2,
"3": KeyEvent.VK_3,
"4": KeyEvent.VK_4,
"5": KeyEvent.VK_5,
"6": KeyEvent.VK_6,
"7": KeyEvent.VK_7,
"8": KeyEvent.VK_8,
"9": KeyEvent.VK_9,
"+": 43, //KeyEvent.VK_PLUS does not work, see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4262044
"-": KeyEvent.VK_MINUS,
"/": 47, //KeyEvent.VK_DIVIDE does not work
"*": 42, //KeyEvent.VK_MULTIPLY does not work
"=": KeyEvent.VK_EQUALS
};
var keyboardHandlers = {};
/*
* Support for chaining computations
*/
var lastComputation = {operator: null, value: null}
var clearOnNextSymbol = false
/*
* Laying out the Calculator's controls
*/
var frame = new JFrame("Rhino Calculator");
var layout = new GridBagLayout();
frame.getContentPane().setLayout(layout);
var resultField = addResultField();
var buttonsPanel = addButtonsPanel();
addCalculatorButtons();
frame.pack();
frame.setResizable(false);
frame.visible = true;
frame.addWindowListener(function(e, name) {
if ("windowClosing" === name) {
java.lang.System.exit(0);
}
});
function addResultField() {
var resultField = new JTextField();
resultField.setPreferredSize(new Dimension(buttonColumns * widgetDimension, widgetDimension));
resultField.setHorizontalAlignment(JTextField.RIGHT);
resultField.setFont(font);
resultField.setEditable(false);
resultField.setBackground(Color.WHITE);
resultField.addKeyListener(keyboardListener);
resultField.text = "0";
var constraints = new GridBagConstraints();
constraints.gridx = 0
constraints.gridy = 0
constraints.gridwidth = buttonColumns
constraints.gridheight = 1
frame.add(resultField, constraints);
return resultField;
}
function addButtonsPanel() {
var buttonsPanel = new JPanel();
var constraints = new GridBagConstraints();
constraints.gridx = 0
constraints.gridy = 1
constraints.gridwidth = buttonColumns
constraints.gridheight = buttonRows
var buttonsPanelLayout = new GridLayout(buttonRows, buttonColumns);
buttonsPanel.setLayout(buttonsPanelLayout);
frame.add(buttonsPanel, constraints);
return buttonsPanel;
}
function addCalculatorButtons() {
for (var i = 0; i <= 9; i++) {
addNumberButton(i.toString());
}
addSignButton();
addDotButton();
addOperatorButton("+", add);
addOperatorButton("-", minus);
addOperatorButton("*", multiply);
addOperatorButton("/", divide);
addOperatorButton("=");
addCancelEntryButton();
addCancelButton();
}
function addNumberButton(number) {
addButton(number, function() {
if (clearOnNextSymbol) {
setDisplay(number);
clearOnNextSymbol = false;
} else {
setDisplay(resultField.text + number);
}
});
}
function addOperatorButton(operatorSymbol, operator) {
addButton(operatorSymbol, function() {
if ((lastComputation.operator != null) && (lastComputation.value != null)) {
var inputValue = parseFloat(resultField.text);
setDisplay(lastComputation.operator.call(this, lastComputation.value, inputValue));
}
if (operator != null) {
lastComputation = {operator: operator, value: parseFloat(resultField.text)};
} else {
lastComputation = {operator: null, value: null};
}
clearOnNextSymbol = true
});
}
function addSignButton() {
addButton("+-", function() {
if (resultField.text.startsWith("-")) {
setDisplay(resultField.text.substring(1));
} else {
setDisplay("-" + resultField.text);
}
});
}
function addDotButton() {
addButton(".", function() {
var indexOfDot = resultField.text.indexOf(".");
if (indexOfDot >= 0) {
setDisplay(resultField.text.substring(0, indexOfDot));
} else {
if (clearOnNextSymbol) {
setDisplay("0.");
clearOnNextSymbol = false;
} else {
setDisplay(resultField.text + ".");
}
}
});
}
function addCancelEntryButton() {
addButton("CE", function() {
setDisplay("0");
});
}
function addCancelButton() {
addButton("C", function() {
setDisplay("0");
lastComputation = {operator: null, value: null};
});
}
function addButton(buttonLabel, eventHandler) {
var button = new JButton(buttonLabel);
button.setPreferredSize(new Dimension(widgetDimension, widgetDimension));
button.setFont(font);
registerKeyboardEventHandlerIfDefined(buttonLabel, eventHandler);
button.addActionListener(function() {
eventHandler.call(null);
});
button.addKeyListener(keyboardListener);
buttonsPanel.add(button);
}
function registerKeyboardEventHandlerIfDefined(buttonLabel, eventHandler) {
var keyboardKey = keyboardMappings[buttonLabel];
if (undefined !== keyboardKey) {
keyboardHandlers[keyboardKey] = eventHandler;
}
}
function keyboardListener(event, name) {
if ("keyTyped" === name) {
keyboardHandler = keyboardHandlers[event.getKeyChar()];
if (undefined !== keyboardHandler) {
keyboardHandler.call(null);
}
}
}
function setDisplay(content) {
resultField.text = content;
removeLeadingZeroIfNeeded();
}
function removeLeadingZeroIfNeeded() {
var leadingZeroRegex = new RegExp("^0[0-9]+");
match = leadingZeroRegex.exec(resultField.text);
if (null !== match) {
resultField.text = resultField.text.substring(1);
}
}