blob: e7e4d5c920da20243c9d098580838f55b1320065 (
plain)
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
|
package tesseract.objects.remote;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import tesseract.World;
/**
* Remote object menu.
*
* @author jesse
*/
public class RemoteObjectMenu extends JMenu {
private ArrayList<RemoteObject> myControlledObjects;
private World myWorld;
private RemoteObjectCommunicator myCommunicator;
private SocketAddress myHome;
public RemoteObjectMenu(final World theWorld) {
super("RC Objects");
// Added by Steve: Fixes viewing menu problem with Canvas3D on both my windows machines
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
myWorld = theWorld;
myControlledObjects = new ArrayList<RemoteObject>();
myCommunicator = new RemoteObjectCommunicator();
new Thread(myCommunicator).start();
try {
myHome = new InetSocketAddress(InetAddress.getLocalHost(), myCommunicator.getPort());
} catch (UnknownHostException e) {
System.err.println(e);
}
// Home submenu
JMenu home = new JMenu("Home Address");
populateHomeMenu(home);
add(home);
// Objects that can be added
add(new TankMenuItem(this));
add(new BlimpMenuItem(this));
// Separator
addSeparator();
// Living Objects here...
}
private void populateHomeMenu(JMenu home) {
List<InetAddress> addrList = new ArrayList<InetAddress>();
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
ButtonGroup group = new ButtonGroup();
while (interfaces.hasMoreElements()) {
NetworkInterface i = interfaces.nextElement();
try {
if (i.isUp()) {
// Get addresses
Enumeration<InetAddress> ips = i.getInetAddresses();
while (ips.hasMoreElements()) {
final InetAddress ip = ips.nextElement();
JRadioButtonMenuItem item = new JRadioButtonMenuItem(ip.toString());
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myHome = new InetSocketAddress(ip, myCommunicator.getPort());
}
});
try {
if (ip.equals(InetAddress.getLocalHost())) {
item.setSelected(true);
}
} catch (UnknownHostException e1) {
}
home.add(item);
group.add(item);
}
}
} catch (SocketException e) {
// I suppose we'll ignore this address.
}
}
} catch (SocketException e) {
// I suppose we'll ignore all addresses?
}
}
public void addObject(final RemoteObject theObject) {
JCheckBoxMenuItem item = new JCheckBoxMenuItem();
item.setText(theObject.getName());
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (((JCheckBoxMenuItem) e.getSource()).isSelected()) {
myControlledObjects.add(theObject);
} else {
myControlledObjects.remove(theObject);
}
}
});
myWorld.addObject(theObject);
myControlledObjects.add(theObject);
theObject.setHome(myHome);
item.setSelected(true);
add(item);
}
public void sendKeyToObjects(final KeyEvent e) {
for (RemoteObject o : myControlledObjects) {
myCommunicator.sendKeyToObject(o.getId(), e);
}
}
}
|