Skip to content

Commit 0e3c90b

Browse files
committed
支持字段类型转换配置
1 parent 7ab14ad commit 0e3c90b

File tree

4 files changed

+267
-10
lines changed

4 files changed

+267
-10
lines changed

src/main/java/com/king/java/field/plugin/dialog/GenerateFieldDialog.form

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.king.java.field.plugin.dialog.GenerateFieldDialog">
3-
<grid id="cbd77" binding="panel" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="cbd77" binding="panel" layout-manager="GridLayoutManager" row-count="6" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="5" left="10" bottom="10" right="10"/>
55
<constraints>
6-
<xy x="48" y="54" width="688" height="425"/>
6+
<xy x="48" y="54" width="708" height="425"/>
77
</constraints>
88
<properties/>
99
<border type="none"/>
1010
<children>
1111
<component id="d8816" class="javax.swing.JLabel">
1212
<constraints>
13-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
13+
<grid row="0" column="0" row-span="1" col-span="5" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
1414
</constraints>
1515
<properties>
1616
<text value="Generate Field By String:"/>
1717
</properties>
1818
</component>
1919
<scrollpane id="ea7ad">
2020
<constraints>
21-
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false">
21+
<grid row="1" column="0" row-span="1" col-span="6" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false">
2222
<preferred-size width="400" height="200"/>
2323
</grid>
2424
</constraints>
@@ -34,7 +34,7 @@
3434
<grid id="43bd" layout-manager="GridLayoutManager" row-count="1" column-count="8" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3535
<margin top="0" left="0" bottom="0" right="0"/>
3636
<constraints>
37-
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
37+
<grid row="2" column="0" row-span="1" col-span="6" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
3838
</constraints>
3939
<properties/>
4040
<border type="none"/>
@@ -108,7 +108,7 @@
108108
<grid id="d3a44" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
109109
<margin top="0" left="0" bottom="0" right="0"/>
110110
<constraints>
111-
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
111+
<grid row="3" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
112112
</constraints>
113113
<properties/>
114114
<border type="none"/>
@@ -152,7 +152,7 @@
152152
<grid id="cd8f8" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
153153
<margin top="0" left="0" bottom="0" right="0"/>
154154
<constraints>
155-
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
155+
<grid row="4" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
156156
</constraints>
157157
<properties/>
158158
<border type="none"/>
@@ -195,7 +195,7 @@
195195
<grid id="82b9a" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
196196
<margin top="0" left="0" bottom="0" right="0"/>
197197
<constraints>
198-
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="4" fill="2" indent="0" use-parent-layout="false"/>
198+
<grid row="5" column="4" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="4" fill="2" indent="0" use-parent-layout="false"/>
199199
</constraints>
200200
<properties/>
201201
<border type="none"/>
@@ -205,9 +205,12 @@
205205
<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"/>
206206
</constraints>
207207
<properties>
208+
<autoscrolls value="false"/>
208209
<background color="-12828863"/>
209210
<foreground color="-4473925"/>
210211
<hideActionText value="true"/>
212+
<inheritsPopupMenu value="false"/>
213+
<selected value="false"/>
211214
<text value="Generate"/>
212215
</properties>
213216
</component>
@@ -221,6 +224,17 @@
221224
</component>
222225
</children>
223226
</grid>
227+
<component id="99e39" class="javax.swing.JButton" binding="btnSetting">
228+
<constraints>
229+
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
230+
</constraints>
231+
<properties>
232+
<background color="-12828863"/>
233+
<foreground color="-4473925"/>
234+
<hideActionText value="true"/>
235+
<text value="Setting"/>
236+
</properties>
237+
</component>
224238
</children>
225239
</grid>
226240
</form>

src/main/java/com/king/java/field/plugin/dialog/GenerateFieldDialog.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 Jenly Yu
2+
* Copyright (C) 2020 Jenly Yu, https://github.com/jenly1314/JavaFieldGenerator
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package com.king.java.field.plugin.dialog;
1717

