Skip to content

Commit 3aa7e76

Browse files
committed
feature(File Template):support set template for file when create module template
1 parent c39951f commit 3aa7e76

6 files changed

Lines changed: 108 additions & 36 deletions

File tree

src/com/dengzii/plugin/template/model/FileTreeNode.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ open class FileTreeNode private constructor() {
140140
return template ?: getFileTemplateInherit()?.get(name)
141141
}
142142

143+
fun setTemplate(name: String) {
144+
template = name
145+
}
146+
143147
fun placeholder(name: String, value: String) {
144148
if (this.placeholders == null) {
145149
this.placeholders = kotlin.collections.mutableMapOf()

src/com/dengzii/plugin/template/ui/ConfigurePanel.form

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.dengzii.plugin.template.ui.ConfigurePanel">
33
<grid id="cbd77" binding="contentPane" layout-manager="BorderLayout" hgap="0" vgap="0">
44
<constraints>
5-
<xy x="48" y="54" width="887" height="625"/>
5+
<xy x="48" y="54" width="888" height="631"/>
66
</constraints>
77
<properties/>
88
<border type="empty">
@@ -145,6 +145,24 @@
145145
</grid>
146146
</children>
147147
</grid>
148+
<scrollpane id="96fb5">
149+
<constraints border-constraint="South"/>
150+
<properties/>
151+
<border type="empty"/>
152+
<children>
153+
<component id="184d" class="javax.swing.JLabel">
154+
<constraints/>
155+
<properties>
156+
<enabled value="true"/>
157+
<font size="11"/>
158+
<text value="&lt;html&gt;What is Auc? &lt;br&gt;&#10;Auc is the abbreviation of AndroidUtilCode, you can see github repo &lt;a href='https://github.com/Blankj/AucFrameTemplate'&gt;[Blankj/AucFrameTemplate]&lt;/a&gt; for more information.&lt;/html&gt;"/>
159+
</properties>
160+
<clientProperties>
161+
<html.disable class="java.lang.Boolean" value="false"/>
162+
</clientProperties>
163+
</component>
164+
</children>
165+
</scrollpane>
148166
</children>
149167
</grid>
150168
</form>

src/com/dengzii/plugin/template/ui/CreateFileDialog.form

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.dengzii.plugin.template.ui.CreateFileDialog">
3-
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>
66
<xy x="48" y="54" width="436" height="297"/>
@@ -16,29 +16,29 @@
1616
<properties/>
1717
<border type="none"/>
1818
<children>
19-
<component id="f9ae9" class="javax.swing.JComboBox" binding="comboBox1" default-binding="true">
19+
<component id="f9ae9" class="javax.swing.JComboBox" binding="cbTemplate">
2020
<constraints>
2121
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
2222
</constraints>
2323
<properties/>
2424
</component>
25-
<component id="bfb4b" class="javax.swing.JTextField" binding="textField1" default-binding="true">
25+
<component id="bfb4b" class="javax.swing.JTextField" binding="tfName">
2626
<constraints>
2727
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
2828
<preferred-size width="150" height="-1"/>
2929
</grid>
3030
</constraints>
3131
<properties/>
3232
</component>
33-
<component id="4d049" class="javax.swing.JLabel" binding="label2">
33+
<component id="4d049" class="javax.swing.JLabel" binding="lbName">
3434
<constraints>
3535
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
3636
</constraints>
3737
<properties>
3838
<text value="Name"/>
3939
</properties>
4040
</component>
41-
<component id="5b56d" class="javax.swing.JLabel" binding="label1">
41+
<component id="5b56d" class="javax.swing.JLabel" binding="lbTemplate">
4242
<constraints>
4343
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
4444
</constraints>
@@ -48,6 +48,14 @@
4848
</component>
4949
</children>
5050
</grid>
51+
<component id="4eb7c" class="javax.swing.JButton" binding="btConfirm">
52+
<constraints>
53+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
54+
</constraints>
55+
<properties>
56+
<text value="Confirm"/>
57+
</properties>
58+
</component>
5159
</children>
5260
</grid>
5361
</form>

src/com/dengzii/plugin/template/ui/CreateFileDialog.java

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
package com.dengzii.plugin.template.ui;
22

33
import com.dengzii.plugin.template.model.FileTreeNode;
4+
import com.dengzii.plugin.template.utils.PluginKit;
5+
import com.intellij.ide.fileTemplates.FileTemplate;
46

57
import javax.swing.*;
68
import java.awt.*;
79
import java.awt.event.KeyAdapter;
810
import java.awt.event.KeyEvent;
911

1012
public class CreateFileDialog extends JDialog {
13+
private static final String NONE = "None";
14+
1115
private JPanel contentPane;
12-
private JComboBox comboBox1;
13-
private JTextField textField1;
14-
private JLabel label1;
15-
private JLabel label2;
16+
private JComboBox cbTemplate;
17+
private JTextField tfName;
18+
private JLabel lbTemplate;
19+
private JLabel lbName;
20+
private JButton btConfirm;
1621

1722
private CreateFileCallback createFileCallback;
1823
private boolean isDir;
1924
private FileTreeNode parent;
2025
private FileTreeNode current;
2126

22-
public static void showForRename(FileTreeNode node, CreateFileCallback createFileCallback) {
27+
public static void showForRefactor(FileTreeNode node, CreateFileCallback createFileCallback) {
2328
CreateFileDialog createFileDialog = new CreateFileDialog();
2429
createFileDialog.createFileCallback = createFileCallback;
2530
createFileDialog.isDir = node.isDir();
@@ -40,18 +45,52 @@ private CreateFileDialog() {
4045
setContentPane(contentPane);
4146
setModal(true);
4247
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
48+
}
49+
50+
private void initFileTemplateList() {
51+
FileTemplate[] fileTemplates = PluginKit.Companion.getAllFileTemplate();
52+
String[] items = new String[fileTemplates.length + 1];
53+
items[0] = NONE;
54+
for (int i = 0; i < fileTemplates.length; i++) {
55+
items[i + 1] = fileTemplates[i].getName();
56+
}
57+
cbTemplate.setModel(new DefaultComboBoxModel<>(items));
58+
if (isRefactor() && current.getTemplateFile() != null
59+
&& PluginKit.Companion.getFileTemplate(current.getTemplateFile()) != null) {
60+
cbTemplate.setSelectedItem(current.getTemplateFile());
61+
}
62+
}
63+
64+
private void onConfirm() {
65+
if (tfName.getText().trim().isEmpty()) {
66+
return;
67+
}
68+
if (isRefactor()) {
69+
current.setName(tfName.getText());
70+
// setup template
71+
if (!isDir && getSelectedTemplate() != null && !getSelectedTemplate().equals(NONE)) {
72+
current.setTemplate(getSelectedTemplate());
73+
}
74+
} else {
75+
current = new FileTreeNode(parent, tfName.getText(), isDir);
76+
}
77+
78+
if (!isRefactor() && parent != null && parent.hasChild(tfName.getText(), isDir)) {
79+
lbName.setText(lbName.getText() + " (already exist.)");
80+
return;
81+
}
4382

44-
contentPane.registerKeyboardAction(e -> {
45-
},
46-
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
47-
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
83+
createFileCallback.callback(current);
84+
dispose();
4885
}
4986

5087
private void initDialog() {
5188

5289
if (isDir) {
53-
label1.setVisible(false);
54-
comboBox1.setVisible(false);
90+
lbTemplate.setVisible(false);
91+
cbTemplate.setVisible(false);
92+
} else {
93+
initFileTemplateList();
5594
}
5695
pack();
5796

@@ -63,35 +102,32 @@ private void initDialog() {
63102
setLocation(x, y);
64103
setPreferredSize(new Dimension(w, h));
65104

66-
setTitle((isRename() ? "Rename " : "New ") + (isDir ? "Directory" : "File"));
67-
if (isRename()) {
68-
textField1.setText(current.getRealName());
105+
setTitle((isRefactor() ? "Refactor " : "New ") + (isDir ? "Directory" : "File"));
106+
if (isRefactor()) {
107+
tfName.setText(current.getRealName());
69108
}
70-
textField1.addKeyListener(new KeyAdapter() {
109+
tfName.addKeyListener(new KeyAdapter() {
71110
@Override
72111
public void keyReleased(KeyEvent e) {
73112
super.keyReleased(e);
74113
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
75-
if (parent != null && parent.hasChild(textField1.getText(), isDir)) {
76-
label2.setText(label2.getText() + " (already exist.)");
77-
return;
78-
}
79-
if (isRename()) {
80-
current.setName(textField1.getText());
81-
createFileCallback.callback(current);
82-
} else {
83-
createFileCallback.callback(new FileTreeNode(parent, textField1.getText(), isDir));
84-
}
85-
86-
dispose();
114+
onConfirm();
87115
}
88116
}
89117
});
118+
btConfirm.addActionListener(e -> onConfirm());
90119
setVisible(true);
91-
textField1.requestFocus();
120+
tfName.requestFocus();
121+
}
122+
123+
private String getSelectedTemplate() {
124+
if (isDir) {
125+
return null;
126+
}
127+
return cbTemplate.getSelectedItem().toString();
92128
}
93129

94-
private boolean isRename() {
130+
private boolean isRefactor() {
95131
return current != null;
96132
}
97133

src/com/dengzii/plugin/template/ui/PreviewPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private void showEditMenu(Component anchor, FileTreeNode current, int x, int y)
149149
}
150150
if (!current.isRoot()) {
151151
jbPopupMenu.add(getMenuItem("Rename", e -> {
152-
CreateFileDialog.showForRename(current, fileTreeNode -> {
152+
CreateFileDialog.showForRefactor(current, fileTreeNode -> {
153153
((DefaultMutableTreeNode) fileTree.getLastSelectedPathComponent()).setUserObject(fileTreeNode);
154154
fileTree.updateUI();
155155
});

src/com/dengzii/plugin/template/utils/PluginKit.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.dengzii.plugin.template.utils
22

3+
import com.intellij.ide.fileTemplates.FileTemplate
34
import com.intellij.ide.fileTemplates.FileTemplateManager
45
import com.intellij.ide.fileTemplates.FileTemplateUtil
6+
import com.intellij.ide.fileTemplates.impl.FileTemplateManagerImpl
57
import com.intellij.openapi.actionSystem.AnActionEvent
68
import com.intellij.openapi.actionSystem.PlatformDataKeys
79
import com.intellij.openapi.module.Module
@@ -136,5 +138,9 @@ class PluginKit private constructor(e: AnActionEvent) {
136138
fun get(e: AnActionEvent): PluginKit {
137139
return PluginKit(e)
138140
}
141+
142+
fun getAllFileTemplate(): Array<FileTemplate> = FileTemplateManagerImpl.getDefaultInstance().allTemplates
143+
144+
fun getFileTemplate(name: String): FileTemplate? = FileTemplateManagerImpl.getDefaultInstance().getTemplate(name)
139145
}
140146
}

0 commit comments

Comments
 (0)