完成模板配置功能!

This commit is contained in:
javas 2018-01-11 12:55:54 +08:00
parent 43ce13328b
commit 92e0255771
4 changed files with 158 additions and 27 deletions

View File

@ -11,6 +11,7 @@ import com.sjhy.plugin.entity.TypeMapperGroup;
import com.sjhy.plugin.service.ConfigService;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@ -110,11 +111,10 @@ public class ConfigServiceImpl implements ConfigService {
private String loadTemplate(String name) {
try {
return FileUtil.loadTextAndClose(getClass().getResourceAsStream("/template/"+name+".vm"));
return FileUtil.loadFile(new File(getClass().getResource("/template/"+name+".vm").getFile()), "UTF-8").replaceAll("\r", "");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return "";
}
//GET SET

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.sjhy.plugin.ui.EditTemplatePanel">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
@ -8,14 +8,20 @@
<properties/>
<border type="none"/>
<children>
<component id="d3e6e" class="javax.swing.JTextArea" binding="textArea">
<grid id="4043d" binding="editPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<border type="none"/>
<children/>
</grid>
<vspacer id="1f073">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</form>

View File

@ -3,34 +3,41 @@ package com.sjhy.plugin.ui;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.uiDesigner.core.GridConstraints;
import com.sjhy.plugin.entity.Template;
import javax.swing.*;
import java.awt.*;
public class EditTemplatePanel {
private JTextArea textArea;
private JPanel mainPanel;
private Editor editor;
private JPanel editPanel;
private Template template;
public EditTemplatePanel() {
addVmEditor("test");
EditTemplatePanel(Template template) {
this.template = template;
init();
}
private void addVmEditor(String template) {
private FileType getVelocityFileType() {
return FileTypeManager.getInstance().getFileTypeByExtension("vm");
}
private GridConstraints getGridConstraints() {
return new GridConstraints(0, 0, 1, 1,
GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED,
null, new Dimension(600, 400), null,
0, true);
}
private void init() {
//初始化系统编辑器
EditorFactory factory = EditorFactory.getInstance();
Document velocityTemplate = factory.createDocument(template);
editor = factory.createEditor(velocityTemplate, null, FileTypeManager.getInstance()
.getFileTypeByExtension("vm"), false);
GridConstraints constraints = new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST,
GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW,
GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(300, 300), null, 0, true);
mainPanel.add(editor.getComponent(), constraints);
}
public JTextArea getTextArea() {
return textArea;
Document velocityTemplate = factory.createDocument(template.getCode());
Editor editor = factory.createEditor(velocityTemplate, null, getVelocityFileType(), false);
editPanel.add(editor.getComponent(), getGridConstraints());
}
public JPanel getMainPanel() {

View File

@ -1,11 +1,15 @@
package com.sjhy.plugin.ui;
import com.intellij.openapi.options.Configurable;
import com.sjhy.plugin.entity.Template;
import com.sjhy.plugin.entity.TemplateGroup;
import com.sjhy.plugin.service.ConfigService;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.LinkedHashMap;
import java.util.Map;
public class TemplateSetting implements Configurable {
private JPanel mainPanel;
@ -16,15 +20,123 @@ public class TemplateSetting implements Configurable {
private JButton addButton;
private JButton removeButton;
private Map<String, TemplateGroup> templateGroupMap;
private String currGroupName;
private ConfigService configService;
private Boolean init = false;
private TemplateGroup getTemplateGroup() {
return this.templateGroupMap.get(currGroupName);
}
TemplateSetting(ConfigService configService) {
this.configService = configService;
//新增选项卡
addButton.addActionListener(e -> {
String value = JOptionPane.showInputDialog(null, "Input Tab Name:", "Demo");
if (value==null) {
return;
}
if (value.trim().length()==0){
JOptionPane.showMessageDialog(null, "Tab Name Can't Is Empty!");
return;
}
for (Template template : getTemplateGroup().getTemplateList()) {
if (template.getName().equals(value)){
JOptionPane.showMessageDialog(null, "Tab Name Already exist!");
return;
}
}
getTemplateGroup().getTemplateList().add(new Template(value, ""));
refresh();
});
//删除选项卡
removeButton.addActionListener(e -> {
getTemplateGroup().getTemplateList().remove(templateTabbedPane.getSelectedIndex());
refresh();
});
//切换分组
this.groupButton.addActionListener(e -> {
if (!init) {
return;
}
String val = (String) groupButton.getSelectedItem();
if (val==null) {
return;
}
if (val.equals(currGroupName)){
return;
}
currGroupName = val;
refresh();
});
//复制分组
copyGroupButton.addActionListener(e -> {
String value = JOptionPane.showInputDialog(null, "Input Group Name:", currGroupName+" Copy");
if (value==null) {
return;
}
if (value.trim().length()==0){
JOptionPane.showMessageDialog(null, "Group Name Can't Is Empty!");
return;
}
if (templateGroupMap.containsKey(value)){
JOptionPane.showMessageDialog(null, "Group Name Already exist!");
return;
}
TemplateGroup templateGroup = templateGroupMap.get(currGroupName).cloneTemplateGroup();
templateGroup.setName(value);
templateGroupMap.put(value, templateGroup);
currGroupName = value;
refresh();
});
//删除分组
deleteButton.addActionListener(e -> {
int result = JOptionPane.showConfirmDialog(null, "Confirm Delete Group "+currGroupName+"?", "温馨提示", JOptionPane.OK_CANCEL_OPTION);
if (result==0){
if(ConfigService.DEFAULT_NAME.equals(currGroupName)){
JOptionPane.showMessageDialog(null, "Can't Delete Default Group!");
return;
}
templateGroupMap.remove(currGroupName);
currGroupName = ConfigService.DEFAULT_NAME;
refresh();
}
});
init();
}
private void init() {
this.templateTabbedPane.addTab("entity", new EditTemplatePanel().getMainPanel());
//复制一份
this.templateGroupMap = new LinkedHashMap<>();
configService.getTemplateGroupMap().forEach((s, templateGroup) -> this.templateGroupMap.put(s, templateGroup.cloneTemplateGroup()));
this.currGroupName = configService.getCurrTemplateGroupName();
refresh();
}
@SuppressWarnings("unchecked")
private void refresh() {
this.init = false;
//初始化分组
this.groupButton.removeAllItems();
this.templateGroupMap.keySet().forEach(this.groupButton::addItem);
this.groupButton.setSelectedItem(this.currGroupName);
//初始化选项卡
int count = this.templateTabbedPane.getTabCount();
if (count>0) {
for (int i = 0; i < count; i++) {
this.templateTabbedPane.removeTabAt(0);
}
}
getTemplateGroup().getTemplateList().forEach(template -> {
EditTemplatePanel editTemplatePanel = new EditTemplatePanel(template);
this.templateTabbedPane.addTab(template.getName(), editTemplatePanel.getMainPanel());
});
this.init = true;
}
@Nls
@ -41,11 +153,17 @@ public class TemplateSetting implements Configurable {
@Override
public boolean isModified() {
return false;
return !configService.getTemplateGroupMap().equals(this.templateGroupMap);
}
@Override
public void apply() {
this.configService.setCurrTemplateGroupName(currGroupName);
this.configService.setTemplateGroupMap(templateGroupMap);
}
@Override
public void reset() {
init();
}
}