protected void validate() {
    boolean fail = true;
    if (textPortName.getText().trim().equals("")) setErrorMessage("Connection name can't be empty");
    else if (client.getConnectionPool().hasConnection(textPortName.getText().trim()))
      setErrorMessage("Connection name has already been used by other port");
    else if (familyList.getSelection().isEmpty())
      setErrorMessage("Should select at least one family");
    else if (textServerAddress.getText().trim().equals(""))
      setErrorMessage("Server address can't be empty");
    else if (Util.getInt(textServerPort.getText().trim(), 0) == 0)
      setErrorMessage("The server port format is incorrect");
    else if (chkUdp.getSelection() && comboProxyType.getSelectionIndex() == 1)
      setErrorMessage("Can't use Http proxy with UDP mode");
    else if (comboProxyType.getSelectionIndex() != 0
        && textProxyAddress.getText().trim().equals(""))
      setErrorMessage("Proxy address can't be empty");
    else if (comboProxyType.getSelectionIndex() != 0
        && Util.getInt(textProxyPort.getText().trim(), 0) == 0)
      setErrorMessage("The proxy port format is incorrect");
    else {
      setErrorMessage(null);
      fail = false;
    }

    getButton(IDialogConstants.OK_ID).setEnabled(!fail);
  }
  @Override
  @SuppressWarnings("unchecked")
  protected void okPressed() {
    String portName = textPortName.getText().trim();
    int supportedFamily = 0;
    for (Iterator<Integer> i = ((IStructuredSelection) familyList.getSelection()).iterator();
        i.hasNext(); ) supportedFamily |= i.next();

    IConnection conn = null;

    IConnectionPolicy policy =
        client
            .getConnectionPolicyFactory()
            .createPolicy(
                client,
                portName,
                supportedFamily,
                (Integer) ((IStructuredSelection) familyList.getSelection()).getFirstElement(),
                null,
                null,
                null);
    try {
      if (chkUdp.getSelection()) {
        switch (comboProxyType.getSelectionIndex()) {
          case 0:
            conn =
                client
                    .getConnectionPool()
                    .newUDPConnection(
                        policy,
                        new InetSocketAddress(
                            textServerAddress.getText().trim(),
                            Util.getInt(textServerPort.getText().trim(), 0)),
                        true);
            break;
          case 2:
            policy.setProxy(
                new InetSocketAddress(
                    textProxyAddress.getText().trim(),
                    Util.getInt(textProxyPort.getText().trim(), 0)));
            policy.setProxyUsername(textUsername.getText().trim());
            policy.setProxyPassword(textPassword.getText());
            conn =
                client
                    .getConnectionPool()
                    .newUDPSocks5Connection(
                        policy,
                        new InetSocketAddress(
                            textServerAddress.getText().trim(),
                            Util.getInt(textServerPort.getText().trim(), 0)),
                        true);
            break;
        }
      } else {
        switch (comboProxyType.getSelectionIndex()) {
          case 0:
            conn =
                client
                    .getConnectionPool()
                    .newTCPConnection(
                        policy,
                        new InetSocketAddress(
                            textServerAddress.getText().trim(),
                            Util.getInt(textServerPort.getText().trim(), 0)),
                        true);
            break;
          case 1:
            policy.setProxy(
                new InetSocketAddress(
                    textProxyAddress.getText().trim(),
                    Util.getInt(textProxyPort.getText().trim(), 0)));
            policy.setProxyUsername(textUsername.getText().trim());
            policy.setProxyPassword(textPassword.getText());
            conn =
                client
                    .getConnectionPool()
                    .newTCPHttpConnection(
                        policy,
                        new InetSocketAddress(
                            textServerAddress.getText().trim(),
                            Util.getInt(textServerPort.getText().trim(), 0)),
                        true);
            break;
          case 2:
            policy.setProxy(
                new InetSocketAddress(
                    textProxyAddress.getText().trim(),
                    Util.getInt(textProxyPort.getText().trim(), 0)));
            policy.setProxyUsername(textUsername.getText().trim());
            policy.setProxyPassword(textPassword.getText());
            conn =
                client
                    .getConnectionPool()
                    .newTCPSocks5Connection(
                        policy,
                        new InetSocketAddress(
                            textServerAddress.getText().trim(),
                            Util.getInt(textServerPort.getText().trim(), 0)),
                        true);
            break;
        }
      }
    } catch (UnresolvedAddressException e) {
      conn = null;
    }

    if (conn == null) MessageDialog.openError(getShell(), "Failed", "Create connection failed");
    else super.okPressed();
  }