-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritanceList.java
More file actions
152 lines (127 loc) · 3.34 KB
/
InheritanceList.java
File metadata and controls
152 lines (127 loc) · 3.34 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
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.reflect.*;
import java.util.*;
/** @author Gergely Kota
InheritanceList is a graphical component that allows the user to select from a list of classes that inherit from some baseclass
*/
public class InheritanceList extends JPanel implements ClassSelector, ActionListener
{
//private Class superclass;
private Class[] classes;
private ArrayList listeners;
private ClassList clist;
private InfoPanel ip;
public InheritanceList()
{
this(Object.class);
}
public InheritanceList(Class superclass)
{
Class[] c1 = Util.getLocalClasses(superclass);
Class[] c2 = Util.getJarClasses(superclass);
classes = Util.merge(c1, c2);
listeners = new ArrayList();
clist = new ClassList(classes);
setLayout(new BorderLayout());
JPanel top = new JPanel(new GridLayout(1,2));
JPanel subtop = new JPanel(new BorderLayout());
subtop.add(new JLabel("Select a(n) " + superclass.getName() + ":"), BorderLayout.WEST);
subtop.add(clist, BorderLayout.EAST);
subtop.add(GUtil.filler(10), BorderLayout.CENTER);
top.add(subtop);
JPanel right = new JPanel(new BorderLayout());
right.add(GUtil.filler(10), BorderLayout.WEST);
right.add(ip = new InfoPanel(), BorderLayout.CENTER);
top.add(right);
add(top, BorderLayout.WEST);
clist.addActionListener(this);
}
public Component toComponent()
{
return this;
}
public void setClass(Class c)
{
clist.setClass(c);
}
public void addActionListener(ActionListener al)
{
listeners.add(al);
}
private void notifyListeners()
{
ActionEvent ae = new ActionEvent(this, hashCode(), "inheritance");
for(int i = 0; i < listeners.size(); i++)
((ActionListener) listeners.get(i)).actionPerformed(ae);
}
public Class getSelectedClass()
{
return clist.getSelectedClass();
}
public void actionPerformed(ActionEvent ae)
{
ip.setDescription(getSelectedClass());
}
/* -------------------------------------------------------- */
/* -------------------------------------------------------- */
/* -------------------------------------------------------- */
private class ClassList extends JComboBox
{
Object[] list;
public ClassList(Class[] c)
{
ArrayList temp = new ArrayList();
for(int i = 0; i < c.length; i++)
if(ok(c[i]))
temp.add(c[i]);
list = temp.toArray();
for(int i = 0; i < list.length; i++)
addItem(((Class)list[i]).getName());
addActionListener(new ListListener());
}
public Class getSelectedClass()
{
try
{
String s = (String) getSelectedItem();
Class temp = Class.forName(s);
for(int i = 0; i < list.length; i++)
if(list[i].equals(temp))
return (Class) list[i];
}
catch(Exception e) {}
return null;
}
public boolean contains(Class c)
{
for(int i = 0; i < list.length; i++)
if(c.equals(list[i]))
return true;
return false;
}
public boolean setClass(Class c)
{
if(!contains(c))
return false;
for(int i = 0; i < list.length; i++)
if(c.equals(list[i]))
setSelectedIndex(i);
return true;
}
private boolean ok(Class c)
{
return !Modifier.isAbstract(c.getModifiers());
}
private class ListListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// current = (Class) getSelectedValue();
notifyListeners();
}
}
}
}