You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.1 KiB

package com.fr.plugin.event.manager.ui.tree;
import com.fr.design.gui.itree.UITreeUI;
import com.fr.plugin.event.manager.data.MyTree;
import com.fr.plugin.event.manager.ui.EventConfigPane;
import javax.swing.BorderFactory;
import javax.swing.JTree;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import java.awt.Color;
import java.util.List;
/**
* @author Joe
* Created by Joe on 12/14/2020
*/
public class MyComponentTree extends JTree {
private EventConfigPane configPane;
private MyComponentTreeModel model;
private UITreeUI uiTreeUI = new UITreeUI();
private static final int PADDING_LEFT = 5;
private static final int PADDING_TOP = 5;
public MyComponentTree(MyTree tree, EventConfigPane configPane, boolean rootVisible) {
this.configPane = configPane;
model = new MyComponentTreeModel(tree);
setModel(model);
this.setBackground(new Color(255, 255, 255));
// 根的可见性
setRootVisible(rootVisible);
setCellRenderer(new MyComponentCellRenderer());
getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
this.setDragEnabled(false);
initListener();
setEditable(false);
setUI(uiTreeUI);
setBorder(BorderFactory.createEmptyBorder(PADDING_TOP, PADDING_LEFT, 0, 0));
// 展开树
expandTree();
}
private void initListener() {
this.addTreeSelectionListener(configPane);
}
/**
* 最多展开一层
*/
private void expandTree() {
MyTree root = (MyTree) this.getModel().getRoot();
expandTree(new TreePath(root), 1);
}
private void expandTree(TreePath path, int layer) {
MyTree node = (MyTree) path.getLastPathComponent();
// Go to leaf
if (node.getChildrenSize() > 0 && layer > 0) {
List<MyTree> children = node.getChildren();
for (MyTree n : children) {
TreePath newPath = path.pathByAddingChild(n);
expandTree(newPath, --layer);
}
}
this.expandPath(path);
}
}