-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditStockController.java
More file actions
127 lines (101 loc) · 3.83 KB
/
EditStockController.java
File metadata and controls
127 lines (101 loc) · 3.83 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
import javafx.fxml.FXML;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.ChoiceBox;
import javafx.event.EventHandler;
import javafx.stage.WindowEvent;
import javafx.collections.ObservableList;
import java.util.List;
import javafx.collections.FXCollections;
public class EditStockController
{
private Stage stage;
private MenuSceneController parent;
@FXML private TextField nameTextField;
@FXML private TextField priceTextField;
@FXML private TextField quantityTextField;
@FXML private ChoiceBox<Location> locationChoiceBox;
@FXML private Button saveButton;
@FXML private Button cancelButton;
private StockInformation stockInformation;
public EditStockController()
{
System.out.println("Initialising controllers...");
}
public void prepareStageEvents(Stage stage)
{
System.out.println("Preparing stage events...");
this.stage = stage;
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we) {
System.out.println("Close button was clicked!");
stage.close();
}
});
}
@FXML void initialize()
{
System.out.println("Asserting controls...");
try
{
assert nameTextField != null : "Can't find nameTextField";
assert priceTextField != null : "Can't find priceTextField";
assert locationChoiceBox != null : "Can't find locationChoiceBox";
assert saveButton != null : "Can't find saveButton";
assert cancelButton != null : "Can't find cancelButton";
}
catch (AssertionError ae)
{
System.out.println("FXML assertion failure: " + ae.getMessage());
Application.terminate();
}
System.out.println("Populating scene with items from the database...");
List<Location> productList = locationChoiceBox.getItems();
Location.readAll(productList);
locationChoiceBox.getSelectionModel().selectFirst();
}
public void setParent(MenuSceneController parent)
{
this.parent = parent;
}
public void loadItem(int productID)
{
stockInformation = StockInformation.getByProductID(productID);
nameTextField.setText(stockInformation.getProductName());
priceTextField.setText(stockInformation.getProductPriceString());
quantityTextField.setText(stockInformation.getProductQuantityString());
List<Location> productList = locationChoiceBox.getItems();
for(Location l : productList)
{
if (l.id == stockInformation.getLocationID())
{
locationChoiceBox.getSelectionModel().select(l);
}
}
}
@FXML void saveButtonClicked()
{
System.out.println("Save button clicked!");
double price = Double.parseDouble(priceTextField.getText());
int quantity = Integer.parseInt(quantityTextField.getText());
price = Math.rint(price * 100) / 100;
if (stockInformation == null)
{
stockInformation = new StockInformation();
}
stockInformation.setProductName(nameTextField.getText());
stockInformation.setProductPrice(price);
stockInformation.setProductQuantity(quantity);
Location selectedLocation = (Location) locationChoiceBox.getSelectionModel().getSelectedItem();
stockInformation.setLocationID(selectedLocation.id);
stockInformation.save();
parent.refreshTable();
stage.close();
}
@FXML void cancelButtonClicked()
{
System.out.println("Cancel button clicked!");
stage.close();
}
}