package com.ruoyi.common.utils.XMind;
import com.tool.commons.ICommons;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmind.core.*;
import org.xmind.core.style.IStyle;
import org.xmind.core.style.IStyleSheet;
import org.xmind.ui.style.Styles;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
/**
* @Author: haifei
* @Date: 2024/12/3 15:17
*/
public class XMindUtil {
private static final Logger log = LoggerFactory.getLogger(XMindUtil.class);
private static String[] SUPERVISECOLORARRAY = {"#F6A04D", "#FDD834", "#7986CB", "#EA8ABA", "#0096BF", "#8EDE99", "#FF9595", "#D0D0D0", "#0CE6CF"};
/**
* 生成思维脑图
*
* @param mindMapNodeList 思维脑图节点
* @param type 类型,1:右逻辑图,2:右头鱼骨
*/
public static Boolean createXMind(List<MindMapNode> mindMapNodeList, String type, String xmindFilePath, String optionalRootName) {
if (ICommons.isNullOrEmpty(mindMapNodeList) || ICommons.isNullOrEmpty(type) || ICommons.isNullOrEmpty(xmindFilePath) || ICommons.isNullOrEmpty(optionalRootName)) {
return false;
}
if (!xmindFilePath.endsWith(".xmind")) {
return false;
}
// 创建XMind工作簿
IWorkbookBuilder workbookBuilder = Core.getWorkbookBuilder();
IWorkbook workbook = workbookBuilder.createWorkbook();
ISheet primarySheet = workbook.getPrimarySheet();
// 创建根节点
ITopic topTopic = primarySheet.getRootTopic();
// 指定中心脑图的结构
topTopic.setStructureClass(XMindStructureEnum.LOGIC_RIGHT.getClassName());
IStyle style = setTopicIStyle(workbook, topTopic);
topTopic.setStyleId(style.getId());
if (mindMapNodeList.size() == 1) {
MindMapNode mindMapNode = mindMapNodeList.get(0);
String topTopicText = mindMapNode.getContentTxt();
topTopic.setTitleText(topTopicText);
// 设置备注
IPlainNotesContent notesContent = (IPlainNotesContent) workbook.createNotesContent(INotes.PLAIN);
notesContent.setTextContent(mindMapNode.getRemark());
INotes notes = topTopic.getNotes();
notes.setContent(INotes.PLAIN, notesContent);
List<MindMapNode> sonNode = mindMapNode.getSonNode();
for (MindMapNode mapNode : sonNode) {
buildTopic(workbook, topTopic, mindMapNode, mapNode.getSonNode(), 1, type);
}
}
else {
// 设置根节点名称
// topTopic.setTitleText("根节点");
topTopic.setTitleText(optionalRootName);
// 设置内容 -- 构建脑图内容
for (MindMapNode mindMapNode : mindMapNodeList) {
// 孩子标题
List<MindMapNode> childMindMapNode = mindMapNode.getSonNode();
buildTopic(workbook, topTopic, mindMapNode, childMindMapNode, 1, type);
}
}
File file = new File(xmindFilePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
// 生成文件到指定路径
workbook.save(Files.newOutputStream(Paths.get(xmindFilePath)));
log.info("XMind思维导图生成成功!");
return true;
} catch (Exception e) {
log.error("XMind思维导图生成失败,原因:", e);
return false;
}
}
private static IStyle setTopicIStyle(IWorkbook workbook, ITopic topTopic) {
IStyleSheet styleSheet = workbook.getStyleSheet();
IStyle style = styleSheet.findStyle(topTopic.getStyleId());
if (style == null) {
style = styleSheet.createStyle(IStyle.TOPIC);
styleSheet.addStyle(style, IStyleSheet.NORMAL_STYLES);
}
// 设置主题的Style
style.setProperty(Styles.FillColor, SUPERVISECOLORARRAY[0]);
style.setProperty(Styles.TextColor, "#FFFFFF");
style.setProperty(Styles.FontWeight, Styles.FontWeight);
style.setProperty(Styles.FontSize, "20pt");
style.setProperty(Styles.LineWidth, "2pt");
style.setProperty(Styles.LineClass, Styles.BRANCH_CONN_STRAIGHT);
style.setProperty(Styles.LineColor, Styles.DEF_TOPIC_LINE_COLOR);
return style;
}
/**
* 构建脑图分支主题
*
* @param workbook 工作簿
* @param rootTopic 父主题
* @param childMindNode 孩子节点
* @param mindMapNodes 子孙节点列表
* @param level 层级
*/
private static void buildTopic(IWorkbook workbook, ITopic rootTopic, MindMapNode childMindNode, List<MindMapNode> mindMapNodes, int level, String type) {
// 子标题
ITopic topic = workbook.createTopic();
topic.setTitleText(childMindNode.getContentTxt());
if ("2".equals(type)) {
topic.setStructureClass(XMindStructureEnum.FISH_BONE_LEFT_HEADED.getClassName());
}
// 设置备注
IPlainNotesContent notesContent = (IPlainNotesContent) workbook.createNotesContent(INotes.PLAIN);
notesContent.setTextContent(childMindNode.getRemark());
INotes notes = topic.getNotes();
notes.setContent(INotes.PLAIN, notesContent);
// 设置样式
IStyleSheet styleSheet = workbook.getStyleSheet();
IStyle style = styleSheet.findStyle(topic.getStyleId());
if (style == null) {
style = styleSheet.createStyle(IStyle.TOPIC);
styleSheet.addStyle(style, IStyleSheet.NORMAL_STYLES);
}
style.setProperty(Styles.FillColor, SUPERVISECOLORARRAY[level]);
style.setProperty(Styles.TextColor, "#000229");
style.setProperty(Styles.FontWeight, Styles.FontWeight);
style.setProperty(Styles.FontSize, "20pt");
style.setProperty(Styles.LineWidth, "2pt");
style.setProperty(Styles.LineColor, Styles.DEF_TOPIC_LINE_COLOR);
style.setProperty(Styles.ShapeClass, Styles.TOPIC_SHAPE_ROUNDEDRECT);
topic.setStyleId(style.getId());
for (MindMapNode mindMapNode : mindMapNodes) {
buildTopic(workbook, topic, mindMapNode, mindMapNode.getSonNode(), level + 1, type);
}
rootTopic.add(topic);
}
}
package com.ruoyi.common.utils.XMind;
import java.util.List;
/**
* @Author: haifei
* @Date: 2024/12/3 15:23
*
* Xmind 脑图节点结构
*/
public class MindMapNode {
/**
* 内容
*/
private String contentTxt;
/**
* 备注
*/
private String remark;
/**
* 子节点
*/
private List<MindMapNode> sonNode;
public String getContentTxt() {
return contentTxt;
}
public void setContentTxt(String contentTxt) {
this.contentTxt = contentTxt;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public List<MindMapNode> getSonNode() {
return sonNode;
}
public void setSonNode(List<MindMapNode> sonNode) {
this.sonNode = sonNode;
}
@Override
public String toString() {
return "MindMapNode{" +
"contentTxt='" + contentTxt + '\'' +
", remark='" + remark + '\'' +
", sonNode=" + sonNode +
'}';
}
}
package com.ruoyi.common.utils.XMind;
/**
* @Author: haifei
* @Date: 2024/12/3 15:28
*
* Xmind 骨架类型
*/
public enum XMindStructureEnum {
FISH_BONE_LEFT_HEADED("左头鱼骨", "org.xmind.ui.fishbone.leftHeaded"),
FISH_BONE_RIGHT_HEADED("右头鱼骨", "org.xmind.ui.fishbone.rightHeaded"),
LOGIC_LEFT("左逻辑图", "org.xmind.ui.logic.left"),
LOGIC_RIGHT("右逻辑图", "org.xmind.ui.logic.right"),
ORG_CHART_DOWN("向下组织图", "org.xmind.ui.org-chart.down"),
ORG_CHART_UP("向上组织图", "org.xmind.ui.org-chart.up"),
SPREADSHEET("电子表格", "org.xmind.ui.spreadsheet"),
TREE_LEFT("左树", "org.xmind.ui.tree.left"),
TREE_RIGHT("右树", "org.xmind.ui.tree.right");
/**
* 结构类型
*/
private final String type;
/**
* 结构类名
*/
private final String className;
XMindStructureEnum(String type, String className) {
this.type = type;
this.className = className;
}
public String getType() {
return type;
}
public String getClassName() {
return className;
}
}
标签:Styles,思维,style,String,导图,XMindUtil,setProperty,workbook,org
From: https://www.cnblogs.com/yppah/p/18672861