mirror of https://gitee.com/makejava/EasyCode.git
优化模板编辑代码,修复已知BUG
This commit is contained in:
parent
9321b1bded
commit
70909e95c1
|
@ -7,7 +7,7 @@ package com.sjhy.plugin.entity;
|
|||
* @version 1.0.0
|
||||
* @date 2021/08/11 13:45
|
||||
*/
|
||||
public interface AbstractEditorItem {
|
||||
public interface AbstractEditorItem<T extends AbstractItem> extends AbstractItem<T> {
|
||||
/**
|
||||
* 更改文件名称
|
||||
*
|
||||
|
|
|
@ -15,7 +15,7 @@ import lombok.NoArgsConstructor;
|
|||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Template implements AbstractItem<Template>, AbstractEditorItem, Item {
|
||||
public class Template implements AbstractEditorItem<Template>, Item {
|
||||
/**
|
||||
* 模板名称
|
||||
*/
|
||||
|
|
|
@ -3,7 +3,6 @@ package com.sjhy.plugin.ui;
|
|||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.intellij.ide.fileTemplates.impl.UrlUtil;
|
||||
import com.intellij.openapi.options.Configurable;
|
||||
import com.intellij.openapi.options.ConfigurationException;
|
||||
import com.intellij.util.ExceptionUtil;
|
||||
import com.sjhy.plugin.dict.GlobalDict;
|
||||
import com.sjhy.plugin.dto.SettingsStorageDTO;
|
||||
|
@ -20,9 +19,7 @@ import javax.swing.*;
|
|||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author makejava
|
||||
|
@ -66,7 +63,7 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
|
|||
/**
|
||||
* 编辑列表框
|
||||
*/
|
||||
private EditListComponent editListComponent;
|
||||
private EditListComponent<Template> editListComponent;
|
||||
|
||||
|
||||
public TemplateSettingForm() {
|
||||
|
@ -85,30 +82,12 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
|
|||
}
|
||||
|
||||
private void initEditList() {
|
||||
BiConsumer<String, String> copyItemFun = (newName, oldName) -> {
|
||||
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);
|
||||
}
|
||||
Consumer<Template> switchItemFun = template -> {
|
||||
refreshUiVal();
|
||||
this.editListComponent.setCurrentItem(template.getName());
|
||||
editorComponent.setFile(template);
|
||||
};
|
||||
Consumer<String> createItemFun = name -> {
|
||||
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:");
|
||||
this.editListComponent = new EditListComponent<>(switchItemFun, "Template Name:", Template.class, this.currTemplateGroup.getElementList());
|
||||
}
|
||||
|
||||
private void initEditor() {
|
||||
|
@ -164,7 +143,7 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void apply() throws ConfigurationException {
|
||||
public void apply() {
|
||||
getSettingsStorage().setTemplateGroupMap(this.templateGroupMap);
|
||||
getSettingsStorage().setCurrTypeMapperGroupName(this.currTemplateGroup.getName());
|
||||
// 保存包后重新加载配置
|
||||
|
@ -177,7 +156,7 @@ public class TemplateSettingForm implements Configurable, BaseSettings {
|
|||
this.groupNameComponent.setCurrGroupName(this.currTemplateGroup.getName());
|
||||
}
|
||||
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) {
|
||||
this.editorComponent.setFile(null);
|
||||
|
|
|
@ -6,7 +6,9 @@ import com.intellij.openapi.ui.Messages;
|
|||
import com.intellij.ui.CollectionListModel;
|
||||
import com.intellij.ui.border.CustomLineBorder;
|
||||
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.ui.base.InputExistsValidator;
|
||||
import lombok.Getter;
|
||||
|
@ -14,10 +16,10 @@ import org.jetbrains.annotations.NotNull;
|
|||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 编辑列表组件
|
||||
|
@ -26,24 +28,11 @@ import java.util.function.Consumer;
|
|||
* @version 1.0.0
|
||||
* @date 2021/08/10 16:57
|
||||
*/
|
||||
public class EditListComponent<T extends AbstractGroup> {
|
||||
public class EditListComponent<E extends AbstractEditorItem<E>> {
|
||||
@Getter
|
||||
private JPanel mainPanel;
|
||||
/**
|
||||
* 复制分组,param1=新分组名 param2=旧分组名
|
||||
*/
|
||||
private BiConsumer<String, String> copyItemFun;
|
||||
|
||||
private Consumer<String> createItemFun;
|
||||
|
||||
private Consumer<String> deleteItemFun;
|
||||
|
||||
private Consumer<String> switchItemFun;
|
||||
/**
|
||||
* 所有列表项
|
||||
*/
|
||||
@Getter
|
||||
private List<String> itemList;
|
||||
private Consumer<E> switchItemFun;
|
||||
/**
|
||||
* 当前选中项
|
||||
*/
|
||||
|
@ -53,13 +42,20 @@ public class EditListComponent<T extends AbstractGroup> {
|
|||
|
||||
private JBList<String> jbList;
|
||||
|
||||
public EditListComponent(BiConsumer<String, String> copyItemFun, Consumer<String> createItemFun, Consumer<String> deleteItemFun, Consumer<String> switchItemFun, String label) {
|
||||
this.copyItemFun = copyItemFun;
|
||||
this.createItemFun = createItemFun;
|
||||
this.deleteItemFun = deleteItemFun;
|
||||
private Class<E> cls;
|
||||
|
||||
/**
|
||||
* 分组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.label = label;
|
||||
this.itemList = new ArrayList<>();
|
||||
this.cls = cls;
|
||||
this.elementList = elementList;
|
||||
this.init();
|
||||
}
|
||||
|
||||
|
@ -73,7 +69,7 @@ public class EditListComponent<T extends AbstractGroup> {
|
|||
}
|
||||
|
||||
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)) {
|
||||
return;
|
||||
}
|
||||
|
@ -84,12 +80,20 @@ public class EditListComponent<T extends AbstractGroup> {
|
|||
return new AnAction(AllIcons.Actions.Copy) {
|
||||
@Override
|
||||
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
|
||||
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) {
|
||||
@Override
|
||||
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) {
|
||||
@Override
|
||||
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) {
|
||||
@Override
|
||||
public void actionPerformed(@NotNull AnActionEvent e) {
|
||||
int index = itemList.indexOf(currentItem);
|
||||
E selectItem = findByName(currentItem);
|
||||
int index = elementList.indexOf(selectItem);
|
||||
if (index <= 0) {
|
||||
return;
|
||||
}
|
||||
String target = itemList.remove(index);
|
||||
itemList.add(index - 1, target);
|
||||
setItemList(itemList);
|
||||
setCurrentItem(currentItem);
|
||||
E target = elementList.remove(index);
|
||||
elementList.add(index - 1, target);
|
||||
switchItemFun.accept(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
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) {
|
||||
@Override
|
||||
public void actionPerformed(@NotNull AnActionEvent e) {
|
||||
int index = itemList.indexOf(currentItem);
|
||||
if (index < 0 || index >= itemList.size() - 1) {
|
||||
E selectItem = findByName(currentItem);
|
||||
int index = elementList.indexOf(selectItem);
|
||||
if (index < 0 || index >= elementList.size() - 1) {
|
||||
return;
|
||||
}
|
||||
String target = itemList.remove(index);
|
||||
itemList.add(index + 1, target);
|
||||
setItemList(itemList);
|
||||
setCurrentItem(currentItem);
|
||||
E target = elementList.remove(index);
|
||||
elementList.add(index + 1, target);
|
||||
switchItemFun.accept(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(@NotNull AnActionEvent e) {
|
||||
int index = itemList.indexOf(currentItem);
|
||||
e.getPresentation().setEnabled(index >= 0 && index < itemList.size() - 1);
|
||||
E selectItem = findByName(currentItem);
|
||||
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() {
|
||||
this.jbList = new JBList<>(itemList);
|
||||
this.jbList = new JBList<>(getAllItemName());
|
||||
this.mainPanel.add(this.jbList, BorderLayout.CENTER);
|
||||
this.jbList.addListSelectionListener(e -> {
|
||||
if (this.refresh) {
|
||||
return;
|
||||
}
|
||||
String selectedValue = jbList.getSelectedValue();
|
||||
if (StringUtils.isEmpty(selectedValue)) {
|
||||
return;
|
||||
}
|
||||
switchItemFun.accept(currentItem = selectedValue);
|
||||
this.currentItem = selectedValue;
|
||||
switchItemFun.accept(findByName(selectedValue));
|
||||
});
|
||||
}
|
||||
|
||||
public void setItemList(List<String> itemList) {
|
||||
this.itemList = itemList;
|
||||
if (StringUtils.isEmpty(this.currentItem) && itemList != null && itemList.size() > 0) {
|
||||
setCurrentItem(itemList.get(0));
|
||||
private E findByName(String name) {
|
||||
return this.elementList.stream().filter(item -> Objects.equals(item.fileName(), name)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.sjhy.plugin.ui.component;
|
||||
|
||||
import com.intellij.ide.IdeBundle;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
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.UIUtil;
|
||||
import com.sjhy.plugin.entity.AbstractEditorItem;
|
||||
import com.sjhy.plugin.tool.ProjectUtils;
|
||||
import lombok.Getter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
@ -82,8 +84,10 @@ public class EditorComponent<T extends AbstractEditorItem> {
|
|||
this.editor.getDocument().addDocumentListener(new DocumentListener() {
|
||||
@Override
|
||||
public void documentChanged(@NotNull DocumentEvent event) {
|
||||
if (file != null) {
|
||||
file.changeFileContent(editor.getDocument().getText());
|
||||
}
|
||||
}
|
||||
});
|
||||
// 初始化描述面板
|
||||
this.initRemarkPanel();
|
||||
|
@ -126,12 +130,14 @@ public class EditorComponent<T extends AbstractEditorItem> {
|
|||
private void refreshUI() {
|
||||
if (this.file == null) {
|
||||
((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"));
|
||||
} else {
|
||||
((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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue