导读
由于项目需要,需要写一个工具类生成思维脑图。
使用
工具类
引用依赖
<dependency>
<groupId>com.github.eljah</groupId>
<artifactId>xmindjbehaveplugin</artifactId>
<version>0.8</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
工具类Tutis
import cn.hutool.system.SystemUtil;
import com.google.common.collect.Lists;
import org.xmind.core.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 生成思维脑图工具类
*
* @date 2020/10/16
*/
public class Tutis {
/**
* 当前类路径
*/
public static final String CLASS_PATH = GeneratorDoubanXmind.class.getResource("/").getPath();
/**
* 文件分隔符
*/
public static final String FILE_SEPARATOR = SystemUtil.getOsInfo().getFileSeparator();
/**
* 生成思维脑图
*
* @param list 集合数据
* @param topicName 中心主题名称
* @param fileName 文件名.xmind
* @throws IOException
* @throws CoreException
*/
public void exportXmind(List<Tree> list,String topicName, String fileName) throws IOException, CoreException {
// 创建思维导图的工作空间
IWorkbookBuilder workbookBuilder = Core.getWorkbookBuilder();
IWorkbook workbook = workbookBuilder.createWorkbook();
// 获得默认sheet
ISheet primarySheet = workbook.getPrimarySheet();
// 获得根主题
ITopic rootTopic = primarySheet.getRootTopic();
// 设置根主题的标题
rootTopic.setTitleText(topicName);
// 章节 topic 的列表
ArrayList<ITopic> chapterTopics = Lists.newArrayList();
recycle(chapterTopics, workbook, list);
// 把章节节点添加到要节点上
chapterTopics.forEach(it -> rootTopic.add(it, ITopic.ATTACHED));
// 保存
workbook.save(CLASS_PATH + FILE_SEPARATOR + fileName + ".xmind");
}
/**
* 思维导图递归调用
*
* @param chapterTopics
* @param workbook
* @param childrens 集合数据
*/
public void recycle(ArrayList<ITopic> chapterTopics, IWorkbook workbook, List<Tree> childrens) {
for (Tree children : childrens) {
if (children != null) {
if (children.getChildrens() != null && children.getChildrens().size() > 0) {
// 创建章节节点
ITopic topic = workbook.createTopic();
topic.setTitleText(children.getName());
chapterTopics.add(topic);
List<Tree> list = children.getChildrens();
//如果还有子节点,那么采用地柜调用继续生成
if (list != null && list.size() > 0) {
recycle(chapterTopics, workbook, list);
} else {
//否则的结束当前循环
break;
}
} else {
// 创建小节节点
ITopic topic = workbook.createTopic();
topic.setTitleText(children.getName());
chapterTopics.get(chapterTopics.size() - 1).add(topic, ITopic.ATTACHED);
}
}
}
}
}
树状实体类Tree
import java.util.List;
/**
* 树状类
*/
public class Tree {
/**
* id
*/
private int id;
/**
* 标题
*/
private String name;
/**
* 父级Id
*/
private int parentId;
/**
* 子集
*/
private List<Tree> childrens;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public List<Tree> getChildrens() {
return childrens;
}
public void setChildrens(List<Tree> childrens) {
this.childrens = childrens;
}
public Tree(int id, String name, int parentId) {
this.id = id;
this.name = name;
this.parentId = parentId;
}
@Override
public String toString() {
return "Tree [id=" + id + ", name=" + name + ", parentId=" + parentId
+ ", childrens=" + childrens + "]";
}
}
测试数据
import java.util.ArrayList;
import java.util.List;
public class Tes {
public static void main(String[] args) {
Tree tree1 = new Tree(1, "顶层节点1", 0);
Tree tree2 = new Tree(2, "顶层节点2", 0);
Tree tree3 = new Tree(3, "顶层节点3", 0);
Tree tree4 = new Tree(4, "二级节点4", 1);
Tree tree5 = new Tree(5, "二级节点5", 2);
Tree tree6 = new Tree(6, "二级节点6", 3);
Tree tree7 = new Tree(7, "三级节点7", 4);
Tree tree8 = new Tree(8, "三级节点8", 4);
Tree tree9 = new Tree(9, "三级节点9", 5);
Tree m7 = new Tree(10, "M7", 4);
Tree m8 = new Tree(11, "M8", 4);
Tree m9 = new Tree(12, "M9", 5);
Tree k7 = new Tree(13, "k7", 10);
Tree k8 = new Tree(14, "k8", 10);
Tree k9 = new Tree(15, "k9", 5);
List<Tree> trees = new ArrayList<Tree>();
trees.add(tree9);
trees.add(tree8);
trees.add(tree7);
trees.add(tree6);
trees.add(tree5);
trees.add(tree4);
trees.add(tree3);
trees.add(tree2);
trees.add(tree1);
trees.add(m7);
trees.add(m8);
trees.add(m9);
trees.add(k7);
trees.add(k8);
trees.add(k9);
List<Tree> rootTrees = new ArrayList<Tree>();
for (Tree tree : trees) {
if (tree.getParentId() == 0) {
rootTrees.add(tree);
}
for (Tree t : trees) {
if (t.getParentId() == tree.getId()) {
if (tree.getChildrens() == null) {
List<Tree> myChildrens = new ArrayList<Tree>();
myChildrens.add(t);
tree.setChildrens(myChildrens);
} else {
tree.getChildrens().add(t);
}
}
}
}
for (Tree tree : rootTrees) {
System.out.println(tree.toString());
}
try {
Tutis tutis = new Tutis();
tutis.exportXmind(rootTrees,"根节点描述","测试思维导出");
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果
控制台数据
Tree [id=3, name=顶层节点3, parentId=0, childrens=[Tree [id=6, name=二级节点6, parentId=3, childrens=null]]]
Tree [id=2, name=顶层节点2, parentId=0, childrens=[Tree [id=5, name=二级节点5, parentId=2, childrens=[Tree [id=9, name=三级节点9, parentId=5, childrens=null], Tree [id=12, name=M9, parentId=5, childrens=null], Tree [id=15, name=k9, parentId=5, childrens=null]]]]]
Tree [id=1, name=顶层节点1, parentId=0, childrens=[Tree [id=4, name=二级节点4, parentId=1, childrens=[Tree [id=8, name=三级节点8, parentId=4, childrens=null], Tree [id=7, name=三级节点7, parentId=4, childrens=null], Tree [id=10, name=M7, parentId=4, childrens=[Tree [id=13, name=k7, parentId=10, childrens=null], Tree [id=14, name=k8, parentId=10, childrens=null]]], Tree [id=11, name=M8, parentId=4, childrens=null]]]]]
生成的脑图
END
参考:java生成思维导图
搞定~