All Downloads are FREE. Search and download functionalities are using the official Maven repository.

griffon.javafx.scene.control.TableCellFactories Maven / Gradle / Ivy

/*
 * Copyright 2008-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package griffon.javafx.scene.control;

import javafx.collections.ObservableList;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.ChoiceBoxTableCell;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.control.cell.ProgressBarTableCell;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.StringConverter;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;
import static javafx.collections.FXCollections.observableArrayList;

/**
 * @author Andres Almiray
 * @since 2.11.0
 */
public class TableCellFactories {
    private static final String ERROR_ITEMS_NULL = "Argument 'items' must not be null";

    @Nonnull
    public static  TableCellFactory checkboxTableCellFactory() {
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                CheckBoxTableCell cell = new CheckBoxTableCell<>();
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory editableCheckboxTableCellFactory() {
        return new EditableTableCellFactory(checkboxTableCellFactory());
    }

    @Nonnull
    public static  TableCellFactory choiceBoxTableCellFactory(@Nonnull E... items) {
        requireNonNull(items, ERROR_ITEMS_NULL);
        final ObservableList observableItems = observableArrayList(items);
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ChoiceBoxTableCell cell = new ChoiceBoxTableCell<>((ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory choiceBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull E... items) {
        requireNonNull(items, ERROR_ITEMS_NULL);
        final ObservableList observableItems = observableArrayList(items);
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ChoiceBoxTableCell cell = new ChoiceBoxTableCell<>((StringConverter) converter, (ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory choiceBoxTableCellFactory(@Nonnull final ObservableList observableItems) {
        requireNonNull(observableItems, ERROR_ITEMS_NULL);
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ChoiceBoxTableCell cell = new ChoiceBoxTableCell<>((ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory choiceBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull final ObservableList observableItems) {
        requireNonNull(observableItems, ERROR_ITEMS_NULL);
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ChoiceBoxTableCell cell = new ChoiceBoxTableCell<>((StringConverter) converter, (ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory choiceBoxTableCellFactory(@Nonnull Class enumType) {
        requireNonNull(enumType, "Argument 'enumType' must not be null");
        final ObservableList observableItems = observableArrayList(enumType.getEnumConstants());
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ChoiceBoxTableCell cell = new ChoiceBoxTableCell<>((ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory choiceBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull Class enumType) {
        requireNonNull(enumType, "Argument 'enumType' must not be null");
        final ObservableList observableItems = observableArrayList(enumType.getEnumConstants());
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ChoiceBoxTableCell cell = new ChoiceBoxTableCell<>((StringConverter) converter, (ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory editableChoiceBoxTableCellFactory(@Nonnull E... items) {
        return new EditableTableCellFactory(choiceBoxTableCellFactory(items));
    }

    @Nonnull
    public static  TableCellFactory editableChoiceBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull E... items) {
        return new EditableTableCellFactory(choiceBoxTableCellFactory(converter, items));
    }

    @Nonnull
    public static  TableCellFactory editableChoiceBoxTableCellFactory(@Nonnull final ObservableList observableItems) {
        return new EditableTableCellFactory(choiceBoxTableCellFactory(observableItems));
    }

    @Nonnull
    public static  TableCellFactory editableChoiceBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull final ObservableList observableItems) {
        return new EditableTableCellFactory(choiceBoxTableCellFactory(converter, observableItems));
    }

    @Nonnull
    public static  TableCellFactory editableChoiceBoxTableCellFactory(@Nonnull Class enumType) {
        return new EditableTableCellFactory(choiceBoxTableCellFactory(enumType));
    }

    @Nonnull
    public static  TableCellFactory editableChoiceBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull Class enumType) {
        return new EditableTableCellFactory(choiceBoxTableCellFactory(converter, enumType));
    }

    @Nonnull
    public static  TableCellFactory comboBoxTableCellFactory(@Nonnull E... items) {
        requireNonNull(items, ERROR_ITEMS_NULL);
        final ObservableList observableItems = observableArrayList(items);
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ComboBoxTableCell cell = new ComboBoxTableCell<>((ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory comboBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull E... items) {
        requireNonNull(items, ERROR_ITEMS_NULL);
        final ObservableList observableItems = observableArrayList(items);
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ComboBoxTableCell cell = new ComboBoxTableCell<>((StringConverter) converter, (ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory comboBoxTableCellFactory(@Nonnull final ObservableList observableItems) {
        requireNonNull(observableItems, ERROR_ITEMS_NULL);
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ComboBoxTableCell cell = new ComboBoxTableCell<>((ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory comboBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull final ObservableList observableItems) {
        requireNonNull(observableItems, ERROR_ITEMS_NULL);
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ComboBoxTableCell cell = new ComboBoxTableCell<>((StringConverter) converter, (ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory comboBoxTableCellFactory(@Nonnull Class enumType) {
        requireNonNull(enumType, "Argument 'enumType' must not be null");
        final ObservableList observableItems = observableArrayList(enumType.getEnumConstants());
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ComboBoxTableCell cell = new ComboBoxTableCell<>((ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory comboBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull Class enumType) {
        requireNonNull(enumType, "Argument 'enumType' must not be null");
        final ObservableList observableItems = observableArrayList(enumType.getEnumConstants());
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                ComboBoxTableCell cell = new ComboBoxTableCell<>((StringConverter) converter, (ObservableList) observableItems);
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory editableComboBoxTableCellFactory(@Nonnull E... items) {
        return new EditableTableCellFactory(comboBoxTableCellFactory(items));
    }

    @Nonnull
    public static  TableCellFactory editableComboBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull E... items) {
        return new EditableTableCellFactory(comboBoxTableCellFactory(converter, items));
    }

    @Nonnull
    public static  TableCellFactory editableComboBoxTableCellFactory(@Nonnull final ObservableList observableItems) {
        return new EditableTableCellFactory(comboBoxTableCellFactory(observableItems));
    }

    @Nonnull
    public static  TableCellFactory editableComboBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull final ObservableList observableItems) {
        return new EditableTableCellFactory(comboBoxTableCellFactory(converter, observableItems));
    }

    @Nonnull
    public static  TableCellFactory editableComboBoxTableCellFactory(@Nonnull Class enumType) {
        return new EditableTableCellFactory(comboBoxTableCellFactory(enumType));
    }

    @Nonnull
    public static  TableCellFactory editableComboBoxTableCellFactory(@Nonnull final StringConverter converter, @Nonnull Class enumType) {
        return new EditableTableCellFactory(comboBoxTableCellFactory(converter, enumType));
    }

    @Nonnull
    public static  TableCellFactory progressBarTableCellFactory() {
        return new TableCellFactory() {
            @Nonnull
            @Override
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                return new ProgressBarTableCell<>();
            }
        };
    }

    @Nonnull
    public static  TableCellFactory textFieldTableCellFactory() {
        return new TableCellFactory() {
            @Nonnull
            @Override
            @SuppressWarnings("unchecked")
            public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
                TextFieldTableCell cell = new TextFieldTableCell<>();
                cell.setEditable(false);
                return cell;
            }
        };
    }

    @Nonnull
    public static  TableCellFactory editableTextFieldTableCellFactory() {
        return new EditableTableCellFactory(textFieldTableCellFactory());
    }

    private static class EditableTableCellFactory implements TableCellFactory {
        private final TableCellFactory delegate;

        private EditableTableCellFactory(@Nonnull TableCellFactory delegate) {
            this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null");
        }

        @Nonnull
        @Override
        public TableCell createTableCell(@Nonnull TableColumn tableColumn) {
            TableCell cell = delegate.createTableCell(tableColumn);
            cell.setEditable(true);
            return cell;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy