优化模板编辑代码,修复已知BUG

This commit is contained in:
makejava 2021-08-11 16:33:53 +08:00
parent 9321b1bded
commit 70909e95c1
5 changed files with 113 additions and 84 deletions

View File

@ -7,7 +7,7 @@ package com.sjhy.plugin.entity;
* @version 1.0.0 * @version 1.0.0
* @date 2021/08/11 13:45 * @date 2021/08/11 13:45
*/ */
public interface AbstractEditorItem { public interface AbstractEditorItem<T extends AbstractItem> extends AbstractItem<T> {
/** /**
* 更改文件名称 * 更改文件名称
* *

View File

@ -15,7 +15,7 @@ import lombok.NoArgsConstructor;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class Template implements AbstractItem<Template>, AbstractEditorItem, Item { public class Template implements AbstractEditorItem<Template>, Item {
/** /**
* 模板名称 * 模板名称
*/ */

View File

@ -3,7 +3,6 @@ package com.sjhy.plugin.ui;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.intellij.ide.fileTemplates.impl.UrlUtil; import com.intellij.ide.fileTemplates.impl.UrlUtil;
import com.intellij.openapi.options.Configurable; import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.util.ExceptionUtil; import com.intellij.util.ExceptionUtil;
import com.sjhy.plugin.dict.GlobalDict; import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.dto.SettingsStorageDTO; import com.sjhy.plugin.dto.SettingsStorageDTO;
@ -20,9 +19,7 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors;
/** /**
* @author makejava * @author makejava
@ -66,7 +63,7 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
/** /**
* 编辑列表框 * 编辑列表框
*/ */
private EditListComponent editListComponent; private EditListComponent<Template> editListComponent;
public TemplateSettingForm() { public TemplateSettingForm() {
@ -85,30 +82,12 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
} }
private void initEditList() { private void initEditList() {
BiConsumer<String, String> copyItemFun = (newName, oldName) -> { Consumer<Template> switchItemFun = template -> {
Template template = currTemplateGroup.getElementList().stream().filter(item -> item.getName().equals(oldName)).findFirst().orElse(null);
template = CloneUtils.cloneByJson(template);
if (template != null) {
template.setName(newName);
currTemplateGroup.getElementList().add(template);
}
refreshUiVal(); refreshUiVal();
this.editListComponent.setCurrentItem(template.getName());
editorComponent.setFile(template);
}; };
Consumer<String> createItemFun = name -> { this.editListComponent = new EditListComponent<>(switchItemFun, "Template Name:", Template.class, this.currTemplateGroup.getElementList());
Template template = new Template(name, "");
currTemplateGroup.getElementList().add(template);
refreshUiVal();
};
Consumer<String> deleteItemFun = name -> {
Template template = currTemplateGroup.getElementList().stream().filter(item -> item.getName().equals(name)).findFirst().orElse(null);
currTemplateGroup.getElementList().remove(template);
refreshUiVal();
};
Consumer<String> switchItemFun = name -> {
editorComponent.setFile(currTemplateGroup.getElementList().stream().filter(item -> item.getName().equals(name)).findFirst().orElse(null));
};
this.editListComponent = new EditListComponent(copyItemFun, createItemFun, deleteItemFun, switchItemFun, "Template Name:");
} }
private void initEditor() { private void initEditor() {
@ -164,7 +143,7 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
} }
@Override @Override
public void apply() throws ConfigurationException { public void apply() {
getSettingsStorage().setTemplateGroupMap(this.templateGroupMap); getSettingsStorage().setTemplateGroupMap(this.templateGroupMap);
getSettingsStorage().setCurrTypeMapperGroupName(this.currTemplateGroup.getName()); getSettingsStorage().setCurrTypeMapperGroupName(this.currTemplateGroup.getName());
// 保存包后重新加载配置 // 保存包后重新加载配置
@ -177,7 +156,7 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
this.groupNameComponent.setCurrGroupName(this.currTemplateGroup.getName()); this.groupNameComponent.setCurrGroupName(this.currTemplateGroup.getName());
} }
if (this.editListComponent != null) { if (this.editListComponent != null) {
this.editListComponent.setItemList(this.currTemplateGroup.getElementList().stream().map(Template::getName).collect(Collectors.toList())); this.editListComponent.setElementList(this.currTemplateGroup.getElementList());
} }
if (this.editorComponent != null) { if (this.editorComponent != null) {
this.editorComponent.setFile(null); this.editorComponent.setFile(null);

View File

@ -6,7 +6,9 @@ import com.intellij.openapi.ui.Messages;
import com.intellij.ui.CollectionListModel; import com.intellij.ui.CollectionListModel;
import com.intellij.ui.border.CustomLineBorder; import com.intellij.ui.border.CustomLineBorder;
import com.intellij.ui.components.JBList; import com.intellij.ui.components.JBList;
import com.sjhy.plugin.entity.AbstractGroup; import com.sjhy.plugin.entity.AbstractEditorItem;
import com.sjhy.plugin.factory.AbstractItemFactory;
import com.sjhy.plugin.tool.CollectionUtil;
import com.sjhy.plugin.tool.StringUtils; import com.sjhy.plugin.tool.StringUtils;
import com.sjhy.plugin.ui.base.InputExistsValidator; import com.sjhy.plugin.ui.base.InputExistsValidator;
import lombok.Getter; import lombok.Getter;
@ -14,10 +16,10 @@ import org.jetbrains.annotations.NotNull;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.BiConsumer; import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors;
/** /**
* 编辑列表组件 * 编辑列表组件
@ -26,24 +28,11 @@ import java.util.function.Consumer;
* @version 1.0.0 * @version 1.0.0
* @date 2021/08/10 16:57 * @date 2021/08/10 16:57
*/ */
public class EditListComponent<T extends AbstractGroup> { public class EditListComponent<E extends AbstractEditorItem<E>> {
@Getter @Getter
private JPanel mainPanel; private JPanel mainPanel;
/**
* 复制分组param1=新分组名 param2=旧分组名
*/
private BiConsumer<String, String> copyItemFun;
private Consumer<String> createItemFun; private Consumer<E> switchItemFun;
private Consumer<String> deleteItemFun;
private Consumer<String> switchItemFun;
/**
* 所有列表项
*/
@Getter
private List<String> itemList;
/** /**
* 当前选中项 * 当前选中项
*/ */
@ -53,13 +42,20 @@ public class EditListComponent<T extends AbstractGroup> {
private JBList<String> jbList; private JBList<String> jbList;
public EditListComponent(BiConsumer<String, String> copyItemFun, Consumer<String> createItemFun, Consumer<String> deleteItemFun, Consumer<String> switchItemFun, String label) { private Class<E> cls;
this.copyItemFun = copyItemFun;
this.createItemFun = createItemFun; /**
this.deleteItemFun = deleteItemFun; * 分组Map
*/
private List<E> elementList;
private boolean refresh;
public EditListComponent(Consumer<E> switchItemFun, String label, Class<E> cls, List<E> elementList) {
this.switchItemFun = switchItemFun; this.switchItemFun = switchItemFun;
this.label = label; this.label = label;
this.itemList = new ArrayList<>(); this.cls = cls;
this.elementList = elementList;
this.init(); this.init();
} }
@ -73,7 +69,7 @@ public class EditListComponent<T extends AbstractGroup> {
} }
private void inputItemName(String initValue, Consumer<String> consumer) { private void inputItemName(String initValue, Consumer<String> consumer) {
String value = Messages.showInputDialog(label, "Input " + label, Messages.getQuestionIcon(), initValue, new InputExistsValidator(itemList)); String value = Messages.showInputDialog(label, "Input " + label, Messages.getQuestionIcon(), initValue, new InputExistsValidator(getAllItemName()));
if (StringUtils.isEmpty(value)) { if (StringUtils.isEmpty(value)) {
return; return;
} }
@ -84,12 +80,20 @@ public class EditListComponent<T extends AbstractGroup> {
return new AnAction(AllIcons.Actions.Copy) { return new AnAction(AllIcons.Actions.Copy) {
@Override @Override
public void actionPerformed(@NotNull AnActionEvent e) { public void actionPerformed(@NotNull AnActionEvent e) {
inputItemName(currentItem + "Copy", itemName -> copyItemFun.accept(itemName, currentItem)); inputItemName(jbList.getSelectedValue() + "Copy", itemName -> elementList.stream()
.filter(item -> Objects.equals(item.fileName(), jbList.getSelectedValue()))
.findFirst()
.ifPresent(item -> {
E cloneObj = item.cloneObj();
cloneObj.changeFileName(itemName);
elementList.add(cloneObj);
switchItemFun.accept(cloneObj);
}));
} }
@Override @Override
public void update(@NotNull AnActionEvent e) { public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(!StringUtils.isEmpty(jbList.getSelectedValue())); e.getPresentation().setEnabled(!CollectionUtil.isEmpty(elementList) && !StringUtils.isEmpty(currentItem));
} }
}; };
} }
@ -98,7 +102,12 @@ public class EditListComponent<T extends AbstractGroup> {
return new AnAction(AllIcons.General.Add) { return new AnAction(AllIcons.General.Add) {
@Override @Override
public void actionPerformed(@NotNull AnActionEvent e) { public void actionPerformed(@NotNull AnActionEvent e) {
inputItemName("demo", createItemFun); inputItemName("demo", itemName -> {
E defaultVal = AbstractItemFactory.createDefaultVal(cls);
defaultVal.changeFileName(itemName);
elementList.add(defaultVal);
switchItemFun.accept(defaultVal);
});
} }
}; };
} }
@ -107,7 +116,13 @@ public class EditListComponent<T extends AbstractGroup> {
return new AnAction(AllIcons.General.Remove) { return new AnAction(AllIcons.General.Remove) {
@Override @Override
public void actionPerformed(@NotNull AnActionEvent e) { public void actionPerformed(@NotNull AnActionEvent e) {
deleteItemFun.accept(currentItem); elementList.removeIf(item -> Objects.equals(item.fileName(), jbList.getSelectedValue()));
switchItemFun.accept(elementList.stream().findFirst().orElse(null));
}
@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(!CollectionUtil.isEmpty(elementList) && !StringUtils.isEmpty(currentItem));
} }
}; };
} }
@ -116,19 +131,21 @@ public class EditListComponent<T extends AbstractGroup> {
return new AnAction(AllIcons.Actions.MoveUp) { return new AnAction(AllIcons.Actions.MoveUp) {
@Override @Override
public void actionPerformed(@NotNull AnActionEvent e) { public void actionPerformed(@NotNull AnActionEvent e) {
int index = itemList.indexOf(currentItem); E selectItem = findByName(currentItem);
int index = elementList.indexOf(selectItem);
if (index <= 0) { if (index <= 0) {
return; return;
} }
String target = itemList.remove(index); E target = elementList.remove(index);
itemList.add(index - 1, target); elementList.add(index - 1, target);
setItemList(itemList); switchItemFun.accept(target);
setCurrentItem(currentItem);
} }
@Override @Override
public void update(@NotNull AnActionEvent e) { public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(itemList.indexOf(currentItem) > 0); E selectItem = findByName(currentItem);
boolean enabled = selectItem != null && elementList.indexOf(selectItem) > 0;
e.getPresentation().setEnabled(enabled);
} }
}; };
} }
@ -137,20 +154,21 @@ public class EditListComponent<T extends AbstractGroup> {
return new AnAction(AllIcons.Actions.MoveDown) { return new AnAction(AllIcons.Actions.MoveDown) {
@Override @Override
public void actionPerformed(@NotNull AnActionEvent e) { public void actionPerformed(@NotNull AnActionEvent e) {
int index = itemList.indexOf(currentItem); E selectItem = findByName(currentItem);
if (index < 0 || index >= itemList.size() - 1) { int index = elementList.indexOf(selectItem);
if (index < 0 || index >= elementList.size() - 1) {
return; return;
} }
String target = itemList.remove(index); E target = elementList.remove(index);
itemList.add(index + 1, target); elementList.add(index + 1, target);
setItemList(itemList); switchItemFun.accept(target);
setCurrentItem(currentItem);
} }
@Override @Override
public void update(@NotNull AnActionEvent e) { public void update(@NotNull AnActionEvent e) {
int index = itemList.indexOf(currentItem); E selectItem = findByName(currentItem);
e.getPresentation().setEnabled(index >= 0 && index < itemList.size() - 1); boolean enabled = selectItem != null && elementList.indexOf(selectItem) < elementList.size() - 1;
e.getPresentation().setEnabled(enabled);
} }
}; };
} }
@ -172,27 +190,53 @@ public class EditListComponent<T extends AbstractGroup> {
} }
private void initList() { private void initList() {
this.jbList = new JBList<>(itemList); this.jbList = new JBList<>(getAllItemName());
this.mainPanel.add(this.jbList, BorderLayout.CENTER); this.mainPanel.add(this.jbList, BorderLayout.CENTER);
this.jbList.addListSelectionListener(e -> { this.jbList.addListSelectionListener(e -> {
if (this.refresh) {
return;
}
String selectedValue = jbList.getSelectedValue(); String selectedValue = jbList.getSelectedValue();
if (StringUtils.isEmpty(selectedValue)) { if (StringUtils.isEmpty(selectedValue)) {
return; return;
} }
switchItemFun.accept(currentItem = selectedValue); this.currentItem = selectedValue;
switchItemFun.accept(findByName(selectedValue));
}); });
} }
public void setItemList(List<String> itemList) { private E findByName(String name) {
this.itemList = itemList; return this.elementList.stream().filter(item -> Objects.equals(item.fileName(), name)).findFirst().orElse(null);
if (StringUtils.isEmpty(this.currentItem) && itemList != null && itemList.size() > 0) { }
setCurrentItem(itemList.get(0));
private List<String> getAllItemName() {
return elementList.stream().map(AbstractEditorItem::fileName).collect(Collectors.toList());
}
public void setElementList(List<E> elementList) {
this.elementList = elementList;
try {
this.refresh = true;
this.jbList.setModel(new CollectionListModel<>(getAllItemName()));
} finally {
this.refresh = false;
}
if (StringUtils.isEmpty(this.currentItem) && elementList != null && elementList.size() > 0) {
setCurrentItem(elementList.get(0).fileName());
} }
this.jbList.setModel(new CollectionListModel<>(this.itemList));
} }
public void setCurrentItem(String currentItem) { public void setCurrentItem(String currentItem) {
this.currentItem = currentItem; this.currentItem = currentItem;
this.jbList.setSelectedIndex(this.itemList.indexOf(this.currentItem)); E element = findByName(this.currentItem);
int index = this.elementList.indexOf(element);
if (index >= 0) {
try {
this.refresh = true;
this.jbList.setSelectedIndex(index);
} finally {
this.refresh = false;
}
}
} }
} }

