|
| 1 | +/* |
| 2 | + * Hello Minecraft! Launcher |
| 3 | + * Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package org.jackhuang.hmcl.ui.construct; |
| 19 | + |
| 20 | +import com.jfoenix.controls.JFXCheckBox; |
| 21 | +import javafx.beans.binding.Bindings; |
| 22 | +import javafx.beans.property.BooleanProperty; |
| 23 | +import javafx.scene.control.TableCell; |
| 24 | +import javafx.scene.control.TableColumn; |
| 25 | +import javafx.util.Callback; |
| 26 | + |
| 27 | +/// @author Glavo |
| 28 | +public final class JFXCheckBoxTableCell<S, T> extends TableCell<S, T> { |
| 29 | + public static <S> Callback<TableColumn<S, Boolean>, TableCell<S, Boolean>> forTableColumn( |
| 30 | + final TableColumn<S, Boolean> column) { |
| 31 | + return list -> new JFXCheckBoxTableCell<>(); |
| 32 | + } |
| 33 | + |
| 34 | + private final JFXCheckBox checkBox = new JFXCheckBox(); |
| 35 | + private BooleanProperty booleanProperty; |
| 36 | + |
| 37 | + public JFXCheckBoxTableCell() { |
| 38 | + this.getStyleClass().add("jfx-checkbox-table-cell"); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected void updateItem(T item, boolean empty) { |
| 43 | + super.updateItem(item, empty); |
| 44 | + |
| 45 | + if (empty) { |
| 46 | + setText(null); |
| 47 | + setGraphic(null); |
| 48 | + checkBox.disableProperty().unbind(); |
| 49 | + } else { |
| 50 | + setGraphic(checkBox); |
| 51 | + |
| 52 | + if (booleanProperty != null) { |
| 53 | + checkBox.selectedProperty().unbindBidirectional(booleanProperty); |
| 54 | + } |
| 55 | + if (getTableColumn().getCellObservableValue(getIndex()) instanceof BooleanProperty obsValue) { |
| 56 | + booleanProperty = obsValue; |
| 57 | + checkBox.selectedProperty().bindBidirectional(booleanProperty); |
| 58 | + } |
| 59 | + |
| 60 | + checkBox.disableProperty().bind(Bindings.not( |
| 61 | + getTableView().editableProperty().and( |
| 62 | + getTableColumn().editableProperty()).and( |
| 63 | + editableProperty()) |
| 64 | + )); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments