View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.gui.panels;
2   
3   import java.util.Collection;
4   
5   import javax.swing.JList;
6   import javax.swing.JPanel;
7   import javax.swing.SwingUtilities;
8   
9   import nl.tudelft.goal.ut2004.visualizer.connection.EnvironmentService;
10  import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
11  import nl.tudelft.goal.ut2004.visualizer.data.EnvironmentData;
12  import nl.tudelft.goal.ut2004.visualizer.util.CollectionEventAdaptor;
13  
14  import cz.cuni.amis.utils.collections.ObservableSet;
15  
16  public class EnvironmentPanel extends JPanel {
17  
18  	private JList connected;
19  
20  	public EnvironmentPanel() {
21  
22  		EnvironmentData data = ServerController.getInstance()
23  				.getEnvironmentData();
24  
25  		data.getEnvironments().addCollectionListener(
26  				new CollectionEventAdaptor<EnvironmentService>() {
27  
28  					@Override
29  					public void postAddEvent(
30  							Collection<EnvironmentService> alreadyAdded,
31  							Collection<EnvironmentService> whereWereAdded) {
32  						SwingUtilities.invokeLater(new Runnable() {
33  
34  							@Override
35  							public void run() {
36  								updateText();
37  							}
38  
39  						});
40  
41  					}
42  
43  					@Override
44  					public void postRemoveEvent(
45  							Collection<EnvironmentService> alreadyAdded,
46  							Collection<EnvironmentService> whereWereRemoved) {
47  						SwingUtilities.invokeLater(new Runnable() {
48  
49  							@Override
50  							public void run() {
51  								updateText();
52  							}
53  
54  						});
55  					}
56  				});
57  
58  		updateText();
59  	}
60  
61  	private void updateText() {
62  		EnvironmentData environments = ServerController.getInstance()
63  				.getEnvironmentData();
64  		ObservableSet<EnvironmentService> clients = environments.getEnvironments();
65  
66  		removeAll();
67  
68  		synchronized (clients) {
69  			connected = new JList(clients.toArray());
70  		}
71  
72  		add(connected);
73  
74  	}
75  }