TableCellFactories.java
001 /*
002  * Copyright 2008-2017 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package griffon.javafx.scene.control;
017 
018 import javafx.collections.ObservableList;
019 import javafx.scene.control.TableCell;
020 import javafx.scene.control.TableColumn;
021 import javafx.scene.control.cell.CheckBoxTableCell;
022 import javafx.scene.control.cell.ChoiceBoxTableCell;
023 import javafx.scene.control.cell.ComboBoxTableCell;
024 import javafx.scene.control.cell.ProgressBarTableCell;
025 import javafx.scene.control.cell.TextFieldTableCell;
026 import javafx.util.StringConverter;
027 
028 import javax.annotation.Nonnull;
029 
030 import static java.util.Objects.requireNonNull;
031 import static javafx.collections.FXCollections.observableArrayList;
032 
033 /**
034  @author Andres Almiray
035  @since 2.11.0
036  */
037 public class TableCellFactories {
038     private static final String ERROR_ITEMS_NULL = "Argument 'items' must not be null";
039     private static final String ERROR_ENUMTYPE_NULL = "Argument 'enumType' must not be null";
040 
041     @Nonnull
042     public static <E, T> TableCellFactory<E, T> checkboxTableCellFactory() {
043         return new TableCellFactory<E, T>() {
044             @Nonnull
045             @Override
046             @SuppressWarnings("unchecked")
047             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
048                 CheckBoxTableCell<E, T> cell = new CheckBoxTableCell<>();
049                 cell.setEditable(false);
050                 return cell;
051             }
052         };
053     }
054 
055     @Nonnull
056     public static <E, T> TableCellFactory<E, T> editableCheckboxTableCellFactory() {
057         return new EditableTableCellFactory<E, T>(checkboxTableCellFactory());
058     }
059 
060     @Nonnull
061     public static <E, T> TableCellFactory<E, T> choiceBoxTableCellFactory(@Nonnull E... items) {
062         requireNonNull(items, ERROR_ITEMS_NULL);
063         final ObservableList<E> observableItems = observableArrayList(items);
064         return new TableCellFactory<E, T>() {
065             @Nonnull
066             @Override
067             @SuppressWarnings("unchecked")
068             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
069                 ChoiceBoxTableCell<E, T> cell = new ChoiceBoxTableCell<>((ObservableList<T>observableItems);
070                 cell.setEditable(false);
071                 return cell;
072             }
073         };
074     }
075 
076     @Nonnull
077     public static <E, T> TableCellFactory<E, T> choiceBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull E... items) {
078         requireNonNull(items, ERROR_ITEMS_NULL);
079         final ObservableList<E> observableItems = observableArrayList(items);
080         return new TableCellFactory<E, T>() {
081             @Nonnull
082             @Override
083             @SuppressWarnings("unchecked")
084             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
085                 ChoiceBoxTableCell<E, T> cell = new ChoiceBoxTableCell<>((StringConverter<T>converter, (ObservableList<T>observableItems);
086                 cell.setEditable(false);
087                 return cell;
088             }
089         };
090     }
091 
092     @Nonnull
093     public static <E, T> TableCellFactory<E, T> choiceBoxTableCellFactory(@Nonnull final ObservableList<E> observableItems) {
094         requireNonNull(observableItems, ERROR_ITEMS_NULL);
095         return new TableCellFactory<E, T>() {
096             @Nonnull
097             @Override
098             @SuppressWarnings("unchecked")
099             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
100                 ChoiceBoxTableCell<E, T> cell = new ChoiceBoxTableCell<>((ObservableList<T>observableItems);
101                 cell.setEditable(false);
102                 return cell;
103             }
104         };
105     }
106 
107     @Nonnull
108     public static <E, T> TableCellFactory<E, T> choiceBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull final ObservableList<E> observableItems) {
109         requireNonNull(observableItems, ERROR_ITEMS_NULL);
110         return new TableCellFactory<E, T>() {
111             @Nonnull
112             @Override
113             @SuppressWarnings("unchecked")
114             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
115                 ChoiceBoxTableCell<E, T> cell = new ChoiceBoxTableCell<>((StringConverter<T>converter, (ObservableList<T>observableItems);
116                 cell.setEditable(false);
117                 return cell;
118             }
119         };
120     }
121 
122     @Nonnull
123     public static <E extends Enum, T> TableCellFactory<E, T> choiceBoxTableCellFactory(@Nonnull Class<E> enumType) {
124         requireNonNull(enumType, ERROR_ENUMTYPE_NULL);
125         final ObservableList<E> observableItems = observableArrayList(enumType.getEnumConstants());
126         return new TableCellFactory<E, T>() {
127             @Nonnull
128             @Override
129             @SuppressWarnings("unchecked")
130             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
131                 ChoiceBoxTableCell<E, T> cell = new ChoiceBoxTableCell<>((ObservableList<T>observableItems);
132                 cell.setEditable(false);
133                 return cell;
134             }
135         };
136     }
137 
138     @Nonnull
139     public static <E extends Enum, T> TableCellFactory<E, T> choiceBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull Class<E> enumType) {
140         requireNonNull(enumType, ERROR_ENUMTYPE_NULL);
141         final ObservableList<E> observableItems = observableArrayList(enumType.getEnumConstants());
142         return new TableCellFactory<E, T>() {
143             @Nonnull
144             @Override
145             @SuppressWarnings("unchecked")
146             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
147                 ChoiceBoxTableCell<E, T> cell = new ChoiceBoxTableCell<>((StringConverter<T>converter, (ObservableList<T>observableItems);
148                 cell.setEditable(false);
149                 return cell;
150             }
151         };
152     }
153 
154     @Nonnull
155     public static <E, T> TableCellFactory<E, T> editableChoiceBoxTableCellFactory(@Nonnull E... items) {
156         return new EditableTableCellFactory<E, T>(choiceBoxTableCellFactory(items));
157     }
158 
159     @Nonnull
160     public static <E, T> TableCellFactory<E, T> editableChoiceBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull E... items) {
161         return new EditableTableCellFactory<E, T>(choiceBoxTableCellFactory(converter, items));
162     }
163 
164     @Nonnull
165     public static <E, T> TableCellFactory<E, T> editableChoiceBoxTableCellFactory(@Nonnull final ObservableList<E> observableItems) {
166         return new EditableTableCellFactory<E, T>(choiceBoxTableCellFactory(observableItems));
167     }
168 
169     @Nonnull
170     public static <E, T> TableCellFactory<E, T> editableChoiceBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull final ObservableList<E> observableItems) {
171         return new EditableTableCellFactory<E, T>(choiceBoxTableCellFactory(converter, observableItems));
172     }
173 
174     @Nonnull
175     public static <E extends Enum, T> TableCellFactory<E, T> editableChoiceBoxTableCellFactory(@Nonnull Class<E> enumType) {
176         return new EditableTableCellFactory<E, T>(choiceBoxTableCellFactory(enumType));
177     }
178 
179     @Nonnull
180     public static <E extends Enum, T> TableCellFactory<E, T> editableChoiceBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull Class<E> enumType) {
181         return new EditableTableCellFactory<E, T>(choiceBoxTableCellFactory(converter, enumType));
182     }
183 
184     @Nonnull
185     public static <E, T> TableCellFactory<E, T> comboBoxTableCellFactory(@Nonnull E... items) {
186         requireNonNull(items, ERROR_ITEMS_NULL);
187         final ObservableList<E> observableItems = observableArrayList(items);
188         return new TableCellFactory<E, T>() {
189             @Nonnull
190             @Override
191             @SuppressWarnings("unchecked")
192             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
193                 ComboBoxTableCell<E, T> cell = new ComboBoxTableCell<>((ObservableList<T>observableItems);
194                 cell.setEditable(false);
195                 return cell;
196             }
197         };
198     }
199 
200     @Nonnull
201     public static <E, T> TableCellFactory<E, T> comboBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull E... items) {
202         requireNonNull(items, ERROR_ITEMS_NULL);
203         final ObservableList<E> observableItems = observableArrayList(items);
204         return new TableCellFactory<E, T>() {
205             @Nonnull
206             @Override
207             @SuppressWarnings("unchecked")
208             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
209                 ComboBoxTableCell<E, T> cell = new ComboBoxTableCell<>((StringConverter<T>converter, (ObservableList<T>observableItems);
210                 cell.setEditable(false);
211                 return cell;
212             }
213         };
214     }
215 
216     @Nonnull
217     public static <E, T> TableCellFactory<E, T> comboBoxTableCellFactory(@Nonnull final ObservableList<E> observableItems) {
218         requireNonNull(observableItems, ERROR_ITEMS_NULL);
219         return new TableCellFactory<E, T>() {
220             @Nonnull
221             @Override
222             @SuppressWarnings("unchecked")
223             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
224                 ComboBoxTableCell<E, T> cell = new ComboBoxTableCell<>((ObservableList<T>observableItems);
225                 cell.setEditable(false);
226                 return cell;
227             }
228         };
229     }
230 
231     @Nonnull
232     public static <E, T> TableCellFactory<E, T> comboBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull final ObservableList<E> observableItems) {
233         requireNonNull(observableItems, ERROR_ITEMS_NULL);
234         return new TableCellFactory<E, T>() {
235             @Nonnull
236             @Override
237             @SuppressWarnings("unchecked")
238             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
239                 ComboBoxTableCell<E, T> cell = new ComboBoxTableCell<>((StringConverter<T>converter, (ObservableList<T>observableItems);
240                 cell.setEditable(false);
241                 return cell;
242             }
243         };
244     }
245 
246     @Nonnull
247     public static <E extends Enum, T> TableCellFactory<E, T> comboBoxTableCellFactory(@Nonnull Class<E> enumType) {
248         requireNonNull(enumType, ERROR_ENUMTYPE_NULL);
249         final ObservableList<E> observableItems = observableArrayList(enumType.getEnumConstants());
250         return new TableCellFactory<E, T>() {
251             @Nonnull
252             @Override
253             @SuppressWarnings("unchecked")
254             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
255                 ComboBoxTableCell<E, T> cell = new ComboBoxTableCell<>((ObservableList<T>observableItems);
256                 cell.setEditable(false);
257                 return cell;
258             }
259         };
260     }
261 
262     @Nonnull
263     public static <E extends Enum, T> TableCellFactory<E, T> comboBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull Class<E> enumType) {
264         requireNonNull(enumType, ERROR_ENUMTYPE_NULL);
265         final ObservableList<E> observableItems = observableArrayList(enumType.getEnumConstants());
266         return new TableCellFactory<E, T>() {
267             @Nonnull
268             @Override
269             @SuppressWarnings("unchecked")
270             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
271                 ComboBoxTableCell<E, T> cell = new ComboBoxTableCell<>((StringConverter<T>converter, (ObservableList<T>observableItems);
272                 cell.setEditable(false);
273                 return cell;
274             }
275         };
276     }
277 
278     @Nonnull
279     public static <E, T> TableCellFactory<E, T> editableComboBoxTableCellFactory(@Nonnull E... items) {
280         return new EditableTableCellFactory<E, T>(comboBoxTableCellFactory(items));
281     }
282 
283     @Nonnull
284     public static <E, T> TableCellFactory<E, T> editableComboBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull E... items) {
285         return new EditableTableCellFactory<E, T>(comboBoxTableCellFactory(converter, items));
286     }
287 
288     @Nonnull
289     public static <E, T> TableCellFactory<E, T> editableComboBoxTableCellFactory(@Nonnull final ObservableList<E> observableItems) {
290         return new EditableTableCellFactory<E, T>(comboBoxTableCellFactory(observableItems));
291     }
292 
293     @Nonnull
294     public static <E, T> TableCellFactory<E, T> editableComboBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull final ObservableList<E> observableItems) {
295         return new EditableTableCellFactory<E, T>(comboBoxTableCellFactory(converter, observableItems));
296     }
297 
298     @Nonnull
299     public static <E extends Enum, T> TableCellFactory<E, T> editableComboBoxTableCellFactory(@Nonnull Class<E> enumType) {
300         return new EditableTableCellFactory<E, T>(comboBoxTableCellFactory(enumType));
301     }
302 
303     @Nonnull
304     public static <E extends Enum, T> TableCellFactory<E, T> editableComboBoxTableCellFactory(@Nonnull final StringConverter<E> converter, @Nonnull Class<E> enumType) {
305         return new EditableTableCellFactory<E, T>(comboBoxTableCellFactory(converter, enumType));
306     }
307 
308     @Nonnull
309     public static <E> TableCellFactory<E, Double> progressBarTableCellFactory() {
310         return new TableCellFactory<E, Double>() {
311             @Nonnull
312             @Override
313             public TableCell<E, Double> createTableCell(@Nonnull TableColumn<E, Double> tableColumn) {
314                 return new ProgressBarTableCell<>();
315             }
316         };
317     }
318 
319     @Nonnull
320     public static <E, T> TableCellFactory<E, T> textFieldTableCellFactory() {
321         return new TableCellFactory<E, T>() {
322             @Nonnull
323             @Override
324             @SuppressWarnings("unchecked")
325             public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
326                 TextFieldTableCell<E, T> cell = new TextFieldTableCell<>();
327                 cell.setEditable(false);
328                 return cell;
329             }
330         };
331     }
332 
333     @Nonnull
334     public static <E, T> TableCellFactory<E, T> editableTextFieldTableCellFactory() {
335         return new EditableTableCellFactory<E, T>(textFieldTableCellFactory());
336     }
337 
338     private static class EditableTableCellFactory<E, T> implements TableCellFactory<E, T> {
339         private final TableCellFactory delegate;
340 
341         private EditableTableCellFactory(@Nonnull TableCellFactory<E, T> delegate) {
342             this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null");
343         }
344 
345         @Nonnull
346         @Override
347         public TableCell<E, T> createTableCell(@Nonnull TableColumn<E, T> tableColumn) {
348             TableCell<E, T> cell = delegate.createTableCell(tableColumn);
349             cell.setEditable(true);
350             return cell;
351         }
352     }
353 }