-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoroughWindow.java
300 lines (224 loc) · 10.6 KB
/
BoroughWindow.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
/**
* Group SWIM:
* 1: Israfeel Ashraf K21008936
* 2: Shakeeb Jumaan K21087021
* 3: Michael Seiranian K20127931
* 4: William Atta K21097986
*
* This class deals with the borough window, which is opened, when the user
* clicks on a borough button.
*
* @author israfeel_ashraf K21008936
* @version 22/03/22
*/
public class BoroughWindow {
private AirbnbFilteredListings filteredListings;
private ArrayList<AirbnbListing> filteredBoroughListings, hostNameSort, priceSort, reviewsSort;
//Ways to sort the listings that the user sees.
private SortingTypes sortByHostName = new SortByHostName(), sortByPrice = new SortByPrice(), sortByReviews = new SortByReviews();
//A table view to show the data to the user.
private TableView tableView;
private TableColumn<AirbnbListing, String> column1, column2, column3, column4;
/**
* Constructor to set up the filtered listings.
*/
public BoroughWindow() {
filteredListings = new AirbnbFilteredListings();
}
/**
* The most important method of this class, as it creates the new window and sets
* up the components of the gui.
*
* @param button The borough button that was clicked.
* @return The whole stage/window to show the user.
*/
public Stage openBoroughWindow(Button button) {
BorderPane mainBorderPain = new BorderPane();
BorderPane centralContentPane = new BorderPane();
mainBorderPain.setCenter(centralContentPane);
//Create the scene of this window.
Scene root = new Scene(mainBorderPain, 750, 500);
Stage boroughStage = new Stage();
boroughStage.setScene(root);
HashMap<String, String> boroughsDict = filteredListings.createBoroughDictionary();
String name = boroughsDict.get(button.getText());
MenuBar menuBar = makeMenuBar(mainBorderPain);
centralContentPane.setTop(menuBar);
BorderPane topPane = (BorderPane) titlePane(name);
mainBorderPain.setTop(topPane);
//Create the table view and add it into the centre of the root view.
BorderPane tableView = createTableView();
centralContentPane.setCenter(tableView);
//Add a styling sheet to style this window.
root.getStylesheets().add("Panel_2_New_Window_StyleSheet.css");
//Show the title with the borough's name.
boroughStage.setTitle("Borough: " + name);
return boroughStage;
}
/**
* Create the title pane of this window
* @param boroughName The name of the borough to display.
* @return The title pane to be at the top of the root view.
*/
private Pane titlePane(String boroughName) {
BorderPane titlePane = new BorderPane();
Label labelShowingBorough = new Label("BOROUGH SELECTED: " + boroughName.toUpperCase());
titlePane.setLeft(labelShowingBorough);
titlePane.setId("titlePane");
return titlePane;
}
/**
* A method to make the menubar, which will allow the user to filter based on hostname, price and the number of reviews.
* @param parent The pane to add the menubar to.
* @return The menubar.
*/
private MenuBar makeMenuBar(Pane parent) {
//Create a menubar and set its id for styling.
MenuBar menuBar = new MenuBar();
menuBar.setId("menuBar");
parent.getChildren().add(menuBar);
//Create a new menu.
Menu sortMenu = new Menu("Sort by: ");
//Add items to the menu
MenuItem reviewsItem = new MenuItem("Reviews");
MenuItem priceItem = new MenuItem("Price");
MenuItem alphabeticalOrderHostNameItem = new MenuItem("Host Name");
//Add all of these items to the menu.
sortMenu.getItems().addAll(reviewsItem, priceItem, alphabeticalOrderHostNameItem);
//Add the menu to the menubar.
menuBar.getMenus().addAll(sortMenu);
//Set the items on the openItemCLick method.
reviewsItem.setOnAction(this::onOpenItemClick);
priceItem.setOnAction(this::onOpenItemClick);
alphabeticalOrderHostNameItem.setOnAction(this::onOpenItemClick);
return menuBar;
}
/**
* A method to deal with updating the table columns when the user wants to sort the data.
*/
private void removeColumns() {
tableView.getItems().clear();
tableView.getColumns().removeAll(column1, column2, column3, column4);
}
/**
* An action event method which deals with the menu items being clicked.
* @param event The event which occured.
*/
private void onOpenItemClick(ActionEvent event) {
//Get the menu item that was clicked.
MenuItem menuItemClicked = (MenuItem) event.getSource();
//If the menu item text is reviews, then sort based on reviews
//and make that column the first one in the table.
if (menuItemClicked.getText().equals("Reviews")) {
reviewsSort = sortDependingOnType(sortByReviews);
//reverse the list to make it go from the highest reviews to lowest.
Collections.reverse(reviewsSort);
removeColumns();
//Add the columns back with the reviews column at the front.
tableView.getColumns().addAll(column4, column1, column2, column3);
//Repopulate the data into the table.
for (int i = 0; i < reviewsSort.size(); i++) {
tableView.getItems().add(reviewsSort.get(i));
}
}
//The same occurs here, but just for price.
else if (menuItemClicked.getText().equals("Price")) {
priceSort = sortDependingOnType(sortByPrice);
Collections.reverse(priceSort);
removeColumns();
//Add the columns back with price at the front.
tableView.getColumns().addAll(column2, column3, column4, column1);
//Repopulate the table with data.
for (int i = 0; i < priceSort.size(); i++) {
tableView.getItems().add(priceSort.get(i));
}
}
//The same occurs here, but for the host name.
else if (menuItemClicked.getText().equals("Host Name")) {
hostNameSort = sortDependingOnType(sortByHostName);
removeColumns();
tableView.getColumns().addAll(column1, column2, column3, column4);
for (int i = 0; i < hostNameSort.size(); i++) {
tableView.getItems().add(hostNameSort.get(i));
}
}
}
/**
* Set the filtered borough listings field as the list passed in.
* @param listingsToReceive The list to pass in.
*/
public void loadFilteredBoroughListings(ArrayList<AirbnbListing> listingsToReceive) {
filteredBoroughListings = listingsToReceive;
}
/**
* A method to sort the listings based on the sort method the user selects.
* @param sortingType The type of sort the user wants to do (host name etc.)
* @return The sorted list.
*/
private ArrayList<AirbnbListing> sortDependingOnType(SortingTypes sortingType) {
return sortingType.sort(filteredBoroughListings);
}
/**
* A method to create the table to show the data,
* and deal with the row selection.
*
* @return The table created and populated.
*/
private BorderPane createTableView() {
tableView = new TableView();
//If there are no houses available, display a standard label.
tableView.setPlaceholder(new Label("No houses available in this borough and price range."));
//Create the different columns populate them from the airbnbListing type according to hostname, price, minimumnights and numberofreviews.
column1 = new TableColumn<>("Host Name");
column1.setCellValueFactory(new PropertyValueFactory<>("host_name"));
column2 = new TableColumn<>("Price (£)");
column2.setCellValueFactory(new PropertyValueFactory<>("price"));
column3 = new TableColumn<>("Minimum Number Of Nights");
column3.setCellValueFactory(new PropertyValueFactory<>("minimumNights"));
column4 = new TableColumn<>("Number of Reviews");
column4.setCellValueFactory(new PropertyValueFactory<>("numberOfReviews"));
//Add these columns to the table.
tableView.getColumns().addAll(column1, column2, column3, column4);
//Make the sorting and reordering unavailable to the user as this will be done by the menubar.
column1.setSortable(false); column2.setSortable(false); column3.setSortable(false); column4.setSortable(false);
column1.setReorderable(false); column2.setReorderable(false); column3.setReorderable(false); column4.setReorderable(false);
//Add a sizing policy to the table.
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
//Do not allow the user to select a cell, as the rows are more important.
tableView.getSelectionModel().setCellSelectionEnabled(false);
//Populate the data into the table from the filtered borough arraylist.
for (int i = 0; i < filteredBoroughListings.size(); i++) {
tableView.getItems().add(filteredBoroughListings.get(i));
}
BorderPane vbox = new BorderPane(tableView);
//A lambda is used to get the rows of the table when selected.
tableView.setRowFactory(tv -> {
//Create a new table row.
TableRow<AirbnbListing> row = new TableRow<>();
//On the mouse click of the row, if the row is populated, and a double click occurs, then get the Airbnblistng from the row.
row.setOnMouseClicked(event -> {
if (! row.isEmpty() && event.getButton()== MouseButton.PRIMARY && event.getClickCount() == 2) {
AirbnbListing clickedRow = row.getItem();
//Create the new description window, to show the description of the property.
DescriptionWindow description = new DescriptionWindow();
//Show the window, giving the window the borough name and the description of the property.
Stage windowToShow = description.openDescriptionWindow(clickedRow.getNeighbourhood(), clickedRow.getName());
windowToShow.show();
}
});
return row ;
});
//return the tableview.
return vbox;
}
}