From 92e02557716f99f1327f0824a7e47dd7a2f81386 Mon Sep 17 00:00:00 2001 From: javas <634464916@qq.com> Date: Thu, 11 Jan 2018 12:55:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=A8=A1=E6=9D=BF=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8A=9F=E8=83=BD=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ConfigServiceImpl.java | 6 +- src/com/sjhy/plugin/ui/EditTemplatePanel.form | 18 ++- src/com/sjhy/plugin/ui/EditTemplatePanel.java | 39 +++--- src/com/sjhy/plugin/ui/TemplateSetting.java | 122 +++++++++++++++++- 4 files changed, 158 insertions(+), 27 deletions(-) diff --git a/src/com/sjhy/plugin/service/impl/ConfigServiceImpl.java b/src/com/sjhy/plugin/service/impl/ConfigServiceImpl.java index 6df5460..ac8c2be 100644 --- a/src/com/sjhy/plugin/service/impl/ConfigServiceImpl.java +++ b/src/com/sjhy/plugin/service/impl/ConfigServiceImpl.java @@ -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 diff --git a/src/com/sjhy/plugin/ui/EditTemplatePanel.form b/src/com/sjhy/plugin/ui/EditTemplatePanel.form index 2ee3cc3..f8fc922 100644 --- a/src/com/sjhy/plugin/ui/EditTemplatePanel.form +++ b/src/com/sjhy/plugin/ui/EditTemplatePanel.form @@ -1,6 +1,6 @@
- + @@ -8,14 +8,20 @@ - + + - - - + - + + + + + + + +
diff --git a/src/com/sjhy/plugin/ui/EditTemplatePanel.java b/src/com/sjhy/plugin/ui/EditTemplatePanel.java index 5104b78..e93fc47 100644 --- a/src/com/sjhy/plugin/ui/EditTemplatePanel.java +++ b/src/com/sjhy/plugin/ui/EditTemplatePanel.java @@ -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() { diff --git a/src/com/sjhy/plugin/ui/TemplateSetting.java b/src/com/sjhy/plugin/ui/TemplateSetting.java index 109f2af..dfd4af9 100644 --- a/src/com/sjhy/plugin/ui/TemplateSetting.java +++ b/src/com/sjhy/plugin/ui/TemplateSetting.java @@ -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 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(); } }