-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.java
280 lines (230 loc) · 9.51 KB
/
Controller.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
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
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.stage.Stage;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.Scanner;
/**
* Author: Asif Qureshi, Cole Keller, Goldhuli, Sharjeel
* Controller class handle GUI manipulation according to the current state of the B+ Tree.
* It's control UI.fxml.
*/
public class Controller implements Initializable {
/**
* Binding fxml components
*/
@FXML
TextField productField, descriptionField;
@FXML
RadioButton searchRadio, insertRadio, deleteRadio, updateRadio;
@FXML
Button exitButton, executeButton;
@FXML
Label resultText, reportText;
@FXML
TreeView<String> treeView;
Stage stage;
BPlusTree bPlusTree;
@Override
public void initialize(URL location, ResourceBundle resources) {
//Grouped all radio buttons
ToggleGroup group = new ToggleGroup();
searchRadio.setToggleGroup(group);
insertRadio.setToggleGroup(group);
deleteRadio.setToggleGroup(group);
updateRadio.setToggleGroup(group);
//On load Search radio button selected by default
searchRadio.setSelected(true);
//Search radio button selected means no need for description
descriptionField.setDisable(true);
//initiate B+ tree
bPlusTree = new BPlusTree();
//load data from file
loadData();
//built the tree virtually
buildTree();
//add action event to different elements
addActions();
}
/**
* Helper method to add action event in GUI component
*/
private void addActions() {
//exit will be handled by close method
exitButton.setOnAction(event -> close());
//description field will only available for update and insert
updateRadio.setOnAction(event -> descriptionField.setDisable(false));
searchRadio.setOnAction(event -> descriptionField.setDisable(true));
deleteRadio.setOnAction(event -> descriptionField.setDisable(true));
insertRadio.setOnAction(event -> descriptionField.setDisable(false));
executeButton.setOnAction(event -> {
String id = productField.getText().toUpperCase();
String description = descriptionField.getText();
//Check all available field are filled up or not...
if (id.equals("") || (!descriptionField.isDisabled() && description.equals(""))) {
//show alert to fill all fields...
ButtonType yes = new ButtonType("yes", ButtonBar.ButtonData.OK_DONE);
Alert alert = new Alert(Alert.AlertType.WARNING,
"Please fill up the field first!", yes);
alert.show();
} else {
//all available field are filled up...
if (searchRadio.isSelected()) {
String productDescription = bPlusTree.search(id);
if (productDescription != null) { //successful
StringBuilder nextParts=new StringBuilder();
for (Product product : bPlusTree.getNextTen(id)) {
nextParts.append(product).append("\n");
}
resultText.setText("Product " + id + " was found.\nProduct Description: " + productDescription +"\n\n" +
"Next parts:\n"+ nextParts);
} else { //failed
resultText.setText("Product " + id + " not found.");
}
} else if (insertRadio.isSelected()) {
if (bPlusTree.insert(id, description)) { //successful
resultText.setText("Product " + id + " was added.");
buildTree();
} else { //failed
resultText.setText("Unable to add product, product " + id + " already exist");
}
} else if (deleteRadio.isSelected()) {
if (bPlusTree.delete(id)) { //successful
resultText.setText("Product " + id + " was deleted.");
buildTree();
} else { //failed
resultText.setText("Unable to delete product " + id + " product doesn't exist");
}
} else {
if (bPlusTree.update(id, description)) { //successful
resultText.setText("Product " + id + " was updated.");
buildTree();
} else { //failed
resultText.setText("Unable to update product " + id + " product doesn't exist");
}
}
}
});
}
/**
* Helper method to build the B+ tree virtually
*/
private void buildTree() {
//calling drawTree which recursively build the whole tree...
TreeItem<String> root = drawTree(bPlusTree.getRoot());
root.setValue("Root");
treeView.setRoot(root);
root.setExpanded(true);
}
/**
* A recursive method to build the tree virtually.
* This method recursively traverse the whole B+ tree and add tree node in a Top-to-Bottom approach.
*
* @param node B+ Tree Node
* @return TreeItem object
*/
private TreeItem<String> drawTree(BPlusNode node) {
if (node == null) {
//base case 1 : if reached to a null node, then return empty item.
return new TreeItem<>("Empty");
} else if (node instanceof BPlusLeafNode) {
// base case 2: if reached to a leaf node, then return leaf node items.
TreeItem<String> treeItem = new TreeItem<>();
BPlusLeafNode cur = (BPlusLeafNode) node;
for (Product product : cur.getProducts()) {
if (product != null) treeItem.getChildren().add(new TreeItem<>(product.toString()));
}
return treeItem;
} else {
//recursive case: call the method again until reached a base case...
TreeItem<String> treeItem = new TreeItem<>("x");
BPlusIndexNode cur = (BPlusIndexNode) node;
int i = 0;
for (i = 0; i < cur.getIndexPointers().length - 1; i++) {
TreeItem<String> child = drawTree(cur.getIndexPointers()[i]);
child.setValue("Index-" + i);
treeItem.getChildren().add(child);
if (cur.getProductIDs()[i] == null) treeItem.getChildren().add(new TreeItem<>("null"));
else treeItem.getChildren().add(new TreeItem<>(cur.getProductIDs()[i]));
}
TreeItem<String> child = drawTree(cur.getIndexPointers()[i]);
child.setValue("Index-" + i);
treeItem.getChildren().add(child);
return treeItem;
}
}
/**
* Setter method for stage.
*
* @param stage primary stage
*/
public void setStage(Stage stage) {
this.stage = stage;
// all close operation will be handled by close method...
this.stage.setOnCloseRequest(event -> {
event.consume();
close();
});
}
/**
* On any close operation this method will be called.
* It asks user - Do they want to save changes to file?
* If answer is "No", then close the program.
* If answer is "Yes", then save the data to file and close the program.
*/
public void close() {
//asks user - Do they want to save changes to file?
ButtonType yes = new ButtonType("yes", ButtonBar.ButtonData.OK_DONE);
ButtonType no = new ButtonType("no", ButtonBar.ButtonData.CANCEL_CLOSE);
Alert alert = new Alert(Alert.AlertType.CONFIRMATION,
"Do you want to save changes to file?", yes, no);
Optional<ButtonType> result = alert.showAndWait();
if (result.orElse(no) == yes) {
//If answer is "Yes", then save the data to file and close the program
System.out.println("yes");
saveData();
Platform.exit();
} else {
//If answer is "No", then close the program
System.out.println("no");
Platform.exit();
}
}
/**
* Helper method to load data from AutoPartFile.txt into the B+ tree.
*/
private void loadData() {
try {
Scanner scanner = new Scanner(new FileReader("AutoPartFile.txt"));
while (scanner.hasNextLine()) {
String id = scanner.next();
String description = scanner.nextLine().trim();
bPlusTree.insert(id, description);
}
System.out.println("Data loaded.");
} catch (FileNotFoundException e) {
System.out.println("AutoPartFile.txt file not fount.");
}
}
/**
* Helper method to save data into AutoPartFile.txt into the B+ tree.
*/
private void saveData() {
try {
PrintWriter out = new PrintWriter("AutoPartFile.txt");
for (Product Product : bPlusTree.getAllEntry()) {
out.println(Product.getId() + "\t\t" + Product.getDescription());
}
out.close();
System.out.println("Data saved.");
} catch (FileNotFoundException e) {
System.out.println("Unable to save.");
}
}
}