package com.googlecode.lanterna.gui2.dialogs; import com.googlecode.lanterna.gui2.Window; import java.util.Collections; import java.util.HashSet; import java.util.Set; /** * Abstract class for dialog building, containing much shared code between different kinds of dialogs * @param The real type of the builder class * @param Type of dialog this builder is building * @author Martin */ public abstract class AbstractDialogBuilder { protected String title; protected String description; protected Set extraWindowHints; /** * Default constructor for a dialog builder * @param title Title to assign to the dialog */ public AbstractDialogBuilder(String title) { this.title = title; this.description = null; this.extraWindowHints = Collections.singleton(Window.Hint.CENTERED); } /** * Changes the title of the dialog * @param title New title * @return Itself */ public B setTitle(String title) { if(title == null) { title = ""; } this.title = title; return self(); } /** * Returns the title that the built dialog will have * @return Title that the built dialog will have */ public String getTitle() { return title; } /** * Changes the description of the dialog * @param description New description * @return Itself */ public B setDescription(String description) { this.description = description; return self(); } /** * Returns the description that the built dialog will have * @return Description that the built dialog will have */ public String getDescription() { return description; } /** * Assigns a set of extra window hints that you want the built dialog to have * @param extraWindowHints Window hints to assign to the window in addition to the ones the builder will put * @return Itself */ public B setExtraWindowHints(Set extraWindowHints) { this.extraWindowHints = extraWindowHints; return self(); } /** * Returns the list of extra window hints that will be assigned to the window when built * @return List of extra window hints that will be assigned to the window when built */ public Set getExtraWindowHints() { return extraWindowHints; } /** * Helper method for casting this to {@code type} parameter {@code B} * @return {@code this} as {@code B} */ protected abstract B self(); /** * Builds the dialog according to the builder implementation * @return New dialog object */ protected abstract T buildDialog(); /** * Builds a new dialog following the specifications of this builder * @return New dialog built following the specifications of this builder */ public final T build() { T dialog = buildDialog(); if(!extraWindowHints.isEmpty()) { Set combinedHints = new HashSet(dialog.getHints()); combinedHints.addAll(extraWindowHints); dialog.setHints(combinedHints); } return dialog; } }