mirror of https://gitee.com/makejava/EasyCode.git
添加了数据库表配置界面,以及部分操作功能。
This commit is contained in:
parent
61effd1e00
commit
f1064f4aa7
|
@ -1,8 +1,8 @@
|
|||
package com.sjhy.plugin.action;
|
||||
|
||||
import com.intellij.openapi.actionSystem.ActionManager;
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.sjhy.plugin.ui.ConfigTableDialog;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ConfigAction extends AnAction {
|
||||
|
@ -12,6 +12,6 @@ public class ConfigAction extends AnAction {
|
|||
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent anActionEvent) {
|
||||
|
||||
new ConfigTableDialog().open();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package com.sjhy.plugin.action;
|
|||
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.sjhy.plugin.tool.CacheDataUtils;
|
||||
import com.sjhy.plugin.ui.SelectSavePath;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
|
@ -11,8 +10,6 @@ public class MainAction extends AnAction {
|
|||
super(text);
|
||||
}
|
||||
|
||||
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
|
||||
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
//开始处理
|
||||
|
|
|
@ -120,7 +120,7 @@ public class ConfigServiceImpl implements ConfigService {
|
|||
}
|
||||
ColumnConfigGroup columnConfigGroup = new ColumnConfigGroup();
|
||||
List<ColumnConfig> columnConfigList = new ArrayList<>();
|
||||
columnConfigList.add(new ColumnConfig("required", ColumnConfigType.BOOLEAN));
|
||||
columnConfigList.add(new ColumnConfig("disable", ColumnConfigType.BOOLEAN));
|
||||
columnConfigGroup.setName(DEFAULT_NAME);
|
||||
columnConfigGroup.setElementList(columnConfigList);
|
||||
columnConfigGroupMap.put(DEFAULT_NAME, columnConfigGroup);
|
||||
|
|
|
@ -0,0 +1,103 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.sjhy.plugin.ui.ConfigTableDialog">
|
||||
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="10" left="10" bottom="10" right="10"/>
|
||||
<constraints>
|
||||
<xy x="48" y="54" width="436" height="297"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<hspacer id="98af6">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="OK"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Cancel"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
<grid id="e3588" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<scrollpane id="48c8f">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="caf2" class="javax.swing.JTable" binding="table">
|
||||
<constraints/>
|
||||
<properties/>
|
||||
</component>
|
||||
</children>
|
||||
</scrollpane>
|
||||
<toolbar id="610ba">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="-1" height="20"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<borderPainted value="false"/>
|
||||
<floatable value="false"/>
|
||||
<orientation value="1"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="3f668" class="javax.swing.JButton" binding="addButton">
|
||||
<constraints/>
|
||||
<properties>
|
||||
<icon value="general/add.png"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</toolbar>
|
||||
<vspacer id="77c1b">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
|
@ -0,0 +1,183 @@
|
|||
package com.sjhy.plugin.ui;
|
||||
|
||||
import com.intellij.ui.BooleanTableCellEditor;
|
||||
import com.intellij.util.ui.ComboBoxCellEditor;
|
||||
import com.sjhy.plugin.entity.*;
|
||||
import com.sjhy.plugin.service.ConfigService;
|
||||
import com.sjhy.plugin.tool.CacheDataUtils;
|
||||
import com.sjhy.plugin.tool.TableInfoUtils;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import javax.swing.table.TableColumn;
|
||||
import java.awt.event.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class ConfigTableDialog extends JDialog {
|
||||
private JPanel contentPane;
|
||||
private JButton buttonOK;
|
||||
private JButton buttonCancel;
|
||||
private JTable table;
|
||||
private JButton addButton;
|
||||
|
||||
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
|
||||
|
||||
private TableInfoUtils tableInfoUtils = TableInfoUtils.getInstance();
|
||||
|
||||
private DefaultTableModel tableModel;
|
||||
|
||||
private List<ColumnConfig> columnConfigList;
|
||||
|
||||
private TableInfo tableInfo;
|
||||
|
||||
private boolean initFlag;
|
||||
|
||||
public ConfigTableDialog() {
|
||||
setContentPane(contentPane);
|
||||
setModal(true);
|
||||
getRootPane().setDefaultButton(buttonOK);
|
||||
|
||||
buttonOK.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
onOK();
|
||||
}
|
||||
});
|
||||
|
||||
buttonCancel.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
onCancel();
|
||||
}
|
||||
});
|
||||
|
||||
// call onCancel() when cross is clicked
|
||||
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
||||
addWindowListener(new WindowAdapter() {
|
||||
public void windowClosing(WindowEvent e) {
|
||||
onCancel();
|
||||
}
|
||||
});
|
||||
|
||||
// call onCancel() on ESCAPE
|
||||
contentPane.registerKeyboardAction(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
onCancel();
|
||||
}
|
||||
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
|
||||
|
||||
init();
|
||||
initEvent();
|
||||
}
|
||||
|
||||
private void onOK() {
|
||||
// add your code here
|
||||
dispose();
|
||||
}
|
||||
|
||||
private void onCancel() {
|
||||
// add your code here if necessary
|
||||
dispose();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
initFlag = false;
|
||||
ConfigService configService = ConfigService.getInstance();
|
||||
ColumnConfigGroup columnConfigGroup = configService.getColumnConfigGroupMap().get(configService.getCurrColumnConfigGroupName());
|
||||
columnConfigList = getInitColumn(columnConfigGroup.getElementList());
|
||||
//绑定数据
|
||||
tableInfo = tableInfoUtils.handler(Collections.singletonList(cacheDataUtils.getSelectDbTable())).get(0);
|
||||
|
||||
refresh();
|
||||
|
||||
initFlag = true;
|
||||
}
|
||||
|
||||
private void refresh() {
|
||||
tableModel = new DefaultTableModel();
|
||||
columnConfigList.forEach(columnConfig -> {
|
||||
tableModel.addColumn(columnConfig.getTitle());
|
||||
});
|
||||
//追加数据
|
||||
tableInfo.getFullColumn().forEach(columnInfo -> {
|
||||
tableModel.addRow(new Object[]{columnInfo.getName(), columnInfo.getType(), columnInfo.getComment()});
|
||||
});
|
||||
table.setModel(tableModel);
|
||||
//刷新列编辑器
|
||||
refreshColumnEditor(columnConfigList);
|
||||
}
|
||||
|
||||
private void initEvent() {
|
||||
//添加元素事件
|
||||
addButton.addActionListener(e -> {
|
||||
if (!initFlag) {
|
||||
return;
|
||||
}
|
||||
String value = JOptionPane.showInputDialog(null, "Input Column Name:", "Demo");
|
||||
if (value==null) {
|
||||
return;
|
||||
}
|
||||
if (value.trim().length()==0){
|
||||
JOptionPane.showMessageDialog(null, "Column Name Can't Is Empty!");
|
||||
return;
|
||||
}
|
||||
|
||||
for (ColumnInfo columnInfo : tableInfo.getFullColumn()) {
|
||||
if (columnInfo.getName().equals(value)){
|
||||
JOptionPane.showMessageDialog(null, "Column Name Already exist!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
ColumnInfo columnInfo = new ColumnInfo();
|
||||
columnInfo.setName(value);
|
||||
columnInfo.setType("java.lang.String");
|
||||
tableInfo.getFullColumn().add(columnInfo);
|
||||
refresh();
|
||||
});
|
||||
}
|
||||
|
||||
private void refreshColumnEditor(List<ColumnConfig> columnConfigList) {
|
||||
columnConfigList.forEach(columnConfig -> {
|
||||
TableColumn tableColumn = table.getColumn(columnConfig.getTitle());
|
||||
int index = tableColumn.getModelIndex();
|
||||
switch (columnConfig.getType()) {
|
||||
case TEXT:
|
||||
break;
|
||||
case SELECT:
|
||||
tableColumn.setCellEditor(new ComboBoxCellEditor() {
|
||||
@Override
|
||||
protected List<String> getComboBoxItems() {
|
||||
return Arrays.asList(columnConfig.getSelectValue().split(","));
|
||||
}
|
||||
});
|
||||
break;
|
||||
case BOOLEAN:
|
||||
//给空列赋初始值
|
||||
for (int row=0; row< tableModel.getRowCount(); row++) {
|
||||
if (tableModel.getValueAt(row, index)==null){
|
||||
tableModel.setValueAt(false, row, index);
|
||||
}
|
||||
}
|
||||
tableColumn.setCellEditor(new BooleanTableCellEditor());
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private List<ColumnConfig> getInitColumn(List<ColumnConfig> columnConfigList) {
|
||||
List<ColumnConfig> result = new ArrayList<>();
|
||||
result.add(new ColumnConfig("name", ColumnConfigType.TEXT));
|
||||
result.add(new ColumnConfig("type", ColumnConfigType.TEXT));
|
||||
result.add(new ColumnConfig("comment", ColumnConfigType.TEXT));
|
||||
result.addAll(columnConfigList);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void open() {
|
||||
setTitle("Config Table "+cacheDataUtils.getSelectDbTable().getName());
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue