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.collections.transformation.SortedList;
26  import javafx.geometry.Pos;
27  import javafx.scene.control.TableColumn;
28  import net.sourceforge.jhunters.pool.data.Competition;
29  import net.sourceforge.jhunters.pool.data.CompetitionFixtures.PlayerFixtureField;
30  import net.sourceforge.jhunters.pool.data.Player;
31  import net.sourceforge.jhunters.pool.data.Player.PlayerComparator;
32  import net.sourceforge.jhunters.pool.data.Player.PlayerField;
33  import net.sourceforge.jhunters.pool.ui.ReportPanel.TableSource;
34  import net.sourceforge.jhunters.pool.ui.TableUtils.FieldTableColumn;
35  import net.sourceforge.jhunters.pool.ui.TableUtils.FieldTableView;
36  
37  /**
38   * Participants Table.
39   */
40  public class ParticipantsTable
41          extends FieldTableView<Player, PlayerField>
42          implements TableSource {
43      /**
44       * Contacts format.
45       */
46      private static final String CONTACTS_FORMAT = GuiResource.REPORT_CONTACTS_TITLE.getValue();
47  
48      /**
49       * Player Comparator.
50       */
51      private final PlayerComparator theComparator;
52  
53      /**
54       * The players.
55       */
56      private SortedList<Player> theList;
57  
58      /**
59       * Constructor.
60       */
61      protected ParticipantsTable() {
62          /* Disallow editing */
63          setEditable(false);
64  
65          /* Allocate the comparator */
66          theComparator = new PlayerComparator();
67  
68          /* Access column list */
69          ObservableList<TableColumn<Player, ?>> myColumns = getColumns();
70  
71          /* Create the name column */
72          TableColumn<Player, String> myNameCol = new FieldTableColumn<Player, PlayerField, String>(PlayerField.NAME);
73          myNameCol.setCellValueFactory(p -> p.getValue().nameProperty());
74          myNameCol.setCellFactory(TableUtils.stringCellFactory(Pos.CENTER_RIGHT));
75          myNameCol.setMinWidth(PlayerTable.WIDTH_NAME);
76          myNameCol.setSortable(false);
77          myColumns.add(myNameCol);
78  
79          /* Create the contact column */
80          TableColumn<Player, String> myContactCol = new FieldTableColumn<Player, PlayerField, String>(PlayerField.CONTACT);
81          myContactCol.setCellValueFactory(p -> p.getValue().contactProperty());
82          myContactCol.setCellFactory(TableUtils.stringCellFactory(Pos.CENTER));
83          myContactCol.setSortable(false);
84          myContactCol.setMinWidth(PlayerTable.WIDTH_CONTACT);
85          myColumns.add(myContactCol);
86      }
87  
88      /**
89       * Set table data.
90       * @param pCompetition the competition
91       */
92      protected void setCompetition(final Competition pCompetition) {
93          /* Access the player list */
94          ObservableList<Player> myList = pCompetition != null
95                                                               ? pCompetition.getParticipants().getPlayers()
96                                                               : FXCollections.observableArrayList();
97          theList = (myList != null)
98                                     ? myList.sorted(theComparator)
99                                     : null;
100 
101         /* Declare the list */
102         setItems(theList);
103     }
104 
105     @Override
106     public void buildReport(final ReportBuilder pBuilder) {
107         /* Build the main report */
108         buildMainReport(pBuilder);
109     }
110 
111     @Override
112     public void createWeb(final ReportBuilder pBuilder) {
113         /* Build the main report */
114         buildMainReport(pBuilder);
115     }
116 
117     /**
118      * build the main report.
119      * @param pBuilder the builder
120      */
121     private void buildMainReport(final ReportBuilder pBuilder) {
122         /* Set the heading */
123         pBuilder.setHeading(CONTACTS_FORMAT);
124 
125         /* Create a new table */
126         pBuilder.newTable();
127 
128         /* Set the column headings */
129         pBuilder.setColumnHeading(PlayerFixtureField.NAME.toString());
130         pBuilder.setColumnHeading(PlayerFixtureField.CONTACT.toString());
131 
132         /* Loop through the players */
133         boolean bFirst = true;
134         Iterator<Player> myIterator = theList.iterator();
135         while (myIterator.hasNext()) {
136             Player myPlayer = myIterator.next();
137 
138             /* Create a new row */
139             Element myRow = pBuilder.newRow();
140 
141             /* Add separator for first row */
142             if (bFirst) {
143                 pBuilder.addClassAttribute(myRow, ReportBuilder.CLASS_SEPARATOR);
144                 bFirst = false;
145             }
146 
147             /* Set the column values */
148             pBuilder.setCellPlayerRight(myPlayer);
149             pBuilder.setCellContact(myPlayer);
150         }
151     }
152 }