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.time.LocalDate;
20  
21  import javafx.collections.ObservableList;
22  import javafx.geometry.Pos;
23  import javafx.scene.control.TableColumn;
24  import net.sourceforge.jhunters.pool.data.Competition;
25  import net.sourceforge.jhunters.pool.data.Competition.CompetitionComparator;
26  import net.sourceforge.jhunters.pool.data.Competition.CompetitionField;
27  import net.sourceforge.jhunters.pool.data.CompetitionList;
28  import net.sourceforge.jhunters.pool.data.DataSet;
29  import net.sourceforge.jhunters.pool.data.PlayerList;
30  import net.sourceforge.jhunters.pool.ui.GuiIcons.IconId;
31  import net.sourceforge.jhunters.pool.ui.TableUtils.FieldCell;
32  import net.sourceforge.jhunters.pool.ui.TableUtils.FieldTableColumn;
33  import net.sourceforge.jhunters.pool.ui.TableUtils.FieldTableView;
34  import net.sourceforge.jhunters.pool.ui.TableUtils.IconFieldMap;
35  
36  /**
37   * Competition Table.
38   */
39  public class CompetitionTable extends FieldTableView<Competition, CompetitionField> {
40      /**
41       * Width for competition name.
42       */
43      private static final int WIDTH_NAME = 150;
44  
45      /**
46       * Width for value.
47       */
48      private static final int WIDTH_PLAYERS = 50;
49  
50      /**
51       * Competition Panel.
52       */
53      private final CompetitionPanel theCompPanel;
54  
55      /**
56       * Competition Comparator.
57       */
58      private final CompetitionComparator theComparator;
59  
60      /**
61       * Current dataSet.
62       */
63      private DataSet theDataSet;
64  
65      /**
66       * Current list.
67       */
68      private ObservableList<Competition> theList;
69  
70      /**
71       * Constructor.
72       * @param pPanel the competition panel
73       */
74      protected CompetitionTable(final CompetitionPanel pPanel) {
75          /* Store parameters */
76          theCompPanel = pPanel;
77  
78          /* Allow editing */
79          setEditable(true);
80  
81          /* Allocate the comparator */
82          theComparator = new CompetitionComparator();
83  
84          /* Access column list */
85          ObservableList<TableColumn<Competition, ?>> myColumns = getColumns();
86  
87          /* Create the name column */
88          TableColumn<Competition, String> myNameCol = new FieldTableColumn<Competition, CompetitionField, String>(CompetitionField.NAME);
89          myNameCol.setCellValueFactory(p -> p.getValue().nameProperty());
90          myNameCol.setCellFactory(TableUtils.stringCellFactory(Pos.CENTER_RIGHT));
91          myNameCol.setMinWidth(WIDTH_NAME);
92          myNameCol.setSortable(false);
93          myColumns.add(myNameCol);
94  
95          /* Create the startDate */
96          TableColumn<Competition, LocalDate> myDateCol = new FieldTableColumn<Competition, CompetitionField, LocalDate>(CompetitionField.STARTDATE);
97          myDateCol.setCellValueFactory(p -> p.getValue().startDateProperty());
98          myDateCol.setCellFactory(TableUtils.dateCellFactory());
99          myDateCol.setSortable(false);
100         myDateCol.setMinWidth(FixtureSetTable.WIDTH_DATE);
101         myColumns.add(myDateCol);
102 
103         /* Create the #players column */
104         TableColumn<Competition, Integer> myPlayersCol = new FieldTableColumn<Competition, CompetitionField, Integer>(CompetitionField.PLAYERS);
105         myPlayersCol.setUserData(CompetitionField.PLAYERS);
106         myPlayersCol.setCellValueFactory(p -> p.getValue().playerCountProperty());
107         myPlayersCol.setCellFactory(TableUtils.integerCellFactory());
108         myPlayersCol.setSortable(false);
109         myPlayersCol.setMinWidth(WIDTH_PLAYERS);
110         myColumns.add(myPlayersCol);
111 
112         /* Build IconStateMap */
113         IconFieldMap<Boolean> myActiveMap = new IconFieldMap<Boolean>();
114         myActiveMap.declareIconState(Boolean.TRUE, IconId.ACTIVE, null);
115         myActiveMap.declareIconState(Boolean.FALSE, IconId.DELETE, Boolean.TRUE);
116 
117         /* Create the active column */
118         TableColumn<Competition, Boolean> myActiveCol = new FieldTableColumn<Competition, CompetitionField, Boolean>(CompetitionField.ACTIVE);
119         myActiveCol.setUserData(CompetitionField.ACTIVE);
120         myActiveCol.setCellValueFactory(p -> p.getValue().activeProperty());
121         myActiveCol.setCellFactory(TableUtils.iconCellFactory(myActiveMap));
122         myActiveCol.setSortable(false);
123         myActiveCol.setMinWidth(PlayerTable.WIDTH_SELECTED);
124         myColumns.add(myActiveCol);
125     }
126 
127     /**
128      * Set table data.
129      * @param pData the dataSet
130      */
131     protected void setData(final DataSet pData) {
132         /* Store the DataSet */
133         theDataSet = pData;
134 
135         /* Access the competition list */
136         CompetitionList myComps = pData.getCompetitions();
137         theList = myComps.getCompetitions();
138 
139         /* Declare the sorted list */
140         setItems(theList.sorted(theComparator));
141     }
142 
143     /**
144      * Add New Item.
145      */
146     protected void addNewItem() {
147         /* Create a new competition */
148         PlayerList myPlayers = theDataSet.getPlayers();
149         CompetitionList myCompetitions = theDataSet.getCompetitions();
150         myCompetitions.createCompetition(myPlayers);
151 
152         /* Note that the data is updated */
153         theCompPanel.updateCompetitions();
154         theDataSet.setUpdated(Boolean.TRUE);
155     }
156 
157     @Override
158     public boolean preEditHook(final FieldCell<Competition, CompetitionField> pCell) {
159         /* Access the details */
160         Competition myCompetition = pCell.getTheRow();
161         CompetitionField myField = pCell.getTheField();
162 
163         /* Switch on field */
164         switch (myField) {
165             case NAME:
166                 return true;
167             case STARTDATE:
168             case ACTIVE:
169                 return myCompetition.isActive();
170             case PLAYERS:
171             default:
172                 return false;
173         }
174     }
175 
176     @Override
177     public boolean preCommitHook(final FieldCell<Competition, CompetitionField> pCell,
178                                  final Object pValue) {
179         /* Access the details */
180         CompetitionField myField = pCell.getTheField();
181 
182         /* Switch on field */
183         switch (myField) {
184             case NAME:
185                 CompetitionList myCompetitions = theDataSet.getCompetitions();
186                 return myCompetitions.getCompetition((String) pValue) == null;
187             default:
188                 return true;
189         }
190     }
191 
192     @Override
193     public void postCommitHook(final FieldCell<Competition, CompetitionField> pCell) {
194         /* Access the details */
195         Competition myCompetition = pCell.getTheRow();
196         CompetitionField myField = pCell.getTheField();
197 
198         /* If this is the startDate */
199         if (CompetitionField.STARTDATE.equals(myField)) {
200             /* Update dates for the competition */
201             myCompetition.updateDates();
202 
203             /* process fixtures */
204             myCompetition.processFixtures();
205 
206             /* If the competition is deleted */
207         } else if (CompetitionField.ACTIVE.equals(myField)) {
208             /* Remove competition from the list */
209             CompetitionList myComps = theDataSet.getCompetitions();
210             myComps.removeCompetition(myCompetition);
211             theCompPanel.updateCompetitions();
212         }
213 
214         /* Note that the data is updated */
215         theDataSet.setUpdated(Boolean.TRUE);
216     }
217 }