优化部分界面,部分功能未完善。

This commit is contained in:
makejava 2018-08-13 19:49:56 +08:00
parent 28adab9b74
commit 444a7b5918
8 changed files with 254 additions and 96 deletions

View File

@ -5,7 +5,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.project.Project;
import com.sjhy.plugin.core.TemplateEditor;
import com.sjhy.plugin.ui.base.TemplateEditor;
import org.jetbrains.annotations.Nullable;
/**

View File

@ -0,0 +1,57 @@
package com.sjhy.plugin.config;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* 全局设置
*
* @author makejava
* @version 1.0.0
* @since 2018/08/09 15:00
*/
@Data
@State(name = "EasyCodeSetting", storages = @Storage("easy-code-setting.xml"))
public class Settings implements PersistentStateComponent<Settings> {
/**
* 获取实例对象
*/
public static Settings getInstance() {
return ServiceManager.getService(Settings.class);
}
/**
* 获取持久化状态信息
*
* @return 持久化状态信息
*/
@Nullable
@Override
public Settings getState() {
return this;
}
/**
* 读取系统中保存的持久化状态信息
*
* @param state 读取到的持久化状态信息
*/
@Override
public void loadState(@NotNull Settings state) {
XmlSerializerUtil.copyBean(state, this);
}
/**
* 未读取到持久化状态信息前进行初始化操作
*/
@Override
public void noStateLoaded() {
}
}

View File

@ -1,5 +1,6 @@
package com.sjhy.plugin.entity;
import com.sjhy.plugin.ui.base.Item;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -14,7 +15,7 @@ import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Template {
public class Template implements Item {
/**
* 模板名称
*/

View File

@ -1,23 +1,24 @@
package com.sjhy.plugin.ui;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.project.ProjectManager;
import com.sjhy.plugin.core.BaseGroupPanel;
import com.sjhy.plugin.core.BaseItemSelectPanel;
import com.sjhy.plugin.core.TemplateEditor;
import com.sjhy.plugin.entity.Template;
import com.sjhy.plugin.entity.TemplateGroup;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.tool.ConfigInfo;
import com.sjhy.plugin.ui.base.BaseGroupPanel;
import com.sjhy.plugin.ui.base.BaseItemSelectPanel;
import com.sjhy.plugin.ui.base.TemplateEditor;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
/**
* 模板编辑主面板
@ -26,50 +27,50 @@ import java.util.List;
* @version 1.0.0
* @since 2018/07/18 09:33
*/
public class TemplateSettingPanel extends AbstractGroupPanel<TemplateGroup, Template> implements Configurable {
public class TemplateSettingPanel implements Configurable {
/**
* 配置信息
*/
private ConfigInfo configInfo = ConfigInfo.getInstance();
private ConfigInfo configInfo;
/**
* 编辑框面板
*/
private TemplateEditor templateEditor;
/**
* 默认构造方法
* 基本的分组面板
*/
public TemplateSettingPanel() {
super(CloneUtils.getInstance().cloneMap(ConfigInfo.getInstance().getTemplateGroupMap()), ConfigInfo.getInstance().getCurrTemplateGroupName());
}
private Template item;
private BaseGroupPanel baseGroupPanel;
/**
* 切换模板编辑时
*
* @param itemPanel 面板对象
* @param item 模板对象
* 基本的元素选择面板
*/
@Override
protected void initItemPanel(JPanel itemPanel, Template item) {
// 如果编辑面板已经实例化需要选释放后再初始化
this.item = item;
}
private BaseItemSelectPanel<Template> baseItemSelectPanel;
@Override
protected String getItemName(Template item) {
return item.getName();
}
/**
* 当前分组
*/
private Map<String, TemplateGroup> group;
@Override
protected void setItemName(Template item, String itemName) {
item.setName(itemName);
}
/**
* 当前选中分组
*/
private String currGroupName;
@Override
protected Template createItem(String name) {
return new Template(name, "Demo!");
/**
* 克隆工具
*/
private CloneUtils cloneUtils;
public TemplateSettingPanel() {
// 配置服务实例化
this.configInfo = ConfigInfo.getInstance();
// 克隆工具实例化
this.cloneUtils = CloneUtils.getInstance();
// 克隆对象
this.currGroupName = this.configInfo.getCurrTemplateGroupName();
this.group = this.cloneUtils.cloneMap(this.configInfo.getTemplateGroupMap());
}
/**
@ -91,33 +92,11 @@ public class TemplateSettingPanel extends AbstractGroupPanel<TemplateGroup, Temp
@Nullable
@Override
public JComponent createComponent() {
// 如果编辑面板已经实例化需要选释放后再初始化
if (templateEditor == null) {
FileType velocityFileType = FileTypeManager.getInstance().getFileTypeByExtension("vm");
templateEditor = new TemplateEditor(ProjectManager.getInstance().getDefaultProject(), item.getName() + ".vm", item.getCode(), "描述", velocityFileType);
}
// com.sjhy.plugin.core.AbstractGroupPanel groupPanel = new com.sjhy.plugin.core.AbstractGroupPanel();
// return groupPanel.createComponent(templateEditor.createComponent());
BaseItemSelectPanel<String> baseItemSelectPanel = new BaseItemSelectPanel<String>(Arrays.asList("item1", "item2", "item3")) {
// 创建主面板
JPanel mainPanel = new JPanel(new BorderLayout());
@Override
protected void addItem(String name) {
}
@Override
protected void copyItem(String item) {
}
@Override
protected void deleteItem(List<String> itemList) {
}
};
baseItemSelectPanel.getRightPanel().add(templateEditor.createComponent(), BorderLayout.CENTER);
BaseGroupPanel groupPanel = new BaseGroupPanel(Arrays.asList("Default", "Mybatis Plus")) {
// 实例化分组面板
this.baseGroupPanel = new BaseGroupPanel(new ArrayList<>(group.keySet())) {
@Override
protected void createGroup(String name) {
@ -134,15 +113,55 @@ public class TemplateSettingPanel extends AbstractGroupPanel<TemplateGroup, Temp
}
@Override
protected void chageGroup(String name) {
protected void changeGroup(String name) {
}
};
JPanel result = new JPanel(new BorderLayout());
result.add(groupPanel, BorderLayout.NORTH);
result.add(baseItemSelectPanel, BorderLayout.CENTER);
return result;
// 创建元素选择面板
this.baseItemSelectPanel = new BaseItemSelectPanel<Template>(group.get(currGroupName).getElementList()) {
@Override
protected void addItem(String name) {
}
@Override
protected void copyItem(Template item) {
}
@Override
protected void deleteItem(Template item) {
}
@Override
protected void selectedItem(Template item) {
// 如果编辑面板已经实例化需要选释放后再初始化
if (templateEditor == null) {
FileType velocityFileType = FileTypeManager.getInstance().getFileTypeByExtension("vm");
templateEditor = new TemplateEditor(ProjectManager.getInstance().getDefaultProject(), item.getName() + ".vm", item.getCode(), "描述", velocityFileType);
// 代码修改回调
templateEditor.setCallback(() -> onUpdate());
baseItemSelectPanel.getRightPanel().add(templateEditor.createComponent(), BorderLayout.CENTER);
} else {
// 代码修改回调
templateEditor.setCallback(() -> onUpdate());
WriteAction.run(() -> templateEditor.getEditor().getDocument().setText(item.getCode()));
}
}
};
mainPanel.add(baseGroupPanel, BorderLayout.NORTH);
mainPanel.add(baseItemSelectPanel.getComponent(), BorderLayout.CENTER);
return mainPanel;
}
/**
* 数据发生修改时调用
*/
private void onUpdate() {
}
/**
@ -169,10 +188,12 @@ public class TemplateSettingPanel extends AbstractGroupPanel<TemplateGroup, Temp
*/
@Override
public void reset() {
if (!isModified()) {
return;
}
// 防止对象篡改需要进行克隆
super.group = cloneUtils.cloneMap(configInfo.getTemplateGroupMap());
super.currGroupName = configInfo.getCurrTemplateGroupName();
super.init();
this.group = cloneUtils.cloneMap(configInfo.getTemplateGroupMap());
this.currGroupName = configInfo.getCurrTemplateGroupName();
}
/**

View File

@ -1,4 +1,4 @@
package com.sjhy.plugin.core;
package com.sjhy.plugin.ui.base;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
@ -76,9 +76,10 @@ public abstract class BaseGroupPanel extends JPanel {
/**
* 切换分组
*
* @param name 分组名称
*/
protected abstract void chageGroup(String name);
protected abstract void changeGroup(String name);
/**
* 初始化方法

View File

@ -1,8 +1,7 @@
package com.sjhy.plugin.core;
package com.sjhy.plugin.ui.base;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.diff.impl.GenericDataProvider;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Splitter;
import com.intellij.ui.border.CustomLineBorder;
@ -10,10 +9,13 @@ import com.intellij.ui.components.JBList;
import com.intellij.util.ui.JBUI;
import com.sjhy.plugin.constants.MsgValue;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* 元素选择面板
@ -23,7 +25,7 @@ import java.util.List;
* @since 2018/08/12 22:26
*/
@Getter
public abstract class BaseItemSelectPanel<T> extends JPanel {
public abstract class BaseItemSelectPanel<T extends Item> {
/**
* 可选面板集合
*/
@ -42,36 +44,46 @@ public abstract class BaseItemSelectPanel<T> extends JPanel {
/**
* 列表面板
*/
private JBList<T> listPanel;
private JBList<String> listPanel;
public BaseItemSelectPanel(List<T> itemList) {
super(new BorderLayout());
public BaseItemSelectPanel(@NotNull List<T> itemList) {
this.itemList = itemList;
this.init();
}
/**
* 新增元素
*
* @param name 元素名称
*/
protected abstract void addItem(String name);
/**
* 复制元素
*
* @param item 元素对象
*/
protected abstract void copyItem(T item);
/**
* 删除多个元素
* @param itemList 元素对象列表
*
* @param item 元素对象
*/
protected abstract void deleteItem(List<T> itemList);
protected abstract void deleteItem(T item);
/**
* 初始化操作
* 选中元素
*
* @param item 元素对象
*/
private void init() {
protected abstract void selectedItem(T item);
/**
* 获取面板
*/
public JComponent getComponent() {
// 创建主面板
JPanel mainPanel = new JPanel(new BorderLayout());
// 左边的选择列表
this.leftPanel = new JPanel(new BorderLayout());
@ -91,7 +103,12 @@ public abstract class BaseItemSelectPanel<T> extends JPanel {
actionGroup.add(new AnAction(AllIcons.Actions.Copy) {
@Override
public void actionPerformed(AnActionEvent e) {
copyItem(null);
copyItem(getSelectedItem());
}
@Override
public void update(AnActionEvent e) {
e.getPresentation().setEnabled(getSelectedItem() != null);
}
});
// 删除事件
@ -100,13 +117,13 @@ public abstract class BaseItemSelectPanel<T> extends JPanel {
public void actionPerformed(AnActionEvent e) {
// 确认删除
if (MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, String.format(MsgValue.CONFIRM_DELETE_MESSAGE, "")).isYes()) {
deleteItem(null);
deleteItem(getSelectedItem());
}
}
@Override
public void update(AnActionEvent e) {
e.getPresentation().setEnabled(!listPanel.getSelectedValuesList().isEmpty());
e.getPresentation().setEnabled(getSelectedItem() != null);
}
});
@ -120,9 +137,17 @@ public abstract class BaseItemSelectPanel<T> extends JPanel {
leftPanel.add(actionToolbar.getComponent(), BorderLayout.NORTH);
// 元素列表
listPanel = new JBList<>(itemList);
listPanel = new JBList<>(dataConvert());
// 只能单选
listPanel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// 添加元素选中事件
listPanel.addListSelectionListener(e -> {
T item = getSelectedItem();
if (item == null) {
return;
}
selectedItem(item);
});
// 添加边框
listPanel.setBorder(new CustomLineBorder(0, 1, 1, 1));
@ -133,13 +158,49 @@ public abstract class BaseItemSelectPanel<T> extends JPanel {
this.rightPanel = new JPanel(new BorderLayout());
// 左右分割面板并添加至主面板
Splitter splitter = new Splitter(false, 0.3F);
Splitter splitter = new Splitter(false, 0.2F);
splitter.setFirstComponent(leftPanel);
splitter.setSecondComponent(rightPanel);
this.add(splitter, BorderLayout.CENTER);
mainPanel.add(splitter, BorderLayout.CENTER);
this.setPreferredSize(JBUI.size(400, 300));
mainPanel.setPreferredSize(JBUI.size(400, 300));
// 存在元素时默认选中第一个元素
if (!itemList.isEmpty()) {
listPanel.setSelectedIndex(0);
}
return mainPanel;
}
/**
* 数据转换
*
* @return 转换结果
*/
private List<String> dataConvert() {
List<String> data = new ArrayList<>();
itemList.forEach(item -> data.add(item.getName()));
return data;
}
/**
* 获取选中元素
*
* @return 选中元素
*/
private T getSelectedItem() {
String selectedName = listPanel.getSelectedValue();
if (selectedName == null) {
return null;
}
for (T t : itemList) {
if (Objects.equals(t.getName(), selectedName)) {
return t;
}
}
return null;
}
}

View File

@ -0,0 +1,17 @@
package com.sjhy.plugin.ui.base;
/**
* 元素接口
*
* @author makejava
* @version 1.0.0
* @since 2018/08/13 09:08
*/
public interface Item {
/**
* 获取元素名称
*
* @return 元素名称
*/
String getName();
}

View File

@ -1,4 +1,4 @@
package com.sjhy.plugin.core;
package com.sjhy.plugin.ui.base;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.editor.Document;
@ -23,6 +23,7 @@ import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
import java.util.Objects;
/**
* 模板编辑
@ -107,9 +108,10 @@ public class TemplateEditor {
editor.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void documentChanged(DocumentEvent event) {
String text = editor.getDocument().getText();
// 回调事件
if (callback != null) {
callback.call(editor.getDocument().getText());
if (callback != null && !Objects.equals(text, content)) {
callback.call();
}
}
});
@ -175,12 +177,10 @@ public class TemplateEditor {
/**
* 回调接口
*/
interface Callback {
public interface Callback {
/**
* 文档修改回调
*
* @param text 修改后的内容
*/
void call(String text);
void call();
}
}