Skip to content

Commit 6e7ae4d

Browse files
author
dengzi
committed
edit file tree
1 parent f3ba2be commit 6e7ae4d

10 files changed

Lines changed: 338 additions & 79 deletions

File tree

resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<extensions defaultExtensionNs="com.intellij">
2626
<fileTemplateGroup implementation="com.dengzii.plugin.template.template.FileTemplateFactory" order="first"/>
2727
<internalFileTemplate name="AndroidManifest" id="com.dengzii.plugin.template.ft.manifest"/>
28-
<applicationConfigurable groupId="tools" displayName="Template Module Generator"
28+
<applicationConfigurable groupId="tools" displayName="Directory Template Settings"
2929
id="preferences.ModuleTemplateConfig"
3030
instance="com.dengzii.plugin.template.ui.ConfigurePanel"/>
3131
</extensions>

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ object Config {
2929
Module.create(AucTemplate.EXPORT, "export", "com.example.feature", "Java", "Auc Export Module")
3030
)
3131

32-
3332
fun clear() {
3433
PropertiesComponent.getInstance().unsetValue(KEY_TEMPLATES)
3534
}
@@ -46,15 +45,13 @@ object Config {
4645
try {
4746
val module = GSON.fromJson(it, Module::class.java)
4847
setParent(module.template)
49-
println(module.template.getTreeGraph())
5048
result.add(module)
5149
} catch (e: Exception) {
5250
clear()
53-
e.printStackTrace()
51+
Logger.e(Config::class.java.simpleName, e)
5452
return result
5553
}
5654
}
57-
result.addAll(DEFAULT_MODULE_TEMPLATE)
5855
return result
5956
}
6057

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ open class FileTreeNode private constructor() {
7070
return this
7171
}
7272

