View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.panels.connection;
2   
3   import java.awt.CardLayout;
4   import java.net.URI;
5   
6   import javax.swing.JPanel;
7   import javax.swing.SwingUtilities;
8   
9   import nl.tudelft.pogamut.base.server.ReconnectingServerDefinition;
10  import cz.cuni.amis.utils.flag.FlagListener;
11  
12  /**
13   * Smart widget that handles the connecting to the server.
14   * 
15   * Shows only buttons applicable to the state of the widget.
16   * 
17   * For now assumes that the ServerDefinition is a subclass of {@link ReconnectingServerDefinition}.
18   * 
19   * Possible transitions are:
20   * 
21   * NotConnected --ConnectButton--> Connecting
22   * 
23   * Connecting --ServerChanged(!=null)--> Connected Connecting --CancelButton--> NotConnected
24   * 
25   * Connected --DisconnectButton--> NotConnected Connected --ServerChanged(==null)--> Connecting
26   * 
27   * Threads interacting are AWT event queue, server connection thread, server thread.
28   * 
29   * @author M.P. Korstanje
30   * 
31   */
32  public class ServerConnectionPanel extends JPanel {
33  
34  	/**
35  	 * Generated.
36  	 */
37  	private static final long serialVersionUID = -3524243884561574625L;
38  	private final static String NOT_CONNECTED_PANEL = "notConnected";
39  	private final static String CONNECTED_PANEL = "connected";
40  	private final static String CONNECTING_PANEL = "connecting";
41  	private final CardLayout cardLayout;
42  	private final ReconnectingServerDefinition serverDefinition;
43  	private final Listener listener;
44  
45  	public ServerConnectionPanel(ReconnectingServerDefinition serverDefinition) {
46  		assert serverDefinition != null;
47  		this.cardLayout = new CardLayout();
48  		this.serverDefinition = serverDefinition;
49  		this.listener = new Listener();
50  
51  		initPanel();
52  		addServerListener();
53  	}
54  
55  	private void initPanel() {
56  		JPanel notConnected = makeNotConnectedPanel();
57  		JPanel connecting = makeConnectingPanel();
58  		JPanel connected = makeConnectedPanel();
59  
60  		this.setLayout(cardLayout);
61  		add(notConnected, NOT_CONNECTED_PANEL);
62  		add(connecting, CONNECTING_PANEL);
63  		add(connected, CONNECTED_PANEL);
64  
65  		showPanel(NOT_CONNECTED_PANEL);
66  	}
67  
68  	private JPanel makeConnectedPanel() {
69  		return new ConnectedPanel(serverDefinition.getUriFlag(),
70  				new IDisconnect() {
71  
72  					@Override
73  					public void disconnect() {
74  						removeServerListener();
75  						showPanel(NOT_CONNECTED_PANEL);
76  						serverDefinition.stopServer();
77  					}
78  
79  				});
80  	}
81  
82  	private JPanel makeConnectingPanel() {
83  		return new ConnectingPanel(serverDefinition.getUriFlag(),
84  				new IDisconnect() {
85  
86  					@Override
87  					public void disconnect() {
88  						removeServerListener();
89  						showPanel(NOT_CONNECTED_PANEL);
90  						serverDefinition.stopServer();
91  					}
92  
93  				});
94  	}
95  
96  	private JPanel makeNotConnectedPanel() {
97  		return new NotConnectedPanel(new IConnect() {
98  
99  			@Override
100 			public void connect(URI uri) {
101 				showPanel(CONNECTING_PANEL);
102 				addServerListener();
103 				serverDefinition.setUri(uri);
104 			}
105 		});
106 	}
107 
108 	@SuppressWarnings("unchecked")
109 	private void addServerListener() {
110 		serverDefinition.getServerFlag().addListener(listener);
111 	}
112 
113 	@SuppressWarnings("unchecked")
114 	private void removeServerListener() {
115 		serverDefinition.getServerFlag().removeListener(listener);
116 	}
117 
118 	private class Listener implements FlagListener {
119 
120 		@Override
121 		public void flagChanged(Object changedValue) {
122 			//Execute on swing thread.
123 			SwingUtilities.invokeLater(new Runnable() {
124 				
125 				
126 				@Override
127 				public void run() {
128 					//Check what the status is now.
129 					Object server = serverDefinition.getServerFlag().getFlag();
130 					if (server == null) {
131 						showPanel(CONNECTING_PANEL);
132 					} else {
133 						showPanel(CONNECTED_PANEL);
134 					}
135 				}
136 			});
137 
138 		}
139 	}
140 
141 	private void showPanel(String panel) {
142 		cardLayout.show(this, panel);
143 	}
144 
145 }