/.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
- package info.reflectionsofmind.connexion.platform.gui.connect;
-
- import info.reflectionsofmind.connexion.platform.control.IRoot;
- import info.reflectionsofmind.connexion.platform.control.ServerInfo;
- import info.reflectionsofmind.connexion.platform.control.control.IConnectControl;
- import info.reflectionsofmind.connexion.platform.control.control.IJoinControl;
- import info.reflectionsofmind.connexion.platform.control.cts.CTSMessageCoder;
- import info.reflectionsofmind.connexion.platform.control.cts.message.MsgParticipationRequest;
- import info.reflectionsofmind.connexion.platform.control.stc.message.MsgParticipationAccepted;
- import info.reflectionsofmind.connexion.platform.game.IGame;
- import info.reflectionsofmind.connexion.platform.gui.MessagePanel;
- import info.reflectionsofmind.connexion.platform.gui.TransportEventLogger;
- import info.reflectionsofmind.connexion.platform.gui.join.JoinGameFrame;
- import info.reflectionsofmind.connexion.transport.ITransport;
- import info.reflectionsofmind.connexion.transport.ITransport.INode;
- import info.reflectionsofmind.connexion.util.convert.EncodingException;
-
- import java.awt.event.ActionEvent;
- import java.util.concurrent.Callable;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
-
- import javax.swing.AbstractAction;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
-
- import net.miginfocom.swing.MigLayout;
-
- public class ConnectFrame extends JFrame implements IConnectControl
- {
- private final IRoot root;
- private final IGame game;
-
- private final JComboBox serverCombo;
- private final JTextField nameField;
- private MessagePanel messagePanel;
-
- private INode currentServer = null;
-
- public ConnectFrame(final IRoot root, final IGame game)
- {
- super("Connexion :: Connect to server");
-
- this.root = root;
- this.game = game;
-
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- setLayout(new MigLayout("", "[][120::]12[][max][][]", "[][]"));
-
- add(new JLabel("Name:"));
- add(this.nameField = new JTextField("Player"), "grow");
- add(new JLabel("Server:"));
-
- this.serverCombo = new JComboBox(getRoot().getConfiguration().getServers().toArray());
-
- add(this.serverCombo, "grow");
- add(new JButton(new ConnectAction()), "grow");
- add(new JButton("Edit server list"), "grow, wrap");
- add(this.messagePanel = new MessagePanel(), "span, wrap");
- add(new JButton(new CloseAction()), "span, al right, wrap");
-
- pack();
- setSize(640, 400);
- setLocationRelativeTo(null);
- }
-
- public Future<IJoinControl> connect(final String name, final INode server)
- {
- this.currentServer = server;
-
- return Executors.newSingleThreadExecutor().submit(new Callable<IJoinControl>()
- {
- public IJoinControl call() throws Exception
- {
- final TransportEventLogger logger = new TransportEventLogger(ConnectFrame.this.messagePanel);
- getServer().getTransport().addListener(logger);
- getServer().getTransport().start();
-
- try
- {
- final String message = CTSMessageCoder.INSTANCE.encode(new MsgParticipationRequest(name));
- ConnectFrame.this.messagePanel.addRawLine("Sending participation request.");
- getServer().send(message);
- ConnectFrame.this.messagePanel.addRawLine("Participation request sent, waiting for response.");
- }
- catch (final EncodingException exception)
- {
- throw new RuntimeException(exception);
- }
-
- final WaitForParticipationAccepted listener = new WaitForParticipationAccepted();
- getServer().getTransport().addListener(listener);
- final MsgParticipationAccepted message = listener.take();
- getServer().getTransport().removeListener(listener);
- getServer().getTransport().removeListener(logger);
-
- dispose();
-
- final JoinGameFrame joinGameFrame = new JoinGameFrame( //
- getRoot(), getGame(), getServer(), message.getNames());
- joinGameFrame.setVisible(true);
- return joinGameFrame;
- }
- });
- }
-
- public IRoot getRoot()
- {
- return this.root;
- }
-
- public IGame getGame()
- {
- return this.game;
- }
-
- public INode getServer()
- {
- return this.currentServer;
- }
-
- private void connect()
- {
- final ServerInfo serverInfo = (ServerInfo) this.serverCombo.getSelectedItem();
- final ITransport transport = serverInfo.getFactory().createTransport(serverInfo.getParameters());
- final INode serverNode = transport.createNode(serverInfo.getAddress());
- connect(this.nameField.getText(), serverNode);
- }
-
- private final class CloseAction extends AbstractAction
- {
- public CloseAction()
- {
- super("Close");
- }
-
- public void actionPerformed(final ActionEvent arg0)
- {
- if (getServer() != null) getServer().getTransport().stop();
- dispose();
- getRoot().restart();
- }
- }
-
- private final class ConnectAction extends AbstractAction
- {
- private ConnectAction()
- {
- super("Connect");
- }
-
- public void actionPerformed(final ActionEvent e)
- {
- connect();
- }
- }
- }