/**
  * Capture page changed event and if current page is WATabPage, then by default select JDK tab.
  */
 @Override
 public void pageChanged(PageChangedEvent event) {
   IWizardPage page = (IWizardPage) event.getSelectedPage();
   if (page.getName().equals(Messages.tbPg)) {
     WATabPage.getFolder().setSelection(WATabPage.getJdkTab());
   }
 }
 /**
  * This method gets called when wizard's finish button is clicked.
  *
  * @return True, if project gets created successfully; else false.
  */
 @Override
 public boolean performFinish() {
   final String projName = waProjWizPage.getTextProjName();
   final String projLocation = waProjWizPage.getTextLocation();
   final boolean isDefault = waProjWizPage.isDefaultLocation();
   final WorkingSetGroup workingSetGroup = waProjWizPage.getWorkingSetGroup();
   final IWorkingSet[] selWorkingSets = workingSetGroup.getSelectedWorkingSets();
   final IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
   final Map<String, String> depParams = getDeployPageValues();
   final Map<String, Boolean> keyFtr = getKeyFtrPageValues();
   final IProject proj = getSelectProject();
   boolean retVal = true;
   IRunnableWithProgress runnable =
       new IRunnableWithProgress() {
         public void run(IProgressMonitor monitor) throws InvocationTargetException {
           try {
             doFinish(
                 projName,
                 projLocation,
                 isDefault,
                 selWorkingSets,
                 workingSetManager,
                 depParams,
                 keyFtr,
                 proj);
           } finally {
             monitor.done();
           }
         }
       };
   try {
     /*
      * Check if third party JDK and server is selected
      * then license is accepted or not.
      */
     boolean tempAccepted = true;
     if (WATabPage.isThirdPartyJdkChecked() && !WATabPage.isAccepted()) {
       tempAccepted = JdkSrvConfig.createAccLicenseAggDlg(getShell(), true);
     }
     if (WATabPage.isThirdPartySrvChecked() && !WATabPage.isServerAccepted()) {
       tempAccepted = JdkSrvConfig.createAccLicenseAggDlg(getShell(), false);
     }
     if (tempAccepted) {
       getContainer().run(true, false, runnable);
     } else {
       return false;
     }
   } catch (InterruptedException e) {
     PluginUtil.displayErrorDialog(this.getShell(), Messages.pWizErrTitle, Messages.pWizErrMsg);
     retVal = false;
   } catch (InvocationTargetException e) {
     PluginUtil.displayErrorDialogAndLog(
         this.getShell(), Messages.pWizErrTitle, Messages.pWizErrMsg, e);
     retVal = false;
   }
   // re-initializing context menu to default option : false
   Activator.getDefault().setContextMenu(false);
   return retVal;
 }
 /**
  * If wizard can be finished or not.
  *
  * @return boolean
  */
 @Override
 public boolean canFinish() {
   boolean validPage = false;
   // check starting page is valid
   if (waProjWizPage.canFlipToNextPage()) {
     validPage = waProjWizPage.isPageComplete();
   }
   if (validPage) {
     validPage = waProjWizPage.isPageComplete() && tabPg.isPageComplete();
   }
   return validPage;
 }
  /** Add page to the wizard. */
  @Override
  public void addPages() {
    String sdkPath = null;
    if (Activator.IS_WINDOWS) {
      try {
        sdkPath = WindowsAzureProjectManager.getLatestAzureSdkDir();
      } catch (IOException e) {
        sdkPath = null;
        Activator.getDefault().log(errorMessage, e);
      }
    } else {
      Activator.getDefault().log("Not Windows OS, skipping getSDK");
    }
    try {
      if (sdkPath == null && Activator.IS_WINDOWS) {
        errorTitle = Messages.sdkInsErrTtl;
        errorMessage = Messages.sdkInsErrMsg;
        boolean choice = MessageDialog.openQuestion(getShell(), errorTitle, errorMessage);
        if (choice) {
          PlatformUI.getWorkbench()
              .getBrowserSupport()
              .getExternalBrowser()
              .openURL(new URL(Messages.sdkInsUrl));
        }
        addPage(null);
      } else {
        waProjWizPage = new WAProjectWizardPage(Messages.projWizPg);
        tabPg = new WATabPage(Messages.tbPg, waRole, this, true);
        /*
         * If wizard activated through right click on
         * Dynamic web project then
         * Add that as an WAR application.
         */
        if (Activator.getDefault().isContextMenu()) {
          IProject selProj = getSelectProject();
          tabPg.addToAppList(
              selProj.getLocation().toOSString(),
              selProj.getName() + ".war",
              WindowsAzureRoleComponentImportMethod.auto.name());
        }

        waKeyPage = new WAKeyFeaturesPage(Messages.keyPg);
        addPage(waProjWizPage);
        addPage(tabPg);
        addPage(waKeyPage);
      }
    } catch (Exception ex) {
      // only logging the error in log file not showing anything to
      // end user
      Activator.getDefault().log(errorMessage, ex);
    }
  }
 /**
  * Returns configurations done on WATagPage.
  *
  * @return Map<String, String>
  */
 @SuppressWarnings("static-access")
 private Map<String, String> getDeployPageValues() {
   Map<String, String> values = new HashMap<String, String>();
   // JDK
   values.put("jdkChecked", Boolean.valueOf(tabPg.isJdkChecked()).toString());
   values.put("jdkLoc", tabPg.getJdkLoc());
   // JDK download group
   values.put("jdkDwnldChecked", Boolean.valueOf(tabPg.isJdkDownloadChecked()).toString());
   values.put("jdkAutoDwnldChecked", Boolean.valueOf(tabPg.isJdkAutoUploadChecked()).toString());
   values.put("jdkThrdPartyChecked", Boolean.valueOf(tabPg.isThirdPartyJdkChecked()).toString());
   values.put("jdkName", tabPg.getJdkName());
   values.put("jdkUrl", tabPg.getJdkUrl());
   values.put("jdkKey", tabPg.getJdkKey());
   values.put("javaHome", tabPg.getJavaHome());
   // Server
   values.put("serChecked", Boolean.valueOf(tabPg.isSrvChecked()).toString());
   values.put("servername", JdkSrvConfig.getServerName());
   values.put("serLoc", tabPg.getServerLoc());
   values.put("tempFile", WAEclipseHelper.getTemplateFile(Messages.cmpntFile));
   // Server download group
   values.put("srvDwnldChecked", Boolean.valueOf(JdkSrvConfig.isSrvDownloadChecked()).toString());
   values.put(
       "srvAutoDwnldChecked", Boolean.valueOf(JdkSrvConfig.isSrvAutoUploadChecked()).toString());
   values.put("srvThrdPartyChecked", Boolean.valueOf(tabPg.isThirdPartySrvChecked()).toString());
   values.put("srvThrdPartyName", tabPg.getThirdPartyServerName());
   values.put("srvThrdAltSrc", JdkSrvConfig.getServerCloudAltSource());
   values.put("srvUrl", tabPg.getSrvUrl());
   values.put("srvKey", tabPg.getSrvKey());
   values.put("srvHome", tabPg.getSrvHomeDir());
   return values;
 }
  /**
   * Move the project structure to the location provided by user. Also configure JDK, server, server
   * application and key features like session affinity, caching, debugging if user wants to do so.
   *
   * @param projName : Name of the project
   * @param projLocation : Location of the project
   * @param isDefault : whether location of project is default
   * @param selWorkingSets
   * @param workingSetManager
   * @param depMap : stores configurations done on WATagPage
   * @param ftrMap : stores configurations done on WAKeyFeaturesPage
   * @param selProj
   */
  private void doFinish(
      String projName,
      String projLocation,
      boolean isDefault,
      IWorkingSet[] selWorkingSets,
      IWorkingSetManager workingSetManager,
      Map<String, String> depMap,
      Map<String, Boolean> ftrMap,
      IProject selProj) {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IProject project = null;
    try {
      WindowsAzureRole role = waProjMgr.getRoles().get(0);
      // logic for handling deploy page components and their values.
      if (!depMap.isEmpty()) {
        File templateFile = new File(depMap.get("tempFile"));
        role = WizardUtilMethods.configureJDKServer(role, depMap);
        /*
         * Handling adding server application
         * without configuring server/JDK.
         * Always add cloud attributes as
         * approot directory is not created yet
         * hence all applications are
         * imported from outside of the Azure project
         */
        if (!tabPg.getAppsAsNames().isEmpty()) {
          for (int i = 0; i < tabPg.getAppsList().size(); i++) {
            AppCmpntParam app = tabPg.getAppsList().get(i);
            if (!app.getImpAs().equalsIgnoreCase(Messages.helloWorld)) {
              role.addServerApplication(
                  app.getImpSrc(), app.getImpAs(), app.getImpMethod(), templateFile, true);
            }
          }
        }
      }

      /** Handling for HelloWorld application in plug-in */
      if (tabPg != null) {
        if (!tabPg.getAppsAsNames().contains(Messages.helloWorld)) {
          List<WindowsAzureRoleComponent> waCompList =
              waProjMgr.getRoles().get(0).getServerApplications();
          for (WindowsAzureRoleComponent waComp : waCompList) {
            if (waComp.getDeployName().equalsIgnoreCase(Messages.helloWorld)
                && waComp.getImportPath().isEmpty()) {
              waComp.delete();
            }
          }
        }
      }
      role = WizardUtilMethods.configureKeyFeatures(role, ftrMap);

      waProjMgr.save();
      WindowsAzureProjectManager.moveProjFromTemp(projName, projLocation);
      String launchFilePath = projLocation + File.separator + projName + LAUNCH_FILE_PATH;
      ParseXML.setProjectNameinLaunch(launchFilePath, Messages.pWizWinAzureProj, projName);

      root.touch(null);
      project = root.getProject(projName);
      IProjectDescription projDescription = workspace.newProjectDescription(projName);

      Path path = new Path(projLocation + File.separator + projName);
      projDescription.setLocation(path);
      projDescription.setNatureIds(new String[] {WAProjectNature.NATURE_ID});

      if (!project.exists()) {
        if (isDefault) {
          project.create(null);
        } else {
          project.create(projDescription, null);
        }
      }
      project.open(null);

      projDescription = project.getDescription();
      projDescription.setName(projName);
      projDescription.setNatureIds(new String[] {WAProjectNature.NATURE_ID});

      project.move(projDescription, IResource.FORCE, null);

      workingSetManager.addToWorkingSets(project, selWorkingSets);

      root.touch(null);
      if (project != null) {
        WADependencyBuilder builder = new WADependencyBuilder();
        builder.addBuilder(project, "com.persistent.winazure.eclipseplugin.Builder");
      }
    } catch (Exception e) {
      Display.getDefault()
          .syncExec(
              new Runnable() {
                public void run() {
                  MessageDialog.openError(null, Messages.pWizErrTitle, Messages.pWizErrMsg);
                }
              });
      Activator.getDefault().log(Messages.pWizErrMsg, e);
    }
  }