Preface
In our daily work and study, we often use mind mapping as a tool to transform abstract and intangible thinking into tangible and concrete images, which is a great artifact to clarify ideas and sort out logic.
To be exact, mind mapping is not a specific tool, but a method. It's the Tao, not the art. When landing, we usually need external tools. From the most primitive paper and pen, to a variety of software, it can be said that there are all kinds of applications. At present, I am using xmind software, and I will use xmind as the medium to draw mind map indirectly through Java code.
Put it in code.
Generate mind map through catalog
When I read a book, I have a habit of drawing out the outline of a book by thinking first, and then reading it by the outline. Maybe some students are confused and haven't read the book yet. How to understand the outline? In fact, the catalogue of a book is the best outline of the book.
In this paper, we learn the basic api of xmind by using an example of directory generating mind map. Of course, if you want to learn systematically, you can refer to the official api link at the end of the article. The figure below is the final result we want to generate.
Introducing dependency
xmind was originally developed on eclipse by customization (yes, this eclipse is the programming software you abandoned after meeting idea), so it has good support for Java by nature. this address It's XMIND's warehouse on github. The api for XMIND operation is all under the package of org.xmind.core. Pull down the code according to the official prompt and make a local package, and then import it. But what? Idleness is the first productivity. Immediately ran to maven warehouse and searched. It was found that someone had uploaded the official package and used it directly. (the time is a little old, but the basic operation is enough. If you want to use the new function, you can pull the code to pack by yourself)
- pom.xml
<dependency> <groupId>com.github.eljah</groupId> <artifactId>xmindjbehaveplugin</artifactId> <version>0.8</version> </dependency>
Preparation data
To generate mind maps, we must first have data. The data here is the catalogue of a book.
First of all, I chose books< 24 kinds of Internet thinking in one book >, this book is not selected because of how good it is, but because it's more typical. It's typical. After reading the contents of this book, there's no need to read it again, because the contents have been clearly stated.
How can I get the catalogue of a book if I know its title? Very simple, open Douban, find the details of this book, there is a book directory, directly copy it down, the directory 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 * @version GeneratorDoubanXmind.java, v 0.1 2019-12-02 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 directory String bookName = "24 kinds of Internet thinking in one book"; List<String> contents = FileUtil.readLines(CLASS_PATH + bookName + ".txt", "utf-8"); // Creating a workspace for mind mapping IWorkbookBuilder workbookBuilder = Core.getWorkbookBuilder(); IWorkbook workbook = workbookBuilder.createWorkbook(); // Get default sheet ISheet primarySheet = workbook.getPrimarySheet(); // Get root theme ITopic rootTopic = primarySheet.getRootTopic(); // Set the title of the root theme rootTopic.setTitleText(bookName); // List of chapter topic s ArrayList<ITopic> chapterTopics = Lists.newArrayList(); for (String content : contents) { // If the number starts with the chapter name if (ReUtil.isMatch("^[1-24].*?", content)) { // Create chapter node ITopic topic = workbook.createTopic(); topic.setTitleText(content); chapterTopics.add(topic); } else { // Create section node ITopic topic = workbook.createTopic(); topic.setTitleText(content); chapterTopics.get(chapterTopics.size() - 1).add(topic, ITopic.ATTACHED); } } // Add chapter node to the node to be added chapterTopics.forEach(it -> rootTopic.add(it, ITopic.ATTACHED)); // Preservation workbook.save(CLASS_PATH + FILE_SEPARATOR + bookName + ".xmind"); } }
code analysis
Basically, the code is annotated, and the core code is analyzed briefly.
// Read directory String bookName = "24 kinds of Internet thinking in one book"; List<String> contents = FileUtil.readLines(CLASS_PATH + bookName + ".txt", "utf-8");
First of all, the first two lines needless to say, I saved the directory data to a book under the resources directory to read 24 kinds of Internet thinking.txt. These two lines of code are simply reading the data.
// Creating a workspace for mind mapping IWorkbookBuilder workbookBuilder = Core.getWorkbookBuilder(); IWorkbook workbook = workbookBuilder.createWorkbook();
The next two lines of code create a workspace builder class through the Core class, and then create a blank workspace through its createWorkbook method. After the creation, you will get a blank map without any nodes.
// Get default sheet ISheet primarySheet = workbook.getPrimarySheet(); // Get root theme ITopic rootTopic = primarySheet.getRootTopic(); // Set the title of the root theme rootTopic.setTitleText(bookName);
Then, we get the main sheet by just creating a workspace. This sheet is similar to the concept of excel, just like a tab in a browser. The effect is as follows
In addition, the root topic is obtained through the main sheet, and its title is set as the title of the book, which corresponds to the following figure
// List of chapter topic s ArrayList<ITopic> chapterTopics = Lists.newArrayList(); for (String content : contents) { // If the number starts with the chapter name if (ReUtil.isMatch("^[1-24].*?", content)) { // Create chapter node ITopic topic = workbook.createTopic(); topic.setTitleText(content); chapterTopics.add(topic); } else { // Create section node ITopic topic = workbook.createTopic(); topic.setTitleText(content); chapterTopics.get(chapterTopics.size() - 1).add(topic, ITopic.ATTACHED); } }
There are many codes in this section, but the focus is on the operation of creating topics. Each topic is a node of mind map, which can be created by workbook.createTopic(). Similarly, the title can be set by setTitleText. This code actually analyzes the rules of the directory. There are large chapters at the beginning of 1-24, followed by small chapters. There are several small chapters under a large chapter (see the screenshot of the directory above). Therefore, if the judgment is a large chapter, a new node will be created, and if the judgment is a small chapter, it will be added to the last large chapter (through the add method).
// Add chapter node to the node to be added chapterTopics.forEach(it -> rootTopic.add(it, ITopic.ATTACHED));
Just now, I added all the small chapters to the big chapters, but now the big chapters are still helpless, so I added all the chapters to the root Topic through a cycle.
// Preservation workbook.save(CLASS_PATH + FILE_SEPARATOR + bookName + ".xmind");
This is the end of our coding, but these operations are still in memory. We need to save the file to the hard disk through the save method of the workbook. In addition, remember to change the file suffix to xmind, otherwise the software cannot recognize it.
Other
Explain
Because of the limited space, you can't post all the codes. If you have any problems, you can go to the example in the relevant links to check the source code.