Skip to content

Commit

Permalink
新增模板排序
Browse files Browse the repository at this point in the history
  • Loading branch information
hykes committed Jan 29, 2019
1 parent ebddb90 commit 1eb4c13
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 20 deletions.
6 changes: 5 additions & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>me.hehaiyang.codegen</id>
<name>CodeGen</name>
<version>1.2.4</version>
<version>1.2.5</version>
<vendor email="[email protected]" url="https://github.com/hykes">hehaiyang</vendor>

<description><![CDATA[
Expand All @@ -18,6 +18,10 @@
]]></description>

<change-notes><![CDATA[
<p>v1.2.5 2019-01-29</p>
<ul>
<li>Add template sequence</li>
</ul>
<p>v1.2.4 2018-05-12</p>
<ul>
<li>Fix template init</li>
Expand Down
6 changes: 6 additions & 0 deletions resources/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ Predefined Directive:

* 组合用法
#Append("#LowerCase('AbcDefGhi')" '$' '%') => $abcDefGhi%

* 获取类路径
#GetPackage(${model}) => xx.xxx.model

* 引用类路径
#ImportPackage(${model}) => import xx.xxx.model
8 changes: 8 additions & 0 deletions src/com/github/hykes/codegen/configurable/ui/TemplatesUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public boolean isModified(){
DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) childEnum.nextElement();
CodeTemplate template = (CodeTemplate) childNode.getUserObject();
CodeTemplate tmp = templateEditor.getCodeTemplate();
if (Objects.isNull(tmp.getOrder())) {
tmp.setOrder(Integer.valueOf(1));
}
if(template.getId().equals(tmp.getId()) && !template.equals(tmp)){
return true;
}
Expand Down Expand Up @@ -138,9 +141,13 @@ public void apply() {
template = tmp;
}
}
if (Objects.isNull(template.getOrder())) {
template.setOrder(Integer.valueOf(1));
}
templates.add(template);
}
CodeGroup group = (CodeGroup) node.getUserObject();
Collections.sort(templates, Comparator.comparing(CodeTemplate::getOrder));
group.setTemplates(templates);
groups.add(group);
}
Expand Down Expand Up @@ -274,6 +281,7 @@ public void actionPerformed(AnActionEvent e) {
o.write(("group: "+ group.getName() +";\n").getBytes());
o.write(("level: "+ group.getLevel() +";\n").getBytes());
o.write(("isResources: "+ template.getResources().toString() +";\n").getBytes());
o.write(("order: "+ template.getOrder().toString() +";\n").getBytes());
o.write("*#\n".getBytes());
o.write(template.getTemplate().getBytes(Charset.defaultCharset()));
exportTemplate.setBytes(o.toByteArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,13 @@
<text value=""/>
</properties>
</component>
<component id="6d86d" class="javax.swing.JLabel" binding="isTestLab">
<component id="6d86d" class="javax.swing.JLabel" binding="orderLab">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="IsTest"/>
</properties>
</component>
<component id="5562b" class="javax.swing.JCheckBox" binding="testCheckBox">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
<labelFor value="6510d"/>
<text value="Order"/>
</properties>
</component>
<component id="c389a" class="javax.swing.JLabel" binding="extensionLab">
Expand All @@ -109,6 +102,14 @@
<text value="Extension"/>
</properties>
</component>
<component id="6510d" class="javax.swing.JTextField" binding="orderTextField">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
</children>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.hykes.codegen.model.CodeTemplate;
import com.github.hykes.codegen.utils.StringUtils;
import com.google.common.base.Strings;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
Expand Down Expand Up @@ -33,8 +34,8 @@ public class TemplateEditorUI {
private JLabel extensionLab;
private JLabel subPathLab;
private JLabel resourceLab;
private JCheckBox testCheckBox;
private JLabel isTestLab;
private JLabel orderLab;
private JTextField orderTextField;

private final JTextField id = new JTextField();

Expand All @@ -56,6 +57,7 @@ public void refresh(@NotNull CodeTemplate codeTemplate) {
extensionTextField.setText(codeTemplate.getExtension());
filenameTextField.setText(codeTemplate.getFilename());
subPathTextField.setText(codeTemplate.getSubPath());
orderTextField.setText(codeTemplate.getOrder().toString());
resourceCheckBox.setSelected(Objects.isNull(codeTemplate.getResources()) ? false : codeTemplate.getResources());
editor = createEditor(codeTemplate.getTemplate(), codeTemplate.getExtension());

Expand Down Expand Up @@ -97,6 +99,8 @@ public CodeTemplate getCodeTemplate() {
codeTemplate.setTemplate(editor.getDocument().getText());
codeTemplate.setSubPath(subPathTextField.getText().trim());
codeTemplate.setResources(resourceCheckBox.isSelected());
Integer order = Strings.isNullOrEmpty(orderTextField.getText().trim()) ? Integer.valueOf(1) : Integer.valueOf(orderTextField.getText().trim());
codeTemplate.setOrder(order);
return codeTemplate;
}

Expand Down Expand Up @@ -154,12 +158,9 @@ public JPanel getRootPanel() {
resourceCheckBox = new JCheckBox();
resourceCheckBox.setText("");
infoPanel.add(resourceCheckBox, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
isTestLab = new JLabel();
isTestLab.setText("IsTest");
infoPanel.add(isTestLab, new GridConstraints(2, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
testCheckBox = new JCheckBox();
testCheckBox.setText("");
infoPanel.add(testCheckBox, new GridConstraints(2, 3, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
orderLab = new JLabel();
orderLab.setText("Order");
infoPanel.add(orderLab, new GridConstraints(2, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
extensionLab = new JLabel();
extensionLab.setText("Extension");
infoPanel.add(extensionLab, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
Expand Down
4 changes: 3 additions & 1 deletion src/com/github/hykes/codegen/gui/ColumnEditorFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
String outputPath = groupPathMap.get(group.getId());
if (StringUtils.isNotEmpty(outputPath)) {
for (CodeContext context : contexts) {
for (CodeTemplate codeTemplate : group.getTemplates()) {
List<CodeTemplate> codeTemplates = group.getTemplates();
Collections.sort(codeTemplates, Comparator.comparing(CodeTemplate::getOrder));
for (CodeTemplate codeTemplate : codeTemplates) {
progressIndicator.setText(String.format("generator template %s ...", codeTemplate.getDisplay()));

FileProviderFactory fileFactory = new FileProviderFactory(ideaContext.getProject(), outputPath);
Expand Down
27 changes: 27 additions & 0 deletions src/com/github/hykes/codegen/model/CodeTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ public CodeTemplate(String id, String display, String extension, String filename
this.template = template;
this.subPath = subPath;
this.isResources = isResources;
this.order = 1;
}

public CodeTemplate(String id, String display, String extension, String filename, String template, String subPath, Boolean isResources, Integer order) {
this.id = id;
this.display = display;
this.extension = extension;
this.filename = filename;
this.template = template;
this.subPath = subPath;
this.isResources = isResources;
this.order = order;
}

/**
Expand Down Expand Up @@ -61,6 +73,16 @@ public CodeTemplate(String id, String display, String extension, String filename

private String group;

private Integer order;

public Integer getOrder() {
return order;
}

public void setOrder(Integer order) {
this.order = order;
}

public String getId() {
return id;
}
Expand Down Expand Up @@ -154,6 +176,9 @@ public boolean equals(Object o) {
if (subPath != null ? !subPath.equals(that.subPath) : that.subPath != null) {
return false;
}
if (order != null ? !order.equals(that.order) : that.order != null) {
return false;
}
return isResources != null ? isResources.equals(that.isResources) : that.isResources == null;
}

Expand All @@ -166,6 +191,7 @@ public int hashCode() {
result = 31 * result + (template != null ? template.hashCode() : 0);
result = 31 * result + (subPath != null ? subPath.hashCode() : 0);
result = 31 * result + (isResources != null ? isResources.hashCode() : 0);
result = 31 * result + (order != null ? order.hashCode() : 0);
return result;
}

Expand All @@ -179,6 +205,7 @@ public String toString() {
", subPath='" + subPath + '\'' +
", isResources=" + isResources +
", group='" + group + '\'' +
", order='" + order + '\'' +
'}';
}
}

0 comments on commit 1eb4c13

Please sign in to comment.