73+
fun removeFromParent(): Boolean {
74+
if (parent != null) {
75+
parent!!.children.remove(this)
76+
return true
77+
}
78+
return false
79+
}
80+
81+
fun hasChild(name: String, isDir: Boolean): Boolean {
82+
children.forEach {
83+
if (it.isDir == isDir && it.realName == name) {
84+
return true
85+
}
86+
}
87+
return false
88+
}
89+
7390
fun getRealName(): String {
7491
return realName
7592
}

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,34 +119,18 @@
119119
<children>
120120
<tabbedpane id="e9ea8" class="com.intellij.ui.components.JBTabbedPane">
121121
<constraints>
122-
<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"/>
122+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="4" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
123123
</constraints>
124124
<properties/>
125125
<border type="none"/>
126126
<children>
127-
<grid id="42887" layout-manager="CardLayout" hgap="0" vgap="0">
127+
<grid id="42887" binding="panelStructure" layout-manager="CardLayout" hgap="0" vgap="0">
128128
<constraints>
129129
<tabbedpane title="Structure"/>
130130
</constraints>
131131
<properties/>
132-
<border type="none"/>
133-
<children>
134-
<scrollpane id="b337b">
135-
<constraints>
136-
<card name="Card2"/>
137-
</constraints>
138-
<properties/>
139-
<border type="none"/>
140-
<children>
141-
<component id="548e9" class="javax.swing.JTextArea" binding="taTemplate">
142-
<constraints/>
143-
<properties>
144-
<margin top="5" left="5" bottom="5" right="5"/>
145-
</properties>
146-
</component>
147-
</children>
148-
</scrollpane>
149-
</children>
132+
<border type="empty"/>
133+
<children/>
150134
</grid>
151135
<grid id="9bfd" layout-manager="BorderLayout" hgap="0" vgap="0">
152136
<constraints>

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.dengzii.plugin.template.template.AucTemplate;
66
import com.intellij.icons.AllIcons;
77
import com.intellij.openapi.options.SearchableConfigurable;
8+
import com.intellij.openapi.roots.ui.componentsList.components.ScrollablePanel;
89
import com.intellij.ui.DocumentAdapter;
910
import com.intellij.ui.components.JBList;
1011
import com.intellij.ui.table.JBTable;
@@ -34,18 +35,19 @@ public class ConfigurePanel extends JPanel implements SearchableConfigurable {
3435
private JButton btRemove;
3536
private JButton btCopy;
3637

37-
private JTextArea taTemplate;
3838
private JTextArea taPlaceholder;
3939
private JTextField tfName;
4040
private JBTable JB;
4141
private JBList listTemplate;
42+
private JPanel panelStructure;
4243

4344
private List<Module> configs;
4445
private DefaultListModel<String> model;
4546

4647
private Module currentConfig;
4748

4849
private ConfigurePanel panel;
50+
private PreviewPanel previewPanel;
4951

5052
private ConfigurePanel() {
5153
setLayout(new BorderLayout());
@@ -116,7 +118,6 @@ private int getSelectedIndex() {
116118
}
117119

118120
private void loadConfig() {
119-
System.out.println("ConfigurePanel.loadConfig");
120121
configs = Config.INSTANCE.loadModuleTemplates();
121122
model = new DefaultListModel<>();
122123
configs.forEach(module -> {
@@ -125,6 +126,15 @@ private void loadConfig() {
125126
listTemplate.setModel(model);
126127
}
127128

129+
private void onTemplateSelect(int index) {
130+
if (currentConfig == configs.get(index)) {
131+
return;
132+
}
133+
currentConfig = configs.get(index);
134+
tfName.setText(currentConfig.getTemplateName());
135+
previewPanel.setModuleConfig(currentConfig);
136+
}
137+
128138
private void initPanel() {
129139

130140
setIconButton(btAdd, AllIcons.General.Add);
@@ -137,8 +147,8 @@ private void initPanel() {
137147
listTemplate.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
138148
listTemplate.addListSelectionListener(e -> {
139149
if (getSelectedIndex() == -1) return;
140-
currentConfig = configs.get(getSelectedIndex());
141-
tfName.setText(currentConfig.getTemplateName());
150+
onTemplateSelect(getSelectedIndex());
151+
142152
});
143153
tfName.getDocument().addDocumentListener(new DocumentAdapter() {
144154
@Override
@@ -148,6 +158,11 @@ protected void textChanged(@NotNull DocumentEvent documentEvent) {
148158
}
149159
});
150160
loadConfig();
161+
previewPanel = new PreviewPanel();
162+
panelStructure.add(previewPanel);
163+
if (model.size() > 1) {
164+
listTemplate.setSelectedIndex(0);
165+
}
151166
}
152167

153168
private void setIconButton(JButton button, Icon icon) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
4+
<margin top="10" left="10" bottom="10" right="10"/>
5+
<constraints>
6+
<xy x="48" y="54" width="436" height="297"/>
7+
</constraints>
8+
<properties/>
9+
<border type="none"/>
10+
<children>
11+
<grid id="e3588" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
12+
<margin top="0" left="0" bottom="0" right="0"/>
13+
<constraints>
14+
<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"/>
15+
</constraints>
16+
<properties/>
17+
<border type="none"/>
18+
<children>
19+
<component id="f9ae9" class="javax.swing.JComboBox" binding="comboBox1" default-binding="true">
20+
<constraints>
21+
<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"/>
22+
</constraints>
23+
<properties/>
24+
</component>
25+
<component id="bfb4b" class="javax.swing.JTextField" binding="textField1" default-binding="true">
26+
<constraints>
27+
<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">
28+
<preferred-size width="150" height="-1"/>
29+
</grid>
30+
</constraints>
31+
<properties/>
32+
</component>
33+
<component id="4d049" class="javax.swing.JLabel" binding="label2">
34+
<constraints>
35+
<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"/>
36+
</constraints>
37+
<properties>
38+
<text value="Name"/>
39+
</properties>
40+
</component>
41+
<component id="5b56d" class="javax.swing.JLabel" binding="label1">
42+
<constraints>
43+
<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"/>
44+
</constraints>
45+
<properties>
46+
<text value="Template"/>
47+
</properties>
48+
</component>
49+
</children>
50+
</grid>
51+
</children>
52+
</grid>
53+
</form>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.dengzii.plugin.template.ui;
2+
3+
import com.dengzii.plugin.template.model.FileTreeNode;
4+
5+
import javax.swing.*;
6+
import java.awt.*;
7+
import java.awt.event.KeyAdapter;
8+
import java.awt.event.KeyEvent;
9+
10+
public class CreateFileDialog extends JDialog {
11+
private JPanel contentPane;
12+
private JComboBox comboBox1;
13+
private JTextField textField1;
14+
private JLabel label1;
15+
private JLabel label2;
16+
17+
private CreateFileCallback createFileCallback;
18+
private boolean isDir;
19+
private FileTreeNode parent;
20+
private FileTreeNode current;
21+
22+
public static void showForRename(FileTreeNode node, CreateFileCallback createFileCallback) {
23+
CreateFileDialog createFileDialog = new CreateFileDialog();
24+
createFileDialog.createFileCallback = createFileCallback;
25+
createFileDialog.isDir = node.isDir();
26+
createFileDialog.parent = node.getParent();
27+
createFileDialog.current = node;
28+
createFileDialog.initDialog();
29+
}
30+
31+
public static void showForCreate(FileTreeNode parent, boolean isDir, CreateFileCallback createFileCallback) {
32+
CreateFileDialog createFileDialog = new CreateFileDialog();
33+
createFileDialog.createFileCallback = createFileCallback;
34+
createFileDialog.isDir = isDir;
35+
createFileDialog.parent = parent;
36+
createFileDialog.initDialog();
37+
}
38+
39+
private CreateFileDialog() {
40+
setContentPane(contentPane);
41+
setModal(true);
42+
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
43+
44+
contentPane.registerKeyboardAction(e -> {
45+
},
46+
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
47+
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
48+
}
49+
50+
private void initDialog() {
51+
52+
if (isDir) {
53+
label1.setVisible(false);
54+
comboBox1.setVisible(false);
55+
}
56+
pack();
57+
58+
Dimension screen = getToolkit().getScreenSize();
59+
int w = getWidth();
60+
int h = getHeight();
61+
int x = screen.width / 2 - w / 2;
62+
int y = screen.height / 2 - h / 2 - 100;
63+
setLocation(x, y);
64+
setPreferredSize(new Dimension(w, h));
65+
66+
setTitle((isRename() ? "New " : "Rename ") + (isDir ? "Directory" : "File"));
67+
if (isRename()) {
68+
textField1.setText(current.getRealName());
69+
}
70+
textField1.addKeyListener(new KeyAdapter() {
71+
@Override
72+
public void keyReleased(KeyEvent e) {
73+
super.keyReleased(e);
74+
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();
87+
}
88+
}
89+
});
90+
setVisible(true);
91+
textField1.requestFocus();
92+
}
93+
94+
private boolean isRename() {
95+
return current != null;
96+
}
97+
98+
public interface CreateFileCallback {
99+
void callback(FileTreeNode fileTreeNode);
100+
}
101+
102+
public static void main(String[] args) {
103+
CreateFileDialog.showForCreate(null, true, fileTreeNode -> {
104+
System.out.println("CreateFileDialog.main " + fileTreeNode.getName());
105+
});
106+
}
107+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<constraints border-constraint="Center"/>
1919
<properties>
2020
<enabled value="true"/>
21-
<font name="Droid Sans Mono" size="36" style="1"/>
21+
<font name="DejaVu Sans Mono" size="36" style="1"/>
2222
<horizontalTextPosition value="2"/>
2323
<text value="Create Module From Template"/>
2424
</properties>
Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
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.PreviewPanel">
3-
<grid id="27dc6" binding="panel1" default-binding="true" layout-manager="BorderLayout" hgap="0" vgap="0">
3+
<grid id="27dc6" binding="contentPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
44
<constraints>
55
<xy x="20" y="20" width="500" height="400"/>
66
</constraints>
77
<properties/>
88
<border type="none"/>
99
<children>
10-
<grid id="176b2" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
11-
<margin top="10" left="26" bottom="10" right="26"/>
10+
<scrollpane id="99f06" class="com.intellij.ui.components.JBScrollPane">
1211
<constraints border-constraint="Center"/>
1312
<properties/>
1413
<border type="none"/>
1514
<children>
16-
<scrollpane id="b8ae1" binding="scrollPane">
17-
<constraints>
18-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
19-
</constraints>
15+
<component id="33c08" class="com.intellij.ui.treeStructure.Tree" binding="fileTree">
16+
<constraints/>
2017
<properties/>
21-
<border type="none"/>
22-
<children>
23-
<component id="796a4" class="com.intellij.ui.treeStructure.Tree" binding="fileTree">
24-
<constraints/>
25-
<properties/>
26-
</component>
27-
</children>
28-
</scrollpane>
18+
</component>
2919
</children>
30-
</grid>
20+
</scrollpane>
3121
</children>
3222
</grid>
3323
</form>

0 commit comments

Comments
 (0)