preface

In our daily work and study, we often use the tool of mind mapping to transform abstract and intangible thinking into tangible and specific images, which is a great tool for clarifying thoughts and sorting out logic.

To be precise, mind mapping is not a specific tool, but a method. The Tao, not the art. When we actually land, we usually have to use external tools. From the most primitive paper and pen, to all kinds of software, it can be said that there are applications. At present, I am using the software XMind. Next, I will draw mind maps indirectly through Java code through the media of XMind.

Put the code to come over

Generate mind maps from directories

When I read books, I have a habit of drawing an outline of a book by thinking guide, and then reading it in order of priority by this outline. Maybe some students are confused, haven’t read the book, how to understand the outline? In fact, the table of contents of a book is the best outline of the book.

This article will learn the basic API usage of XMind by using an example of directory generation mind maps. Of course, if you want to study systematically, you can refer to the official API link at the end of the article. The image below is the final result we want to generate.

Introduction of depend on

Xmind was originally custom developed on Eclipse (yes, the same programming software you abandoned when you encountered Idea), so Java is naturally well supported. That address is xMind’s github repository. The API for xmind operation is all in org.xmind. Core package, follow the official instructions to pull down the code in the local package, then import ok. But? Laziness is the first productive force. I immediately went to the Maven repository and found that someone had uploaded the official package and used it directly. (A little bit old, but the basic operation is enough, if you want to use the new feature, you can pull the code to package itself)

  • pom.xml
<dependency>
  <groupId>com.github.eljah</groupId>
  <artifactId>xmindjbehaveplugin</artifactId>
  <version>0.8</version>
</dependency>
Copy the code

To prepare data

To generate a mind map, we first need data. The data here is the table of contents of a book.

First of all, I choose the book “A Book to read 24 Kinds of Internet thinking”, I choose this book, not because of how good the book is, but because it is more typical, typical in, after reading the book catalog, there is no need to read this book, because the content of the catalog has been said very clearly.

So if you know the title, how do you get the table of contents? Very simple, open douban, find the details of the book, there is a book catalogue, directly copy it down, the catalogue is as follows.

Start coding

  • GeneratorDoubanXmind
/* * * * * * * * blog.coder4j.cn * * * Copyright (C) 2016-2019 All Rights Reserved. * * * */
package cn.coder4j.study.example;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.system.SystemUtil;
import com.google.common.collect.Lists;
import org.xmind.core.Core;
import org.xmind.core.CoreException;
import org.xmind.core.ISheet;
import org.xmind.core.ITopic;
import org.xmind.core.IWorkbook;
import org.xmind.core.IWorkbookBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/ * * *@author buhao
 * @versionGeneratorDoubanXmind. Java, 0.1 2019-12-02 v 22:54 buhao * /
public class GeneratorDoubanXmind {

    /** * current classpath */
    public static final String CLASS_PATH = GeneratorDoubanXmind.class.getResource("/").getPath();
    /** * File separator */
    public static final String FILE_SEPARATOR = SystemUtil.getOsInfo().getFileSeparator();

    public static void main(String[] args) throws IOException, CoreException {
        // Read the directory
        String bookName = "24 Ways to Think on the Internet in one book.";
        List<String> contents = FileUtil.readLines(CLASS_PATH + bookName + ".txt"."utf-8");

        // Create mind map workspace
        IWorkbookBuilder workbookBuilder = Core.getWorkbookBuilder();
        IWorkbook workbook = workbookBuilder.createWorkbook();

        // Get the default sheet
        ISheet primarySheet = workbook.getPrimarySheet();

        // Get the root theme
        ITopic rootTopic = primarySheet.getRootTopic();
        // Set the title of the root theme
        rootTopic.setTitleText(bookName);

        // List of chapter topics
        ArrayList<ITopic> chapterTopics = Lists.newArrayList();
        for (String content : contents) {
            // If it is a number, start with the chapter name
            if (ReUtil.isMatch("^ 1-24 []. *?", content)) {
                // Create chapter nodes
                ITopic topic = workbook.createTopic();
                topic.setTitleText(content);
                chapterTopics.add(topic);
            } else {
                // Create a section node
                ITopic topic = workbook.createTopic();
                topic.setTitleText(content);
                chapterTopics.get(chapterTopics.size() - 1).add(topic, ITopic.ATTACHED); }}// Add the chapter node to the desired node
        chapterTopics.forEach(it -> rootTopic.add(it, ITopic.ATTACHED));

        / / save
        workbook.save(CLASS_PATH + FILE_SEPARATOR + bookName + ".xmind"); }}Copy the code

The code analysis

Basically the code has done the annotation, for the core code again simple analysis.

        // Read the directory
        String bookName = "24 Ways to Think on the Internet in one book.";
        List<String> contents = FileUtil.readLines(CLASS_PATH + bookName + ".txt"."utf-8");
Copy the code

First of all, the first two lines of code, needless to say, I saved the directory data to a book in the Resources directory to read 24 Internet minds.txt, these two lines of code are simply reading the data.

        // Create mind map workspace
        IWorkbookBuilder workbookBuilder = Core.getWorkbookBuilder();
        IWorkbook workbook = workbookBuilder.createWorkbook();
Copy the code

The next two lines of code create the workspace builder class from the Core class and create a blank workspace with its createWorkbook method, which results in a blank map without any nodes.

        // Get the default sheet
        ISheet primarySheet = workbook.getPrimarySheet();

        // Get the root theme
        ITopic rootTopic = primarySheet.getRootTopic();
        // Set the title of the root theme
        rootTopic.setTitleText(bookName);
Copy the code

Next, you get the main sheet by just creating the workspace, which is similar in concept to Excel, just like a TAB in a browser. The effect is shown below.

Also get the root topic from the main sheet and set its title as the title of the book, which corresponds to the following figure

        // List of chapter topics
        ArrayList<ITopic> chapterTopics = Lists.newArrayList();
        for (String content : contents) {
            // If it is a number, start with the chapter name
            if (ReUtil.isMatch("^ 1-24 []. *?", content)) {
                // Create chapter nodes
                ITopic topic = workbook.createTopic();
                topic.setTitleText(content);
                chapterTopics.add(topic);
            } else {
                // Create a section node
                ITopic topic = workbook.createTopic();
                topic.setTitleText(content);
                chapterTopics.get(chapterTopics.size() - 1).add(topic, ITopic.ATTACHED); }}Copy the code

This is a bit more code, but the focus is on creating a topic. Each topic is a node in the mind map and can be created using workbook.createTopic(), as well as setTitleText to set the title. This code is actually an analysis of the rules of the directory, there are 1-24 beginning is a large chapter, followed by a small chapter, a large chapter is a number of small chapters (see the directory screenshot above). So the big section creates a new node, and the small section adds to the last big section (via add).

        // Add the chapter node to the desired node
        chapterTopics.forEach(it -> rootTopic.add(it, ITopic.ATTACHED));
Copy the code

The smaller chapters are added to the larger chapters, but the larger chapters are still unsupported, so they are all added to the root Topic through a loop.

        / / save
        workbook.save(CLASS_PATH + FILE_SEPARATOR + bookName + ".xmind");
Copy the code

At this point, we are finished coding, but these operations are still in memory, we need to save the file to the hard disk through the workbook save method, and remember to change the file suffix to Xmind, otherwise the software can not recognize.

other

instructions

Because the space is limited, can not post all the code, if you have a problem can go to the relevant link inside the example, view the source code.

A link to the

  1. Xmind API address
  2. study-xmind-example