PageRenderTime 64ms CodeModel.GetById 17ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 0ms

/.metadata/.plugins/org.eclipse.core.resources/.history/7d/808eadd869a3001e1d98c762f0e1eac2

https://bitbucket.org/fixpoint/connexion
#! | 210 lines | 172 code | 38 blank | 0 comment | 0 complexity | 85e0cc4cb4a999644ae6b7f2a8c55ced MD5 | raw file
  1package info.reflectionsofmind.connexion.platform.gui.join;
  2
  3import info.reflectionsofmind.connexion.platform.control.ClientParticipant;
  4import info.reflectionsofmind.connexion.platform.control.IGameControlFactory;
  5import info.reflectionsofmind.connexion.platform.control.IRoot;
  6import info.reflectionsofmind.connexion.platform.control.control.IGameControl;
  7import info.reflectionsofmind.connexion.platform.control.control.IJoinControl;
  8import info.reflectionsofmind.connexion.platform.control.cts.CTSMessageCoder;
  9import info.reflectionsofmind.connexion.platform.control.cts.message.MsgDisconnectNotice;
 10import info.reflectionsofmind.connexion.platform.control.stc.IServerToClientMessageDispatchTarget;
 11import info.reflectionsofmind.connexion.platform.control.stc.STCMessageDispatcher;
 12import info.reflectionsofmind.connexion.platform.control.stc.message.MsgChangeNotice;
 13import info.reflectionsofmind.connexion.platform.control.stc.message.MsgGameStarted;
 14import info.reflectionsofmind.connexion.platform.control.stc.message.MsgKickNotice;
 15import info.reflectionsofmind.connexion.platform.control.stc.message.MsgParticipantConnected;
 16import info.reflectionsofmind.connexion.platform.control.stc.message.MsgParticipationAccepted;
 17import info.reflectionsofmind.connexion.platform.game.IGame;
 18import info.reflectionsofmind.connexion.platform.game.client.IClientGameParameters;
 19import info.reflectionsofmind.connexion.platform.gui.ConnexionGUI;
 20import info.reflectionsofmind.connexion.platform.gui.MessagePanel;
 21import info.reflectionsofmind.connexion.platform.gui.TransportEventLogger;
 22import info.reflectionsofmind.connexion.transport.ITransport.INode;
 23import info.reflectionsofmind.connexion.util.convert.EncodingException;
 24
 25import java.awt.event.ActionEvent;
 26import java.awt.event.WindowAdapter;
 27import java.awt.event.WindowEvent;
 28import java.util.ArrayList;
 29import java.util.List;
 30import java.util.concurrent.BlockingQueue;
 31import java.util.concurrent.Callable;
 32import java.util.concurrent.Executors;
 33import java.util.concurrent.Future;
 34import java.util.concurrent.SynchronousQueue;
 35
 36import javax.swing.AbstractAction;
 37import javax.swing.JButton;
 38import javax.swing.JFrame;
 39
 40import net.miginfocom.swing.MigLayout;
 41
 42public class JoinGameFrame extends JFrame implements IServerToClientMessageDispatchTarget, IJoinControl
 43{
 44	private final IGame game;
 45	private final IRoot root;
 46	private final INode server;
 47
 48	private MessagePanel messagePanel;
 49	private ParticipantsPanel participantsPanel;
 50	private AbstractJoinGamePanel gamePanel;
 51
 52	private final List<ClientParticipant> participants = new ArrayList<ClientParticipant>();
 53
 54	public final BlockingQueue<IGameControl> gameQueue = new SynchronousQueue<IGameControl>();
 55	private final Callable<IGameControl> gameWaiter = new Callable<IGameControl>()
 56	{
 57
 58		public IGameControl call() throws Exception
 59		{
 60			return this.queue.take();
 61		}
 62	};
 63
 64	private final Future<IGameControl> futureGame = Executors.newSingleThreadExecutor().submit(
 65			new Callable<IGameControl>()
 66			{
 67				public IGameControl call() throws Exception
 68				{
 69				}
 70			}
 71	);
 72
 73	public JoinGameFrame(final IRoot root, final IGame game, final INode server, final List<String> participantNames)
 74	{
 75		super("Connexion :: Join game");
 76
 77		this.game = game;
 78
 79		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 80		setLayout(new MigLayout("", "[240!, fill][max]", "[][max, fill][]"));
 81
 82		addWindowListener(new WindowAdapter()
 83		{
 84			@Override
 85			public void windowClosed(final WindowEvent e)
 86			{
 87				disconnect();
 88			}
 89		});
 90
 91		this.root = root;
 92		this.server = server;
 93
 94		add(this.gamePanel = ((ConnexionGUI) root).getJoinPanelFactories().get(game).createGamePanel(this), "wrap");
 95		add(this.participantsPanel = new ParticipantsPanel(), "wrap");
 96		add(new JButton(new DisconnectAction()), "wrap");
 97		add(this.messagePanel = new MessagePanel(), "cell 1 0, spany");
 98
 99		for (final String name : participantNames)
100		{
101			final ClientParticipant participant = new ClientParticipant(name);
102			this.participants.add(participant);
103			this.participantsPanel.addParticipant(participant);
104		}
105
106		this.server.getTransport().addListener(new STCMessageDispatcher(this));
107		this.server.getTransport().addListener(new TransportEventLogger(this.messagePanel));
108
109		pack();
110		setSize(800, 600);
111		setLocationRelativeTo(null);
112	}
113
114	public void onKickNotice(final MsgKickNotice message, final INode sender)
115	{
116	}
117
118	public void onParticipantConnected(final MsgParticipantConnected message, final INode sender)
119	{
120		final ClientParticipant participant = new ClientParticipant(message.getName());
121		this.participants.add(participant);
122		JoinGameFrame.this.participantsPanel.addParticipant(participant);
123	}
124
125	public void onChangeNotice(final MsgChangeNotice message, final INode sender)
126	{
127		// This is processed by game control
128	}
129
130	public void onParticipationAccepted(final MsgParticipationAccepted message, final INode sender)
131	{
132		// This is processed by ConnectFrame.
133	}
134
135	public void onGameStarted(final MsgGameStarted message, final INode sender)
136	{
137		dispose();
138
139		final IGameControlFactory gameControlFactory = getRoot().getConfiguration().getGameControlFactories().get(getGame());
140		gameControlFactory.createGameControl(getGame(), getParameters(), getParticipants());
141	}
142
143	// ====================================================================================================
144	// === JOIN CONTROL IMPLEMENTATION
145	// ====================================================================================================
146
147	public INode getServer()
148	{
149		return this.server;
150	}
151
152	public IGame getGame()
153	{
154		return this.game;
155	}
156
157	public IRoot getRoot()
158	{
159		return this.root;
160	}
161
162	public List<ClientParticipant> getParticipants()
163	{
164		return this.participants;
165	}
166
167	public IClientGameParameters getParameters()
168	{
169		return this.gamePanel.getGameParameters();
170	}
171
172	public Future<IGameControl> waitForGame()
173	{
174		return null;
175	}
176
177	public void disconnect()
178	{
179		try
180		{
181			JoinGameFrame.this.server.send(CTSMessageCoder.INSTANCE.encode(new MsgDisconnectNotice()));
182		}
183		catch (final EncodingException exception)
184		{
185			throw new RuntimeException(exception);
186		}
187
188		JoinGameFrame.this.server.getTransport().stop();
189
190		getRoot().restart();
191	}
192
193	public void sendMessage(final String message)
194	{
195		throw new UnsupportedOperationException();
196	}
197
198	private final class DisconnectAction extends AbstractAction
199	{
200		public DisconnectAction()
201		{
202			super("Disconnect");
203		}
204
205		public void actionPerformed(final ActionEvent arg0)
206		{
207			disconnect();
208		}
209	}
210}