Skip to content

Commit fa457a2

Browse files
author
dengzi
committed
file tree persist remove 'realName' field, support disable placeholder
1 parent bfd3613 commit fa457a2

8 files changed

Lines changed: 58 additions & 36 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class FileWriteCommand(private var kit: PluginKit, private var module: Module) :
4747
private fun createFileTree(treeNode: FileTreeNode, currentDirectory: VirtualFile) {
4848
Logger.i(TAG, "Create ${treeNode.getPath()}")
4949
if (treeNode.isDir) {
50-
val dir = kit.createDir(treeNode.name, currentDirectory)
50+
val dir = kit.createDir(treeNode.getRealName(), currentDirectory)
5151
if (dir == null) {
52-
Logger.e(TAG, "create directory failure: ${treeNode.name}")
52+
Logger.e(TAG, "create directory failure: ${treeNode.getRealName()}")
5353
return
5454
}
5555
treeNode.children.forEach {
@@ -58,15 +58,15 @@ class FileWriteCommand(private var kit: PluginKit, private var module: Module) :
5858
} else {
5959
if (treeNode.hasFileTemplate()) {
6060
val result = kit.createFileFromTemplate(
61-
treeNode.name,
61+
treeNode.getRealName(),
6262
treeNode.getTemplateName()!!,
6363
treeNode.placeholders.orEmpty(),
6464
currentDirectory)
6565
if (result == null) {
66-
Logger.e(TAG, "create file from template failed, file: ${treeNode.name} template:${treeNode.getTemplateName()}")
66+
Logger.e(TAG, "create file from template failed, file: ${treeNode.getRealName()} template:${treeNode.getTemplateName()}")
6767
}
6868
} else {
69-
kit.createFile(treeNode.name, currentDirectory)
69+
kit.createFile(treeNode.getRealName(), currentDirectory)
7070
}
7171
}
7272
}

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ import java.util.*
1818
</pre> */
1919
open class FileTreeNode private constructor() {
2020

21-
// the backing field is 'realName'
22-
var name: String
23-
get() = realName.replacePlaceholder(placeholders)
24-
set(value) {
25-
realName = value
26-
}
21+
var name: String = ""
2722

2823
var isDir = true
2924
var children
@@ -44,11 +39,12 @@ open class FileTreeNode private constructor() {
4439
get() = field ?: parent?.fileTemplates
4540

4641
private var realChildren = mutableSetOf<FileTreeNode>()
47-
// the origin name with original placeholder
48-
private var realName: String = ""
42+
4943
// the label composed by 'name' and 'isDir'.
50-
@Transient private val labeledChildren = mutableMapOf<String, FileTreeNode>()
51-
@Transient var parent: FileTreeNode? = null
44+
@Transient
45+
private val labeledChildren = mutableMapOf<String, FileTreeNode>()
46+
@Transient
47+
var parent: FileTreeNode? = null
5248

5349
companion object {
5450

@@ -107,7 +103,7 @@ open class FileTreeNode private constructor() {
107103

108104
fun hasChild(name: String, isDir: Boolean): Boolean {
109105
children.forEach {
110-
if (it.isDir == isDir && it.realName == name) {
106+
if (it.name == name && isDir == it.isDir) {
111107
return true
112108
}
113109
}
@@ -119,11 +115,10 @@ open class FileTreeNode private constructor() {
119115
}
120116

121117
/**
122-
* get the origin name without replace with placeholder
123-
* real name is the value when you set field 'name'
118+
* get the real name replace with placeholder
124119
*/
125120
fun getRealName(): String {
126-
return realName
121+
return name.replacePlaceholder(placeholders)
127122
}
128123

129124
fun fileTemplate(fileName: String, template: String) {
@@ -134,11 +129,11 @@ open class FileTreeNode private constructor() {
134129
}
135130

136131
fun hasFileTemplate(): Boolean {
137-
return template != null || fileTemplates?.containsKey(realName) == true
132+
return template != null || fileTemplates?.containsKey(name) == true
138133
}
139134

140135
fun getTemplateName(): String? {
141-
return template ?: fileTemplates?.get(realName)
136+
return template ?: fileTemplates?.get(name)
142137
}
143138

144139
fun placeholder(name: String, value: String) {
@@ -258,10 +253,10 @@ open class FileTreeNode private constructor() {
258253
* @return The intact path of current node
259254
*/
260255
fun getPath(): String {
261-
if (isRoot() || parent == null || parent!!.name == "") {
262-
return name
256+
if (isRoot() || parent == null || parent!!.getRealName() == "") {
257+
return getRealName()
263258
}
264-
return parent!!.getPath() + "/" + name
259+
return parent!!.getPath() + "/" + getRealName()
265260
}
266261

267262
fun isRoot(): Boolean {
@@ -306,7 +301,7 @@ open class FileTreeNode private constructor() {
306301
parent?.children?.first() -> "├─"
307302
else -> if (parent?.parent != null) "├─" else "┌─"
308303
})
309-
str.append(name).append("\n")
304+
str.append(getRealName()).append("\n")
310305

311306
if (!children.isNullOrEmpty()) {
312307
head.push(when {
@@ -342,7 +337,7 @@ open class FileTreeNode private constructor() {
342337
}
343338

344339
private fun getLabel(): String {
345-
return "name_$isDir"
340+
return "${name}_$isDir"
346341
}
347342

348343
override fun toString(): String {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
</component>
4545
</children>
4646
</grid>
47+
<component id="d7b5e" class="javax.swing.JCheckBox" binding="cbPlaceholder">
48+
<constraints/>
49+
<properties>
50+
<selected value="true"/>
51+
<text value="replace placeholder"/>
52+
</properties>
53+
</component>
4754
</children>
4855
</grid>
4956
<grid id="e3588" layout-manager="BorderLayout" hgap="0" vgap="0">

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class ConfigurePanel extends JPanel implements SearchableConfigurable {
3333
private JPanel panelPlaceholder;
3434
private JPanel panelFileTemp;
3535
private EditToolbar actionbar;
36+
private JCheckBox cbPlaceholder;
3637

3738
private List<Module> configs;
3839
private DefaultListModel<String> templateListModel;
@@ -80,6 +81,9 @@ private void initData() {
8081
return null;
8182
});
8283

84+
cbPlaceholder.addChangeListener(e -> {
85+
panelPreview.setReplacePlaceholder(cbPlaceholder.isSelected());
86+
});
8387
listTemplate.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
8488
listTemplate.addListSelectionListener(e -> {
8589
if (noSelectedConfig()) return;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public interface CreateFileCallback {
101101

102102
public static void main(String[] args) {
103103
CreateFileDialog.showForCreate(null, true, fileTreeNode -> {
104-
System.out.println("CreateFileDialog.main " + fileTreeNode.getName());
104+
105105
});
106106
}
107107
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
</component>
126126
</children>
127127
</grid>
128-
<grid id="d3c26" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
128+
<grid id="d3c26" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
129129
<margin top="0" left="0" bottom="0" right="0"/>
130130
<constraints>
131131
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -144,12 +144,21 @@
144144
</component>
145145
<grid id="7aa5e" binding="panelPlaceholder" layout-manager="BorderLayout" hgap="0" vgap="0">
146146
<constraints>
147-
<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"/>
147+
<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"/>
148148
</constraints>
149149
<properties/>
150150
<border type="none"/>
151151
<children/>
152152
</grid>
153+
<component id="6106a" class="javax.swing.JLabel">
154+
<constraints>
155+
<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"/>
156+
</constraints>
157+
<properties>
158+
<enabled value="false"/>
159+
<text value="Only the value of the root node is listed"/>
160+
</properties>
161+
</component>
153162
</children>
154163
</grid>
155164
<vspacer id="ed063">

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private void initDialog() {
132132
rootPanel.registerKeyboardAction(e -> dispose(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
133133
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
134134

135-
tablePlaceholder = new EditableTable(new String[]{"Placeholder", "Value"}, new Boolean[]{false, true});
135+
tablePlaceholder = new EditableTable(new String[]{"Key", "Value"}, new Boolean[]{false, true});
136136
tablePlaceholder.setToolBarVisible(false);
137137
panelPlaceholder.add(tablePlaceholder, BorderLayout.CENTER);
138138
cbLanguage.setModel(new DefaultComboBoxModel<>(Module.Companion.getLangList()));
@@ -166,7 +166,9 @@ private void initData() {
166166
List<String> temp = new ArrayList<>();
167167
moduleTemplates.forEach(i -> temp.add(i.getTemplateName()));
168168
cbModuleType.setModel(new DefaultComboBoxModel<>(temp.toArray()));
169-
cbModuleType.setSelectedIndex(1);
169+
if (cbModuleType.getModel().getSize() > 0){
170+
cbModuleType.setSelectedIndex(0);
171+
}
170172
}
171173

172174
public interface OnFinishListener {

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
import org.jetbrains.annotations.NotNull;
1414

1515
import javax.swing.*;
16-
import javax.swing.tree.DefaultMutableTreeNode;
17-
import javax.swing.tree.TreeCellRenderer;
18-
import javax.swing.tree.TreeNode;
19-
import javax.swing.tree.TreePath;
16+
import javax.swing.tree.*;
2017
import java.awt.*;
2118
import java.awt.event.ActionListener;
2219
import java.awt.event.MouseAdapter;
@@ -42,6 +39,7 @@ public class PreviewPanel extends JPanel {
4239
private Tree fileTree;
4340

4441
private Map<String, Icon> fileIconMap = new HashMap<>();
42+
private boolean replacePlaceholder = true;
4543

4644
// render tree node icon, title
4745
private TreeCellRenderer treeCellRenderer = new ColoredTreeCellRenderer() {
@@ -52,7 +50,7 @@ public void customizeCellRenderer(@NotNull JTree jTree, Object value, boolean b,
5250
if (object instanceof FileTreeNode) {
5351
FileTreeNode node = (FileTreeNode) object;
5452
setIcon(IconUtil.getAddClassIcon());
55-
this.append(node.getName());
53+
this.append(replacePlaceholder ? node.getRealName() : node.getName());
5654
if (node.isDir()) {
5755
setIcon(AllIcons.Nodes.Package);
5856
} else {
@@ -75,6 +73,13 @@ public void customizeCellRenderer(@NotNull JTree jTree, Object value, boolean b,
7573
initPanel();
7674
}
7775

76+
public void setReplacePlaceholder(boolean replace){
77+
if (replace != replacePlaceholder){
78+
replacePlaceholder = replace;
79+
fileTree.updateUI();
80+
}
81+
}
82+
7883
public void setModuleConfig(Module module) {
7984
Logger.INSTANCE.i("PreviewPanel", "setModuleConfig");
8085

0 commit comments

Comments
 (0)