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 java.util.Iterator;
20  
21  import org.w3c.dom.Element;
22  
23  import javafx.collections.FXCollections;
24  import javafx.collections.ObservableList;
25  import javafx.geometry.Pos;
26  import javafx.scene.control.TableCell;
27  import javafx.scene.control.TableColumn;
28  import javafx.util.Callback;
29  import net.sourceforge.jhunters.pool.data.Competition;
30  import net.sourceforge.jhunters.pool.data.CompetitionStandings;
31  import net.sourceforge.jhunters.pool.data.DataParser;
32  import net.sourceforge.jhunters.pool.data.DataSet;
33  import net.sourceforge.jhunters.pool.data.PlayerAnalysis;
34  import net.sourceforge.jhunters.pool.data.PlayerAnalysis.AnalysisField;
35  import net.sourceforge.jhunters.pool.ui.ReportPanel.TableSource;
36  import net.sourceforge.jhunters.pool.ui.TableUtils.FieldTableColumn;
37  import net.sourceforge.jhunters.pool.ui.TableUtils.FieldTableView;
38  
39  /**
40   * League Table.
41   */
42  public class LeagueTable
43          extends FieldTableView<PlayerAnalysis, AnalysisField>
44          implements TableSource {
45      /**
46       * Width for player.
47       */
48      private static final int WIDTH_PLAYER = 120;
49  
50      /**
51       * Width for value.
52       */
53      private static final int WIDTH_VALUE = 65;
54  
55      /**
56       * League table format.
57       */
58      private static final String LEAGUE_FORMAT = GuiResource.REPORT_LEAGUE_TITLE.getValue();
59  
60      /**
61       * TieBreak format.
62       */
63      private static final String TIEBREAK_FORMAT = GuiResource.REPORT_LEAGUE_TIEBREAKS.getValue();
64  
65      /**
66       * TieBreak Table format.
67       */
68      private static final String TIEBREAK_TABLE_FORMAT = GuiResource.REPORT_LEAGUE_TIEBREAK_TABLE.getValue();
69  
70      /**
71       * The Outstanding Table.
72       */
73      private final OutstandingTable theOutstanding;
74  
75      /**
76       * Current dataSet.
77       */
78      private DataSet theDataSet;
79  
80      /**
81       * Current competition.
82       */
83      private Competition theCompetition;
84  
85      /**
86       * Current analysis list.
87       */
88      private ObservableList<PlayerAnalysis> theList;
89  
90      /**
91       * Integer cell factory.
92       */
93      private final Callback<TableColumn<PlayerAnalysis, Integer>, TableCell<PlayerAnalysis, Integer>> theIntegerCellFactory;
94  
95      /**
96       * Constructor.
97       * @param pOutstanding the outstanding table
98       */
99      protected LeagueTable(final OutstandingTable pOutstanding) {
100         /* Store parameters */
101         theOutstanding = pOutstanding;
102 
103         /* Disallow editing */
104         setEditable(false);
105 
106         /* Create the integer cell factory */
107         theIntegerCellFactory = TableUtils.integerCellFactory();
108 
109         /* Access column list */
110         ObservableList<TableColumn<PlayerAnalysis, ?>> myColumns = getColumns();
111 
112         /* Create the player column */
113         TableColumn<PlayerAnalysis, String> myPlayerCol = new FieldTableColumn<PlayerAnalysis, AnalysisField, String>(AnalysisField.NAME);
114         myPlayerCol.setCellValueFactory(p -> p.getValue().getPlayer().nameProperty());
115         myPlayerCol.setCellFactory(TableUtils.itemCellFactory(Pos.CENTER_RIGHT));
116         myPlayerCol.setMinWidth(WIDTH_PLAYER);
117         myColumns.add(myPlayerCol);
118 
119         /* Create the played column */
120         TableColumn<PlayerAnalysis, Integer> myPlayedCol = new FieldTableColumn<PlayerAnalysis, AnalysisField, Integer>(AnalysisField.PLAYED);
121         myPlayedCol.setCellValueFactory(p -> p.getValue().playedProperty());
122         myPlayedCol.setCellFactory(theIntegerCellFactory);
123         myPlayedCol.setMinWidth(WIDTH_VALUE);
124         myColumns.add(myPlayedCol);
125 
126         /* Create the won column */
127         TableColumn<PlayerAnalysis, Integer> myWonCol = new FieldTableColumn<PlayerAnalysis, AnalysisField, Integer>(AnalysisField.WON);
128         myWonCol.setCellValueFactory(p -> p.getValue().wonProperty());
129         myWonCol.setCellFactory(theIntegerCellFactory);
130         myWonCol.setMinWidth(WIDTH_VALUE);
131         myColumns.add(myWonCol);
132 
133         /* Create the lost column */
134         TableColumn<PlayerAnalysis, Integer> myLostCol = new FieldTableColumn<PlayerAnalysis, AnalysisField, Integer>(AnalysisField.LOST);
135         myLostCol.setCellValueFactory(p -> p.getValue().lostProperty());
136         myLostCol.setCellFactory(theIntegerCellFactory);
137         myLostCol.setMinWidth(WIDTH_VALUE);
138         myColumns.add(myLostCol);
139 
140         /* Create the for column */
141         TableColumn<PlayerAnalysis, Integer> myForCol = new FieldTableColumn<PlayerAnalysis, AnalysisField, Integer>(AnalysisField.FOR);
142         myForCol.setCellValueFactory(p -> p.getValue().forProperty());
143         myForCol.setCellFactory(theIntegerCellFactory);
144         myForCol.setMinWidth(WIDTH_VALUE);
145         myColumns.add(myForCol);
146 
147         /* Create the played column */
148         TableColumn<PlayerAnalysis, Integer> myAgainstCol = new FieldTableColumn<PlayerAnalysis, AnalysisField, Integer>(AnalysisField.AGAINST);
149         myAgainstCol.setCellValueFactory(p -> p.getValue().againstProperty());
150         myAgainstCol.setCellFactory(theIntegerCellFactory);
151         myAgainstCol.setMinWidth(WIDTH_VALUE);
152         myColumns.add(myAgainstCol);
153 
154         /* Create the difference column */
155         TableColumn<PlayerAnalysis, Integer> myDiffCol = new FieldTableColumn<PlayerAnalysis, AnalysisField, Integer>(AnalysisField.DIFFERENCE);
156         myDiffCol.setCellValueFactory(p -> p.getValue().differenceProperty());
157         myDiffCol.setCellFactory(theIntegerCellFactory);
158         myDiffCol.setMinWidth(WIDTH_VALUE);
159         myColumns.add(myDiffCol);
160 
161         /* Create the points column */
162         TableColumn<PlayerAnalysis, Integer> myPointsCol = new FieldTableColumn<PlayerAnalysis, AnalysisField, Integer>(AnalysisField.POINTS);
163         myPointsCol.setCellValueFactory(p -> p.getValue().pointsProperty());
164         myPointsCol.setCellFactory(theIntegerCellFactory);
165         myPointsCol.setMinWidth(WIDTH_VALUE);
166         myColumns.add(myPointsCol);
167 
168         /* Create the tieBreak column */
169         TableColumn<PlayerAnalysis, Integer> myBreakCol = new FieldTableColumn<PlayerAnalysis, AnalysisField, Integer>(AnalysisField.TIEBREAK);
170         myBreakCol.setCellValueFactory(p -> p.getValue().tieBreakerProperty());
171         myBreakCol.setCellFactory(theIntegerCellFactory);
172         myBreakCol.setMinWidth(WIDTH_VALUE);
173         myColumns.add(myBreakCol);
174     }
175 
176     /**
177      * Set dataSet.
178      * @param pData the dataSet
179      */
180     protected void setData(final DataSet pData) {
181         /* Store the DataSet */
182         theDataSet = pData;
183     }
184 
185     /**
186      * Set competition.
187      * @param pCompetition the competition
188      */
189     protected void setCompetition(final Competition pCompetition) {
190         /* Access the league table */
191         theList = pCompetition != null
192                                        ? pCompetition.getTable().getTable()
193                                        : FXCollections.observableArrayList();
194 
195         /* Declare the list */
196         theCompetition = pCompetition;
197         setItems(theList);
198     }
199 
200     @Override
201     public void buildReport(final ReportBuilder pBuilder) {
202         /* Build the main report */
203         buildMainReport(pBuilder);
204 
205         /* Add the Outstanding Report */
206         theOutstanding.buildReport(pBuilder);
207     }
208 
209     @Override
210     public void createWeb(final ReportBuilder pBuilder) {
211         /* Build the main report */
212         buildMainReport(pBuilder);
213 
214         /* Build TieBreaks */
215         buildTieBreaks(pBuilder);
216     }
217 
218     /**
219      * build the main report.
220      * @param pBuilder the builder
221      */
222     private void buildMainReport(final ReportBuilder pBuilder) {
223         /* Set the heading */
224         pBuilder.setHeading(LEAGUE_FORMAT, DataParser.formatDate(theDataSet.getLastChange()));
225 
226         /* Create a new table */
227         buildTable(pBuilder, theList);
228     }
229 
230     /**
231      * build the TieBreaks.
232      * @param pBuilder the builder
233      */
234     private void buildTieBreaks(final ReportBuilder pBuilder) {
235         /* Set the heading */
236         pBuilder.setHeading(TIEBREAK_FORMAT);
237 
238         /* Access leading player */
239         PlayerAnalysis myLead = theList.get(0);
240         CompetitionStandings myStandings = theCompetition.getTable();
241 
242         /* Loop through the tieBreaks */
243         for (Integer myPoints = myLead.getPoints(); myPoints > 0; myPoints -= 2) {
244             /* Access the table */
245             ObservableList<PlayerAnalysis> myTable = myStandings.getTieBreakTable(myPoints);
246             if (myTable != null) {
247                 /* Set the heading */
248                 pBuilder.setHeading(TIEBREAK_TABLE_FORMAT, myPoints);
249                 buildTable(pBuilder, myTable);
250             }
251         }
252     }
253 
254     /**
255      * build the table.
256      * @param pBuilder the builder
257      * @param pTable the table
258      */
259     private void buildTable(final ReportBuilder pBuilder,
260                             final ObservableList<PlayerAnalysis> pTable) {
261         /* Create a new table */
262         pBuilder.newTable();
263 
264         /* Set the column headings */
265         pBuilder.setColumnHeading(AnalysisField.NAME.getShortName());
266         pBuilder.setColumnHeading(AnalysisField.PLAYED.getShortName());
267         pBuilder.setColumnHeading(AnalysisField.WON.getShortName());
268         pBuilder.setColumnHeading(AnalysisField.LOST.getShortName());
269         pBuilder.setColumnHeading(AnalysisField.FOR.getShortName());
270         pBuilder.setColumnHeading(AnalysisField.AGAINST.getShortName());
271         pBuilder.setColumnHeading(AnalysisField.DIFFERENCE.getShortName());
272         pBuilder.setColumnHeading(AnalysisField.POINTS.getShortName());
273         pBuilder.setColumnHeading(AnalysisField.TIEBREAK.getShortName());
274 
275         /* Loop through the players */
276         boolean bFirst = true;
277         Iterator<PlayerAnalysis> myIterator = pTable.iterator();
278         while (myIterator.hasNext()) {
279             PlayerAnalysis myAnalysis = myIterator.next();
280 
281             /* Create a new row */
282             Element myRow = pBuilder.newRow();
283 
284             /* Add separator for first row */
285             if (bFirst) {
286                 pBuilder.addClassAttribute(myRow, ReportBuilder.CLASS_SEPARATOR);
287                 bFirst = false;
288             }
289 
290             /* Set the column headings */
291             pBuilder.setCellPlayerRight(myAnalysis.playerProperty().getValue());
292             pBuilder.setCellInteger(myAnalysis.playedProperty().getValue());
293             pBuilder.setCellInteger(myAnalysis.wonProperty().getValue());
294             pBuilder.setCellInteger(myAnalysis.lostProperty().getValue());
295             pBuilder.setCellInteger(myAnalysis.forProperty().getValue());
296             pBuilder.setCellInteger(myAnalysis.againstProperty().getValue());
297             pBuilder.setCellInteger(myAnalysis.differenceProperty().getValue());
298             pBuilder.setCellInteger(myAnalysis.pointsProperty().getValue());
299             pBuilder.setCellInteger(myAnalysis.tieBreakerProperty().getValue());
300         }
301     }
302 }