-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFXMLDocumentController.java
More file actions
244 lines (205 loc) · 8.43 KB
/
FXMLDocumentController.java
File metadata and controls
244 lines (205 loc) · 8.43 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package finalproject;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
/**
*
* @author Kevin
*/
public class FXMLDocumentController implements Initializable {
// Variables for GUI
ObservableList<Team> team = FXCollections.observableArrayList();
private Team selectedTeam;
@FXML
private TextField TeamNametxt;
@FXML
private TextField NumOfPlayerstxt;
@FXML
private TextField Coachtxt;
@FXML
private TextField Agetxt;
@FXML
private TextField Citytxt;
@FXML
private TextField Winstxt;
@FXML
private TextField Lossestxt;
@FXML
private ListView<Team> listView;
@FXML
private Button deleteButtonPushed;
@FXML
private Button Addbttn;
@FXML
private Button fullinfo;
private int selectedIndex = -1;
@FXML
private TextField search;
@FXML
private Button updateButton;
@FXML
private Button saveButton;
@Override
public void initialize(URL url, ResourceBundle rb) {
// Add objects to the list
listView.getItems().add(new Team("Bounce", "Toronto", "Nick Nurse", "14", "21", "41", "41"));
listView.getItems().add(new Team("Saints", "Brampton", "Jamie Miller", "12", "19", "44", "38"));
listView.getItems().add(new Team("Monarchs", "Mississauga", "Mark Brown", "15", "18", "34", "47"));
listView.getItems().add(new Team("WildCats", "Burlington", "Rick Lee", "11", "20", "45", "37"));
listView.getItems().add(new Team("Kings", "Toronto", "Daniel Morris", "14", "21", "41", "41"));
search.textProperty().addListener(new ChangeListener() {
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
filterTeamList((String) oldValue, (String) newValue);
}
});
}
/*
When the delete button is pushed, when a team is selected from the list,
a confirmation dialog will pop up to confirm the command
*/
@FXML
public void deleteButtonPushed() {
ObservableList<Team> selectedTeam, allTeams;
allTeams = listView.getItems();
selectedTeam = listView.getSelectionModel().getSelectedItems();
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText(null);
alert.setContentText("Are you sure to delete the team?");
Optional<ButtonType> action = alert.showAndWait();
if (action.isPresent() && action.get() == ButtonType.OK) {
for (Team team : selectedTeam) {
allTeams.remove(team);
}
}
}
// Adds a team when the proper information is typed into the textfields
@FXML
private void AddBttnHandle(ActionEvent e) throws IOException {
Team newTeam = new Team(TeamNametxt.getText(), Citytxt.getText(),
Coachtxt.getText(), NumOfPlayerstxt.getText(), Agetxt.getText(),
Winstxt.getText(), Lossestxt.getText());
listView.getItems().add(newTeam);
TeamNametxt.clear();
Coachtxt.clear();
Citytxt.clear();
NumOfPlayerstxt.clear();
Agetxt.clear();
Winstxt.clear();
Lossestxt.clear();
}
/*
When a team is selected from the list, the button will bring user to
*/
@FXML
private void fullInfoButtonHandle(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("EditScene.fxml"));
Parent tableViewParent = loader.load();
Scene tableViewScene = new Scene(tableViewParent);
EditSceneController controller = loader.getController();
controller.initData(listView.getSelectionModel().getSelectedItem());
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(tableViewScene);
}
/*
When team is selected from list, all the object's attributes will appear
in the textfield for updating/editing purposes
*/
@FXML
private void listViewClicked() {
String selectedTeamName = listView.getSelectionModel().getSelectedItem().getTeamName();
String selectedCoach = listView.getSelectionModel().getSelectedItem().getCoach();
String selectedCity = listView.getSelectionModel().getSelectedItem().getCity();
String selectedPlayers = listView.getSelectionModel().getSelectedItem().getPlayers();
String selectedAge = listView.getSelectionModel().getSelectedItem().getAge();
String selectedWins = listView.getSelectionModel().getSelectedItem().getWins();
String selectedLosses = listView.getSelectionModel().getSelectedItem().getLosses();
TeamNametxt.setText(selectedTeamName);
Coachtxt.setText(selectedCoach);
Citytxt.setText(selectedCity);
NumOfPlayerstxt.setText(selectedPlayers);
Agetxt.setText(selectedAge);
Winstxt.setText(selectedWins);
Lossestxt.setText(selectedLosses);
}
// Updates the textfield for selected team, and brings a confirmation dialog
@FXML
private void updateButtonHandle(ActionEvent event) {
ObservableList<Team> selectedTeam, allTeams;
Team newTeam = new Team(TeamNametxt.getText(), Citytxt.getText(),
Coachtxt.getText(), NumOfPlayerstxt.getText(), Agetxt.getText(),
Winstxt.getText(), Lossestxt.getText());
allTeams = listView.getItems();
selectedTeam = listView.getSelectionModel().getSelectedItems();
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText(null);
alert.setContentText("Are you sure you want to update team's info?");
Optional<ButtonType> action = alert.showAndWait();
if (action.isPresent() && action.get() == ButtonType.OK) {
for (Team team : selectedTeam) {
allTeams.remove(team);
allTeams.add(newTeam);
}
}
TeamNametxt.clear();
Coachtxt.clear();
Citytxt.clear();
NumOfPlayerstxt.clear();
Agetxt.clear();
Winstxt.clear();
Lossestxt.clear();
}
//Using the search textfield, will filter the proper teams
public void filterTeamList(String oldValue, String newValue) {
ObservableList<Team> filterList = FXCollections.observableArrayList();
if (search == null || newValue.isEmpty()) {
listView.setItems(team);
} else {
newValue = newValue.toLowerCase();
for (Team team: listView.getItems()) {
String filterTeamName = team.getTeamName();
String filterCity = team.getCity();
if (filterTeamName.toLowerCase().contains(newValue) || filterCity.toLowerCase().contains(newValue)) {
filterList.add(team);
}
}
listView.setItems(filterList);
}
}
@FXML
private void SaveHandle(ActionEvent event) throws IOException {
File teamFile = new File("TeamList.txt");
PrintWriter output = new PrintWriter(teamFile);
output.println(listView);
output.close();
((Node) event.getSource()).getScene().getWindow().hide();
}
}