/**
  * Disables the debugging if debug endpoint's type is changed to 'Internal', and if private port
  * is modified then assigns the new debugging port by setting the modified endpoint as a debugging
  * endpoint.
  *
  * @param oldType : old type of the endpoint.
  * @return retVal : false if any error occurs.
  * @throws WindowsAzureInvalidProjectOperationException
  */
 private boolean handleChangeForDebugEndpt(WindowsAzureEndpointType oldType, String privatePort)
     throws WindowsAzureInvalidProjectOperationException {
   boolean retVal = true;
   if (oldType.equals(WindowsAzureEndpointType.Input)
       && comboType.getSelectedItem().equals(WindowsAzureEndpointType.Internal.toString())) {
     int choice =
         Messages.showYesNoDialog(
             String.format(
                 "%s%s%s", message("dlgEPDel"), message("dlgEPChangeType"), message("dlgEPDel2")),
             message("dlgTypeTitle"),
             Messages.getQuestionIcon());
     if (choice == Messages.YES) {
       waEndpt.setEndPointType((WindowsAzureEndpointType) comboType.getSelectedItem());
       waRole.setDebuggingEndpoint(null);
     } else {
       retVal = false;
     }
   } else if (privatePort == null) {
     PluginUtil.displayErrorDialog(message("dlgInvldPort"), message("dbgPort"));
     retVal = false;
   } else if (!waEndpt.getPrivatePort().equalsIgnoreCase(privatePort)) {
     boolean isSuspended = waRole.getStartSuspended();
     waRole.setDebuggingEndpoint(null);
     waEndpt.setPrivatePort(privatePort);
     waRole.setDebuggingEndpoint(waEndpt);
     waRole.setStartSuspended(isSuspended);
   }
   return retVal;
 }
  /** Default constructor. */
  public WAProjectWizard() {
    setWindowTitle(Messages.pWizWindowTitle);
    String zipFile = "";
    try {
      zipFile =
          String.format(
              "%s%s%s%s%s",
              PluginUtil.pluginFolder,
              File.separator,
              Messages.pluginId,
              File.separator,
              Messages.starterKitFileName);

      // Extract the WAStarterKitForJava.zip to temp dir
      waProjMgr = WindowsAzureProjectManager.create(zipFile);
      //	By deafult - disabling remote access
      //  when creating new project
      waProjMgr.setRemoteAccessAllRoles(false);
      waProjMgr.setClassPathInPackage("azure.lib.dir", PluginUtil.getAzureLibLocation());
      waRole = waProjMgr.getRoles().get(0);
      // remove http endpoint
      WindowsAzureEndpoint endpoint = waRole.getEndpoint(Messages.httpEp);
      if (endpoint != null) {
        endpoint.delete();
      }
    } catch (IOException e) {
      PluginUtil.displayErrorDialogAndLog(
          this.getShell(), Messages.pWizErrTitle, Messages.pWizErrMsg, e);
    } catch (Exception e) {
      errorTitle = Messages.adRolErrTitle;
      errorMessage = Messages.pWizErrMsgBox1 + Messages.pWizErrMsgBox2;
      PluginUtil.displayErrorDialogAndLog(this.getShell(), errorTitle, errorMessage, e);
    }
  }
 /**
  * Disables the debugging if debug endpoint's type is changed to 'Internal', and if private port
  * is modified then assigns the new debugging port by setting the modified endpoint as a debugging
  * endpoint.
  *
  * @param oldType : old type of the endpoint.
  * @return retVal : false if any error occurs.
  * @throws WindowsAzureInvalidProjectOperationException
  */
 private boolean handleChangeForDebugEndpt(WindowsAzureEndpointType oldType)
     throws WindowsAzureInvalidProjectOperationException {
   boolean retVal = true;
   if (oldType.equals(WindowsAzureEndpointType.Input)
       && comboType.getText().equalsIgnoreCase(WindowsAzureEndpointType.Internal.toString())) {
     StringBuffer msg = new StringBuffer(Messages.dlgEPDel);
     msg.append(Messages.dlgEPChangeType);
     msg.append(Messages.dlgEPDel2);
     boolean choice =
         MessageDialog.openQuestion(new Shell(), Messages.dlgTypeTitle, msg.toString());
     if (choice) {
       waEndpt.setEndPointType(WindowsAzureEndpointType.valueOf(comboType.getText()));
       windowsAzureRole.setDebuggingEndpoint(null);
     } else {
       retVal = false;
     }
   } else if (!waEndpt.getPrivatePort().equalsIgnoreCase(txtPrivatePort.getText())) {
     boolean isSuspended = windowsAzureRole.getStartSuspended();
     windowsAzureRole.setDebuggingEndpoint(null);
     waEndpt.setPrivatePort(txtPrivatePort.getText());
     windowsAzureRole.setDebuggingEndpoint(waEndpt);
     windowsAzureRole.setStartSuspended(isSuspended);
   }
   return retVal;
 }
  protected Control createContents(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    GridLayout gridLayout = new GridLayout(2, false);
    container.setLayout(gridLayout);
    GridData gridData = new GridData();
    gridData.widthHint = 200;
    container.setLayoutData(gridData);

    Label lblName = new Label(container, SWT.LEFT);
    gridData = new GridData();
    gridData.horizontalIndent = 10;
    lblName.setLayoutData(gridData);
    lblName.setText(Messages.adRolName);

    txtName = new Text(container, SWT.SINGLE | SWT.BORDER);
    gridData = new GridData();
    gridData.horizontalAlignment = GridData.END;
    gridData.grabExcessHorizontalSpace = true;
    gridData.widthHint = 75;
    txtName.setLayoutData(gridData);

    createEndptTypeComponent(container);

    createPublicPortComponent(container);

    Label lblPrivatePort = new Label(container, SWT.LEFT);
    gridData = new GridData();
    gridData.horizontalIndent = 10;
    lblPrivatePort.setLayoutData(gridData);
    lblPrivatePort.setText(Messages.adRolPrivatePort);

    txtPrivatePort = new Text(container, SWT.SINGLE | SWT.BORDER);
    gridData = new GridData();
    gridData.horizontalAlignment = GridData.END;
    gridData.grabExcessHorizontalSpace = true;
    gridData.widthHint = 75;
    txtPrivatePort.setLayoutData(gridData);

    if (isEditEndpt) {
      txtName.setText(waEndpt.getName());
      txtPrivatePort.setText(waEndpt.getPrivatePort());
      txtPublicPort.setText(waEndpt.getPort());
      comboType.setText(waEndpt.getEndPointType().toString());
      if (comboType.getText().equalsIgnoreCase(WindowsAzureEndpointType.Internal.toString())) {
        txtPublicPort.setEnabled(false);
        lblPublicPort.setEnabled(false);
        txtPublicPort.setText("");
      }
    }
    return super.createContents(parent);
  }
 @Override
 protected void init() {
   setTitle(waEndpt == null ? message("adRolEndPtTitle") : message("endptEditTitle"));
   comboType.setModel(new DefaultComboBoxModel(WindowsAzureEndpointType.values()));
   comboType.addItemListener(createComboTypeListener());
   lblName.setPreferredSize(lblPrivatePort.getPreferredSize());
   // Edit Endpoint scenario
   if (waEndpt != null) {
     txtName.setText(waEndpt.getName());
     // type
     WindowsAzureEndpointType type = null;
     try {
       type = waEndpt.getEndPointType();
       comboType.setSelectedItem(type);
     } catch (WindowsAzureInvalidProjectOperationException e) {
       PluginUtil.displayErrorDialog(message("dlgDbgEndPtErrTtl"), message("endPtTypeErr"));
     }
     // private port
     String prvPort = waEndpt.getPrivatePort();
     if (prvPort == null && !type.equals(WindowsAzureEndpointType.InstanceInput)) {
       txtPrivatePort.setText(AUTO);
     } else {
       String[] prvPortRange = prvPort.split("-");
       txtPrivatePort.setText(prvPortRange[0]);
       if (prvPortRange.length > 1) {
         txtPrivatePortRangeEnd.setText(prvPortRange[1]);
       }
     }
     // Public port
     String[] portRange = waEndpt.getPort().split("-");
     txtPublicPort.setText(portRange[0]);
     if (portRange.length > 1) {
       txtPublicPortRangeEnd.setText(portRange[1]);
     }
   } else {
     /*
      * Add Endpoint scenario.
      * Endpoint type is Internal for the first time.
      */
     txtPrivatePort.setText(AUTO);
   }
   enableControlsDependingOnEnpointType((WindowsAzureEndpointType) comboType.getSelectedItem());
   txtPrivatePort.addFocusListener(createPortFocusListener());
   txtPrivatePortRangeEnd.addFocusListener(createPortFocusListener());
   super.init();
 }
  /** This method enables all the control on UI if Debug is enabled. */
  private void makeDebugEnable() {
    try {
      createDebug.setEnabled(true);
      comboEndPoint.setEnabled(true);
      jvmCheck.setEnabled(true);
      lblDebugEndPoint.setEnabled(true);

      RoleAndEndpoint obj = WARDebuggingUtilMethods.getDebuggingEndpoint(waRole, waProjManager);
      waRole = obj.getRole();
      WindowsAzureEndpoint endpt = obj.getEndPt();

      populateEndPointList();
      comboEndPoint.setSelectedItem(
          String.format(
              message("dbgEndPtStr"), endpt.getName(), endpt.getPort(), endpt.getPrivatePort()));

      dbgSelEndpoint = endpt;
    } catch (WindowsAzureInvalidProjectOperationException e) {
      PluginUtil.displayErrorDialogAndLog(message("adRolErrTitle"), message("dlgDbgErr"), e);
    }
  }
 /**
  * This method populates the endpoint list every time we made any changes in the endpoint list.
  *
  * @throws com.interopbridges.tools.windowsazure.WindowsAzureInvalidProjectOperationException
  */
 private void populateEndPointList() throws WindowsAzureInvalidProjectOperationException {
   List<WindowsAzureEndpoint> endpointsList;
   endpointsList = new ArrayList<WindowsAzureEndpoint>(waRole.getEndpoints());
   comboEndPoint.removeAll();
   for (WindowsAzureEndpoint endpoint : endpointsList) {
     if (((endpoint.getEndPointType().equals(WindowsAzureEndpointType.Input)
                 && endpoint.getPrivatePort() != null)
             || endpoint.getEndPointType().equals(WindowsAzureEndpointType.InstanceInput))
         && !endpoint.equals(waRole.getSessionAffinityInputEndpoint())
         && !endpoint.equals(waRole.getSslOffloadingInputEndpoint())) {
       comboEndPoint.addItem(
           String.format(
               message("dbgEndPtStr"),
               endpoint.getName(),
               endpoint.getPort(),
               endpoint.getPrivatePort()));
     }
   }
 }
 /** This method removed the associated debug end point if debug check box get unchecked. */
 private void removeDebugAssociatedEndpoint() {
   List<WindowsAzureEndpoint> endpointsList;
   try {
     endpointsList = new ArrayList<WindowsAzureEndpoint>(waRole.getEndpoints());
     for (WindowsAzureEndpoint endpoint : endpointsList) {
       if (((String) comboEndPoint.getSelectedItem())
           .equalsIgnoreCase(
               String.format(
                   message("dbgEndPtStr"),
                   endpoint.getName(),
                   endpoint.getPort(),
                   endpoint.getPrivatePort()))) {
         endpoint.delete();
       }
     }
     comboEndPoint.removeAllItems();
     //            comboEndPoint.setText("");
     makeAllDisable();
     waRole.setDebuggingEndpoint(null);
   } catch (WindowsAzureInvalidProjectOperationException e) {
     PluginUtil.displayErrorDialogAndLog(message("adRolErrTitle"), message("dlgDbgErr"), e);
   }
 }
 /**
  * This method edits an endpoint. For editing it also validates endpoint name and ports.
  *
  * @return retVal : false if any error occurs.
  * @throws WindowsAzureInvalidProjectOperationException
  */
 private boolean editEndpt() throws WindowsAzureInvalidProjectOperationException {
   boolean retVal = true;
   String oldEndptName = waEndpt.getName();
   if (!oldEndptName.equalsIgnoreCase(txtName.getText())) {
     // validate endpoint name
     boolean isValid = windowsAzureRole.isAvailableEndpointName(txtName.getText());
     if (!isValid) {
       // if name is not valid
       errorTitle = Messages.dlgInvdEdPtName1;
       errorMessage = Messages.dlgInvdEdPtName2;
       MessageUtil.displayErrorDialog(this.getShell(), errorTitle, errorMessage);
       retVal = false;
     }
   }
   if (retVal) {
     retVal = validatePorts(oldEndptName);
   }
   return retVal;
 }
 /**
  * This method edits an endpoint. For editing it also validates endpoint name and ports.
  *
  * @return retVal : false if any error occurs.
  * @throws WindowsAzureInvalidProjectOperationException
  */
 private boolean editEndpt() throws WindowsAzureInvalidProjectOperationException {
   boolean retVal = true;
   String oldEndptName = waEndpt.getName();
   String newEndptName = txtName.getText().trim();
   // validate endpoint name
   /*
    * Ignore if end point name is not edited.
    */
   if (!oldEndptName.equalsIgnoreCase(newEndptName)) {
     /*
      * Check endpoint name contain
      * alphanumeric and underscore characters only.
      * Starts with alphabet.
      */
     if (WAEclipseHelperMethods.isAlphaNumericUnderscore(newEndptName)) {
       /*
        * Check already used endpoint name is given.
        */
       boolean isValid =
           waRole.isAvailableEndpointName(
               newEndptName, (WindowsAzureEndpointType) comboType.getSelectedItem());
       if (!isValid) {
         // if name is not valid
         PluginUtil.displayErrorDialog(message("dlgInvdEdPtName1"), message("dlgInvdEdPtName2"));
         retVal = false;
       }
     } else {
       PluginUtil.displayErrorDialog(message("dlgInvdEdPtName1"), message("enPtAlphNuMsg"));
       retVal = false;
     }
   }
   if (retVal) {
     retVal = validatePorts(oldEndptName);
   }
   return retVal;
 }
  /**
   * Validates public and private ports. And also makes changes corresponding to the debug endpoint.
   *
   * @param oldEndptName : old name of the endpoint.
   * @return retVal : false if any error occurs.
   * @throws WindowsAzureInvalidProjectOperationException
   */
  private boolean validatePorts(String oldEndptName)
      throws WindowsAzureInvalidProjectOperationException {
    boolean retVal = true;
    boolean isDash = false;
    WindowsAzureEndpointType oldType = waEndpt.getEndPointType();
    WindowsAzureEndpoint debugEndpt = waRole.getDebuggingEndpoint();
    WindowsAzureEndpoint stickyEndpt = waRole.getSessionAffinityInputEndpoint();
    WindowsAzureEndpoint stickyIntEndpt = waRole.getSessionAffinityInternalEndpoint();
    String stcEndptName = "";
    String dbgEndptName = "";
    String stcIntEndptName = "";
    if (debugEndpt != null) {
      // get the debugging endpoint name
      dbgEndptName = debugEndpt.getName();
    }
    if (stickyEndpt != null) {
      stcEndptName = stickyEndpt.getName();
      stcIntEndptName = stickyIntEndpt.getName();
    }

    WindowsAzureEndpointType newType = (WindowsAzureEndpointType) comboType.getSelectedItem();
    if (newType.equals(WindowsAzureEndpointType.InstanceInput)
        || newType.equals(WindowsAzureEndpointType.Internal)) {
      isDash =
          WAEndpointDialogUtilMethods.isDashPresent(
              newType,
              txtPrivatePort.getText(),
              txtPrivatePortRangeEnd.getText(),
              txtPublicPort.getText(),
              txtPublicPortRangeEnd.getText());
    }
    if (isDash) {
      PluginUtil.displayErrorDialog(message("dlgInvldPort"), message("portRangeErrMsg"));
      retVal = false;
    } else {
      // Check for valid range 1 to 65535
      if (WAEndpointDialogUtilMethods.isValidPortRange(
          newType,
          txtPrivatePort.getText(),
          txtPrivatePortRangeEnd.getText(),
          txtPublicPort.getText(),
          txtPublicPortRangeEnd.getText())) {
        // validate ports
        String publicPort =
            WAEndpointDialogUtilMethods.combinePublicPortRange(
                txtPublicPort.getText(),
                txtPublicPortRangeEnd.getText(),
                comboType.getSelectedItem().toString());
        String privatePort =
            WAEndpointDialogUtilMethods.combinePrivatePortRange(
                txtPrivatePort.getText(),
                txtPrivatePortRangeEnd.getText(),
                comboType.getSelectedItem().toString());
        if (privatePort.equalsIgnoreCase(AUTO)) {
          privatePort = null;
        }

        boolean isValidendpoint =
            waRole.isValidEndpoint(oldEndptName, newType, privatePort, publicPort);
        if (isValidendpoint) {
          if (oldEndptName.equalsIgnoreCase(dbgEndptName)) {
            retVal = handleChangeForDebugEndpt(oldType, privatePort);
          }
          /** Disables the session affinity if endpoint's type is changed to 'Internal'. */
          if (oldEndptName.equalsIgnoreCase(stcEndptName)
              || oldEndptName.equalsIgnoreCase(stcIntEndptName)) {
            retVal = false;
          }
          if (retVal) {
            // set the new values in the endpoint object.
            waEndpt.setEndPointType((WindowsAzureEndpointType) comboType.getSelectedItem());
            waEndpt.setName(txtName.getText());
            /*
             * Type is Input or Instance then
             * set public port as well as private port.
             */
            if (comboType.getSelectedItem() == WindowsAzureEndpointType.Input
                || comboType.getSelectedItem() == WindowsAzureEndpointType.InstanceInput) {
              waEndpt.setPort(publicPort);
            }
            /*
             * Type is Internal then
             * set private port only.
             */
            waEndpt.setPrivatePort(privatePort);
          }
        } else {
          PluginUtil.displayErrorDialog(message("dlgInvldPort"), message("dlgPortInUse"));
          retVal = false;
        }
      } else {
        PluginUtil.displayErrorDialog(message("dlgInvldPort"), message("rngErrMsg"));
        retVal = false;
      }
    }
    return retVal;
  }
  private void init() {
    try {
      WindowsAzureEndpoint endPt = waRole.getDebuggingEndpoint();
      if (endPt == null) {
        debugCheck.setSelected(false);
        comboEndPoint.removeAllItems(); // todo: ???
        makeAllDisable();
      } else {
        populateEndPointList();
        comboEndPoint.setSelectedItem(
            String.format(
                message("dbgEndPtStr"), endPt.getName(), endPt.getPort(), endPt.getPrivatePort()));
      }
    } catch (Exception e) {
      PluginUtil.displayErrorDialogAndLog(message("adRolErrTitle"), message("dlgDbgErr"), e);
    }
    isDebugChecked = false;
    try {
      isDebugChecked = waRole.getDebuggingEndpoint() != null;
    } catch (Exception ex) {
      // As getTitle() is also showing the error message if any exception
      // occurs in role.getDebuggingEndpoint(), so only logging
      // the exception. getTitle() gets called every time this page is
      // selected but createContents() is called only once while creating
      // the page.
      log(message("dlgDbgErr"), ex);
    }
    debugCheck.setSelected(isDebugChecked);
    debugCheck.addItemListener(createDebugCheckListener());
    try {
      populateEndPointList();
    } catch (WindowsAzureInvalidProjectOperationException e1) {
      PluginUtil.displayErrorDialogAndLog(message("adRolErrTitle"), message("dlgDbgErr"), e1);
    }
    comboEndPoint.addItemListener(createComboEndPointListener());

    try {
      if (isDebugChecked) {
        jvmCheck.setSelected(waRole.getStartSuspended());
      } else {
        jvmCheck.setSelected(false);
      }

    } catch (WindowsAzureInvalidProjectOperationException e2) {
      PluginUtil.displayErrorDialogAndLog(message("adRolErrTitle"), message("dlgDbgErr"), e2);
    }
    jvmCheck.addItemListener(createJvmCheckListener());
    createDebug.addActionListener(createCreateDebugListener());

    try {
      if (isDebugChecked) {
        WindowsAzureEndpoint endPt = waRole.getDebuggingEndpoint();
        comboEndPoint.setSelectedItem(
            String.format(
                message("dbgEndPtStr"), endPt.getName(), endPt.getPort(), endPt.getPrivatePort()));
      } else {
        makeAllDisable();
      }
    } catch (WindowsAzureInvalidProjectOperationException e1) {
      PluginUtil.displayErrorDialogAndLog(message("adRolErrTitle"), message("dlgDbgErr"), e1);
    }
    if (debugCheck.isSelected() && comboEndPoint.getSelectedItem().equals("")) {
      //            setValid(false);
    }
  }
 /**
  * Validates public and private ports. And also makes changes corresponding to the debug endpoint.
  *
  * @param oldEndptName : old name of the endpoint.
  * @return retVal : false if any error occurs.
  * @throws WindowsAzureInvalidProjectOperationException
  */
 private boolean validatePorts(String oldEndptName)
     throws WindowsAzureInvalidProjectOperationException {
   boolean retVal = true;
   WindowsAzureEndpointType oldType = waEndpt.getEndPointType();
   WindowsAzureEndpoint debugEndpt = windowsAzureRole.getDebuggingEndpoint();
   WindowsAzureEndpoint stickyEndpt = windowsAzureRole.getSessionAffinityInputEndpoint();
   WindowsAzureEndpoint stickyIntEndpt = windowsAzureRole.getSessionAffinityInternalEndpoint();
   String stcEndptName = "";
   String dbgEndptName = "";
   String stcIntEndptName = "";
   if (debugEndpt != null) {
     // get the debugging endpoint name
     dbgEndptName = debugEndpt.getName();
   }
   if (stickyEndpt != null) {
     stcEndptName = stickyEndpt.getName();
     stcIntEndptName = stickyIntEndpt.getName();
   }
   // validate ports
   boolean isValidendpoint =
       windowsAzureRole.isValidEndpoint(
           oldEndptName,
           WindowsAzureEndpointType.valueOf(comboType.getText()),
           txtPrivatePort.getText(),
           txtPublicPort.getText());
   if (isValidendpoint) {
     if (oldEndptName.equalsIgnoreCase(dbgEndptName)) {
       retVal = handleChangeForDebugEndpt(oldType);
     }
     if (oldEndptName.equalsIgnoreCase(stcEndptName)) {
       retVal = handleChangeForStickyEndpt(oldType);
     }
     if (oldEndptName.equalsIgnoreCase(stcIntEndptName)) {
       retVal = handleChangeForStickyEndpt(oldType);
     }
     if (retVal) {
       // set the new values in the endpoint object.
       waEndpt.setEndPointType(WindowsAzureEndpointType.valueOf(comboType.getText()));
       waEndpt.setName(txtName.getText());
       if (comboType.getText().equalsIgnoreCase(WindowsAzureEndpointType.Input.toString())
           || comboType
               .getText()
               .equalsIgnoreCase(WindowsAzureEndpointType.InstanceInput.toString())) {
         waEndpt.setPort(txtPublicPort.getText());
       }
       waEndpt.setPrivatePort(txtPrivatePort.getText());
     }
   } else {
     errorTitle = Messages.dlgInvldPort;
     errorMessage = Messages.dlgPortInUse;
     MessageUtil.displayErrorDialog(this.getShell(), errorTitle, errorMessage);
     retVal = false;
   }
   return retVal;
 }