PageRenderTime 27ms CodeModel.GetById 10ms app.highlight 15ms RepoModel.GetById 0ms app.codeStats 0ms

/.metadata/.plugins/org.eclipse.core.resources/.history/81/f07c4ffc81a3001e1dff9ae635b3e1ee

https://bitbucket.org/fixpoint/connexion
#! | 160 lines | 132 code | 28 blank | 0 comment | 0 complexity | 6b633c40319c77eac0258887005081ec MD5 | raw file
  1package info.reflectionsofmind.connexion.platform.gui.connect;
  2
  3import info.reflectionsofmind.connexion.platform.control.IRoot;
  4import info.reflectionsofmind.connexion.platform.control.ServerInfo;
  5import info.reflectionsofmind.connexion.platform.control.control.IConnectControl;
  6import info.reflectionsofmind.connexion.platform.control.control.IJoinControl;
  7import info.reflectionsofmind.connexion.platform.control.cts.CTSMessageCoder;
  8import info.reflectionsofmind.connexion.platform.control.cts.message.MsgParticipationRequest;
  9import info.reflectionsofmind.connexion.platform.control.stc.message.MsgParticipationAccepted;
 10import info.reflectionsofmind.connexion.platform.game.IGame;
 11import info.reflectionsofmind.connexion.platform.gui.MessagePanel;
 12import info.reflectionsofmind.connexion.platform.gui.TransportEventLogger;
 13import info.reflectionsofmind.connexion.platform.gui.join.JoinGameFrame;
 14import info.reflectionsofmind.connexion.transport.ITransport;
 15import info.reflectionsofmind.connexion.transport.ITransport.INode;
 16import info.reflectionsofmind.connexion.util.convert.EncodingException;
 17
 18import java.awt.event.ActionEvent;
 19import java.util.concurrent.Callable;
 20import java.util.concurrent.Executors;
 21import java.util.concurrent.Future;
 22
 23import javax.swing.AbstractAction;
 24import javax.swing.JButton;
 25import javax.swing.JComboBox;
 26import javax.swing.JFrame;
 27import javax.swing.JLabel;
 28import javax.swing.JTextField;
 29
 30import net.miginfocom.swing.MigLayout;
 31
 32public class ConnectFrame extends JFrame implements IConnectControl
 33{
 34	private final IRoot root;
 35	private final IGame game;
 36	
 37	private final JComboBox serverCombo;
 38	private final JTextField nameField;
 39	private MessagePanel messagePanel;
 40	
 41	private INode currentServer = null;
 42	
 43	public ConnectFrame(final IRoot root, final IGame game)
 44	{
 45		super("Connexion :: Connect to server");
 46		
 47		this.root = root;
 48		this.game = game;
 49		
 50		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 51		setLayout(new MigLayout("", "[][120::]12[][max][][]", "[][]"));
 52		
 53		add(new JLabel("Name:"));
 54		add(this.nameField = new JTextField("Player"), "grow");
 55		add(new JLabel("Server:"));
 56		
 57		this.serverCombo = new JComboBox(getRoot().getConfiguration().getServers().toArray());
 58		
 59		add(this.serverCombo, "grow");
 60		add(new JButton(new ConnectAction()), "grow");
 61		add(new JButton("Edit server list"), "grow, wrap");
 62		add(this.messagePanel = new MessagePanel(), "span, wrap");
 63		add(new JButton(new CloseAction()), "span, al right, wrap");
 64		
 65		pack();
 66		setSize(640, 400);
 67		setLocationRelativeTo(null);
 68	}
 69	
 70	public Future<IJoinControl> connect(final String name, final INode server)
 71	{
 72		this.currentServer = server;
 73		
 74		return Executors.newSingleThreadExecutor().submit(new Callable<IJoinControl>()
 75				{
 76			public IJoinControl call() throws Exception
 77			{
 78				final TransportEventLogger logger = new TransportEventLogger(ConnectFrame.this.messagePanel);
 79				getServer().getTransport().addListener(logger);
 80				getServer().getTransport().start();
 81				
 82				try
 83				{
 84					final String message = CTSMessageCoder.INSTANCE.encode(new MsgParticipationRequest(name));
 85					ConnectFrame.this.messagePanel.addRawLine("Sending participation request.");
 86					getServer().send(message);
 87					ConnectFrame.this.messagePanel.addRawLine("Participation request sent, waiting for response.");
 88				}
 89				catch (final EncodingException exception)
 90				{
 91					throw new RuntimeException(exception);
 92				}
 93				
 94				final WaitForParticipationAccepted listener = new WaitForParticipationAccepted();
 95				getServer().getTransport().addListener(listener);
 96				final MsgParticipationAccepted message = listener.take();
 97				getServer().getTransport().removeListener(listener);
 98				getServer().getTransport().removeListener(logger);
 99				
100				dispose();
101				
102				final JoinGameFrame joinGameFrame = new JoinGameFrame( //
103						getRoot(), getGame(), getServer(), message.getNames());
104				joinGameFrame.setVisible(true);
105				return joinGameFrame;
106			}
107				});
108	}
109	
110	public IRoot getRoot()
111	{
112		return this.root;
113	}
114	
115	public IGame getGame()
116	{
117		return this.game;
118	}
119	
120	public INode getServer()
121	{
122		return this.currentServer;
123	}
124	
125	private void connect()
126	{
127		final ServerInfo serverInfo = (ServerInfo) this.serverCombo.getSelectedItem();
128		final ITransport transport = serverInfo.getFactory().createTransport(serverInfo.getParameters());
129		final INode serverNode = transport.createNode(serverInfo.getAddress());
130		connect(this.nameField.getText(), serverNode);
131	}
132	
133	private final class CloseAction extends AbstractAction
134	{
135		public CloseAction()
136		{
137			super("Close");
138		}
139		
140		public void actionPerformed(final ActionEvent arg0)
141		{
142			if (getServer() != null) getServer().getTransport().stop();
143			dispose();
144			getRoot().restart();
145		}
146	}
147	
148	private final class ConnectAction extends AbstractAction
149	{
150		private ConnectAction()
151		{
152			super("Connect");
153		}
154		
155		public void actionPerformed(final ActionEvent e)
156		{
157			connect();
158		}
159	}
160}