remove CheckBoxPicker
This commit is contained in:
@@ -1,147 +0,0 @@
|
||||
package com.imyeyu.fx.ui.components;
|
||||
|
||||
import com.imyeyu.fx.ui.TimiFXUI;
|
||||
import com.imyeyu.java.bean.CallbackArgReturn;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ListChangeListener;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.geometry.Bounds;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.CheckBox;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.TilePane;
|
||||
import javafx.stage.Popup;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 多选选择器,文本框弹出复选框组件进行多项选择
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2021-12-29 17:05
|
||||
*/
|
||||
public class CheckBoxPicker<T> extends TextField implements TimiFXUI {
|
||||
|
||||
/** 数据列表 */
|
||||
@Getter
|
||||
private final ObservableList<T> items;
|
||||
private final CheckBoxListPopup<T> checkBoxListPopup;
|
||||
|
||||
/** 默认构造器 */
|
||||
public CheckBoxPicker() {
|
||||
items = FXCollections.observableList(new ArrayList<>());
|
||||
|
||||
checkBoxListPopup = new CheckBoxListPopup<>(items);
|
||||
|
||||
setEditable(false);
|
||||
|
||||
// ---------- 事件 ----------
|
||||
|
||||
setOnMouseClicked(e -> {
|
||||
Bounds b = localToScreen(getLayoutBounds());
|
||||
checkBoxListPopup.show(this, b.getMinX() - 5, b.getMaxY() - 6);
|
||||
});
|
||||
|
||||
checkBoxListPopup.root.prefWidthProperty().bind(widthProperty());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置复选框工厂,工厂入参数据对象,返回复选框组件
|
||||
*
|
||||
* @param factory 复选框工厂
|
||||
*/
|
||||
public void setCheckBoxFactory(CallbackArgReturn<T, CheckBox> factory) {
|
||||
checkBoxListPopup.checkBoxFactory = factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已选项
|
||||
*
|
||||
* @return 已选项
|
||||
*/
|
||||
public ObservableList<T> getSelectedItems() {
|
||||
return checkBoxListPopup.selectedItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* 复选框列表弹窗
|
||||
*
|
||||
* @param <T> 数据类型
|
||||
* @author 夜雨
|
||||
* @since 2021-12-29 19:40
|
||||
*/
|
||||
private static class CheckBoxListPopup<T> extends Popup implements TimiFXUI {
|
||||
|
||||
private static final Insets PADDING = new Insets(6, 8, 6, 8);
|
||||
|
||||
private final TilePane root;
|
||||
private final Map<T, CheckBox> cache; // 数据映射缓存
|
||||
|
||||
final ObservableList<T> selectedItems;
|
||||
CallbackArgReturn<T, CheckBox> checkBoxFactory;
|
||||
|
||||
/**
|
||||
* 默认构造
|
||||
*
|
||||
* @param items 数据列表
|
||||
*/
|
||||
public CheckBoxListPopup(ObservableList<T> items) {
|
||||
cache = new HashMap<>();
|
||||
selectedItems = FXCollections.observableList(new ArrayList<>());
|
||||
|
||||
root = new TilePane();
|
||||
root.setHgap(8);
|
||||
root.setEffect(Shadow.POPUP);
|
||||
root.setBorder(Stroke.DEFAULT);
|
||||
root.setPadding(PADDING);
|
||||
root.setMinWidth(300);
|
||||
root.setBackground(BG.DEFAULT);
|
||||
root.setTileAlignment(Pos.CENTER_LEFT);
|
||||
|
||||
setAutoHide(true);
|
||||
getContent().add(root);
|
||||
|
||||
// ---------- 事件 ----------
|
||||
|
||||
// 列表更新
|
||||
items.addListener((ListChangeListener<T>) c -> {
|
||||
if (c.next()) {
|
||||
if (c.wasAdded()) {
|
||||
// 添加
|
||||
List<? extends T> list = c.getAddedSubList();
|
||||
for (T t : list) {
|
||||
CheckBox box;
|
||||
if (checkBoxFactory != null) {
|
||||
box = checkBoxFactory.handler(t);
|
||||
} else {
|
||||
box = new CheckBox(t.toString());
|
||||
}
|
||||
box.selectedProperty().addListener((obs, o, isSelected) -> {
|
||||
if (isSelected) {
|
||||
selectedItems.add(t);
|
||||
} else {
|
||||
selectedItems.remove(t);
|
||||
}
|
||||
});
|
||||
cache.put(t, box);
|
||||
root.getChildren().add(box);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (c.wasRemoved()) {
|
||||
// 移除
|
||||
List<? extends T> list = c.getRemoved();
|
||||
for (T t : list) {
|
||||
root.getChildren().remove(cache.get(t));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user