-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoSavingDialog_30072018_MS.java
More file actions
292 lines (206 loc) · 9.13 KB
/
Copy pathAutoSavingDialog_30072018_MS.java
File metadata and controls
292 lines (206 loc) · 9.13 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
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
package de.gsi.cs.co.ap.my_test_project.utils;
import java.io.File;
import java.util.regex.Pattern;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
// for library loggers
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;
// for application loggers
// import de.gsi.cs.co.ap.common.gui.elements.logger.AppLogger;
/**
* @author fschirru
*/
public class AutoSavingDialog {
// You can choose a logger (needed imports are given in the import section as comments):
// for libraries:
// private static final Logger LOGGER = LoggerFactory.getLogger(AutoSavingDialog.class);
// for applications:
// private static final AppLogger LOGGER = AppLogger.getLogger();
Stage autoSavingDialogStage;
private boolean destination_directory_isSelected;
private boolean isAutoSavingON_;
private final IntegerProperty destinationDirectoryIsSelected = new SimpleIntegerProperty();
private final IntegerProperty isAutoSavingON = new SimpleIntegerProperty();
final private Pattern validDoubleText = Pattern.compile("([1-9][0-9]{0,2})?$");
private String selected_directory;
private final TextField path_tf;
private final TextField savingFreq_tf;
private final Button path_apply_bt;
private final Button path_stop_bt;
private final Button path_exit_bt;
private final Separator hSeparator = new Separator();
private int savingFileFreq;
private static final AutoSavingDialog INSTANCE = new AutoSavingDialog();
public static AutoSavingDialog getInstance() {
return INSTANCE;
}
/**
*
*/
public AutoSavingDialog() {
savingFreq_tf = new TextField();
path_tf = new TextField();
hSeparator.setStyle("-fx-border-style: solid; -fx-border-width: 0.1px;");
autoSavingDialogStage = new Stage();
autoSavingDialogStage.setTitle("Save [AUTO]");
autoSavingDialogStage.setResizable(false);
autoSavingDialogStage.initModality(Modality.APPLICATION_MODAL);
final Group root = new Group();
final Scene scene = new Scene(root);
autoSavingDialogStage.setScene(scene);
final GridPane gridpane = new GridPane();
// gridpane.setPadding(new Insets(5));
gridpane.setHgap(10);
gridpane.setVgap(10);
final Label path_lb = new Label("Path: ");
gridpane.add(path_lb, 0, 1);
final Label savingFreq_lb = new Label("Frequency (min): ");
gridpane.add(savingFreq_lb, 0, 2);
// final TextField path_tf = new TextField("");
path_tf.setPrefWidth(500);
gridpane.add(path_tf, 1, 1);
/*
* savingFreq_tf.textProperty().addListener((observable, oldValue, newValue) -> {
* if (!newValue.matches("\\([1-9][0-9]{0,3})?$")) {
* savingFreq_tf.setText(newValue.replaceAll("^\\[1-9][0-9]{0,3}", ""));
* }
* });
*/
savingFreq_tf.textProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.matches(validDoubleText.toString())) {
savingFreq_tf.setText(oldValue);
}
});
savingFreq_tf.setMaxWidth(50);
gridpane.add(savingFreq_tf, 1, 2);
final VBox vBox = new VBox();
vBox.setSpacing(15);
vBox.setPadding(new Insets(10, 10, 10, 10));
final ButtonBar button_bar = new ButtonBar();
path_apply_bt = new Button("Start");
path_apply_bt.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent event) {
final File file = new File(path_tf.getText());
final boolean exists = file.exists();
if (exists) {
// It returns true if File or directory does exist
destinationDirectoryIsSelected.set(1);
destination_directory_isSelected = true;
isAutoSavingON.set(1);
isAutoSavingON_ = true;
setSelectedDirectory(path_tf.getText());
setSavingFileFreq(Integer.parseInt(savingFreq_tf.getText()));
// selected_directory = path_tf.getText();
// startDataRecording();
autoSavingDialogStage.close();
} else {
destination_directory_isSelected = false;
destinationDirectoryIsSelected.set(0);
isAutoSavingON_ = false;
isAutoSavingON.set(0);
// autoSavingDialogStage.close();
final Alert alert = new Alert(AlertType.ERROR, "The directory selected does not exists!",
ButtonType.CANCEL);
alert.showAndWait();
if (alert.getResult() == ButtonType.CANCEL) {
// autoSavingDialogStage.close();
// path_tf.clear();
path_tf.setText("/home/rifr/scheidenb/lnx/fmgstat");
alert.close();
}
}
}
});
// path_apply_bt.disableProperty().bind(Bindings.isEmpty(path_tf.textProperty()));
path_stop_bt = new Button("Stop");
path_stop_bt.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent event) {
isAutoSavingON_ = false;
isAutoSavingON.set(0);
autoSavingDialogStage.close();
}
});
path_exit_bt = new Button("Close");
path_exit_bt.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent event) {
autoSavingDialogStage.close();
// destination_directory_isSelected = false;
// setSelectedDirectory(null);
}
});
Platform.runLater(() -> path_exit_bt.requestFocus());
button_bar.getButtons().addAll(path_apply_bt, path_stop_bt, path_exit_bt);
button_bar.setPadding(new Insets(0, 0, 0, 0));
vBox.getChildren().addAll(gridpane, hSeparator, button_bar);
root.getChildren().add(vBox);
path_apply_bt.disableProperty().bind(Bindings.isEmpty(path_tf.textProperty()).or(isAutoSavingON.isEqualTo(1)));
path_stop_bt.disableProperty().bind(isAutoSavingON.isEqualTo(0));
Platform.runLater(() -> path_exit_bt.requestFocus());
}
public Stage getAutoSavingDialogStage() {
return autoSavingDialogStage;
}
public boolean getDestination_directory_isSelected() {
return destination_directory_isSelected;
}
public boolean getIsAutoSavingON() {
return isAutoSavingON_;
}
private void setSelectedDirectory(final String selectedDirectory) {
selected_directory = selectedDirectory;
}
public String getSelectedDirectory() {
return selected_directory;
}
private int setSavingFileFreq(final int savingFileFreq) {
this.savingFileFreq = savingFileFreq;
return savingFileFreq;
}
public int getSavingFileFreq() {
savingFileFreq = Integer.parseInt(savingFreq_tf.getText()) * 60000; // in milleseconds
return savingFileFreq;
}
public void setData() {
// destination_directory_isSelected = false;
// path_tf.clear();
path_tf.setText("/home/rifr/scheidenb/lnx/fmgstat");
savingFreq_tf.setText("1");
// this function is needed because till the stage is not shown we can not focus a node!
// All the request focus invoked before showing the scene are not considered. By the constructor it will be
// considered the first node by default
// The following line makes the trick to focus on the desired node! :D
Platform.runLater(() -> path_exit_bt.requestFocus());
}
public boolean retrieveDestinationDirectoryisSelected() {
return destination_directory_isSelected;
}
public IntegerProperty getDestinationDirectoryIsSelectedProperty() {
return destinationDirectoryIsSelected;
}
public IntegerProperty getIsAutoSavingOnProperty() {
return isAutoSavingON;
}
}