actionButton.java
1 |
|
2 |
import java.lang.Math; |
3 |
|
4 |
enum Action { |
5 |
ADDITION, DIFFERENCE, MULTIPLICATION, DIVISION, NEGATION, ABSOLUTE_VALUE, SINUS, COSINUS, TANGENS |
6 |
} |
7 |
|
8 |
class actionButton extends JButton { // Makes a button that, when pressed, does a given action. |
9 |
private Action m_action; |
10 |
public Action action() { |
11 |
return m_action; |
12 |
} |
13 |
public void setAction(Action action) { |
14 |
m_action = action; |
15 |
actionUpdated(); // The action has been updated, update the symbol too. |
16 |
} |
17 |
|
18 |
private void actionUpdated() { // Fired when the action has been altered, so the presented symbol can be updated accordingly. |
19 |
switch(action()) { |
20 |
case ADDITION: |
21 |
setText("+"); |
22 |
break; |
23 |
case DIFFERENCE: |
24 |
setText("-"); |
25 |
break; |
26 |
case MULTIPLICATION: |
27 |
setText("x"); |
28 |
break; |
29 |
case DIVISION: |
30 |
setText("/"); |
31 |
break; |
32 |
case NEGATION: |
33 |
setText("neg"); |
34 |
break; |
35 |
case ABSOLUTE_VALUE: |
36 |
setText("abs"); |
37 |
break; |
38 |
case SINUS: |
39 |
setText("sin"); |
40 |
break; |
41 |
case COSINUS: |
42 |
setText("cos"); |
43 |
break; |
44 |
case TANGENS: |
45 |
setText("tan"); |
46 |
break; |
47 |
} |
48 |
} |
49 |
|
50 |
/* public actionButton(Action newAction, boolean enabled = true) { |
51 |
setAction(newAction); // Set the given action to this button. |
52 |
setEnabled(enabled); // Set the state of the button. |
53 |
// handle events: pass to controller |
54 |
addActionListener(new ActionListener() { |
55 |
@Override |
56 |
public void actionPerformed(ActionEvent e) { |
57 |
((ClockController)getController()).onStart(); |
58 |
} |
59 |
}); |
60 |
}*/ |
61 |
} |
62 |