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