forked from AlexTheaker04/DCSS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackup.java
More file actions
441 lines (340 loc) · 15.6 KB
/
Backup.java
File metadata and controls
441 lines (340 loc) · 15.6 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package application;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import javafx.geometry.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.paint.Color;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Backup extends Application {
// Create a Stage and Scenes.
Stage mainWindow;
Scene mainScene, viewScene, editScene, addScene;
// Create TextFields.
TextField chemicalNameInput, chemicalMassInput, chemicalVolumeInput, chemicalFormulaInput, chemicalEntryInput, chemicalExpiryInput;
// Create two TableViews.
TableView<editChemicalTable> chemicalTable;
TableView<editChemicalTable> viewCSVTable;
public static void main(String[] args) {
launch(args);
}
/** @author: Andrey Zinovyev.
* @date: 1/7/2021.
*
* start() Displays the starting screen as well as contains all the other scenes in the program.
*
* start() Contains the scenes which are used to create the program, it contains all the necessary
* methods and functions used to make the program run. This method is also responsible for all the
*
* @param Stage mainWindow: Takes in mainWindow.
*
* @throws Exception: If something goes wrong during run, program will not crash.
*
* @return void: Does not return anything.
*/
@Override
public void start(Stage mainWindow) throws Exception{
// Create backgrounds.
BackgroundFill lightGreenBG = new BackgroundFill(Color.LIGHTGREEN, CornerRadii.EMPTY, Insets.EMPTY);
BackgroundFill lightBlueBG = new BackgroundFill(Color.LIGHTBLUE, CornerRadii.EMPTY, Insets.EMPTY);
BackgroundFill lightYellowBG = new BackgroundFill(Color.LIGHTYELLOW, CornerRadii.EMPTY, Insets.EMPTY);
Background mainBG = new Background(lightGreenBG);
Background viewBG = new Background(lightBlueBG);
Background addBG = new Background(lightYellowBG);
// Custom fonts.
Font hugeFont = Font.font("Arial", FontWeight.BOLD, 80);
Font bigFont = Font.font("Courier New", FontWeight.BOLD, 36);
Font mediumFont = Font.font("Courier New", FontWeight.BOLD, 20);
// Create Title label.
Label dcssLabel = new Label("Digital Chemical\nStorage System");
dcssLabel.setFont(hugeFont);
dcssLabel.setTextAlignment(TextAlignment.CENTER);
dcssLabel.setTranslateY(-350);
dcssLabel.setTextFill(Color.color(0, 0, 1));
// Generate a CSV file to contain the information if one does not already exist
File file = CSVCode.generateFile();
// Obtain the current date and set the entry date as the current date
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDateTime currentDate = LocalDateTime.now();
String date = format.format(currentDate);
// VIEW FILE BUTTON AND SCENE
// Create the 'View File' button.
Button csvButton = new Button("View/Edit File");
csvButton.setOnAction(e -> {
// Set the scene as the viewScene
mainWindow.setScene(viewScene);
});
csvButton.setFont(bigFont);
csvButton.setTranslateY(-100);
// Create a column for the chemical name
TableColumn<editChemicalTable, String> chemicalName = new TableColumn<>("Chemical Name");
chemicalName.setMinWidth(200);
chemicalName.setCellValueFactory(new PropertyValueFactory<>("chemicalName"));
// Create a column for the chemical mass
TableColumn<editChemicalTable, String> chemicalMass = new TableColumn<>("Mass");
chemicalMass.setMinWidth(200);
chemicalMass.setCellValueFactory(new PropertyValueFactory<>("chemicalMass"));
// Create a column for the chemical volume
TableColumn<editChemicalTable, String> chemicalVolume = new TableColumn<>("Volume");
chemicalVolume.setMinWidth(200);
chemicalVolume.setCellValueFactory(new PropertyValueFactory<>("chemicalVolume"));
// Create a column for the chemical formula
TableColumn<editChemicalTable, String> chemicalFormula = new TableColumn<>("Formula");
chemicalFormula.setMinWidth(200);
chemicalFormula.setCellValueFactory(new PropertyValueFactory<>("chemicalFormula"));
// Create a column for the entry date
TableColumn<editChemicalTable, String> chemicalEntry = new TableColumn<>("Date of Entry");
chemicalEntry.setMinWidth(200);
chemicalEntry.setCellValueFactory(new PropertyValueFactory<>("dateOfEntry"));
// Create a column for the expiry date
TableColumn<editChemicalTable, String> chemicalExpiry = new TableColumn<>("Date of Expiry");
chemicalExpiry.setMinWidth(200);
chemicalExpiry.setCellValueFactory(new PropertyValueFactory<>("dateOfExpiry"));
// Format the TableView with the created columns
viewCSVTable = new TableView<>();
viewCSVTable.setItems(addChemicals());
viewCSVTable.getColumns().addAll(chemicalName, chemicalMass, chemicalVolume, chemicalFormula,
chemicalEntry, chemicalExpiry);
// Read the CSV file and display its contents.
CSVCode.viewFile(file, viewCSVTable);
// Create 'Back' button for view file scene.
Button viewBackButton = new Button("Back");
viewBackButton.setFont(mediumFont);
viewBackButton.setOnAction(e -> {
// Set the scene as the main scene
mainWindow.setScene(mainScene);
});
// Create the "view file" scene.
HBox viewMiddleMenu = new HBox();
viewMiddleMenu.getChildren().add(viewCSVTable);
HBox viewBottomMenu = new HBox();
viewBottomMenu.getChildren().addAll(viewBackButton);
BorderPane viewBorderPane = new BorderPane();
viewBorderPane.setCenter(viewMiddleMenu);
viewBorderPane.setBottom(viewBottomMenu);
viewBorderPane.setBackground(viewBG);
viewScene = new Scene(viewBorderPane, 1200, 1000);
// ADD/DELETE BUTTON AND SCENE.
// Create the 'Add/Delete' button.
Button addButton = new Button("Add/Delete");
addButton.setOnAction(e -> {
// Code Here.
mainWindow.setScene(addScene);
});
addButton.setFont(bigFont);
// Name column.
TableColumn <editChemicalTable, String> chemicalNameColumn = new TableColumn<>("Chemical Name");
chemicalNameColumn.setMinWidth(200);
chemicalNameColumn.setCellValueFactory(new PropertyValueFactory<>("chemicalName"));
// Mass column.
TableColumn <editChemicalTable, Double> chemicalMassColumn = new TableColumn<>("Mass");
chemicalMassColumn.setMinWidth(200);
chemicalMassColumn.setCellValueFactory(new PropertyValueFactory<>("chemicalMass"));
// Volume column.
TableColumn <editChemicalTable, Double> chemicalVolumeColumn = new TableColumn<>("Volume");
chemicalVolumeColumn.setMinWidth(200);
chemicalVolumeColumn.setCellValueFactory(new PropertyValueFactory<>("chemicalVolume"));
// Formula column.
TableColumn <editChemicalTable, String> chemicalFormulaColumn = new TableColumn<>("Formula");
chemicalFormulaColumn.setMinWidth(200);
chemicalFormulaColumn.setCellValueFactory(new PropertyValueFactory<>("chemicalFormula"));
// Date of entry column.
TableColumn <editChemicalTable, String> chemicalEntryColumn = new TableColumn<>("Date of Entry");
chemicalEntryColumn.setMinWidth(200);
chemicalEntryColumn.setCellValueFactory(new PropertyValueFactory<>("dateOfEntry"));
// Date of expiry column.
TableColumn <editChemicalTable, String> chemicalExpiryColumn = new TableColumn<>("Date of Expiry");
chemicalExpiryColumn.setMinWidth(200);
chemicalExpiryColumn.setCellValueFactory(new PropertyValueFactory<>("dateOfExpiry"));
// Chemical Name.
chemicalNameInput = new TextField();
chemicalNameInput.setPromptText("Chemical Name");
chemicalNameInput.setMinWidth(100);
// Chemical Mass.
chemicalMassInput = new TextField();
chemicalMassInput.setPromptText("Mass");
chemicalMassInput.setMinWidth(100);
// Chemical Volume.
chemicalVolumeInput = new TextField();
chemicalVolumeInput.setPromptText("Volume");
chemicalVolumeInput.setMinWidth(100);
// Chemical Formula.
chemicalFormulaInput = new TextField();
chemicalFormulaInput.setPromptText("Formula");
chemicalFormulaInput.setMinWidth(100);
// Chemical Entry Date.
chemicalEntryInput = new TextField();
chemicalEntryInput.setPromptText("Entry Date");
chemicalEntryInput.setMinWidth(100);
// Chemical Expiry Date.
chemicalExpiryInput = new TextField();
chemicalExpiryInput.setPromptText("Expiry Date");
chemicalExpiryInput.setMinWidth(100);
// Buttons (Add/Delete).
Button addChemicalButton = new Button("Add");
addChemicalButton.setFont(mediumFont);
addChemicalButton.setOnAction(e -> addButtonClicked(file, date));
Button deleteChemicalButton = new Button("Delete");
deleteChemicalButton.setFont(mediumFont);
deleteChemicalButton.setOnAction(e -> deleteButtonClicked(file));
Button updateChemicalButton = new Button("Update");
updateChemicalButton.setFont(mediumFont);
//updateChemicalButton.setOnAction(e -> updateChemicalButton(file));
// Create 'Back' button for view chemicals scene.
Button addBackButton = new Button("Back");
addBackButton.setFont(mediumFont);
addBackButton.setOnAction(e -> {
// Code Here.
mainWindow.setScene(mainScene);
});
HBox textField = new HBox();
textField.setPadding(new Insets(10, 10, 10, 10));
textField.setSpacing(10);
textField.getChildren().addAll(addBackButton, chemicalNameInput, chemicalMassInput, chemicalVolumeInput, chemicalFormulaInput,
chemicalExpiryInput, addChemicalButton, deleteChemicalButton, updateChemicalButton);
chemicalTable = new TableView<>();
chemicalTable.setItems(addChemicals());
chemicalTable.getColumns().addAll(chemicalNameColumn, chemicalMassColumn, chemicalVolumeColumn, chemicalFormulaColumn,
chemicalEntryColumn, chemicalExpiryColumn);
// Read the CSV file and display its contents.
CSVCode.viewFile(file, chemicalTable);
// Create add chemical scene.
HBox addMiddleMenu = new HBox();
addMiddleMenu.getChildren().addAll(chemicalTable);
HBox addBottomMenu = new HBox();
addBottomMenu.getChildren().addAll(textField);
BorderPane addBorderPane = new BorderPane();
addBorderPane.setCenter(addMiddleMenu);
addBorderPane.setBottom(addBottomMenu);
addBorderPane.setBackground(addBG);
addScene = new Scene(addBorderPane, 1200, 1000);
// MAIN BUTTONS AND SCENE.
Button closeButton = new Button("Exit");
closeButton.setOnAction(e -> {
boolean result = ExitBox.display("Exit Dialogue", "Are you sure you want to exit? This action will close the "
+ "program.\nAll work will be automatically saved to the CSV file.");
if (result == true) {
// ANY SAVES HERE.
mainWindow.close();
}
});
closeButton.setFont(bigFont);
closeButton.setTranslateY(100);
// Create main scene.
StackPane mainLayout = new StackPane();
mainLayout.setBackground(mainBG);
mainLayout.getChildren().addAll(dcssLabel, csvButton, addButton, closeButton);
mainScene = new Scene(mainLayout, 1200, 1000);
// Show the mainWindow
mainWindow.setScene(mainScene);
mainWindow.setTitle("Digital Chemical Storage System");
mainWindow.setResizable(true); // Makes it not resizable.
mainWindow.setOnCloseRequest(e -> {
boolean result = ExitBox.display("Exit Dialogue", "Are you sure you want to exit? This action will close the "
+ "program.\nAll work will be automatically saved to the CSV file.");
e.consume();
if (result == true) {
// ANY SAVES HERE.
mainWindow.close();
}
});
mainWindow.show();
}
/** @author: Andrey Zinovyev.
* @date: 1/7/2021.
*
* start() Displays the starting screen as well as contains all the other scenes in the program.
*
* start() Contains the scenes which are used to create the program, it contains all the necessary
* methods and functions used to make the program run. This method is also responsible for all the
*
* @param Stage mainWindow: Takes in mainWindow.
*
* @throws Exception: If something goes wrong during run, program will not crash.
*
* @return void: Does not return anything.
*/
public void addButtonClicked(File file, String date) {
editChemicalTable item = new editChemicalTable();
// Get the text entered into the text fields.
item.setChemicalName(chemicalNameInput.getText());
item.setChemicalMass(Double.parseDouble(chemicalMassInput.getText()));
item.setChemicalVolume(Double.parseDouble(chemicalVolumeInput.getText()));
item.setChemicalFormula(chemicalFormulaInput.getText());
// item.setDateOfEntry(chemicalEntryInput.getText());
item.setDateOfEntry(date);
item.setDateOfExpiry(chemicalExpiryInput.getText());
// Get the items in the text field and add them to the table.
chemicalTable.getItems().add(item);
// Create an array to store all the information on the current chemical
String[] chemicalInformation = new String[6];
// Populate the array based on user inputs in the text fields
chemicalInformation[0] = chemicalNameInput.getText();
chemicalInformation[1] = chemicalMassInput.getText();
chemicalInformation[2] = chemicalVolumeInput.getText();
chemicalInformation[3] = chemicalFormulaInput.getText();
// Set the entry date as the current date
chemicalInformation[4] = date;
chemicalInformation[5] = chemicalExpiryInput.getText();
try {
// Add the information in the array into the CSV file
CSVCode.addFileData(file, chemicalInformation);
} catch (IOException e) {
// Catch any exceptions
e.printStackTrace();
}
// Clear the text fields.
chemicalNameInput.clear();
chemicalMassInput.clear();
chemicalVolumeInput.clear();
chemicalFormulaInput.clear();
chemicalEntryInput.clear();
chemicalExpiryInput.clear();
}
// Delete button Clicked.
public void deleteButtonClicked(File file) {
// Create an Observable list.
ObservableList<editChemicalTable> itemSelected, allItems;
// Get all the items in the table.
allItems = chemicalTable.getItems();
// Get the selected row.
itemSelected = chemicalTable.getSelectionModel().getSelectedItems();
System.out.println(itemSelected);
// Put the information in the selected row into the text fields
// Create an array to store the information in the text fields
String[] chemicalInformation = new String[6];
// Populate the array based on the information in the text fields
chemicalInformation[0] = chemicalNameInput.getText();
chemicalInformation[1] = chemicalMassInput.getText();
chemicalInformation[2] = chemicalVolumeInput.getText();
chemicalInformation[3] = chemicalFormulaInput.getText();
chemicalInformation[4] = chemicalEntryInput.getText();
chemicalInformation[5] = chemicalExpiryInput.getText();
try {
// Delete the selected row from the CSV file
CSVCode.deleteFileData(file, chemicalInformation);
} catch (IOException e) {
// Catch any exceptions
e.printStackTrace();
}
// Remove the selected row's items.
itemSelected.forEach(allItems::remove);
}
// Get all of the products.
public ObservableList<editChemicalTable> addChemicals() {
// Create a new Observable list with an array to add the chemicals.
ObservableList<editChemicalTable> chemicals = FXCollections.observableArrayList();
// Add one placeholder element to the table.
//chemicals.add(new editChemicalTable("----", 0000, 0000, "----", "----", "----"));
// Return the chemicals.
return chemicals;
}
}