forked from joeytwiddle/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuBuilder.java
More file actions
executable file
·134 lines (123 loc) · 5.32 KB
/
MenuBuilder.java
File metadata and controls
executable file
·134 lines (123 loc) · 5.32 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
package visualjava;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import org.neuralyte.Logger;
import org.neuralyte.common.swing.jmenus.SplittingJMenu;
public class MenuBuilder {
public static void addConstructorToMenu(final Constructor con, JMenu menu) {
Class c = con.getDeclaringClass();
JMenuItem menuItem = new JMenuItem("new " + VisualJavaStatics.getSimpleClassName(c) + "(" + VisualJavaStatics.listParams(con.getParameterTypes()) + ")");
menuItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
VisualJava.desktop.displayConstructor(con);
}
}
);
menuItem.setIcon(VisualJavaGUIStatics.getSmallImageIconForClass(c));
menu.add(menuItem);
}
public static void addMethodToMenu(final Method m, JMenu menu, final Object obj) {
// JMenuItem menuItem = new JMenuItem(VisualJavaStatics.getSimpleClassName(m.getReturnType()) + " " + m.getName() + "(" + VisualJavaStatics.listParams(m.getParameterTypes()) + ")");
JMenuItem menuItem = new JMenuItem(m.getName() + "(" + VisualJavaStatics.listParams(m.getParameterTypes()) + ") : " + VisualJavaStatics.getSimpleClassName(m.getReturnType()));
menuItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
VisualJava.desktop.displayMethod(m,obj);
}
}
);
menuItem.setIcon(VisualJavaGUIStatics.getSmallImageIconForClass(m.getReturnType()));
menu.add(menuItem);
}
public static void addStaticsToMenu(JMenu statics, Class c) {
try {
for (int i=0;i<c.getConstructors().length;i++) {
Constructor con = c.getConstructors()[i];
if (Modifier.isPublic(con.getModifiers())) {
// menuItem = new JMenuItem("new " + con.getName() + "(" + listParams(con.getParameterTypes()) + ")");
addConstructorToMenu(con, statics);
}
}
// statics.add(new JMenuItem("--------"));
statics.add(new JSeparator());
for (int i=0;i<c.getDeclaredMethods().length;i++) {
Method m = c.getDeclaredMethods()[i];
if (Modifier.isStatic(m.getModifiers()) && Modifier.isPublic(m.getModifiers())) {
addMethodToMenu(m, statics, null);
}
}
statics.add(new JSeparator());
for (int i=0;i<c.getDeclaredFields().length;i++) {
Field f = c.getDeclaredFields()[i];
if (Modifier.isStatic(f.getModifiers()) && Modifier.isPublic(f.getModifiers())) {
try {
addFieldToMenu(f, statics, null);
} catch (Exception e) {
org.neuralyte.Logger.error(e);
// I once got: java.lang.IllegalAccessException: Class visualjava.VisualJavaGUIStatics can not access a member of class org.neuralyte.common.swing.MoveabilityListener with modifiers "public static final"
}
}
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
/** Do not pass Variable, pass actual Object! Why? Variable is used for Code and Dyno. Did I mean the other way around? **/
public static void addFieldToMenu(final Field f, JMenu menu, final VariableModel variable) {
final Object obj = (variable == null ? null : variable.value);
try {
Object value = f.get(obj);
JMenuItem menuItem = new JMenuItem(
VisualJavaStatics.getSimpleClassName(f.getType())
+ " " + f.getName() + " = " + VisualJavaStatics.niceString(value) + "?" // Added "?" since value may change!
);
menuItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Object value = f.get(obj);
String name = VisualJava.desktop.showObject(value);
Logger.log("[CODE] "+name+" = "+VisualJava.getVariableName(variable)+"."+f.getName());
VisualJava.passToDyno(variable, f, null);
} catch (Exception ex) {
VisualJava.desktop.showObject(ex);
}
}
}
);
menuItem.setIcon(VisualJavaGUIStatics.getSmallImageIconForClass(f.getType()));
menu.add(menuItem);
} catch (Exception e) {
// org.neuralyte.Logger.log("VisualJavaGUIStatics.addFieldToMenu(): cannot work on field \""+f+"\" because: "+e);
}
}
/*
public static JMenu getOrCreateSubMenu(JMenu menu, String text) {
JMenu child = getChildWithText(menu, text);
if (child == null) {
child = new SplittingJMenu(text);
menu.add(child);
Logger.log("Created child "+child+" for menu "+menu);
}
return child;
}
public static JMenu getChildWithText(JMenu menu, String text) {
for (int i=0;i<menu.getMenuComponentCount();i++) {
Component subMenu = menu.getMenuComponent(i);
if (subMenu instanceof JMenu && subMenu.getName().equals(text)) {
return (JMenu)subMenu;
}
}
return null;
}
*/
}