Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package com.googlecode.lanterna.gui2.dialogs; |
2 | ||
3 | import com.googlecode.lanterna.gui2.Window; | |
4 | ||
5 | import java.util.Collections; | |
6 | import java.util.HashSet; | |
7 | import java.util.Set; | |
8 | ||
9 | /** | |
10 | * Abstract class for dialog building, containing much shared code between different kinds of dialogs | |
11 | * @param <B> The real type of the builder class | |
12 | * @param <T> Type of dialog this builder is building | |
13 | * @author Martin | |
14 | */ | |
15 | public abstract class AbstractDialogBuilder<B, T extends DialogWindow> { | |
16 | protected String title; | |
17 | protected String description; | |
18 | protected Set<Window.Hint> extraWindowHints; | |
19 | ||
20 | /** | |
21 | * Default constructor for a dialog builder | |
22 | * @param title Title to assign to the dialog | |
23 | */ | |
24 | public AbstractDialogBuilder(String title) { | |
25 | this.title = title; | |
26 | this.description = null; | |
27 | this.extraWindowHints = Collections.singleton(Window.Hint.CENTERED); | |
28 | } | |
29 | ||
30 | /** | |
31 | * Changes the title of the dialog | |
32 | * @param title New title | |
33 | * @return Itself | |
34 | */ | |
35 | public B setTitle(String title) { | |
36 | if(title == null) { | |
37 | title = ""; | |
38 | } | |
39 | this.title = title; | |
40 | return self(); | |
41 | } | |
42 | ||
43 | /** | |
44 | * Returns the title that the built dialog will have | |
45 | * @return Title that the built dialog will have | |
46 | */ | |
47 | public String getTitle() { | |
48 | return title; | |
49 | } | |
50 | ||
51 | /** | |
52 | * Changes the description of the dialog | |
53 | * @param description New description | |
54 | * @return Itself | |
55 | */ | |
56 | public B setDescription(String description) { | |
57 | this.description = description; | |
58 | return self(); | |
59 | } | |
60 | ||
61 | /** | |
62 | * Returns the description that the built dialog will have | |
63 | * @return Description that the built dialog will have | |
64 | */ | |
65 | public String getDescription() { | |
66 | return description; | |
67 | } | |
68 | ||
69 | /** | |
70 | * Assigns a set of extra window hints that you want the built dialog to have | |
71 | * @param extraWindowHints Window hints to assign to the window in addition to the ones the builder will put | |
72 | * @return Itself | |
73 | */ | |
74 | public B setExtraWindowHints(Set<Window.Hint> extraWindowHints) { | |
75 | this.extraWindowHints = extraWindowHints; | |
76 | return self(); | |
77 | } | |
78 | ||
79 | /** | |
80 | * Returns the list of extra window hints that will be assigned to the window when built | |
81 | * @return List of extra window hints that will be assigned to the window when built | |
82 | */ | |
83 | public Set<Window.Hint> getExtraWindowHints() { | |
84 | return extraWindowHints; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Helper method for casting this to {@code type} parameter {@code B} | |
89 | * @return {@code this} as {@code B} | |
90 | */ | |
91 | protected abstract B self(); | |
92 | ||
93 | /** | |
94 | * Builds the dialog according to the builder implementation | |
95 | * @return New dialog object | |
96 | */ | |
97 | protected abstract T buildDialog(); | |
98 | ||
99 | /** | |
100 | * Builds a new dialog following the specifications of this builder | |
101 | * @return New dialog built following the specifications of this builder | |
102 | */ | |
103 | public final T build() { | |
104 | T dialog = buildDialog(); | |
105 | if(!extraWindowHints.isEmpty()) { | |
106 | Set<Window.Hint> combinedHints = new HashSet<Window.Hint>(dialog.getHints()); | |
107 | combinedHints.addAll(extraWindowHints); | |
108 | dialog.setHints(combinedHints); | |
109 | } | |
110 | return dialog; | |
111 | } | |
112 | } |