View File

@ -1,6 +1,7 @@
package com.sjhy.plugin.ui.component; package com.sjhy.plugin.ui.component;
import com.intellij.ide.IdeBundle; import com.intellij.ide.IdeBundle;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory; import com.intellij.openapi.editor.EditorFactory;
@ -17,6 +18,7 @@ import com.intellij.ui.SeparatorFactory;
import com.intellij.util.ui.JBUI; import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil; import com.intellij.util.ui.UIUtil;
import com.sjhy.plugin.entity.AbstractEditorItem; import com.sjhy.plugin.entity.AbstractEditorItem;
import com.sjhy.plugin.tool.ProjectUtils;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -82,7 +84,9 @@ public class EditorComponent<T extends AbstractEditorItem> {
this.editor.getDocument().addDocumentListener(new DocumentListener() { this.editor.getDocument().addDocumentListener(new DocumentListener() {
@Override @Override
public void documentChanged(@NotNull DocumentEvent event) { public void documentChanged(@NotNull DocumentEvent event) {
file.changeFileContent(editor.getDocument().getText()); if (file != null) {
file.changeFileContent(editor.getDocument().getText());
}
} }
}); });
// 初始化描述面板 // 初始化描述面板
@ -126,12 +130,14 @@ public class EditorComponent<T extends AbstractEditorItem> {
private void refreshUI() { private void refreshUI() {
if (this.file == null) { if (this.file == null) {
((EditorImpl)this.editor).setViewer(true); ((EditorImpl)this.editor).setViewer(true);
this.editor.getDocument().setText(""); // 重置文本内容
WriteCommandAction.runWriteCommandAction(ProjectUtils.getCurrProject(), () -> this.editor.getDocument().setText(""));
((EditorEx)editor).setHighlighter(EditorHighlighterFactory.getInstance().createEditorHighlighter(null, "demo.java.vm")); ((EditorEx)editor).setHighlighter(EditorHighlighterFactory.getInstance().createEditorHighlighter(null, "demo.java.vm"));
} else { } else {
((EditorImpl)this.editor).setViewer(false); ((EditorImpl)this.editor).setViewer(false);
this.editor.getDocument().setText(this.file.fileContent()); // 重置文本内容
((EditorEx)editor).setHighlighter(EditorHighlighterFactory.getInstance().createEditorHighlighter(null, this.file.fileName())); WriteCommandAction.runWriteCommandAction(ProjectUtils.getCurrProject(), () -> this.editor.getDocument().setText(this.file.fileContent()));
((EditorEx)editor).setHighlighter(EditorHighlighterFactory.getInstance().createEditorHighlighter(ProjectUtils.getCurrProject(), this.file.fileName()));
} }
} }
} }