-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdminStage.java
138 lines (124 loc) · 4.44 KB
/
AdminStage.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
import java.util.Optional;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class AdminStage extends GuestStage
{
// the button to add a book
private Button buttonAddBook;
// the button to delete a Book
private Button buttonDeleteBook;
public AdminStage(BookStore bookStore, Stage previousStage)
{
// call the super class constructor
super(bookStore, previousStage);
// setup the stage
setupStage();
}
// the method to setup stage
private void setupStage()
{
// setup the buttons
buttonAddBook = new Button("Add Book");
// add the handler
buttonAddBook.setOnAction(this::buttonAddBooksetOnAction);
buttonDeleteBook = new Button("Delete Book");
// add handler
buttonDeleteBook.setOnAction(this::buttonDeleteBookSetOnAction);
// set properties to these buttons
BookStoreGUI.setButtonProperties(buttonAddBook);
BookStoreGUI.setButtonProperties(buttonDeleteBook);
// add them to the input box
inputBox.getChildren().add(buttonAddBook);
inputBox.getChildren().add(buttonDeleteBook);
// on close request
setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
hide();
previousStage.show();
}
});
// set the text to logout
buttonLogout.setText("Logout");
}
// the handler for the delete book button
private void buttonDeleteBookSetOnAction(ActionEvent actionEvent)
{
// get the selected book
if( listViewBooks.getSelectionModel().getSelectedIndices().size() != 1 )
{
// show alert
Alert alert = new Alert(AlertType.ERROR);
// set context
alert.setContentText("Select 1 book!");
// show
alert.showAndWait();
// return
return;
}
// remove this book
bookStore.getBooksManagement().removeBook(listViewBooks.getSelectionModel().getSelectedIndex());
// remove all items from list view
listViewBooks.getItems().removeAll( listViewBooks.getItems() );
// /show alert
Alert alert = new Alert(AlertType.CONFIRMATION);
// set context
alert.setContentText("The selected book is deleted!");
// show
alert.showAndWait();
}
// the handler for the add book button
private void buttonAddBooksetOnAction(ActionEvent actionEvent)
{
// create a dialog
Dialog<Book> dialog = new Dialog<>();
dialog.setTitle("Add Book");
dialog.setHeaderText("Enter Book Details");
DialogPane dialogPane = dialog.getDialogPane();
dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
TextField textFieldAuthor = new TextField();
TextField textFieldTitle = new TextField();
TextField textFieldQuantity = new TextField("1");
TextField textFieldPrice = new TextField();
ObservableList<BookGenre> options =
FXCollections.observableArrayList(BookGenre.values());
ComboBox<BookGenre> comboBox = new ComboBox<>(options);
comboBox.getSelectionModel().selectFirst();
dialogPane.setContent(new VBox(8,
new HBox(4,new Label("Author:"),textFieldAuthor),
new HBox(4,new Label("Title:"),textFieldTitle),
new HBox(4,new Label("Quantity:"),textFieldQuantity),
new HBox(4,new Label("Genre:"),comboBox),
new HBox(4,new Label("Price:"),textFieldPrice)));
Platform.runLater(textFieldAuthor::requestFocus);
dialog.setResultConverter((ButtonType button) -> {
if (button == ButtonType.OK)
{
return new Book(textFieldTitle.getText(),textFieldAuthor.getText(),1,
comboBox.getSelectionModel().getSelectedItem(), Double.parseDouble(textFieldPrice.getText()));
}
return null;
});
Optional<Book> optionalResult = dialog.showAndWait();
optionalResult.ifPresent((Book book) -> {
// add to the book store
bookStore.getBooksManagement().addBook(book);
});
}
}