Skip to content

Instantly share code, notes, and snippets.

@stephenmirving
Last active February 7, 2020 21:39
Show Gist options
  • Save stephenmirving/343eba7bfd5c1dec98e5d8b4fff141c1 to your computer and use it in GitHub Desktop.
Save stephenmirving/343eba7bfd5c1dec98e5d8b4fff141c1 to your computer and use it in GitHub Desktop.
/**
* FILE - AddInstructorUI.java
* PURPOSE - Generates the user interface and associated event handling for the add instructor window.
* Programmed for the Fall Semester of CPT 237 at Trident Technical College.
* EDIT - 09/22/2016
* @author - Stephen M. Irving
*/
package edu.tridenttech.cpt237.irving.neptuneregistration.view;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.Catalog;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.College;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.Instructor;
import java.util.Optional;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.stage.Stage;
public class AddInstructorUI implements EventHandler<ActionEvent> {
private static final String ICON_IMG = "neptune_ico.png"; // Icon image
private static final String INS_MASTER_FILE = "NeptuneInstructors-A.txt"; //Instructor master file
private final Button addBtn = new Button("Add Instructor");
private final Button cancelBtn = new Button("Cancel");
private final Font fontM = new Font("Helvetica", 13); // Medium
private final Font fontL = new Font("Helvetica", 14); // Large
private final int FIELD_WID = 210; // Field width of text entry fields
private final int BUTTON_WID = 101; // Button width
private TextField fNameField = new TextField(); // First name text field
private TextField lNameField = new TextField(); // Last name text field
private TextField prefCourseField = new TextField();
private Stage addInsStage; // The Add Instructor Stage window
public AddInstructorUI() {
final String ADD_INS_TITLE = "NTC - Add Instructor"; // The Add Instructor Stage Title
// Create and describe the stage
addInsStage = new Stage();
BackgroundFill addStuBGFill = new BackgroundFill(Color.LIGHTGREY, new CornerRadii(1),
new Insets(0.0,0.0,0.0,0.0));
addInsStage.getIcons().add(new Image(this.getClass().getResourceAsStream(ICON_IMG)));
// Create and describe the grid pane
GridPane addInsPane = new GridPane();
addInsPane.setBackground(new Background(addStuBGFill));
addInsPane.setBorder(new Border(new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID, null, null)));
addInsPane.setAlignment(Pos.CENTER);
addInsPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
addInsPane.setHgap(5.5);
addInsPane.setVgap(5.5);
// Placing nodes in the pane...
// Create labels, set their font and position
Label fNameLabel = new Label("First Name ");
fNameLabel.setFont(fontM);
addInsPane.add(fNameLabel, 0, 0);
GridPane.setHalignment(fNameLabel, HPos.RIGHT);
// Make fields tab traversable, set font, width, and placement
fNameField.setFocusTraversable(true);
fNameField.setFont(fontL);
fNameField.setMinWidth(FIELD_WID);
addInsPane.add(fNameField, 1, 0);
Label lNameLabel = new Label("Last Name ");
lNameLabel.setFont(fontM);
addInsPane.add(lNameLabel, 0, 1);
GridPane.setHalignment(lNameLabel, HPos.RIGHT);
lNameField.setFocusTraversable(true);
lNameField.setFont(fontL);
lNameField.setMinWidth(FIELD_WID);
addInsPane.add(lNameField, 1, 1);
Label prefCourseLabel = new Label(String.format("Preferred Course %n%16s", "(Ex: CPT-102)"));
prefCourseLabel.setFont(fontM);
addInsPane.add(prefCourseLabel, 0, 2);
GridPane.setHalignment(prefCourseLabel, HPos.RIGHT);
prefCourseField.setFocusTraversable(true);
prefCourseField.setFont(fontL);
prefCourseField.setMinWidth(FIELD_WID);
addInsPane.add(prefCourseField, 1, 2);
// Set button size, font, and placement
cancelBtn.setMinWidth(BUTTON_WID);
cancelBtn.setFont(fontM);
addBtn.setMaxWidth(BUTTON_WID);
addBtn.setFont(fontM);
addInsPane.add(cancelBtn, 1, 3);
GridPane.setHalignment(cancelBtn, HPos.LEFT);
addInsPane.add(addBtn, 1, 3);
GridPane.setHalignment(addBtn, HPos.RIGHT);
// Create the scene, the title, size the stage to the scene, and make it unresizable
Scene addInsScene = new Scene(addInsPane);
addInsStage.setTitle(ADD_INS_TITLE);
addInsStage.setScene(addInsScene);
addInsStage.sizeToScene();
addInsStage.setResizable(false);
// Set actions for button described in the handle method for this class
addBtn.setOnAction(this);
cancelBtn.setOnAction(this);
}
@Override
public void handle(ActionEvent event) {
// Name Empty Alert Strings
final String FEA_TITLE = "One or More Fields Are Empty!";
final String FEA_HEAD = "The fields in this form have not been completely filled out.";
final String FEA_BODY = "You cannot continue unless you fill out the instructor's full name and preferred "
+ "course to teach.";
// Pattern Matching Alert Strings
final String PMA_TITLE = "That Course is Invalid!";
final String PMA_HEAD = "The course you entered was not formatted correctly.";
final String PMA_BODY = String.format("The course you entered was not entered in the correct format. Please "
+ "try again, and this time enter a course with a valid course pattern.%n(Ex: CPT-102)");
// Course Not Found Alert Strings
final String CNFA_TITLE = "That Course Was Not Found!";
final String CNFA_HEAD = "The course you entered was not found in the course list.";
final String CNFA_BODY = "The course you entered could not be found in the Neptune Technical College course "
+ "list. Please try again, and this time enter a course found in the course list for Neptune Tech.";
// Instructor Added Alert Strings
final String IAA_TITLE = "The Instructor Has Been Added!";
final String IAA_HEAD = "The instructor you entered has been added to Neptune Technical College.";
final String IAA_BODY = "The instructor you just entered has been successfully added to the system at Neptune "
+ "Technical College.";
final String regex = "^[a-zA-Z]{3}-[0-9]{3}$"; // The regex pattern for a course ID
String fName = fNameField.getText(); // The field for the instructor's first name
String lName = lNameField.getText(); // The field for the instructor's last name
String prefCourse = prefCourseField.getText(); // The field for the instructor's preferred course ID
Pattern pattern = Pattern.compile(regex); // The applied pattern for a course ID
Matcher matcher = pattern.matcher(prefCourse); // The matcher for that pattern
Instructor myInstructor; // Local instructor variable used for method calls
Button b = (Button)event.getSource(); // Button to check action's against
if (b == addBtn) {
// If any of the fields are empty give the user an alert
if (fName.isEmpty() || lName.isEmpty() || prefCourse.isEmpty()) {
Alert fieldEmptyAlert = new Alert(AlertType.ERROR);
fieldEmptyAlert.setTitle(FEA_TITLE);
fieldEmptyAlert.setHeaderText(FEA_HEAD);
fieldEmptyAlert.setContentText(FEA_BODY);
Optional<ButtonType> result = fieldEmptyAlert.showAndWait();
// Once the alert is cleared, focus on the most appropriate text field for the situation
if (result.get() == ButtonType.OK) {
if (lName.isEmpty() && !fName.isEmpty()) {
lNameField.requestFocus();
} else if (prefCourse.isEmpty() && !fName.isEmpty() && lName.isEmpty()) {
prefCourseField.requestFocus();
} else {
fNameField.requestFocus();
}
}
} else if (!matcher.matches()) {
// If the preferred course does not match the patter for course strings give the user an alert
Alert patternMatchingAlert = new Alert(AlertType.ERROR);
patternMatchingAlert.setTitle(PMA_TITLE);
patternMatchingAlert.setHeaderText(PMA_HEAD);
patternMatchingAlert.setContentText(PMA_BODY);
Optional<ButtonType> result2 = patternMatchingAlert.showAndWait();
if (result2.get() == ButtonType.OK) {
prefCourseField.requestFocus();
}
} else if (!Catalog.INSTANCE.getCourseList().contains(Catalog.INSTANCE.getCourseById(prefCourse))) {
// If the course the user entered was not found in the course list, give them an alert
Alert courseNotFoundAlert = new Alert(AlertType.ERROR);
courseNotFoundAlert.setTitle(CNFA_TITLE);
courseNotFoundAlert.setHeaderText(CNFA_HEAD);
courseNotFoundAlert.setContentText(CNFA_BODY);
Optional<ButtonType> result3 = courseNotFoundAlert.showAndWait();
if (result3.get() == ButtonType.OK) {
prefCourseField.requestFocus();
}
} else {
// If they got here, everything has been entered correctly. Add the instructor and print confirmation
myInstructor = College.INSTANCE.addInstructor(fName, lName);
myInstructor.addPreferredCourse(prefCourse);;
College.INSTANCE.recordInstructor(myInstructor, INS_MASTER_FILE);
Alert insAddedAlert = new Alert(AlertType.INFORMATION);
insAddedAlert.setTitle(IAA_TITLE);
insAddedAlert.setHeaderText(IAA_HEAD);
insAddedAlert.setContentText(IAA_BODY);
insAddedAlert.show();
addInsStage.close();
}
} else { // If the cancel button is hit then close the window
addInsStage.close();
}
}
public void show() {
if (!addInsStage.isShowing()) {
// Ensure all the fields are zeroed out for each use when Add Instructor button is hit from the MainMenu
fNameField.setText("");
lNameField.setText("");
prefCourseField.setText("");
addInsStage.show();
} else {
addInsStage.toFront(); // If the stage is behind other windows when the button is hit, bring it forward
}
}
public boolean isItShowing() {
boolean showing = false;
if (addInsStage.isShowing()) {
showing = true;
}
return showing;
}
}
/**
* FILE - AddStudentUI.java
* PURPOSE - Generates the user interface and associated event handling for the add student window.
* Programmed for the Fall Semester of CPT 237 at Trident Technical College.
* EDIT - 09/16/2016
* @author - Stephen M. Irving
*/
package edu.tridenttech.cpt237.irving.neptuneregistration.view;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.College;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.Catalog;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.Major;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.Student;
import java.util.ArrayList;
import java.util.Optional;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class AddStudentUI implements EventHandler<ActionEvent>{
private static final String ICON_IMG = "neptune_ico.png"; // Icon image
private static final String STU_MASTER_FILE = "NeptuneStudents.txt"; //Student master file
private static final String UNDECLARED = "UNDECLARED";
private final Button addBtn = new Button("Add Student");
private final Button cancelBtn = new Button("Cancel");
private final Font fontM = new Font("Helvetica", 13); // Medium
private final Font fontL = new Font("Helvetica", 14); // Large
private final int FIELD_WID = 210; // Field width of text entry fields
private final int BUTTON_WID = 101; // Button width
private final ComboBox<String> majorComboBox = new ComboBox<String>(); // Combo box containing possible majors
private TextField fNameField = new TextField(); // First name text field
private TextField lNameField = new TextField(); // Last name text field
private ArrayList<String> majorComboChoices= new ArrayList<String>(); // List of all possible majors
private Stage addStuStage; // The Add Student UI stage window
public AddStudentUI() {
final String ADD_STU_TITLE = "NTC - Add Student";
addStuStage = new Stage();
BackgroundFill addStuBGFill = new BackgroundFill(Color.LIGHTGREY, new CornerRadii(1),
new Insets(0.0,0.0,0.0,0.0));
addStuStage.getIcons().add(new Image(this.getClass().getResourceAsStream(ICON_IMG)));
GridPane addStuPane = new GridPane();
addStuPane.setBackground(new Background(addStuBGFill));
addStuPane.setBorder(new Border(new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID, null, null)));
addStuPane.setAlignment(Pos.CENTER);
addStuPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
addStuPane.setHgap(5.5);
addStuPane.setVgap(5.5);
// Place nodes in the pane
Label fNameLabel = new Label("First Name ");
fNameLabel.setFont(fontM);
addStuPane.add(fNameLabel, 0, 0);
GridPane.setHalignment(fNameLabel, HPos.RIGHT);
fNameField.setFocusTraversable(true);
fNameField.setFont(fontL);
fNameField.setPrefWidth(FIELD_WID);
addStuPane.add(fNameField, 1, 0);
Label lNameLabel = new Label("Last Name ");
lNameLabel.setFont(fontM);
addStuPane.add(lNameLabel, 0, 1);
GridPane.setHalignment(lNameLabel, HPos.RIGHT);
lNameField.setFocusTraversable(true);
lNameField.setFont(fontL);
lNameField.setPrefWidth(FIELD_WID);
addStuPane.add(lNameField, 1, 1);
Label majorLabel = new Label("Major ");
majorLabel.setFont(fontM);
addStuPane.add(majorLabel, 0, 2);
GridPane.setHalignment(majorLabel, HPos.RIGHT);
majorComboBox.setPlaceholder(new Label("Loading Majors"));
for (Major major : Catalog.INSTANCE.getMajorList()) {
majorComboChoices.add(major.getName());
}
majorComboBox.getItems().add(UNDECLARED);
majorComboBox.getItems().addAll(majorComboChoices);
majorComboBox.setFocusTraversable(true);
majorComboBox.setPromptText("Choose a major...");
majorComboBox.setPrefWidth(FIELD_WID);
addStuPane.add(majorComboBox, 1, 2);
cancelBtn.setMinWidth(BUTTON_WID);
cancelBtn.setFont(fontM);
addBtn.setMinWidth(BUTTON_WID);
addBtn.setFont(fontM);
addStuPane.add(cancelBtn, 1, 3);
GridPane.setHalignment(cancelBtn, HPos.LEFT);
addStuPane.add(addBtn, 1, 3);
GridPane.setHalignment(addBtn, HPos.RIGHT);
Scene addStuScene = new Scene(addStuPane);
addStuStage.setTitle(ADD_STU_TITLE);
addStuStage.setScene(addStuScene);
addStuStage.sizeToScene();
addStuStage.setResizable(false);
// Set actions for keyboard ENTER key presses for the fields and the get instructor button
lNameField.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
addingStudent();
}
}
});
fNameField.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
addingStudent();
}
}
});
majorComboBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
addingStudent();
}
}
});
addBtn.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
addingStudent();
}
}
});
// Set actions for graphical buttons - described in the handle method for this class
addBtn.setOnAction(this);
cancelBtn.setOnAction(this);
}
@Override
public void handle(ActionEvent event) {
Button b = (Button)event.getSource();
if (b == addBtn) {
addingStudent();
} else if (b == cancelBtn){
addStuStage.close();
}
}
public void addingStudent() {
// Name Empty Alert Strings
final String NEA_TITLE = "Student Name Field(s) Are Empty!";
final String NEA_HEAD = "The student's first and/or last name field is empty.";
final String NEA_BODY = "You cannot continue unless you fill out the student's name. Please enter both a "
+ "first and last name for the student.";
// Major Empty Alert Strings
final String MEA_TITLE = "A Major Was Not Chosen!";
final String MEA_HEAD = "You did not choose a major for the student.";
final String MEA_BODY = "You must select a major for the student. If the student has not yet chosen a major, "
+ "then please select the \"Undeclared\" option.";
// Major Undeclared Confirmation
final String MUC_TITLE = "The Major is Currently Undeclared";
final String MUC_HEAD = "Are you sure you want to leave the major undeclared?";
final String MUC_BODY = "Going ahead now will leave the student's major undeclared. If that is what you want "
+ "to do, press the OK button. If you would like to go back and select a major, press Cancel.";
String fName = fNameField.getText();
String lName = lNameField.getText();
Student myStudent;
if (fName.isEmpty() || lName.isEmpty()) {
Alert nameEmptyAlert = new Alert(AlertType.ERROR);
nameEmptyAlert.setTitle(NEA_TITLE);
nameEmptyAlert.setHeaderText(NEA_HEAD);
nameEmptyAlert.setContentText(NEA_BODY);
Optional<ButtonType> result = nameEmptyAlert.showAndWait();
if (result.get() == ButtonType.OK) {
fNameField.requestFocus();
}
} else if (majorComboBox.getValue() == null) {
Alert majorEmptyAlert = new Alert(AlertType.ERROR);
majorEmptyAlert.setTitle(MEA_TITLE);
majorEmptyAlert.setHeaderText(MEA_HEAD);
majorEmptyAlert.setContentText(MEA_BODY);
Optional<ButtonType> result = majorEmptyAlert.showAndWait();
if (result.get() == ButtonType.OK) {
majorComboBox.getSelectionModel().selectFirst();
majorComboBox.requestFocus();
}
} else if (majorComboBox.getValue().equalsIgnoreCase(UNDECLARED)) {
Alert majorUDAlert = new Alert(AlertType.CONFIRMATION);
majorUDAlert.setTitle(MUC_TITLE);
majorUDAlert.setHeaderText(MUC_HEAD);
majorUDAlert.setContentText(MUC_BODY);
Optional<ButtonType> result = majorUDAlert.showAndWait();
if (result.get() == ButtonType.OK) {
myStudent = College.INSTANCE.addStudent(fName, lName);
myStudent.setMajor(majorComboBox.getValue());
College.INSTANCE.recordStudent(myStudent, STU_MASTER_FILE);
addStuStage.close();
} else if (result.get() == ButtonType.CANCEL) {
fNameField.setDisable(true);
lNameField.setDisable(true);
majorComboBox.getSelectionModel().selectFirst();
}
} else {
myStudent = College.INSTANCE.addStudent(fName, lName);
myStudent.setMajor(majorComboBox.getValue());
College.INSTANCE.recordStudent(myStudent, STU_MASTER_FILE);
addStuStage.close();
}
}
public void show() {
if (!addStuStage.isShowing()) {
fNameField.setText("");
lNameField.setText("");
majorComboBox.setPromptText("Choose a major...");
fNameField.setDisable(false);
lNameField.setDisable(false);
addStuStage.show();
} else {
addStuStage.toFront();
}
}
public boolean isItShowing() {
boolean showing = false;
if (addStuStage.isShowing()) {
showing = true;
}
return showing;
}
}
/**
* FILE - InstructorInfoUI.java
* PURPOSE - Generates the user interface for the instructor information screen.
* Programmed for the Fall Semester of CPT 237 at Trident Technical College.
* EDIT - 09/22/2016
* @author - Stephen M. Irving
*/
package edu.tridenttech.cpt237.irving.neptuneregistration.view;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.College;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.Instructor;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class InstructorInfoUI implements EventHandler<ActionEvent> {
private static final String ICON_IMG = "neptune_ico.png"; // Icon image
private final Button getBtn = new Button("Get Instructor");
private final Button cancelBtn = new Button("Cancel");
private final Font fontM = new Font("Helvetica", 13); // Medium
private final Font fontL = new Font("Helvetica", 15); // Large
private TextField fNameField = new TextField(); // First name field
private TextField lNameField = new TextField(); // Last name field
private TextArea insInfoText = new TextArea(); // Search result text area
private Stage insInfoStage; // The Instructor Info UI
public InstructorInfoUI() {
final String INS_INFO_TITLE = "NTC - Find Instructor"; // Window title
final int SCENE_WID = 657; // Scene width
final int SCENE_HT = 391; // Scene height
final int FIELD_WID = 210; // Field width for text entry fields
final int BUTTON_WID = 101; // Button width
final int TA_WID = 590; // Text area width
insInfoStage = new Stage();
insInfoStage.getIcons().add(new Image(this.getClass().getResourceAsStream(ICON_IMG)));
insInfoStage.setTitle(INS_INFO_TITLE);
// Create main group, the scene where the group will go, and the border pane that will be the primary pane
Group root = new Group();
Scene insInfoScene = new Scene(root, SCENE_WID, SCENE_HT, Color.LIGHTGREY);
BorderPane insInfoBPane = new BorderPane();
insInfoBPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
// Create and describe an hbox to place the name labels and entry fields
HBox dataEntryBox = new HBox();
dataEntryBox.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
dataEntryBox.setSpacing(10);
dataEntryBox.setStyle("-fx-background-color: #D3D3D3;");
// Create the name labels and describe their and the associated field's settings
Label fNameLabel = new Label("First Name ");
fNameLabel.setFont(fontL);
fNameField.setFocusTraversable(true);
fNameField.setFont(fontL);
fNameField.setMinWidth(FIELD_WID);
Label lNameLabel = new Label("Last Name ");
lNameLabel.setFont(fontL);
lNameField.setFocusTraversable(true);
lNameField.setFont(fontL);
lNameField.setMinWidth(FIELD_WID);
// Put it all in the box
dataEntryBox.getChildren().addAll(fNameLabel, fNameField, lNameLabel, lNameField);
// Create and describe a flow pane where the text area that prints the search results will go
FlowPane insInfoFPane = new FlowPane();
insInfoFPane.setStyle("-fx-background-color: #D3D3D3;");
insInfoFPane.setAlignment(Pos.CENTER);
insInfoFPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
insInfoFPane.setHgap(5.5);
insInfoFPane.setVgap(5.5);
// Create and describe the search result text area and place it in the flow pane
insInfoText.setPrefWidth(TA_WID);
insInfoText.setFont(fontL);
insInfoText.setEditable(false);
insInfoText.setBorder(new Border(new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID, null, null)));
insInfoText.setFocusTraversable(false);
insInfoFPane.getChildren().add(insInfoText);
// Create and describe the grid pane where the buttons will go
GridPane buttonGrid = new GridPane();
buttonGrid.setAlignment(Pos.CENTER);
buttonGrid.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
buttonGrid.setHgap(8);
buttonGrid.setVgap(5.5);
// Set button size, font, and placement in the grind pane
cancelBtn.setMinWidth(BUTTON_WID);
cancelBtn.setFont(fontM);
getBtn.setMaxWidth(BUTTON_WID);
getBtn.setFont(fontM);
buttonGrid.add(cancelBtn, 0, 0);
GridPane.setHalignment(cancelBtn, HPos.RIGHT);
buttonGrid.add(getBtn, 1, 0);
GridPane.setHalignment(getBtn, HPos.LEFT);
// Place everything into the border pane
insInfoBPane.setTop(dataEntryBox);
insInfoBPane.setCenter(insInfoFPane);
insInfoBPane.setBottom(buttonGrid);
// Place the border pane into the root group
root.getChildren().add(insInfoBPane);
//Set the scene and size the stage to it
insInfoStage.setScene(insInfoScene);
insInfoStage.sizeToScene();
insInfoStage.setResizable(false);
// Set actions for keyboard ENTER key presses for the fields and the get instructor button
lNameField.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
insDataPrinter();
}
}
});
fNameField.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
insDataPrinter();
}
}
});
getBtn.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
insDataPrinter();
}
}
});
// Set actions for graphical buttons - described in the handle method for this class
getBtn.setOnAction(this);
cancelBtn.setOnAction(this);
}
@Override
public void handle(ActionEvent event) {
Button b = (Button)event.getSource(); // Button to check action's against
if (b == getBtn) {
insDataPrinter();
} else { // If the cancel button is hit then close the window
insInfoStage.close();
} // End of button handling IF/ELSE
} // End of handle method
public void insDataPrinter() {
final String NOT_FOUND_ERROR = "No instructor by that name was found in our records!";
String fName = fNameField.getText().trim(); // The field for the instructor's first name
String lName = lNameField.getText().trim(); // The field for the instructor's last name
Instructor myInstructor; // Local instructor variable used for method calls
int prefCourseCount;
myInstructor = College.INSTANCE.getInstructorByName(fName, lName);
insInfoText.setMouseTransparent(true);
if (myInstructor == null) {
System.err.println(NOT_FOUND_ERROR);
insInfoText.setText(NOT_FOUND_ERROR);
} else {
prefCourseCount = myInstructor.getPreferredCourses().size();
insInfoText.setText(String.format("%s, %s%n%nPREFERRED COURSES%n%n", myInstructor.getLName(),
myInstructor.getFName()));
// If courses > 5 make sure scroll bar that will appear and the and up/down on keyboard can be used.
if (prefCourseCount > 5) {
insInfoText.setMouseTransparent(false);
insInfoText.setFocusTraversable(true);
}
// Loop through all the preferred courses for the instructor and print them out to the user
for(int i = 0; i < prefCourseCount; i++) {
insInfoText.appendText(myInstructor.getPreferredCourses().get(i).getCourseId() + " - ");
insInfoText.appendText(myInstructor.getPreferredCourses().get(i).getName() + " - ");
insInfoText.appendText(Integer.toString(myInstructor.getPreferredCourses().get(i).getHours())
+ " Credit Hours\n");
}
} // End of instructor found IF/ELSE
}
public void show() {
if (!insInfoStage.isShowing()) {
fNameField.setText("");
lNameField.setText("");
// Set the search area to default with a list of all students in the College
insInfoText.setText(String.format("%s%n", "Instructors at Neptune Technical College"));
for (Instructor ins : College.INSTANCE.getInstructorList()) {
insInfoText.appendText(String.format("%n%s, %s%nPreferred Courses: ", ins.getLName(), ins.getFName()));
for (int i = 0; i < ins.getPreferredCourses().size() - 1; i++) {
insInfoText.appendText(String.format("%s, ", ins.getPreferredCourses().get(i).getCourseId()));
}
insInfoText.appendText(String.format("%s%n", ins.getPreferredCourses()
.get(ins.getPreferredCourses().size() - 1).getCourseId()));
insInfoText.appendText("\n");
insInfoText.wrapTextProperty();
}
insInfoStage.show();
} else {
insInfoStage.toFront();
}
}
public boolean isItShowing() {
boolean showing = false;
if (insInfoStage.isShowing()) {
showing = true;
}
return showing;
}
}
/**
* FILE - MainMenu.java
* PURPOSE - Produces the UI for the starting screen and menu of the application.
* Programmed for the Fall Semester of CPT 237 at Trident Technical College.
* EDIT - 09/16/2016
* @author - Stephen M. Irving
*/
package edu.tridenttech.cpt237.irving.neptuneregistration.view;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import java.util.Optional;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class MainMenu implements EventHandler<ActionEvent> {
// Image titles
private static final String ICON_IMG = "neptune_ico.png"; // Icon image
private static final String BACKGROUND_IMG = "bg.jpg"; // Background image for primary stage
// Buttons
private final Button addStuBtn = new Button("Add Student");
private final Button addInsBtn = new Button("Add Instructor");
private final Button stuInfoBtn = new Button("Student Info");
private final Button insInfoBtn = new Button("Instructor Info");
private final Button quitBtn = new Button("QUIT");
// Add stage members
private Stage mainStage;
private AddStudentUI addStuUI;
private AddInstructorUI addInsUI;
private StudentInfoUI stuInfoUI;
private InstructorInfoUI insInfoUI;
public MainMenu(Stage primaryStage) {
final Font fontN = new Font("Helvetica", 12); // Normal sized font used on buttons
final String TITLE = "NTC - Reg System"; // Main stage title
// Get the visual bounds of the user's screen
final Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
mainStage = primaryStage; // Set the main stage
// Style it - Change icon, background image, button opacity and font
mainStage.getIcons().add(new Image(this.getClass().getResourceAsStream(ICON_IMG)));
BackgroundImage primaryBI= new BackgroundImage(new Image(BACKGROUND_IMG,260,110,false,true),
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
BackgroundSize.DEFAULT);
addStuBtn.setStyle("-fx-opacity: 0.585");
addStuBtn.setFont(fontN);
addInsBtn.setStyle("-fx-opacity: 0.585");
addInsBtn.setFont(fontN);
stuInfoBtn.setStyle("-fx-opacity: 0.585");
stuInfoBtn.setFont(fontN);
insInfoBtn.setStyle("-fx-opacity: 0.585");
insInfoBtn.setFont(fontN);
quitBtn.setStyle("-fx-opacity: 0.455");
quitBtn.setFont(fontN);
// Create the primaryPane and add styling
GridPane primaryPane = new GridPane();
primaryPane.setBackground(new Background(primaryBI));
primaryPane.setBorder(new Border(new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID, null, null)));
primaryPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
primaryPane.setHgap(5.5);
primaryPane.setVgap(5.5);
primaryPane.setAlignment(Pos.CENTER);
// Add all buttons to the primaryPane and set their locations in the grid
primaryPane.add(addStuBtn, 0, 0);
primaryPane.add(addInsBtn, 2, 0);
primaryPane.add(quitBtn, 1, 1);
GridPane.setHalignment(quitBtn, HPos.CENTER);
primaryPane.add(stuInfoBtn, 0, 2);
primaryPane.add(insInfoBtn, 2 , 2);
// Set button actions (Logic in handle method)
addStuBtn.setOnAction(this);
addInsBtn.setOnAction(this);
stuInfoBtn.setOnAction(this);
insInfoBtn.setOnAction(this);
quitBtn.setOnAction(this);
// Create the scene
Scene scene = new Scene(primaryPane, 270, 120); // Create the scene and it's size
scene.setFill(Color.LIGHTGREY); // Create slight bevel effect in gap between background image and border line
mainStage.setTitle(TITLE); // Set the stage's title
mainStage.setScene(scene); // Place the scene into the stage
mainStage.setResizable(false);
mainStage.setAlwaysOnTop(true);
mainStage.sizeToScene();
// Set stage/window to the top right of the screen and then open it
mainStage.setX(primaryScreenBounds.getMaxX() - scene.getWidth() - 4);
mainStage.setY(primaryScreenBounds.getMinY());
mainStage.show();
// Ensure that closing the main menu will close all application windows
mainStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
Platform.exit();
}
});
}
@Override
public void handle(ActionEvent event) {
// Exit Alert Strings
final String EA_TITLE = "Exit Now?";
final String EA_HEAD = "Finished Already?";
final String EA_BODY = "Are you sure that you want to exit the application now?";
// Already Open Alert Strings
final String AO_TITLE = "Window Already Open";
final String AO_HEAD = "A window is already open!";
final String AO_BODY = "You already have a window open. Close the existing window before trying to do "
+ "something else.";
Alert alreadyOpenAlert = new Alert(AlertType.ERROR);
alreadyOpenAlert.setTitle(AO_TITLE);
alreadyOpenAlert.setHeaderText(AO_HEAD);
alreadyOpenAlert.setContentText(AO_BODY);
Optional<ButtonType> result;
Button b = (Button)event.getSource();
if (b == addStuBtn) {
if (addStuUI == null) {
addStuUI = new AddStudentUI();
}
if (addInsUI.isItShowing() || stuInfoUI.isItShowing() || insInfoUI.isItShowing()) {
alreadyOpenAlert.show();
} else {
addStuUI.show();
}
} else if (b == addInsBtn) {
if (addInsUI == null) {
addInsUI = new AddInstructorUI();
}
if (addStuUI.isItShowing() || stuInfoUI.isItShowing() || insInfoUI.isItShowing()) {
alreadyOpenAlert.show();
} else {
addInsUI.show();
}
} else if (b == stuInfoBtn) {
if (stuInfoUI == null) {
stuInfoUI = new StudentInfoUI();
}
if (addStuUI.isItShowing() || addInsUI.isItShowing() || insInfoUI.isItShowing()) {
alreadyOpenAlert.show();
} else {
stuInfoUI.show();
}
} else if (b == insInfoBtn) {
if (insInfoUI == null) {
insInfoUI = new InstructorInfoUI();
}
if (addStuUI.isItShowing() || addInsUI.isItShowing() || stuInfoUI.isItShowing()) {
alreadyOpenAlert.show();
} else {
insInfoUI.show();
}
} else if (b == quitBtn) {
Alert exitAlert = new Alert(AlertType.CONFIRMATION);
exitAlert.setTitle(EA_TITLE);
exitAlert.setHeaderText(EA_HEAD);
exitAlert.setContentText(EA_BODY);
result = exitAlert.showAndWait();
if (result.get() == ButtonType.OK) {
Platform.exit();
}
}
}
public void show() {
mainStage.show();
}
}
/**
* FILE - StudentInfoUI.java
* PURPOSE - Generates the user interface for the student information screen.
* Programmed for the Fall Semester of CPT 237 at Trident Technical College.
* EDIT - 09/22/2016
* @author - Stephen M. Irving
*/
package edu.tridenttech.cpt237.irving.neptuneregistration.view;
import java.util.ArrayList;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.College;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.Course;
import edu.tridenttech.cpt237.irving.neptuneregistration.model.Student;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class StudentInfoUI implements EventHandler<ActionEvent> {
private static final String ICON_IMG = "neptune_ico.png"; // Icon image
private final Button getBtn = new Button("Get Student");
private final Button cancelBtn = new Button("Cancel");
private final Font fontM = new Font("Helvetica", 13); // Medium
private final Font fontL = new Font("Helvetica", 15); // Large
private int taWid = 590; // Text area width
private int taHt = 200; // Text area and listview height
private TextField fNameField = new TextField(); // First name field
private TextField lNameField = new TextField(); // Last name field
private TextArea stuInfoText = new TextArea(); // Search result text area
private ListView<String> reqCoursesList = new ListView<String>(); // ListView of required courses for a major
private FlowPane stuInfoFPane; // Student info flow pane
private Stage stuInfoStage; // The Student Info UI
public StudentInfoUI() {
final String STU_INFO_TITLE = "NTC - Find Student"; // Window title
final int SCENE_WID = 657; // Scene width
final int SCENE_HT = 391; // Scene height
final int FIELD_WID = 210; // Field width for text entry fields
final int BUTTON_WID = 101; // Button width
stuInfoStage = new Stage();
stuInfoStage.getIcons().add(new Image(this.getClass().getResourceAsStream(ICON_IMG)));
stuInfoStage.setTitle(STU_INFO_TITLE);
// Create root group, the scene where the group will go, and the border pane that will be the primary pane
Group root = new Group();
Scene stuInfoScene = new Scene(root, SCENE_WID, SCENE_HT, Color.LIGHTGREY);
BorderPane stuInfoBPane = new BorderPane();
stuInfoBPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
// Create and describe an hbox to place the name labels and entry fields
HBox dataEntryBox = new HBox();
dataEntryBox.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
dataEntryBox.setSpacing(10);
dataEntryBox.setStyle("-fx-background-color: #D3D3D3;");
// Create the name labels and describe their and the associated field's settings
Label fNameLabel = new Label("First Name ");
fNameLabel.setFont(fontL);
fNameField.setFocusTraversable(true);
fNameField.setFont(fontL);
fNameField.setMinWidth(FIELD_WID);
Label lNameLabel = new Label("Last Name ");
lNameLabel.setFont(fontL);
lNameField.setFocusTraversable(true);
lNameField.setFont(fontL);
lNameField.setMinWidth(FIELD_WID);
// Put it all in the box
dataEntryBox.getChildren().addAll(fNameLabel, fNameField, lNameLabel, lNameField);
// Describe the flow pane where the text area that prints the search results will go
stuInfoFPane = new FlowPane(Orientation.HORIZONTAL);
stuInfoFPane.setStyle("-fx-background-color: #D3D3D3;");
stuInfoFPane.setAlignment(Pos.CENTER);
stuInfoFPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
stuInfoFPane.setHgap(5.5);
stuInfoFPane.setVgap(5.5);
// Describe the search result text area and place it in the flow pane
stuInfoText.setFont(fontL);
stuInfoText.setEditable(false);
stuInfoText.setBorder(new Border(new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID, null, null)));
stuInfoText.setFocusTraversable(false);
stuInfoFPane.getChildren().add(stuInfoText);
// Create and describe the grid pane where the buttons will go
GridPane buttonGrid = new GridPane();
buttonGrid.setAlignment(Pos.CENTER);
buttonGrid.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
buttonGrid.setHgap(8);
buttonGrid.setVgap(5.5);
// Set button size, font, and placement in the grind pane
cancelBtn.setMinWidth(BUTTON_WID);
cancelBtn.setPrefWidth(BUTTON_WID);
cancelBtn.setMaxWidth(BUTTON_WID);
cancelBtn.setFont(fontM);
getBtn.setMinWidth(BUTTON_WID);
getBtn.setPrefWidth(BUTTON_WID);
getBtn.setMaxWidth(BUTTON_WID);
getBtn.setFont(fontM);
buttonGrid.add(cancelBtn, 0, 0);
GridPane.setHalignment(cancelBtn, HPos.RIGHT);
buttonGrid.add(getBtn, 1, 0);
GridPane.setHalignment(getBtn, HPos.LEFT);
// Place everything into the border pane
stuInfoBPane.setTop(dataEntryBox);
stuInfoBPane.setCenter(stuInfoFPane);
stuInfoBPane.setBottom(buttonGrid);
// Place the border pane into the root group
root.getChildren().add(stuInfoBPane);
//Set the scene and size the stage to it
stuInfoStage.setScene(stuInfoScene);
stuInfoStage.sizeToScene();
stuInfoStage.setResizable(false);
// Set actions for keyboard ENTER key presses for the fields and the get student button
lNameField.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
stuDataPrinter();
}
}
});
fNameField.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
stuDataPrinter();
}
}
});
getBtn.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
stuDataPrinter();
}
}
});
// Set actions for graphical buttons - described in the handle method for this class
getBtn.setOnAction(this);
cancelBtn.setOnAction(this);
}
@Override
public void handle(ActionEvent event) {
Button b = (Button)event.getSource(); // Button to check action's against
if (b == getBtn) {
stuDataPrinter();
} else { // If the cancel button is hit then close the window
stuInfoStage.close();
}
} // End of handle method
public void stuDataPrinter() {
final String NOT_FOUND_ERROR = "No student by that name was found in our records!";
String fName = fNameField.getText().trim(); // The field for the student's first name
String lName = lNameField.getText().trim(); // The field for the student's last name
ArrayList<String> reqCourseNames = new ArrayList<String>();
Student myStudent; // Local student variable used for method calls
myStudent = College.INSTANCE.getStudentByName(fName, lName);
if (myStudent == null) {
System.err.println(NOT_FOUND_ERROR);
stuInfoText.setText(NOT_FOUND_ERROR);
} else {
stuInfoText.setMaxWidth(taWid / 2);
stuInfoText.setText(String.format("Student Found!%n%n%s, %s%nID#: %d%nMajor: %s%n%nRequired Courses to "
+ "the right - >", myStudent.getLName(),
myStudent.getFName(), myStudent.getStudentId(), myStudent.getStudentMajor().getName()));
for (Course course : myStudent.getStudentMajor().getRequiredCourses()) {
reqCourseNames.add(course.getName());
}
ObservableList<String> items = FXCollections.observableArrayList(reqCourseNames);
reqCoursesList.setItems(items);
reqCoursesList.setPrefWidth(taWid / 2);
reqCoursesList.setPrefHeight(taHt);
reqCoursesList.setBorder(new Border(new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID, null, null)));
stuInfoFPane.getChildren().add(reqCoursesList);
}
}
public void show() {
if (!stuInfoStage.isShowing()) {
fNameField.setText("");
lNameField.setText("");
// Remove previous course list
stuInfoFPane.getChildren().remove(reqCoursesList);
stuInfoText.setMaxWidth(taWid);
// Set the search area to default with a list of all students in the College
stuInfoText.setText(String.format("%s%n", "Students at Neptune Technical College"));
for (Student stu : College.INSTANCE.getStudentList()) {
stuInfoText.appendText(String.format("%nID#: %d - %s, %s - %s%n", stu.getStudentId(),
stu.getLName(), stu.getFName(), stu.getStudentMajor().getName()));
}
stuInfoStage.show();
} else {
stuInfoStage.toFront();
}
}
public boolean isItShowing() {
boolean showing = false;
if (stuInfoStage.isShowing()) {
showing = true;
}
return showing;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment