Skip to content

Commit 58f560d

Browse files
committed
feat: add file template table to crate dialog.
fix: file template doesn't work.
1 parent c9d6737 commit 58f560d

11 files changed

Lines changed: 273 additions & 169 deletions

src/com/dengzii/plugin/template/CreateModuleAction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CreateModuleAction : AnAction() {
2323
}
2424

2525
override fun actionPerformed(e: AnActionEvent) {
26-
val kit = PluginKit.get(e)
26+
val kit = PluginKit.init(e)
2727
if (!kit.isProjectValid()) {
2828
Logger.d(CreateModuleAction::class.java.simpleName, "Project is not valid.")
2929
return

src/com/dengzii/plugin/template/TemplateConfigurable.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.dengzii.plugin.template
22

3-
import com.dengzii.plugin.template.ui.ConfigurePanel
43
import com.dengzii.plugin.template.ui.RealConfigurePanel
54
import com.intellij.openapi.options.SearchableConfigurable
65
import org.jetbrains.annotations.Nls
@@ -34,17 +33,17 @@ class TemplateConfigurable() : SearchableConfigurable {
3433
return "preferences.ModuleTemplateConfig"
3534
}
3635

37-
override fun createComponent(): JComponent? {
36+
override fun createComponent(): JComponent {
3837
panelConfig = RealConfigurePanel()
3938
return panelConfig
4039
}
4140

4241
override fun isModified(): Boolean {
43-
return true
42+
return panelConfig.isModified()
4443
}
4544

4645
@Nls(capitalization = Nls.Capitalization.Title)
47-
override fun getDisplayName(): String? {
46+
override fun getDisplayName(): String {
4847
return "Module Template Settings"
4948
}
5049

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ open class FileTreeNode() {
146146
}
147147
}
148148

149+
/**
150+
* Return all file template in tree node
151+
*/
152+
fun getAllTemplateMap(): Map<String, String> {
153+
val templates = fileTemplates.orEmpty().toMutableMap()
154+
realChildren.forEach {
155+
templates.putAll(it.getAllTemplateMap())
156+
}
157+
return templates
158+
}
159+
149160
fun getNodeHasTemplateInTree(): List<FileTreeNode> {
150161
val nodes = mutableListOf<FileTreeNode>()
151162
if (fileTemplates != null) {
@@ -180,6 +191,21 @@ open class FileTreeNode() {
180191
this.placeholders!!.putAll(placeholders)
181192
}
182193

194+
fun addPlaceholders(placeholders: Array<String>) {
195+
addPlaceholders(placeholders.associate {
196+
it to (this.placeholders?.getOrDefault(it, "") ?: "")
197+
})
198+
}
199+
200+
/**
201+
* Add placeholders with empty value.
202+
*/
203+
fun addFileTemplate(fileName: String, template: String) {
204+
if (fileTemplates == null) {
205+
fileTemplates = mutableMapOf()
206+
}
207+
fileTemplates!![fileName] = template
208+
}
183209

184210
fun addFileTemplates(placeholders: Map<String, String>) {
185211
if (this.fileTemplates == null) {
@@ -325,14 +351,20 @@ open class FileTreeNode() {
325351
}
326352
}
327353

354+
/**
355+
* Return the placeholder key list in node tree.
356+
*/
328357
fun getAllPlaceholderInTree(): List<String> {
329-
val result = mutableSetOf<String>()
358+
val result = allPlaceholder.toMutableList()
330359
traversal({ fileTreeNode: FileTreeNode, _: Int ->
331360
result.addAll(fileTreeNode.allPlaceholder)
332361
})
333362
return result.toList()
334363
}
335364

365+
/**
366+
* Return the placeholder key/replacement map of the node tree.
367+
*/
336368
fun getAllPlaceholdersMap(): Map<String, String> {
337369
val result = mutableMapOf<String, String>()
338370
result.putAll(placeholders.orEmpty())

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

Lines changed: 52 additions & 57 deletions
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.CreateModuleDialog">
33
<grid id="cbd77" binding="rootPanel" layout-manager="BorderLayout" hgap="15" vgap="5">
44
<constraints>
5-
<xy x="156" y="179" width="843" height="637"/>
5+
<xy x="156" y="179" width="759" height="652"/>
66
</constraints>
77
<properties>
88
<font name="Microsoft YaHei" size="14"/>
@@ -39,102 +39,91 @@
3939
<properties/>
4040
<border type="empty"/>
4141
<children>
42-
<grid id="e3588" binding="mainPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="14" vgap="10">
43-
<margin top="10" left="26" bottom="10" right="26"/>
42+
<grid id="e3588" binding="mainPanel" layout-manager="BorderLayout" hgap="0" vgap="10">
4443
<constraints border-constraint="Center"/>
4544
<properties>
4645
<enabled value="true"/>
4746
<visible value="true"/>
4847
</properties>
4948
<border type="empty" title=""/>
5049
<children>
51-
<grid id="2940f" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
52-
<margin top="0" left="0" bottom="0" right="0"/>
53-
<constraints>
54-
<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"/>
55-
</constraints>
50+
<grid id="f3de" layout-manager="BorderLayout" hgap="5" vgap="10">
51+
<constraints border-constraint="North"/>
5652
<properties/>
57-
<border type="empty"/>
53+
<border type="none"/>
5854
<children>
55+
<component id="16b27" class="javax.swing.JComboBox" binding="cbModuleTemplate">
56+
<constraints border-constraint="Center"/>
57+
<properties>
58+
<font size="12"/>
59+
<model>
60+
<item value="Feature Module"/>
61+
<item value="Application Module"/>
62+
<item value="Package Module"/>
63+
<item value="Export Module"/>
64+
</model>
65+
</properties>
66+
</component>
67+
<component id="4e3c9" class="javax.swing.JButton" binding="btConfigure">
68+
<constraints border-constraint="East"/>
69+
<properties>
70+
<text value="Configure"/>
71+
</properties>
72+
</component>
5973
<component id="cdc8a" class="javax.swing.JLabel">
60-
<constraints>
61-
<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"/>
62-
</constraints>
74+
<constraints border-constraint="North"/>
6375
<properties>
6476
<font size="12" style="1"/>
6577
<text value="Template"/>
6678
</properties>
6779
</component>
68-
<grid id="f3de" layout-manager="BorderLayout" hgap="5" vgap="0">
69-
<constraints>
70-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
71-
</constraints>
72-
<properties/>
73-
<border type="none"/>
74-
<children>
75-
<component id="16b27" class="javax.swing.JComboBox" binding="cbModuleTemplate">
76-
<constraints border-constraint="Center"/>
77-
<properties>
78-
<font size="12"/>
79-
<model>
80-
<item value="Feature Module"/>
81-
<item value="Application Module"/>
82-
<item value="Package Module"/>
83-
<item value="Export Module"/>
84-
</model>
85-
</properties>
86-
</component>
87-
<component id="4e3c9" class="javax.swing.JButton" binding="btConfigure">
88-
<constraints border-constraint="East"/>
89-
<properties>
90-
<text value="Configure"/>
91-
</properties>
92-
</component>
93-
</children>
94-
</grid>
9580
</children>
9681
</grid>
97-
<grid id="d3c26" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
82+
<grid id="d3c26" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
9883
<margin top="0" left="0" bottom="0" right="0"/>
99-
<constraints>
100-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
101-
</constraints>
102-
<properties/>
84+
<constraints border-constraint="Center"/>
85+
<properties>
86+
<enabled value="true"/>
87+
</properties>
10388
<border type="empty"/>
10489
<children>
10590
<component id="173ba" class="javax.swing.JLabel">
10691
<constraints>
107-
<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"/>
92+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
10893
</constraints>
10994
<properties>
11095
<font size="12" style="1"/>
11196
<text value="Placeholder"/>
11297
</properties>
11398
</component>
114-
<grid id="7aa5e" binding="panelPlaceholder" layout-manager="BorderLayout" hgap="0" vgap="0">
99+
<scrollpane id="66826" binding="scrollPanePlaceHolder">
100+
<constraints>
101+
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
102+
</constraints>
103+
<properties/>
104+
<border type="none"/>
105+
<children/>
106+
</scrollpane>
107+
<scrollpane id="f8379" binding="scrollPaneFileTemplate">
115108
<constraints>
116-
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
109+
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
117110
</constraints>
118111
<properties/>
119112
<border type="none"/>
120113
<children/>
121-
</grid>
122-
<component id="6106a" class="javax.swing.JLabel">
114+
</scrollpane>
115+
<component id="e72c5" class="javax.swing.JLabel">
123116
<constraints>
124-
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
117+
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
125118
</constraints>
126119
<properties>
127-
<enabled value="false"/>
128-
<text value="Only the value of the root node is listed"/>
120+
<enabled value="true"/>
121+
<font size="12" style="1"/>
122+
<text value="File Template"/>
129123
</properties>
130124
</component>
131125
</children>
132126
</grid>
133-
<vspacer id="ed063">
134-
<constraints>
135-
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
136-
</constraints>
137-
</vspacer>
138127
</children>
139128
</grid>
140129
</children>
@@ -179,6 +168,12 @@
179168
</grid>
180169
</children>
181170
</grid>
171+
<vspacer id="4243a">
172+
<constraints border-constraint="West"/>
173+
</vspacer>
174+
<vspacer id="13b2">
175+
<constraints border-constraint="East"/>
176+
</vspacer>
182177
</children>
183178
</grid>
184179
</form>

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
import java.awt.*;
1313
import java.awt.event.ActionEvent;
1414
import java.awt.event.KeyEvent;
15-
import java.util.ArrayList;
16-
import java.util.Collections;
17-
import java.util.HashMap;
15+
import java.util.*;
1816
import java.util.List;
1917

2018
public class CreateModuleDialog extends JDialog {
@@ -31,9 +29,13 @@ public class CreateModuleDialog extends JDialog {
3129
private JButton btCancel;
3230
private JButton btPrevious;
3331
private JButton btFinish;
34-
private JPanel panelPlaceholder;
32+
private JScrollPane scrollPanePlaceHolder;
33+
private JScrollPane scrollPaneFileTemplate;
34+
// private JPanel panelPlaceholder;
3535

3636
private EditableTable tablePlaceholder;
37+
private EditableTable tableFileTemplate;
38+
3739
private OnFinishListener onFinishListener;
3840

3941
private java.util.List<Module> moduleTemplates = Collections.emptyList();
@@ -72,6 +74,7 @@ public static void main(String[] args) {
7274

7375
private void onNextClick(ActionEvent e) {
7476
selectedModule.getTemplate().setPlaceholders(tablePlaceholder.getPairResult());
77+
selectedModule.getTemplate().setFileTemplates(tableFileTemplate.getPairResult());
7578
previewPanel.setModuleConfigPreview(selectedModule);
7679

7780
if (currentPanelIndex == panels.size() - 1) {
@@ -121,7 +124,8 @@ private void onModuleConfigChange() {
121124
Logger.INSTANCE.i(TAG, "onModuleConfigChange");
122125
selectedModule = moduleTemplates.get(cbModuleTemplate.getSelectedIndex());
123126
previewPanel.setModuleConfigPreview(selectedModule);
124-
tablePlaceholder.setPairData(selectedModule.getTemplate().getPlaceholderInherit());
127+
tablePlaceholder.setPairData(selectedModule.getTemplate().getAllPlaceholdersMap());
128+
tableFileTemplate.setPairData(selectedModule.getTemplate().getAllTemplateMap());
125129
}
126130

127131
private void initDialog() {
@@ -141,7 +145,11 @@ private void initDialog() {
141145

142146
tablePlaceholder = new EditableTable(new String[]{"Key", "Value"}, new Boolean[]{false, true});
143147
tablePlaceholder.setToolBarVisible(false);
144-
panelPlaceholder.add(tablePlaceholder, BorderLayout.CENTER);
148+
tableFileTemplate = new EditableTable(new String[]{"File Name", "Template"}, new Boolean[]{false, true});
149+
tableFileTemplate.setToolBarVisible(false);
150+
151+
scrollPanePlaceHolder.setViewportView(tablePlaceholder);
152+
scrollPaneFileTemplate.setViewportView(tableFileTemplate);
145153

146154
btConfigure.setIcon(AllIcons.General.GearPlain);
147155
previewPanel = new PreviewPanel();

src/com/dengzii/plugin/template/ui/EditableTable.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package com.dengzii.plugin.template.ui
33
import com.intellij.ui.components.JBScrollPane
44
import com.intellij.ui.table.JBTable
55
import java.awt.BorderLayout
6-
import java.awt.event.FocusEvent
7-
import java.awt.event.FocusListener
86
import javax.swing.JPanel
7+
import javax.swing.event.TableModelEvent
98
import javax.swing.table.DefaultTableModel
109

1110
/**
@@ -38,6 +37,12 @@ class EditableTable(header: Array<String>, colEditable: Array<Boolean> = emptyAr
3837
initListener()
3938
}
4039

40+
fun addChangeListener(listener: (TableModelEvent) -> Unit) {
41+
tableModel.addTableModelListener {
42+
listener.invoke(it)
43+
}
44+
}
45+
4146
fun setToolBarVisible(visible: Boolean) {
4247
editToolbar.isVisible = visible
4348
}

0 commit comments

Comments
 (0)