-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsView.java
191 lines (174 loc) · 4.69 KB
/
SettingsView.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
import java.util.function.UnaryOperator;
import java.util.Calendar;
import java.time.LocalDate;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.Event;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Region;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.application.Platform;
public class SettingsView extends View
{
@FXML private VBox root;
@FXML private TextField yearField;
@FXML private TextField monthField;
@FXML private TextField dayField;
@FXML private TextField hourField;
@FXML private Button hourAMToggle;
private boolean am_or_pm; //AM is false, PM is true
@FXML private TextField minuteField;
@FXML private TextField secondField;
public static UnaryOperator<Change> integerFilter(int minVal, int maxVal)
{
return change -> {
String text = change.getControlNewText();
if(text.isEmpty())
{
return change;
}
if(text.matches("[0-9]*"))
{
if(Integer.parseInt(text)>maxVal)
{
String newText = Integer.toString(maxVal);
change.setRange(0, change.getControlText().length());
change.setText(newText);
change.setCaretPosition(change.getControlNewText().length());
}
else if(Integer.parseInt(text)<minVal)
{
String newText = Integer.toString(minVal);
change.setRange(0, change.getControlText().length());
change.setText(newText);
change.setCaretPosition(change.getControlNewText().length());
}
return change;
}
return null;
};
}
public SettingsView()
{
}
public SettingsView(Controller cont)
{
super(cont);
am_or_pm = false;
Stage newRep = new Stage();
try
{
FXMLLoader ldr = new FXMLLoader(getClass().getResource("SettingsView.fxml"));
ldr.setController(this);
ldr.load();
newRep.setScene(new Scene(root));
yearField.setTextFormatter(new TextFormatter<String>(integerFilter(Integer.MIN_VALUE, Integer.MAX_VALUE)));
monthField.setTextFormatter(new TextFormatter<String>(integerFilter(1,12)));
dayField.setTextFormatter(new TextFormatter<String>(integerFilter(1,31)));
hourField.setTextFormatter(new TextFormatter<String>(integerFilter(1,12)));
minuteField.setTextFormatter(new TextFormatter<String>(integerFilter(0,59)));
secondField.setTextFormatter(new TextFormatter<String>(integerFilter(0,59)));
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
setRepresentation(newRep);
getRepresentation().getScene().getWindow().addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, event -> {System.exit(0);});
}
@FXML
public void onYearEntered(Event e)
{
if(!(yearField.getText().isEmpty()))
{
issueCommand(new SetYearCommand(Integer.parseInt(yearField.getText())));
yearField.setText("");
}
}
@FXML
public void onMonthEntered(Event e)
{
if(!(monthField.getText().isEmpty()))
{
issueCommand(new SetMonthCommand(Integer.parseInt(monthField.getText())));
monthField.setText("");
}
}
@FXML
public void onDayEntered(Event e)
{
if(!(dayField.getText().isEmpty()))
{
issueCommand(new SetDayCommand(Integer.parseInt(dayField.getText())));
dayField.setText("");
}
}
@FXML
public void onAMPMToggle(Event e)
{
am_or_pm = !am_or_pm;
hourAMToggle.setText(am_or_pm? "P.M." : "A.M.");
}
@FXML
public void onHourEntered(Event e)
{
if(!(hourField.getText().isEmpty()))
{
issueCommand(new SetHourCommand((Integer.parseInt(hourField.getText())%12)+(am_or_pm?12:0)));
hourField.setText("");
}
}
@FXML
public void onMinuteEntered(Event e)
{
if(!(minuteField.getText().isEmpty()))
{
issueCommand(new SetMinuteCommand(Integer.parseInt(minuteField.getText())));
minuteField.setText("");
}
}
@FXML
public void onSecondEntered(Event e)
{
if(!(secondField.getText().isEmpty()))
{
issueCommand(new SetSecondCommand(Integer.parseInt(secondField.getText())));
secondField.setText("");
}
}
@FXML
public void onUndo(Event e)
{
issueCommand(new UndoCommand());
}
@FXML
public void onRedo(Event e)
{
issueCommand(new RedoCommand());
}
@FXML
public void requestDigital(Event e)
{
issueCommand(new CreateDigitalClockCommand());
}
@FXML
public void requestAnalog(Event e)
{
issueCommand(new CreateAnalogClockCommand());
}
public void update(Calendar c)
{
dayField.setTextFormatter(new TextFormatter<String>(integerFilter(1,c.getActualMaximum(Calendar.DAY_OF_MONTH))));
}
}