Skip to content

Commit 85faf6f

Browse files
author
dengzi
committed
optimize FileTreeNode placeholder
1 parent fa457a2 commit 85faf6f

7 files changed

Lines changed: 28 additions & 11 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ object Config {
2424
private const val KEY_TEMPLATES = "KEY_TEMPLATES"
2525
private const val KEY_INIT = "KEY_INIT"
2626

27-
private val GSON by lazy { GsonBuilder().setLenient().serializeNulls().create() }
27+
private val GSON by lazy { GsonBuilder().setLenient().create() }
2828
private val STORE by lazy { PropertiesComponent.getInstance() }
2929

3030
private val AUC_MODULE_TEMPLATES = listOf(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class FileWriteCommand(private var kit: PluginKit, private var module: Module) :
3838
return
3939
}
4040
val fileTreeNode = module.template
41-
Logger.d(TAG, fileTreeNode.placeholders.toString())
41+
Logger.d(TAG, fileTreeNode.getPlaceholderInherit().toString())
4242
fileTreeNode.children.forEach {
4343
createFileTree(it, current)
4444
}
@@ -60,7 +60,7 @@ class FileWriteCommand(private var kit: PluginKit, private var module: Module) :
6060
val result = kit.createFileFromTemplate(
6161
treeNode.getRealName(),
6262
treeNode.getTemplateName()!!,
63-
treeNode.placeholders.orEmpty(),
63+
treeNode.getPlaceholderInherit().orEmpty(),
6464
currentDirectory)
6565
if (result == null) {
6666
Logger.e(TAG, "create file from template failed, file: ${treeNode.getRealName()} template:${treeNode.getTemplateName()}")

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ open class FileTreeNode private constructor() {
2929
get() = realChildren
3030

3131
var placeholders: MutableMap<String, String>? = null
32-
get() = field ?: parent?.placeholders
3332

3433
// template for node, higher priority than fileTemplates
35-
var template: String? = null
34+
private var template: String? = null
3635

3736
// template of filename
3837
var fileTemplates: MutableMap<String, String>? = null
39-
get() = field ?: parent?.fileTemplates
4038

4139
private var realChildren = mutableSetOf<FileTreeNode>()
4240

@@ -118,7 +116,15 @@ open class FileTreeNode private constructor() {
118116
* get the real name replace with placeholder
119117
*/
120118
fun getRealName(): String {
121-
return name.replacePlaceholder(placeholders)
119+
return name.replacePlaceholder(getPlaceholderInherit())
120+
}
121+
122+
fun getFileTemplateInherit(): MutableMap<String, String>? {
123+
return fileTemplates ?: parent?.getFileTemplateInherit()
124+
}
125+
126+
fun getPlaceholderInherit(): MutableMap<String, String>? {
127+
return placeholders ?: parent?.getPlaceholderInherit()
122128
}
123129

124130
fun fileTemplate(fileName: String, template: String) {
@@ -129,11 +135,11 @@ open class FileTreeNode private constructor() {
129135
}
130136

131137
fun hasFileTemplate(): Boolean {
132-
return template != null || fileTemplates?.containsKey(name) == true
138+
return template != null || getFileTemplateInherit()?.containsKey(name) == true
133139
}
134140

135141
fun getTemplateName(): String? {
136-
return template ?: fileTemplates?.get(name)
142+
return template ?: getFileTemplateInherit()?.get(name)
137143
}
138144

139145
fun placeholder(name: String, value: String) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
<properties/>
105105
<border type="none"/>
106106
<children>
107-
<tabbedpane id="e9ea8" class="com.intellij.ui.components.JBTabbedPane">
107+
<tabbedpane id="e9ea8" class="com.intellij.ui.components.JBTabbedPane" binding="tabbedPane">
108108
<constraints>
109109
<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"/>
110110
</constraints>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.intellij.openapi.options.SearchableConfigurable;
66
import com.intellij.ui.DocumentAdapter;
77
import com.intellij.ui.components.JBList;
8+
import com.intellij.ui.components.JBTabbedPane;
89
import org.jetbrains.annotations.Nls;
910
import org.jetbrains.annotations.NotNull;
1011
import org.jetbrains.annotations.Nullable;
@@ -34,6 +35,7 @@ public class ConfigurePanel extends JPanel implements SearchableConfigurable {
3435
private JPanel panelFileTemp;
3536
private EditToolbar actionbar;
3637
private JCheckBox cbPlaceholder;
38+
private JBTabbedPane tabbedPane;
3739

3840
private List<Module> configs;
3941
private DefaultListModel<String> templateListModel;
@@ -84,6 +86,7 @@ private void initData() {
8486
cbPlaceholder.addChangeListener(e -> {
8587
panelPreview.setReplacePlaceholder(cbPlaceholder.isSelected());
8688
});
89+
tabbedPane.addChangeListener(e -> onChangeTab());
8790
listTemplate.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
8891
listTemplate.addListSelectionListener(e -> {
8992
if (noSelectedConfig()) return;
@@ -104,6 +107,13 @@ protected void textChanged(@NotNull DocumentEvent documentEvent) {
104107
}
105108
}
106109

110+
private void onChangeTab() {
111+
if (0 == tabbedPane.getSelectedIndex()) {
112+
currentConfig.getTemplate().setPlaceholders(tablePlaceholder.getPairResult());
113+
}
114+
panelPreview.setModuleConfig(currentConfig);
115+
}
116+
107117
private void onAddConfig() {
108118
Module newConfig = Config.INSTANCE.getTEMPLATE_ANDROID_APPLICATION().clone();
109119
configs.add(newConfig);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void initDialog() {
6363
setLocation(x, y);
6464
setPreferredSize(new Dimension(w, h));
6565

66-
setTitle((isRename() ? "New " : "Rename ") + (isDir ? "Directory" : "File"));
66+
setTitle((isRename() ? "Rename " : "New ") + (isDir ? "Directory" : "File"));
6767
if (isRename()) {
6868
textField1.setText(current.getRealName());
6969
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public void setModuleConfig(Module module) {
8585

8686
fileTree.setModel(getTreeModel(module.getTemplate()));
8787
fileTree.doLayout();
88+
fileTree.updateUI();
8889
expandAll(fileTree, new TreePath(fileTree.getModel().getRoot()), true);
8990
}
9091

0 commit comments

Comments
 (0)