Пример #1
0
  /** Initializes interface and set event listeners */
  public ClientFrame() {
    super("iChat");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(700, 500);
    this.setMinimumSize(new Dimension(700, 500));

    // Escuta P2P
    this.peerListener = new DefaultPeerListenerImpl();
    peerListener.setFrame(this);
    this.p2pServer = new P2PServer(peerListener);
    new Thread(this.p2pServer).start();

    this.getRegisterButton().addActionListener(new RegisterButtonListener(this));
    this.getCallButton().addActionListener(new CallButtonListener(this));

    this.getQuitButton()
        .addActionListener(
            new ActionListener() {

              @Override
              public void actionPerformed(ActionEvent e) {
                getClient().unregister();
                System.exit(0);
              }
            });

    // Not so cool, but...
    this.getCreateGroupButton()
        .addActionListener(
            new ActionListener() {

              @Override
              public void actionPerformed(ActionEvent arg0) {
                String groupName = JOptionPane.showInputDialog("Enter group name:");
                if (groupName != null) groupClient.createGroup(groupName);
              }
            });

    this.getCallGroupButton()
        .addActionListener(
            new ActionListener() {

              @Override
              public void actionPerformed(ActionEvent arg0) {
                String groupName = getGroupsList().getSelectedValue().toString();
                groupClient.requestJoin(groupName);

                CallDialog l = new CallDialog(ClientFrame.this);
                setCallDialog(l);
                l.setLocationRelativeTo(ClientFrame.this);
                l.getLabel().setText("Calling " + groupName);
                l.setTitle("Call to " + groupName);
                l.setVisible(true);
                l.pack();
              }
            });

    this.getRefreshGroupsButton()
        .addActionListener(
            new ActionListener() {

              @Override
              public void actionPerformed(ActionEvent arg0) {
                groupClient.requestGroupList();
              }
            });

    try {
      this.client = new Client();
      this.client.setPort(p2pServer.getLocalPort());
      this.clientListener = new DefaultClientListenerImpl(this);
      this.client.setListener(clientListener);

      this.groupClient = new GroupClient(client);
      this.groupClient.setListener(new DefaultGroupClientListenerImpl(this));

    } catch (UnknownHostException e) {

      e.printStackTrace();
    } catch (IOException e) {

      e.printStackTrace();
    }
  }