Skip to content
Snippets Groups Projects

Resolve "Improve code quality in MainPageView"

Merged Sverre Grønhaug Halvorsen requested to merge 34-improve-code-quality-in-mainpageview into dev
8 files
+ 575
304
Compare changes
  • Side-by-side
  • Inline
Files
8
package edu.ntnu.idatt2003.view.components;
import java.util.List;
import java.util.function.BiConsumer;
import javafx.event.ActionEvent;
import javafx.scene.control.ComboBox;
/**
@@ -8,7 +10,14 @@ import javafx.scene.control.ComboBox;
* The combo box has a style class "combo-box" and the values are set to the specified values.
* Goal: Create a styled combo box with a specified prompt text, width, height and values.
*/
public class StyledComboBox<T> extends ComboBox<T> {
public class ComboBoxFactory {
/**
* Private constructor to prevent instantiation.
*/
private ComboBoxFactory() {
}
/**
* Creates a styled combo box with a specified prompt text, width, height and values.
* The combo box has a style class "combo-box" and the values are set to the specified values.
@@ -18,13 +27,40 @@ public class StyledComboBox<T> extends ComboBox<T> {
* @param height The preferred height of the combo box.
* @param values The values to set in the combo box.
*/
public StyledComboBox(String promptText, int width, int height, List<T> values) {
super();
this.setPromptText(promptText);
this.setPrefSize(width, height);
this.getStyleClass().add("combo-box");
public static <T> ComboBox<T> createComboBox(String promptText, int width, int height, List<T> values,
BiConsumer<ComboBox<T>, ActionEvent> eventHandler) {
return createComboBoxWithStyle(promptText, width, height, values, eventHandler);
}
public static <T> ComboBox<T> createComboBox(String promptText, int width, int height, List<T> values,
BiConsumer<ComboBox<T>, ActionEvent> eventHandler, T defaultValue) {
ComboBox<T> comboBox = createComboBoxWithStyle(promptText, width, height, values, eventHandler);
comboBox.setValue(defaultValue);
return comboBox;
}
/**
* Creates a styled combo box with a specified prompt text, width, height, values and events
* to be used in other methods in class.
*
* @param promptText The text to display in the combo box when it is empty.
* @param width The preferred width of the combo box.
* @param height The preferred height of the combo box.
* @param values The values to set in the combo box.
* @param eventHandler The event handler to set on the combo box.
* @return The styled combo box.
*/
private static <T> ComboBox<T> createComboBoxWithStyle(String promptText, int width, int height, List<T> values,
BiConsumer<ComboBox<T>, ActionEvent> eventHandler) {
ComboBox<T> comboBox = new ComboBox<>();
comboBox.setPromptText(promptText);
comboBox.setPromptText(promptText);
comboBox.setPrefSize(width, height);
comboBox.getStyleClass().add("combo-box");
comboBox.setOnAction(e -> eventHandler.accept(comboBox, e));
if (values != null) {
this.getItems().addAll(values);
comboBox.getItems().addAll(values);
}
return comboBox;
}
}
Loading