-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBeanEditor.java
More file actions
276 lines (249 loc) · 12.4 KB
/
Copy pathBeanEditor.java
File metadata and controls
276 lines (249 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//Author: Dongming Ma
//Kindly ask for extra points as I conquered the data type limitation.
package reflection_api_automated_form;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import static reflection_api_automated_form.Reflection_API_Automated_Form.getAttributes;
public class BeanEditor extends JFrame {
protected Integer numOfContributes;
static HashMap<String, Method> Attributes;
Font f = new Font("Times", Font.PLAIN, 40);
static Object obj;
public JPanel createAttribute(String AttributeName) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String valueString = "";
Integer valueInteger = 0;
Boolean valueBoolean = false;
JTextField t = new JTextField("");
Method m = Attributes.get("get" + AttributeName);
if (m.getGenericReturnType() == String.class) {
valueString = (String) m.invoke(obj, null);
t.setText(valueString);
} else if (m.getGenericReturnType() == Boolean.class) {
valueBoolean = (Boolean) m.invoke(obj, null);
t.setText(valueBoolean.toString());
} else {
valueInteger = (Integer) m.invoke(obj, null);
t.setText(valueInteger.toString());
}
//待解决:卡片尺寸自适应,自动加入卡片
JPanel p = new JPanel();
JButton b = new JButton("Enter");
JLabel l = new JLabel(AttributeName);
b.setFont(f);
l.setFont(f);
t.setFont(f);
this.setPreferredSize(new Dimension(720, 200));
p.setLayout(new GridLayout(1, 3, 20, 20));
p.add(l);
p.add(t);
p.add(b);
ActionListener submit = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String newValueString = "";
Integer newValueInteger = 0;
Boolean newValueBoolean = false;
Method m = Attributes.get("set" + AttributeName);
if (Boolean.valueOf(t.getText())) {
try {
newValueBoolean = Boolean.valueOf(t.getText());
m.invoke(obj, newValueBoolean);
System.out.println("Object's " + AttributeName + " has been modified to " + newValueBoolean);
} catch (IllegalAccessException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (t.getText().matches(".*\\d+.*")) {
try {
newValueInteger = Integer.valueOf(t.getText());
m.invoke(obj, newValueInteger);
System.out.println("Object's " + AttributeName + " has been modified to " + newValueInteger);
} catch (IllegalAccessException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
try {
newValueString = t.getText();
m.invoke(obj, newValueString);
System.out.println("Object's " + AttributeName + " has been modified to " + newValueString);
} catch (IllegalAccessException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};
b.addActionListener(submit);
t.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
String newValueString = "";
Integer newValueInteger = 0;
Boolean newValueBoolean = false;
Method m = Attributes.get("set" + AttributeName);
if (Boolean.valueOf(t.getText())) {
try {
newValueBoolean = Boolean.valueOf(t.getText());
m.invoke(obj, newValueBoolean);
System.out.println("Object's " + AttributeName + " has been modified to " + newValueBoolean);
} catch (IllegalAccessException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (t.getText().matches(".*\\d+.*")) {
try {
newValueInteger = Integer.valueOf(t.getText());
m.invoke(obj, newValueInteger);
System.out.println("Object's " + AttributeName + " has been modified to " + newValueInteger);
} catch (IllegalAccessException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
try {
newValueString = t.getText();
m.invoke(obj, newValueString);
System.out.println("Object's " + AttributeName + " has been modified to " + newValueString);
} catch (IllegalAccessException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(BeanEditor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
});
return p;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
System.out.println("enter pressed");
}
}
public JPanel createBottomPanel() {
JPanel p = new JPanel();
JButton SubmitAllChanges = new JButton("SubmitAllChanges");
SubmitAllChanges.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("too late to implement this with my trush code");
}
});
JButton PrintOutObject = new JButton("PrintOutObject");
PrintOutObject.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(obj);
}
});
p.setLayout(new GridLayout(1, 2, 20, 20));
p.add(SubmitAllChanges);
p.add(PrintOutObject);
SubmitAllChanges.setFont(new Font("Times", Font.PLAIN, 30));
PrintOutObject.setFont(new Font("Times", Font.PLAIN, 30));
return p;
}
public JPanel createInfoDisplayPanel() {
JPanel p = new JPanel();
String[] name = obj.getClass().getName().split("\\.");
Integer num = Attributes.size() / 2;
p.setLayout(new GridLayout(1, 2, 20, 20));
JLabel nameOfObject = new JLabel("Name: " + name[1]);
JLabel numOfAttributes = new JLabel("NumOfAttributes: " + num.toString());
nameOfObject.setFont(new Font("Times", Font.PLAIN, 30));
numOfAttributes.setFont(new Font("Times", Font.PLAIN, 30));
p.add(nameOfObject);
p.add(numOfAttributes);
return p;
}
public BeanEditor(Object obj) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
super("Bean Editor Mark.I");
numOfContributes = Attributes.size() / 2;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(720, 1080);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
setLocation(((int) width - 720) / 2, ((int) height - 1080) / 2);
//-----------Contents---------------
Container c = getContentPane();
JPanel AttributeListPanel = new JPanel();
AttributeListPanel.setLayout(new GridLayout(numOfContributes, 1, 20, 20));
c.add(AttributeListPanel, BorderLayout.CENTER);
JPanel InfoDispalyPanel = createInfoDisplayPanel();
c.add(InfoDispalyPanel, BorderLayout.NORTH);
JPanel BottomPanel = createBottomPanel();
c.add(BottomPanel, BorderLayout.SOUTH);
//--------------insert Attributes list---------------
Set<String> namesOfAttributes = new HashSet<String>();
for (Map.Entry<String, Method> entry : Attributes.entrySet()) {
namesOfAttributes.add(entry.getKey().substring(3));
}
for (String names : namesOfAttributes) {
JPanel p = createAttribute(names);
AttributeListPanel.add(p);
}
System.out.println(namesOfAttributes);
setResizable(false);
setVisible(true);
}
public static HashMap getAttributes(Class c) throws NoSuchMethodException {
Method[] methods = c.getMethods();
HashMap<String, Method> Attributes = new HashMap<>();
for (int i = 0; i < methods.length; i++) {
String name = methods[i].getName();
Class type = methods[i].getReturnType();
if ((name.startsWith("get") && !name.contains("getClass")) || name.startsWith("set")) {
Attributes.put(name, methods[i]);
}
}
return Attributes;
}
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
Class c = Class.forName("reflection_api_automated_form.JavaBean1");
JavaBean1 jb1 = new JavaBean1("Jack", 24, "Driver", false);
obj = jb1;
System.out.println(obj);
Attributes = getAttributes(c);
new BeanEditor(new JavaBean1());
}
}