导读


由于项目需要,需要写一个工具类生成思维脑图。

使用


工具类

引用依赖

  1. <dependency>
  2. <groupId>com.github.eljah</groupId>
  3. <artifactId>xmindjbehaveplugin</artifactId>
  4. <version>0.8</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.google.guava</groupId>
  8. <artifactId>guava</artifactId>
  9. <version>29.0-jre</version>
  10. </dependency>

工具类Tutis

  1. import cn.hutool.system.SystemUtil;
  2. import com.google.common.collect.Lists;
  3. import org.xmind.core.*;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. /**
  8. * 生成思维脑图工具类
  9. *
  10. * @date 2020/10/16
  11. */
  12. public class Tutis {
  13. /**
  14. * 当前类路径
  15. */
  16. public static final String CLASS_PATH = GeneratorDoubanXmind.class.getResource("/").getPath();
  17. /**
  18. * 文件分隔符
  19. */
  20. public static final String FILE_SEPARATOR = SystemUtil.getOsInfo().getFileSeparator();
  21. /**
  22. * 生成思维脑图
  23. *
  24. * @param list 集合数据
  25. * @param topicName 中心主题名称
  26. * @param fileName 文件名.xmind
  27. * @throws IOException
  28. * @throws CoreException
  29. */
  30. public void exportXmind(List<Tree> list,String topicName, String fileName) throws IOException, CoreException {
  31. // 创建思维导图的工作空间
  32. IWorkbookBuilder workbookBuilder = Core.getWorkbookBuilder();
  33. IWorkbook workbook = workbookBuilder.createWorkbook();
  34. // 获得默认sheet
  35. ISheet primarySheet = workbook.getPrimarySheet();
  36. // 获得根主题
  37. ITopic rootTopic = primarySheet.getRootTopic();
  38. // 设置根主题的标题
  39. rootTopic.setTitleText(topicName);
  40. // 章节 topic 的列表
  41. ArrayList<ITopic> chapterTopics = Lists.newArrayList();
  42. recycle(chapterTopics, workbook, list);
  43. // 把章节节点添加到要节点上
  44. chapterTopics.forEach(it -> rootTopic.add(it, ITopic.ATTACHED));
  45. // 保存
  46. workbook.save(CLASS_PATH + FILE_SEPARATOR + fileName + ".xmind");
  47. }
  48. /**
  49. * 思维导图递归调用
  50. *
  51. * @param chapterTopics
  52. * @param workbook
  53. * @param childrens 集合数据
  54. */
  55. public void recycle(ArrayList<ITopic> chapterTopics, IWorkbook workbook, List<Tree> childrens) {
  56. for (Tree children : childrens) {
  57. if (children != null) {
  58. if (children.getChildrens() != null && children.getChildrens().size() > 0) {
  59. // 创建章节节点
  60. ITopic topic = workbook.createTopic();
  61. topic.setTitleText(children.getName());
  62. chapterTopics.add(topic);
  63. List<Tree> list = children.getChildrens();
  64. //如果还有子节点,那么采用地柜调用继续生成
  65. if (list != null && list.size() > 0) {
  66. recycle(chapterTopics, workbook, list);
  67. } else {
  68. //否则的结束当前循环
  69. break;
  70. }
  71. } else {
  72. // 创建小节节点
  73. ITopic topic = workbook.createTopic();
  74. topic.setTitleText(children.getName());
  75. chapterTopics.get(chapterTopics.size() - 1).add(topic, ITopic.ATTACHED);
  76. }
  77. }
  78. }
  79. }
  80. }

树状实体类Tree

  1. import java.util.List;
  2. /**
  3. * 树状类
  4. */
  5. public class Tree {
  6. /**
  7. * id
  8. */
  9. private int id;
  10. /**
  11. * 标题
  12. */
  13. private String name;
  14. /**
  15. * 父级Id
  16. */
  17. private int parentId;
  18. /**
  19. * 子集
  20. */
  21. private List<Tree> childrens;
  22. public int getId() {
  23. return id;
  24. }
  25. public void setId(int id) {
  26. this.id = id;
  27. }
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public int getParentId() {
  35. return parentId;
  36. }
  37. public void setParentId(int parentId) {
  38. this.parentId = parentId;
  39. }
  40. public List<Tree> getChildrens() {
  41. return childrens;
  42. }
  43. public void setChildrens(List<Tree> childrens) {
  44. this.childrens = childrens;
  45. }
  46. public Tree(int id, String name, int parentId) {
  47. this.id = id;
  48. this.name = name;
  49. this.parentId = parentId;
  50. }
  51. @Override
  52. public String toString() {
  53. return "Tree [id=" + id + ", name=" + name + ", parentId=" + parentId
  54. + ", childrens=" + childrens + "]";
  55. }
  56. }

测试数据

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class Tes {
  4. public static void main(String[] args) {
  5. Tree tree1 = new Tree(1, "顶层节点1", 0);
  6. Tree tree2 = new Tree(2, "顶层节点2", 0);
  7. Tree tree3 = new Tree(3, "顶层节点3", 0);
  8. Tree tree4 = new Tree(4, "二级节点4", 1);
  9. Tree tree5 = new Tree(5, "二级节点5", 2);
  10. Tree tree6 = new Tree(6, "二级节点6", 3);
  11. Tree tree7 = new Tree(7, "三级节点7", 4);
  12. Tree tree8 = new Tree(8, "三级节点8", 4);
  13. Tree tree9 = new Tree(9, "三级节点9", 5);
  14. Tree m7 = new Tree(10, "M7", 4);
  15. Tree m8 = new Tree(11, "M8", 4);
  16. Tree m9 = new Tree(12, "M9", 5);
  17. Tree k7 = new Tree(13, "k7", 10);
  18. Tree k8 = new Tree(14, "k8", 10);
  19. Tree k9 = new Tree(15, "k9", 5);
  20. List<Tree> trees = new ArrayList<Tree>();
  21. trees.add(tree9);
  22. trees.add(tree8);
  23. trees.add(tree7);
  24. trees.add(tree6);
  25. trees.add(tree5);
  26. trees.add(tree4);
  27. trees.add(tree3);
  28. trees.add(tree2);
  29. trees.add(tree1);
  30. trees.add(m7);
  31. trees.add(m8);
  32. trees.add(m9);
  33. trees.add(k7);
  34. trees.add(k8);
  35. trees.add(k9);
  36. List<Tree> rootTrees = new ArrayList<Tree>();
  37. for (Tree tree : trees) {
  38. if (tree.getParentId() == 0) {
  39. rootTrees.add(tree);
  40. }
  41. for (Tree t : trees) {
  42. if (t.getParentId() == tree.getId()) {
  43. if (tree.getChildrens() == null) {
  44. List<Tree> myChildrens = new ArrayList<Tree>();
  45. myChildrens.add(t);
  46. tree.setChildrens(myChildrens);
  47. } else {
  48. tree.getChildrens().add(t);
  49. }
  50. }
  51. }
  52. }
  53. for (Tree tree : rootTrees) {
  54. System.out.println(tree.toString());
  55. }
  56. try {
  57. Tutis tutis = new Tutis();
  58. tutis.exportXmind(rootTrees,"根节点描述","测试思维导出");
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. }

运行结果

控制台数据

  1. Tree [id=3, name=顶层节点3, parentId=0, childrens=[Tree [id=6, name=二级节点6, parentId=3, childrens=null]]]
  2. 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]]]]]
  3. 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]]]]]

image.png

生成的脑图

image.png

END


参考:java生成思维导图

搞定~