-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculateController.java
More file actions
226 lines (164 loc) · 6.82 KB
/
Copy pathCalculateController.java
File metadata and controls
226 lines (164 loc) · 6.82 KB
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
package wf.view;
import java.util.regex.Pattern;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import wf.Main;
import wf.util.CalculateTask;
import wf.util.DataHistoHandler;
import wf.util.RetrieveDateAndTime;
public class CalculateController {
@FXML private AnchorPane calculate_ap;
@FXML private AnchorPane bottom_ap;
@FXML private AnchorPane top_ap;
@FXML private Button apply_bt;
@FXML private Button close_bt;
@FXML private ButtonBar bar_bb;
//@FXML private ComboBox<String> smoothingPoints_cb;
@FXML private GridPane grid_gr;
@FXML private Label title_lb;
@FXML private Label progressBar_lb;
@FXML private Label peakFinderParam1_lb;
@FXML private Label peakFinderParam2_lb;
@FXML private ProgressBar progressBar_pb;
@FXML private Separator north_sp;
@FXML private Separator south_sp;
@FXML private TextField peakFinderParam1_tf;
@FXML private TextField peakFinderParam2_tf;
@FXML private VBox calculate_vb;
private final String WORK_DONE_LABEL_FORMAT = "%.1f";
private final Pattern int_pt = Pattern.compile("\\d{0,3}?$");
private final Pattern num_pt = Pattern.compile("\\d{0,3}(\\.\\d{0,3})?$");
private static CalculateTask calculateTask = CalculateTask.getInstance();
private static DataHistoHandler dataHistoHandler = DataHistoHandler.getInstance();
private BooleanProperty calculationStarted = new SimpleBooleanProperty(false);
//private int maxNumberOfSeries; //max of the series regardless of the channel
private int totalNumberOfSeries;
@FXML
public void initialize() {
initStyle();
initTextFieldListeners();
bindingApplyButton();
peakFinderParam1_lb.setPrefWidth(140);
peakFinderParam2_lb.setPrefWidth(140);
peakFinderParam1_tf.setMaxWidth(120);
peakFinderParam2_tf.setMaxWidth(120);
progressBar_pb.setMaxWidth(Double.MAX_VALUE); // allows the progress bar to expand to fill available horizontal space.
progressBar_pb.setProgress(0.0);
}
@FXML
private void exitAction() {
progressBar_lb.textProperty().unbind();
progressBar_pb.progressProperty().unbind();
calculate_ap.getScene().getWindow().hide();
}
@FXML
private void applyAction() {
close_bt.setDisable(true);
calculationStarted.setValue(true);
totalNumberOfSeries = dataHistoHandler.getTotalNumberOfSeries();
//perform calculation
progressBar_pb.progressProperty().bind(CalculateTask.getInstance().progressProperty());
progressBar_lb.textProperty().bind(
Bindings.format("Calculation started... " + WORK_DONE_LABEL_FORMAT + " %%",
CalculateTask.getInstance().progressProperty().multiply((double)100))
);
//Start task (calculation waveform parameters)
// Get the loading Task
final Task<Void> task = calculateTask.performCalculation(Integer.parseInt(peakFinderParam1_tf.getText()),
Double.parseDouble(peakFinderParam2_tf.getText()), totalNumberOfSeries);
// Catch any exceptions
task.setOnFailed(wse -> {
System.out.println("Error");
task.getException().printStackTrace();
dataHistoHandler.setDataCalculated(0);
});
// Print success when complete
task.setOnSucceeded(wse -> {
close_bt.setDisable(false);
calculationStarted.setValue(false);
resultMessage();
dataHistoHandler.setDataCalculated(1);
});
new Thread(task).start();
}
private void bindingApplyButton() {
apply_bt.disableProperty().bind(((peakFinderParam1_tf.textProperty().isEmpty()
.or(peakFinderParam2_tf.textProperty().isEmpty()))).or(calculationStarted)
.or((peakFinderParam1_tf.textProperty().isEqualTo("0")
.or(peakFinderParam2_tf.textProperty().isEqualTo("0")))));
}
private void initTextFieldListeners() {
peakFinderParam1_tf.textProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.matches(int_pt.toString())) {
peakFinderParam1_tf.setText(oldValue);
}
if(newValue.length() > 1 && newValue.subSequence(0, 1).equals("0")
&& newValue.subSequence(1, 2).equals("0")) {
peakFinderParam1_tf.setText(oldValue);
}
if(newValue.length() > 0 && newValue.subSequence(0, 1).equals("0")) {
peakFinderParam1_tf.setText(oldValue);
}
/*
if(newValue.length() == 1 && newValue.subSequence(0, 1).equals(".")) {
peakFinderParam1_tf.setText(oldValue);
}
if(newValue.length() > 1 && newValue.subSequence(0, 1).equals("0")
&& newValue.subSequence(1, 2).equals("0")) {
peakFinderParam1_tf.setText(oldValue);
}*/
});
peakFinderParam2_tf.textProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.matches(num_pt.toString())) {
peakFinderParam2_tf.setText(oldValue);
}
if(newValue.length() == 1 && newValue.subSequence(0, 1).equals(".")) {
peakFinderParam2_tf.setText(oldValue);
}
if(newValue.length() > 1 && newValue.subSequence(0, 1).equals("0")
&& newValue.subSequence(1, 2).equals("0")) {
peakFinderParam2_tf.setText(oldValue);
}
});
}
public void reset() {
progressBar_lb.setText("Calculation: not started");
peakFinderParam1_tf.setText("");
peakFinderParam2_tf.setText("");
progressBar_pb.setProgress(0);
}
private void resultMessage() {
Main.updateTextArea("\n" + RetrieveDateAndTime.getDateAndTime()
+ " - Calculation performed with [" + peakFinderParam1_tf.getText() +"] points");
Main.updateTextArea("\n" + RetrieveDateAndTime.getDateAndTime()
+ " - Calculation performed with [" + peakFinderParam2_tf.getText() +"] threshold value" );
}
private void initStyle() {
calculate_vb.setStyle(" -fx-background-color: FloralWhite;");
apply_bt.setId("button_bevel-grey");
close_bt.setId("button_bevel-grey");
title_lb.setId("appTitle");
progressBar_lb.setId("labelDecoratedNoBkG");
peakFinderParam1_lb.setId("labelDecoratedNoBkG");
peakFinderParam2_lb.setId("labelDecoratedNoBkG");
top_ap.setId("topLayoutContainer");
bottom_ap.setId("bottomLayoutContainer");
calculate_ap.setId("appBackground");
north_sp.setStyle("-fx-border-style: solid; -fx-border-width: 0.1px;");
south_sp.setStyle("-fx-border-style: solid; -fx-border-width: 0.1px;");
peakFinderParam1_tf.setId("textField2");
peakFinderParam2_tf.setId("textField2");
}
}