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.beans.value.ObservableValue;
24  import javafx.collections.FXCollections;
25  import javafx.collections.ObservableList;
26  import javafx.geometry.Pos;
27  import javafx.scene.control.TableCell;
28  import javafx.scene.control.TableColumn;
29  import javafx.scene.control.TableColumn.CellDataFeatures;
30  import javafx.util.Callback;
31  import net.sourceforge.jhunters.pool.data.Competition;
32  import net.sourceforge.jhunters.pool.data.CompetitionFixtures;
33  import net.sourceforge.jhunters.pool.data.CompetitionFixtures.FixtureOutlook;
34  import net.sourceforge.jhunters.pool.data.CompetitionFixtures.PlayerFixtureField;
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   * Outlook Table.
41   */
42  public class OutlookTable
43          extends FieldTableView<CompetitionFixtures, PlayerFixtureField>
44          implements TableSource {
45      /**
46       * Width for fixture.
47       */
48      private static final int WIDTH_FIXTURE = 100;
49  
50      /**
51       * League table format.
52       */
53      private static final String CONTACTS_FORMAT = GuiResource.REPORT_CONTACTS_TITLE.getValue();
54  
55      /**
56       * Fixture cell factory.
57       */
58      private final Callback<TableColumn<CompetitionFixtures, FixtureOutlook>, TableCell<CompetitionFixtures, FixtureOutlook>> theFixtureCellFactory;
59  
60      /**
61       * Current analysis list.
62       */
63      private ObservableList<CompetitionFixtures> theList;
64  
65      /**
66       * Constructor.
67       */
68      protected OutlookTable() {
69          /* Disallow editing */
70          setEditable(false);
71  
72          /* Access column list */
73          ObservableList<TableColumn<CompetitionFixtures, ?>> myColumns = getColumns();
74  
75          /* Create the player column */
76          TableColumn<CompetitionFixtures, String> myPlayerCol = new FieldTableColumn<CompetitionFixtures, PlayerFixtureField, String>(PlayerFixtureField.NAME);
77          myPlayerCol.setCellValueFactory(p -> p.getValue().getPlayer().nameProperty());
78          myPlayerCol.setCellFactory(TableUtils.itemCellFactory(Pos.CENTER_RIGHT));
79          myPlayerCol.setMinWidth(ResultsTable.WIDTH_PLAYER);
80          myColumns.add(myPlayerCol);
81  
82          /* Create the id column */
83          TableColumn<CompetitionFixtures, String> myIdCol = new FieldTableColumn<CompetitionFixtures, PlayerFixtureField, String>(PlayerFixtureField.ID);
84          myIdCol.setCellValueFactory(p -> p.getValue().idProperty());
85          myIdCol.setCellFactory(TableUtils.itemCellFactory(Pos.CENTER));
86          myIdCol.setMinWidth(ResultsTable.WIDTH_VALUE);
87          myColumns.add(myIdCol);
88  
89          /* Create the fixture cell factory */
90          theFixtureCellFactory = TableUtils.itemCellFactory(Pos.CENTER);
91  
92          /* Create the fixture columns */
93          for (int i = 0; i < CompetitionFixtures.OUTLOOK_DEPTH; i++) {
94              /* Create the fixture column */
95              TableColumn<CompetitionFixtures, FixtureOutlook> myFixCol = new FieldTableColumn<CompetitionFixtures, PlayerFixtureField, FixtureOutlook>(PlayerFixtureField.OUTLOOK,
96                      Integer.toString(i + 1));
97              myFixCol.setCellValueFactory(new FixtureCellValueFactory(i));
98              myFixCol.setCellFactory(theFixtureCellFactory);
99              myFixCol.setMinWidth(WIDTH_FIXTURE);
100             myColumns.add(myFixCol);
101         }
102 
103         /* Create the contact column */
104         TableColumn<CompetitionFixtures, String> myContactCol = new FieldTableColumn<CompetitionFixtures, PlayerFixtureField, String>(PlayerFixtureField.CONTACT);
105         myContactCol.setCellValueFactory(p -> p.getValue().getPlayer().contactProperty());
106         myContactCol.setCellFactory(TableUtils.itemCellFactory(Pos.CENTER_LEFT));
107         myContactCol.setMinWidth(ResultsTable.WIDTH_PLAYER);
108         myColumns.add(myContactCol);
109     }
110 
111     /**
112      * Set table data.
113      * @param pCompetition the competition
114      */
115     protected void setCompetition(final Competition pCompetition) {
116         /* Access the fixture list */
117         theList = pCompetition != null
118                                        ? pCompetition.getMatrix().getMatrix()
119                                        : FXCollections.observableArrayList();
120 
121         /* Declare the list */
122         setItems(theList);
123     }
124 
125     @Override
126     public void buildReport(final ReportBuilder pBuilder) {
127         /* Build the main report */
128         buildMainReport(pBuilder);
129     }
130 
131     @Override
132     public void createWeb(final ReportBuilder pBuilder) {
133         /* Build the main report */
134         buildMainReport(pBuilder);
135     }
136 
137     /**
138      * build the main report.
139      * @param pBuilder the builder
140      */
141     private void buildMainReport(final ReportBuilder pBuilder) {
142         /* Set the heading */
143         pBuilder.setHeading(CONTACTS_FORMAT);
144 
145         /* Create a new table */
146         pBuilder.newTable();
147 
148         /* Set the column headings */
149         pBuilder.setColumnHeading(PlayerFixtureField.NAME.toString());
150         pBuilder.setColumnHeading(PlayerFixtureField.CONTACT.toString());
151 
152         /* Loop through the players */
153         boolean bFirst = true;
154         Iterator<CompetitionFixtures> myIterator = theList.iterator();
155         while (myIterator.hasNext()) {
156             CompetitionFixtures myFixtures = myIterator.next();
157 
158             /* Create a new row */
159             Element myRow = pBuilder.newRow();
160 
161             /* Add separator for first row */
162             if (bFirst) {
163                 pBuilder.addClassAttribute(myRow, ReportBuilder.CLASS_SEPARATOR);
164                 bFirst = false;
165             }
166 
167             /* Set the column headings */
168             pBuilder.setCellPlayerRight(myFixtures.playerProperty().getValue());
169             pBuilder.setCellContact(myFixtures.playerProperty().getValue());
170         }
171     }
172 
173     /**
174      * FixtureCellValue factory.
175      */
176     private static final class FixtureCellValueFactory
177             implements Callback<CellDataFeatures<CompetitionFixtures, FixtureOutlook>, ObservableValue<FixtureOutlook>> {
178         /**
179          * Field value.
180          */
181         private final int theIndex;
182 
183         /**
184          * Constructor.
185          * @param pIndex the index
186          */
187         private FixtureCellValueFactory(final int pIndex) {
188             theIndex = pIndex;
189         }
190 
191         @Override
192         public ObservableValue<FixtureOutlook> call(final CellDataFeatures<CompetitionFixtures, FixtureOutlook> p) {
193             return p.getValue().outlookProperty(theIndex);
194         }
195     }
196 }