1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jhunters.pool.ui;
18
19 import javafx.beans.value.ChangeListener;
20 import javafx.beans.value.ObservableValue;
21 import javafx.collections.ObservableList;
22 import javafx.event.ActionEvent;
23 import javafx.event.EventHandler;
24 import javafx.geometry.Insets;
25 import javafx.scene.Node;
26 import javafx.scene.control.Button;
27 import javafx.scene.control.ChoiceBox;
28 import javafx.scene.control.Label;
29 import javafx.scene.control.Tooltip;
30 import javafx.scene.layout.BorderPane;
31 import javafx.scene.layout.HBox;
32 import javafx.scene.layout.Priority;
33 import javafx.scene.layout.VBox;
34 import net.sourceforge.jhunters.pool.data.DataSet;
35 import net.sourceforge.jhunters.pool.ui.AppControl.WindowAdjustment;
36 import net.sourceforge.jhunters.pool.ui.GuiIcons.IconId;
37
38
39
40
41 public class MaintenancePanel extends VBox {
42
43
44
45 private static final String ADDPLAYER_TEXT = GuiResource.BUTTON_ADDPLAYER.getValue();
46
47
48
49
50 private static final String CREATECOMP_TEXT = GuiResource.BUTTON_CREATECOMP.getValue();
51
52
53
54
55 private static final String SAVE_TEXT = GuiResource.BUTTON_SAVE.getValue();
56
57
58
59
60 private final AppControl theControl;
61
62
63
64
65 private final CompetitionTable theCompetitions;
66
67
68
69
70 private final PlayerTable thePlayers;
71
72
73
74
75 private final ChoiceBox<MaintSelect> theMaintSelect;
76
77
78
79
80 private DataSet theData;
81
82
83
84
85 private final Button theAddButton;
86
87
88
89
90 private final Button theSaveButton;
91
92
93
94
95 private final HBox theButtonsView;
96
97
98
99
100 private MaintSelect theCurSelect;
101
102
103
104
105 private Node theCurView;
106
107
108
109
110
111
112 protected MaintenancePanel(final AppControl pControl,
113 final CompetitionPanel pPanel) {
114
115 theControl = pControl;
116
117
118 theCompetitions = new CompetitionTable(pPanel);
119
120
121 Label mySelected = new Label();
122 thePlayers = new PlayerTable(mySelected);
123 mySelected.setPadding(new Insets(0, 0, 0, 2));
124
125
126 theMaintSelect = new ChoiceBox<MaintSelect>();
127 ObservableList<MaintSelect> myItems = theMaintSelect.getItems();
128 for (MaintSelect myMaint : MaintSelect.values()) {
129 myItems.add(myMaint);
130 }
131 theMaintSelect.setValue(MaintSelect.PLAYERS);
132
133
134 theMaintSelect.valueProperty().addListener(new ChangeListener<MaintSelect>() {
135 @Override
136 public void changed(final ObservableValue<? extends MaintSelect> pProperty,
137 final MaintSelect pOldValue,
138 final MaintSelect pNewValue) {
139 setMaint(pNewValue);
140 }
141 });
142
143
144 theAddButton = new Button();
145 theAddButton.setGraphic(GuiIcons.getButtonIcon(IconId.ADD));
146 theAddButton.setTooltip(new Tooltip(CREATECOMP_TEXT));
147
148
149 theAddButton.setOnAction(new EventHandler<ActionEvent>() {
150 @Override
151 public void handle(final ActionEvent e) {
152 addNewItem();
153 }
154 });
155
156
157 theSaveButton = new Button();
158 theSaveButton.setGraphic(GuiIcons.getButtonIcon(IconId.SAVE));
159 theSaveButton.setTooltip(new Tooltip(SAVE_TEXT));
160
161
162 theSaveButton.setOnAction(new EventHandler<ActionEvent>() {
163 @Override
164 public void handle(final ActionEvent e) {
165 theControl.saveToFile();
166 }
167 });
168
169
170 theMaintSelect.setMaxHeight(Double.MAX_VALUE);
171 mySelected.setMaxHeight(Double.MAX_VALUE);
172
173
174 theButtonsView = new HBox();
175 theButtonsView.setSpacing(2);
176 theButtonsView.getChildren().addAll(theAddButton, theMaintSelect);
177
178
179 BorderPane mySelect = new BorderPane();
180 mySelect.setLeft(mySelected);
181 mySelect.setRight(theButtonsView);
182
183
184 VBox.setVgrow(thePlayers, Priority.ALWAYS);
185 VBox.setVgrow(theCompetitions, Priority.ALWAYS);
186 getChildren().addAll(mySelect, thePlayers);
187 theCurView = thePlayers;
188 theCurSelect = MaintSelect.PLAYERS;
189 }
190
191
192
193
194
195 protected void setData(final DataSet pData) {
196
197 theData = pData;
198
199
200 final ObservableList<Node> myChildren = theButtonsView.getChildren();
201 myChildren.remove(theSaveButton);
202 if (theData.isUpdated()) {
203 myChildren.add(theSaveButton);
204 }
205
206
207 theData.updatedProperty().addListener(
208 new ChangeListener<Boolean>() {
209 public void changed(final ObservableValue<? extends Boolean> pValue,
210 final Boolean pOldState,
211 final Boolean pNewState) {
212 if (pNewState) {
213 myChildren.add(theSaveButton);
214 } else {
215 myChildren.remove(theSaveButton);
216 }
217 }
218 });
219
220
221 thePlayers.setData(pData);
222 theCompetitions.setData(pData);
223 }
224
225
226
227
228
229 private void setMaint(final MaintSelect pView) {
230
231 ObservableList<Node> myChildren = getChildren();
232 myChildren.remove(theCurView);
233 WindowAdjustment myAdjust;
234
235
236 switch (pView) {
237 case COMPETITIONS:
238 myAdjust = new WindowAdjustment(theCompetitions, theCurView);
239 theCurView = theCompetitions;
240 theAddButton.setTooltip(new Tooltip(CREATECOMP_TEXT));
241 break;
242 case PLAYERS:
243 default:
244 myAdjust = new WindowAdjustment(thePlayers, theCurView);
245 theCurView = thePlayers;
246 theAddButton.setTooltip(new Tooltip(ADDPLAYER_TEXT));
247 break;
248 }
249
250
251 myChildren.add(theCurView);
252 theCurSelect = pView;
253
254
255 theControl.adjustStage(myAdjust);
256 }
257
258
259
260
261 private void addNewItem() {
262
263 switch (theCurSelect) {
264 case COMPETITIONS:
265 theCompetitions.addNewItem();
266 break;
267 case PLAYERS:
268 default:
269 thePlayers.addNewItem();
270 break;
271 }
272 }
273
274
275
276
277 protected enum MaintSelect {
278
279
280
281 COMPETITIONS,
282
283
284
285
286 PLAYERS;
287
288
289
290
291 private String theName;
292
293 @Override
294 public String toString() {
295
296 if (theName == null) {
297
298 theName = GuiResource.getKeyForMaintSelect(this).getValue();
299 }
300
301
302 return theName;
303 }
304 }
305 }