-
Notifications
You must be signed in to change notification settings - Fork 98
/
CalculatorButtonsComposite.java
375 lines (333 loc) · 10.9 KB
/
CalculatorButtonsComposite.java
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*******************************************************************************
* Copyright (c) 2012 Laurent CARON
* All rights reserved.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Laurent CARON (laurent.caron at gmail dot com) - Initial API and implementation
*******************************************************************************/
package org.eclipse.nebula.widgets.opal.calculator;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.nebula.widgets.opal.commons.SWTGraphicUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
/**
* This composite contains all buttons
*/
class CalculatorButtonsComposite extends Composite {
private static final String LABEL_C = "C";
private static final String LABEL_CE = "CE";
private static final String LABEL_BACK = "Back";
private final Color darkRedColor;
private final Color darkBlueColor;
private final CalculatorEngine engine;
private Label displayArea;
private Listener keyListener;
private final List<ModifyListener> modifyListeners;
private final CalculatorButtonsBehaviourEngine behaviourEngine;
private boolean readyToEnterNewNumber;
/**
* Constructor
*
* @param parent parent composite
* @param style style
*/
CalculatorButtonsComposite(final Composite parent, final int style) {
super(parent, style);
setLayout(new GridLayout(5, false));
darkRedColor = new Color(getDisplay(), 139, 0, 0);
darkBlueColor = new Color(getDisplay(), 0, 0, 139);
createButtons();
SWTGraphicUtil.addDisposer(this, darkBlueColor);
SWTGraphicUtil.addDisposer(this, darkRedColor);
engine = new CalculatorEngine(this);
behaviourEngine = new CalculatorButtonsBehaviourEngine(this);
addKeyListeners();
modifyListeners = new ArrayList<ModifyListener>();
}
/**
* Create all buttons
*/
private void createButtons() {
final Button buttonBackSpace = createButton(LABEL_BACK, darkRedColor);
buttonBackSpace.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false, 3, 1));
buttonBackSpace.addListener(SWT.Selection, e -> {
behaviourEngine.processBackSpace();
});
final Button buttonCe = createButton(LABEL_CE, darkRedColor);
buttonCe.addListener(SWT.Selection, e -> {
behaviourEngine.clearResult();
});
final Button buttonC = createButton(LABEL_C, darkRedColor);
buttonC.addListener(SWT.Selection, e -> {
behaviourEngine.clearResult();
engine.cancel();
});
createDigitButton(7);
createDigitButton(8);
createDigitButton(9);
final Button buttonDivide = createButton(CalculatorEngine.OPERATOR_DIVIDE, getDisplay().getSystemColor(SWT.COLOR_RED));
buttonDivide.addListener(SWT.Selection, e -> {
engine.processOperation(CalculatorEngine.OPERATOR_DIVIDE);
});
final Button buttonSqrt = createButton("\u221A", darkRedColor);
buttonSqrt.addListener(SWT.Selection, e -> {
engine.processSquareRootOperation();
});
createDigitButton(4);
createDigitButton(5);
createDigitButton(6);
final Button buttonMultiply = createButton(CalculatorEngine.OPERATOR_MULTIPLY, getDisplay().getSystemColor(SWT.COLOR_RED));
buttonMultiply.addListener(SWT.Selection, e -> {
engine.processOperation(CalculatorEngine.OPERATOR_MULTIPLY);
});
final Button buttonInverse = createButton("1/x", darkBlueColor);
buttonInverse.addListener(SWT.Selection, e -> {
engine.processInverseOperation();
});
createDigitButton(1);
createDigitButton(2);
createDigitButton(3);
final Button buttonMinus = createButton(CalculatorEngine.OPERATOR_MINUS, getDisplay().getSystemColor(SWT.COLOR_RED));
buttonMinus.addListener(SWT.Selection, e -> {
engine.processOperation(CalculatorEngine.OPERATOR_MINUS);
});
final Button buttonPercent = createButton("%", darkBlueColor);
buttonPercent.addListener(SWT.Selection, e -> {
engine.processPerCentageOperation();
});
createDigitButton(0);
final Button buttonPlusMinus = createButton("+/-", getDisplay().getSystemColor(SWT.COLOR_BLUE));
buttonPlusMinus.addListener(SWT.Selection, e -> {
engine.processSignChange();
});
final Button buttonDot = createButton(".", getDisplay().getSystemColor(SWT.COLOR_BLUE));
buttonDot.addListener(SWT.Selection, e -> {
behaviourEngine.addDecimalPoint();
});
final Button buttonPlus = createButton(CalculatorEngine.OPERATOR_PLUS, getDisplay().getSystemColor(SWT.COLOR_RED));
buttonPlus.addListener(SWT.Selection, e -> {
engine.processOperation(CalculatorEngine.OPERATOR_PLUS);
});
final Button buttonEquals = createButton("=", getDisplay().getSystemColor(SWT.COLOR_RED));
buttonEquals.addListener(SWT.Selection, e -> {
engine.processEquals();
});
}
private void createDigitButton(final int digit) {
final Button button = createButton(" " + digit + " ", getDisplay().getSystemColor(SWT.COLOR_BLUE));
button.addListener(SWT.Selection, e -> {
behaviourEngine.addDigitToDisplay(digit);
});
}
private Button createButton(final String label, final Color color) {
final Button button = new Button(this, SWT.PUSH | SWT.DOUBLE_BUFFERED);
button.setText("");
final GridData gd = new GridData(GridData.FILL, GridData.FILL, false, false);
gd.widthHint = 30;
button.setLayoutData(gd);
// Use a paint listener because setForeground is not working on Windows
button.addPaintListener(e -> {
e.gc.setForeground(color);
e.gc.setFont(getFont());
final Point textSize = e.gc.textExtent(" " + label + " ", SWT.TRANSPARENT);
e.gc.drawText(" " + label + " ", (button.getBounds().width - textSize.x) / 2, (button.getBounds().height - textSize.y) / 2, true);
});
return button;
}
/**
* Add key listeners
*/
private void addKeyListeners() {
keyListener = e -> {
switch (e.character) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
behaviourEngine.addDigitToDisplay(Integer.parseInt(String.valueOf(e.character)));
return;
case '.':
behaviourEngine.addDecimalPoint();
return;
case '+':
engine.processOperation(CalculatorEngine.OPERATOR_PLUS);
return;
case '-':
engine.processOperation(CalculatorEngine.OPERATOR_MINUS);
return;
case '*':
engine.processOperation(CalculatorEngine.OPERATOR_MULTIPLY);
return;
case '/':
engine.processOperation(CalculatorEngine.OPERATOR_DIVIDE);
return;
case '=':
engine.processEquals();
return;
case '%':
engine.processPerCentageOperation();
return;
}
switch (e.keyCode) {
case SWT.KEYPAD_0:
case SWT.KEYPAD_1:
case SWT.KEYPAD_2:
case SWT.KEYPAD_3:
case SWT.KEYPAD_4:
case SWT.KEYPAD_5:
case SWT.KEYPAD_6:
case SWT.KEYPAD_7:
case SWT.KEYPAD_8:
case SWT.KEYPAD_9:
final int digit = e.keyCode - SWT.KEYCODE_BIT - 47;
behaviourEngine.addDigitToDisplay(digit);
return;
case SWT.KEYPAD_ADD:
engine.processOperation(CalculatorEngine.OPERATOR_PLUS);
return;
case SWT.KEYPAD_SUBTRACT:
engine.processOperation(CalculatorEngine.OPERATOR_MINUS);
return;
case SWT.KEYPAD_DIVIDE:
engine.processOperation(CalculatorEngine.OPERATOR_DIVIDE);
return;
case SWT.KEYPAD_MULTIPLY:
engine.processOperation(CalculatorEngine.OPERATOR_MULTIPLY);
return;
case SWT.KEYPAD_CR:
case SWT.KEYPAD_EQUAL:
case SWT.CR:
engine.processEquals();
return;
case SWT.BS:
behaviourEngine.processBackSpace();
return;
case SWT.ESC:
behaviourEngine.clearResult();
engine.cancel();
return;
}
};
for (final Control control : getChildren()) {
control.addListener(SWT.KeyDown, keyListener);
}
addListener(SWT.KeyDown, keyListener);
}
/**
* Adds the listener to the collection of listeners who will be notified when
* the receiver's text is modified, by sending it one of the messages defined in
* the <code>ModifyListener</code> interface.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been
* disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
* thread that created the receiver</li>
* </ul>
*
* @see ModifyListener
* @see #removeModifyListener
*/
public void addModifyListener(final ModifyListener listener) {
checkWidget();
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
modifyListeners.add(listener);
}
/**
* Fire the modify listeners
*/
void fireModifyListeners() {
for (final ModifyListener listener : modifyListeners) {
final Event e = new Event();
e.widget = this;
final ModifyEvent modifyEvent = new ModifyEvent(e);
listener.modifyText(modifyEvent);
}
}
/**
* @return the keyListener
*/
Listener getKeyListener() {
return keyListener;
}
/**
* @return the text
*/
Label getDisplayArea() {
return displayArea;
}
/**
* Removes the listener from the collection of listeners who will be notified
* when the receiver's text is modified.
*
* @param listener the listener which should no longer be notified
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been
* disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
* thread that created the receiver</li>
* </ul>
*
* @see ModifyListener
* @see #addModifyListener
*/
public void removeModifyListener(final ModifyListener listener) {
checkWidget();
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
modifyListeners.remove(listener);
}
/**
* @param text the text to set
*/
void setDisplayArea(final Label text) {
displayArea = text;
}
void setReadyToEnterNewNumber(final boolean newValue) {
readyToEnterNewNumber = newValue;
}
boolean isReadyToEnterNewNumber() {
return readyToEnterNewNumber;
}
}