View Javadoc
1   /*******************************************************************************
2    * jhunters: Pool League
3    * Copyright 2015 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Maintenance Panel.
40   */
41  public class MaintenancePanel extends VBox {
42      /**
43       * The addPlayer button text.
44       */
45      private static final String ADDPLAYER_TEXT = GuiResource.BUTTON_ADDPLAYER.getValue();
46  
47      /**
48       * The createCompetition button text.
49       */
50      private static final String CREATECOMP_TEXT = GuiResource.BUTTON_CREATECOMP.getValue();
51  
52      /**
53       * The save button text.
54       */
55      private static final String SAVE_TEXT = GuiResource.BUTTON_SAVE.getValue();
56  
57      /**
58       * the Control.
59       */
60      private final AppControl theControl;
61  
62      /**
63       * The competition Table.
64       */
65      private final CompetitionTable theCompetitions;
66  
67      /**
68       * The player Table.
69       */
70      private final PlayerTable thePlayers;
71  
72      /**
73       * Maintenance select.
74       */
75      private final ChoiceBox<MaintSelect> theMaintSelect;
76  
77      /**
78       * The current dataSet.
79       */
80      private DataSet theData;
81  
82      /**
83       * The add button.
84       */
85      private final Button theAddButton;
86  
87      /**
88       * The save button.
89       */
90      private final Button theSaveButton;
91  
92      /**
93       * The buttons view.
94       */
95      private final HBox theButtonsView;
96  
97      /**
98       * Current view.
99       */
100     private MaintSelect theCurSelect;
101 
102     /**
103      * Visible Node.
104      */
105     private Node theCurView;
106 
107     /**
108      * Constructor.
109      * @param pControl the control
110      * @param pPanel the competition panel
111      */
112     protected MaintenancePanel(final AppControl pControl,
113                                final CompetitionPanel pPanel) {
114         /* Store the control */
115         theControl = pControl;
116 
117         /* Create the CompetitionTable */
118         theCompetitions = new CompetitionTable(pPanel);
119 
120         /* Create the PlayerTable */
121         Label mySelected = new Label();
122         thePlayers = new PlayerTable(mySelected);
123         mySelected.setPadding(new Insets(0, 0, 0, 2));
124 
125         /* Create the Maintenance selection and populate it */
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         /* Handle changes of value */
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         /* Create the add button */
144         theAddButton = new Button();
145         theAddButton.setGraphic(GuiIcons.getButtonIcon(IconId.ADD));
146         theAddButton.setTooltip(new Tooltip(CREATECOMP_TEXT));
147 
148         /* Set add button action */
149         theAddButton.setOnAction(new EventHandler<ActionEvent>() {
150             @Override
151             public void handle(final ActionEvent e) {
152                 addNewItem();
153             }
154         });
155 
156         /* Create the save button */
157         theSaveButton = new Button();
158         theSaveButton.setGraphic(GuiIcons.getButtonIcon(IconId.SAVE));
159         theSaveButton.setTooltip(new Tooltip(SAVE_TEXT));
160 
161         /* Set save button action */
162         theSaveButton.setOnAction(new EventHandler<ActionEvent>() {
163             @Override
164             public void handle(final ActionEvent e) {
165                 theControl.saveToFile();
166             }
167         });
168 
169         /* Grow the selection buttons to the size of the print icon */
170         theMaintSelect.setMaxHeight(Double.MAX_VALUE);
171         mySelected.setMaxHeight(Double.MAX_VALUE);
172 
173         /* Create a small HBox for the right corner */
174         theButtonsView = new HBox();
175         theButtonsView.setSpacing(2);
176         theButtonsView.getChildren().addAll(theAddButton, theMaintSelect);
177 
178         /* Create an options sub-panel */
179         BorderPane mySelect = new BorderPane();
180         mySelect.setLeft(mySelected);
181         mySelect.setRight(theButtonsView);
182 
183         /* create panel */
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      * Set data.
193      * @param pData the dataSet
194      */
195     protected void setData(final DataSet pData) {
196         /* Store the data */
197         theData = pData;
198 
199         /* Ensure save button is in correct state */
200         final ObservableList<Node> myChildren = theButtonsView.getChildren();
201         myChildren.remove(theSaveButton);
202         if (theData.isUpdated()) {
203             myChildren.add(theSaveButton);
204         }
205 
206         /* Add listener */
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         /* Declare data to subPanels */
221         thePlayers.setData(pData);
222         theCompetitions.setData(pData);
223     }
224 
225     /**
226      * Set maintenance.
227      * @param pView the view
228      */
229     private void setMaint(final MaintSelect pView) {
230         /* Remove existing view */
231         ObservableList<Node> myChildren = getChildren();
232         myChildren.remove(theCurView);
233         WindowAdjustment myAdjust;
234 
235         /* Switch on view */
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         /* Add the view to the table */
251         myChildren.add(theCurView);
252         theCurSelect = pView;
253 
254         /* Adjust the stage */
255         theControl.adjustStage(myAdjust);
256     }
257 
258     /**
259      * Add New Item.
260      */
261     private void addNewItem() {
262         /* Switch on view */
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      * Maintenance selection.
276      */
277     protected enum MaintSelect {
278         /**
279          * Competitions.
280          */
281         COMPETITIONS,
282 
283         /**
284          * Players.
285          */
286         PLAYERS;
287 
288         /**
289          * The String name.
290          */
291         private String theName;
292 
293         @Override
294         public String toString() {
295             /* If we have not yet loaded the name */
296             if (theName == null) {
297                 /* Load the name */
298                 theName = GuiResource.getKeyForMaintSelect(this).getValue();
299             }
300 
301             /* return the name */
302             return theName;
303         }
304     }
305 }