1818
import com.intellij.openapi.ui.Messages;
19+
import com.intellij.openapi.wm.WindowManager;
1920
import com.king.java.field.plugin.component.ConfigComponent;
2021
import com.king.java.field.plugin.entity.FieldParseConfig;
2122
import org.apache.commons.lang.StringUtils;
@@ -34,7 +35,7 @@ public class GenerateFieldDialog extends JDialog {
3435

3536
private JPanel panel;
3637
private JTextArea textArea;
37-
private JButton btnCancel, btnGenerate;
38+
private JButton btnCancel, btnGenerate, btnSetting;
3839
private OnClickListener onClickListener;
3940
private JRadioButton rbPublic, rbProtected, rbPrivate;
4041
private JCheckBox cbGetAndSet;
@@ -87,6 +88,9 @@ public GenerateFieldDialog() {
8788
setContentPane(panel);
8889
setModal(true);
8990
setTitle("Java Field");
91+
92+
getRootPane().setDefaultButton(btnGenerate);
93+
9094
fieldParseConfig = ConfigComponent.getInstance().getFieldParseConfig();
9195
modifier = ofValue(fieldParseConfig.getFieldModifier());
9296
switch (modifier) {
@@ -168,6 +172,13 @@ public GenerateFieldDialog() {
168172
}
169173
dispose();
170174
});
175+
btnSetting.addActionListener(l -> {
176+
SettingFieldTypeDialog settingFieldTypeDialog = new SettingFieldTypeDialog();
177+
// 自动调整对话框大小
178+
settingFieldTypeDialog.pack();
179+
settingFieldTypeDialog.setLocationRelativeTo(this);
180+
settingFieldTypeDialog.setVisible(true);
181+
});
171182

172183
spField.setModel(createSpinnerNumberModel());
173184
spFieldType.setModel(createSpinnerNumberModel());
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.king.java.field.plugin.dialog.SettingFieldTypeDialog">
3+
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4+
<margin top="10" left="10" bottom="10" right="10"/>
5+
<constraints>
6+
<xy x="48" y="54" width="688" height="492"/>
7+
</constraints>
8+
<properties>
9+
<minimumSize width="500" height="404"/>
10+
<preferredSize width="500" height="404"/>
11+
</properties>
12+
<border type="none"/>
13+
<children>
14+
<grid id="94766" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
15+
<margin top="0" left="0" bottom="0" right="0"/>
16+
<constraints>
17+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
18+
</constraints>
19+
<properties/>
20+
<border type="none"/>
21+
<children>
22+
<hspacer id="98af6">
23+
<constraints>
24+
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
25+
</constraints>
26+
</hspacer>
27+
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
28+
<margin top="0" left="0" bottom="0" right="0"/>
29+
<constraints>
30+
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
31+
</constraints>
32+
<properties/>
33+
<border type="none"/>
34+
<children>
35+
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
36+
<constraints>
37+
<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"/>
38+
</constraints>
39+
<properties>
40+
<text value="OK"/>
41+
</properties>
42+
</component>
43+
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
44+
<constraints>
45+
<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"/>
46+
</constraints>
47+
<properties>
48+
<text value="Cancel"/>
49+
</properties>
50+
</component>
51+
</children>
52+
</grid>
53+
<component id="7a13b" class="javax.swing.JCheckBox" binding="cbArrayToList">
54+
<constraints>
55+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
56+
</constraints>
57+
<properties>
58+
<selected value="true"/>
59+
<text value="Array to List (T[] -&gt; List&lt;T&gt;)"/>
60+
</properties>
61+
</component>
62+
<component id="6385" class="javax.swing.JLabel">
63+
<constraints>
64+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
65+
</constraints>
66+
<properties>
67+
<text value="Field Type:"/>
68+
</properties>
69+
</component>
70+
</children>
71+
</grid>
72+
<grid id="e3588" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
73+
<margin top="0" left="0" bottom="0" right="0"/>
74+
<constraints>
75+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
76+
</constraints>
77+
<properties/>
78+
<border type="none"/>
79+
<children>
80+
<component id="49954" class="javax.swing.JTextArea" binding="textArea">
81+
<constraints>
82+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
83+
<preferred-size width="150" height="50"/>
84+
</grid>
85+
</constraints>
86+
<properties/>
87+
</component>
88+
</children>
89+
</grid>
90+
<component id="26b0c" class="javax.swing.JLabel">
91+
<constraints>
92+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
93+
</constraints>
94+
<properties>
95+
<text value="Configuration (Example: string = String)"/>
96+
</properties>
97+
</component>
98+
</children>
99+
</grid>
100+
</form>
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (C) 2020 Jenly Yu, https://github.com/jenly1314/JavaFieldGenerator
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.king.java.field.plugin.dialog;
17+
18+
import com.king.java.field.plugin.component.ConfigComponent;
19+
import org.apache.commons.lang.StringUtils;
20+
21+
import javax.swing.*;
22+
import javax.swing.event.ChangeEvent;
23+
import javax.swing.event.ChangeListener;
24+
import java.awt.event.*;
25+
import java.util.HashMap;
26+
import java.util.LinkedHashMap;
27+
import java.util.Map;
28+
29+
/**
30+
* 字段类型配置模板对话框
31+
*
32+
* @author <a href="mailto:[email protected]">Jenly</a>
33+
*/
34+
public class SettingFieldTypeDialog extends JDialog {
35+
private JPanel contentPane;
36+
private JButton buttonOK;
37+
private JButton buttonCancel;
38+
private JTextArea textArea;
39+
private JCheckBox cbArrayToList;
40+
41+
public SettingFieldTypeDialog() {
42+
setContentPane(contentPane);
43+
setModal(true);
44+
45+
setTitle("Java Field Type Setting");
46+
47+
getRootPane().setDefaultButton(buttonOK);
48+
49+
cbArrayToList.setSelected(ConfigComponent.getInstance().getFieldParseConfig().isConvertArrayToList());
50+
51+
cbArrayToList.addChangeListener(new ChangeListener() {
52+
@Override
53+
public void stateChanged(ChangeEvent changeEvent) {
54+
ConfigComponent.getInstance().getFieldParseConfig().setConvertArrayToList(cbArrayToList.isSelected());
55+
}
56+
});
57+
58+
buttonOK.addActionListener(new ActionListener() {
59+
@Override
60+
public void actionPerformed(ActionEvent e) {
61+
onOK();
62+
}
63+
});
64+
65+
buttonCancel.addActionListener(new ActionListener() {
66+
@Override
67+
public void actionPerformed(ActionEvent e) {
68+
onCancel();
69+
}
70+
});
71+
72+
// call onCancel() when cross is clicked
73+
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
74+
addWindowListener(new WindowAdapter() {
75+
@Override
76+
public void windowClosing(WindowEvent e) {
77+
onCancel();
78+
}
79+
});
80+
81+
// call onCancel() on ESCAPE
82+
contentPane.registerKeyboardAction(new ActionListener() {
83+
@Override
84+
public void actionPerformed(ActionEvent e) {
85+
onCancel();
86+
}
87+
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
88+
89+
final StringBuilder builder = new StringBuilder();
90+
ConfigComponent.getInstance().getFieldParseConfig().getFiledTypeConvertMap().forEach((key, value) -> {
91+
builder.append(key).append("=").append(value).append("\n");
92+
});
93+
textArea.setText(builder.toString());
94+
}
95+
96+
private void onOK() {
97+
// add your code here
98+
saveSetting(textArea.getText());
99+
dispose();
100+
}
101+
102+
private void onCancel() {
103+
// add your code here if necessary
104+
dispose();
105+
}
106+
107+
private void saveSetting(String text){
108+
Map<String, String> filedTypeConvertMap = new LinkedHashMap<>(16);
109+
String[] strings = text.split("\n");
110+
if(strings != null){
111+
for (String string : strings) {
112+
if(StringUtils.isNotBlank(string)){
113+
String[] kv = string.split("=");
114+
if(kv.length >= 2){
115+
filedTypeConvertMap.put(kv[0].trim(), kv[1].trim());
116+
}else{
117+
filedTypeConvertMap.put(kv[0].trim(), null);
118+
}
119+
}
120+
}
121+
}
122+
123+
ConfigComponent.getInstance().getFieldParseConfig().setFiledTypeConvertMap(filedTypeConvertMap);
124+
}
125+
126+
public static void main(String[] args) {
127+
SettingFieldTypeDialog dialog = new SettingFieldTypeDialog();
128+
dialog.pack();
129+
dialog.setVisible(true);
130+
System.exit(0);
131+
}
132+
}

0 commit comments

Comments
 (0)