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