/**
   * Based on the selected board and parameters save all info needed to the build environments
   *
   * @param confdesc
   */
  public void saveAllSelections(ICConfigurationDescription confdesc) {
    String boardFile = this.mControlBoardsTxtFile.getText().trim();
    String boardName = this.mcontrolBoardName.getText().trim();
    String uploadPort = this.mControlUploadPort.getValue();
    IEnvironmentVariableManager envManager = CCorePlugin.getDefault().getBuildEnvironmentManager();
    IContributedEnvironment contribEnv = envManager.getContributedEnvironment();

    // Set the path variables
    IPath platformPath =
        new Path(new File(this.mControlBoardsTxtFile.getText().trim()).getParent())
            .append(Const.PLATFORM_FILE_NAME);
    Common.setBuildEnvironmentVariable(
        contribEnv, confdesc, Const.ENV_KEY_JANTJE_BOARDS_FILE, boardFile);
    Common.setBuildEnvironmentVariable(
        contribEnv, confdesc, Const.ENV_KEY_JANTJE_PLATFORM_FILE, platformPath.toString());
    Common.setBuildEnvironmentVariable(
        contribEnv, confdesc, Const.ENV_KEY_JANTJE_BOARD_NAME, boardName);
    Common.setBuildEnvironmentVariable(
        contribEnv, confdesc, Const.ENV_KEY_JANTJE_COM_PORT, uploadPort);

    Common.setBuildEnvironmentVariable(
        contribEnv, confdesc, Const.ENV_KEY_JANTJE_PACKAGE_ID, getPackage());
    Common.setBuildEnvironmentVariable(
        contribEnv, confdesc, Const.ENV_KEY_JANTJE_ARCITECTURE_ID, getArchitecture());
    Common.setBuildEnvironmentVariable(
        contribEnv, confdesc, Const.ENV_KEY_JANTJE_BOARD_ID, getBoardID());

    for (LabelCombo curLabelCombo : this.mBoardOptionCombos) {
      curLabelCombo.StoreValue(confdesc);
    }
    saveAllLastUseds();
  }
 protected void EnableControls() {
   this.mcontrolBoardName.setEnabled(true);
   this.mControlUploadPort.setEnabled(true);
   this.mControlBoardsTxtFile.setEnabled((this.mAllBoardsFileNames.length > 1));
   this.mControlBoardsTxtFile.setVisible(this.mAllBoardsFileNames.length > 1);
   for (LabelCombo curLabelCombo : this.mBoardOptionCombos) {
     curLabelCombo.setVisible(true);
   }
 }
  /**
   * Based on the current selection save the last used values$this to make sure you can create the
   * same sketch quickly again
   */
  private void saveAllLastUseds() {
    //
    String boardFile = this.mControlBoardsTxtFile.getText().trim();
    String boardName = this.mcontrolBoardName.getText().trim();
    String uploadPort = this.mControlUploadPort.getValue();
    InstancePreferences.setLastUsedBoardsFile(boardFile);
    InstancePreferences.SetLastUsedArduinoBoard(boardName);
    InstancePreferences.SetLastUsedUploadPort(uploadPort);

    Map<String, String> options = new HashMap<>();
    for (LabelCombo curLabelCombo : BoardSelectionPage.this.mBoardOptionCombos) {

      options.put(curLabelCombo.getMenuName(), curLabelCombo.getValue());
    }
    InstancePreferences.setLastUsedMenuOption(options);
  }
  public boolean isPageComplete() {

    boolean MenuOpionsValidAndComplete = true;
    boolean ret = true;
    int selectedBoardFile = this.mControlBoardsTxtFile.getSelectionIndex();
    if (selectedBoardFile == -1) return false;

    for (LabelCombo curLabelCombo : this.mBoardOptionCombos) {
      MenuOpionsValidAndComplete = MenuOpionsValidAndComplete && curLabelCombo.isValid();
    }

    ret = !this.mcontrolBoardName.getText().trim().isEmpty() && MenuOpionsValidAndComplete;
    if (!this.mFeedbackControl.getText().equals(ret ? Const.TRUE : Const.FALSE)) {
      this.mFeedbackControl.setText(ret ? Const.TRUE : Const.FALSE);
    }
    if (ret) {
      if (this.mBoardSelectionChangedListener != null) {
        this.mBoardSelectionChangedListener.handleEvent(new Event());
      }
    }

    return ret;
  }
        @Override
        public void handleEvent(Event e) {

          int selectedBoardFile = BoardSelectionPage.this.mControlBoardsTxtFile.getSelectionIndex();
          String boardName = BoardSelectionPage.this.mcontrolBoardName.getText();

          for (LabelCombo curLabelCombo : BoardSelectionPage.this.mBoardOptionCombos) {
            curLabelCombo.setItems(
                BoardSelectionPage.this.mAllBoardsFiles[selectedBoardFile].getMenuItemNames(
                    curLabelCombo.getMenuName(), boardName));
          }

          IEnvironmentVariableManager envManager =
              CCorePlugin.getDefault().getBuildEnvironmentManager();
          IContributedEnvironment contribEnv = envManager.getContributedEnvironment();
          ICConfigurationDescription confdesc = getConfdesc();
          if (confdesc != null) {
            IEnvironmentVariable var =
                new EnvironmentVariable(Const.ENV_KEY_JANTJE_BOARD_NAME, boardName);
            contribEnv.addVariable(var, confdesc);
          }
          isPageComplete();
          EnableControls();
        }
  private void setValues(ICConfigurationDescription confdesc) {
    String boardFile = InstancePreferences.getLastUsedBoardsFile();
    String boardName = InstancePreferences.getLastUsedArduinoBoardName();
    String uploadPort = InstancePreferences.getLastUsedUploadPort();
    if (confdesc != null) {
      boardFile =
          Common.getBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_BOARDS_FILE, boardFile);
      boardName =
          Common.getBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_BOARD_NAME, boardName);
      uploadPort =
          Common.getBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_COM_PORT, uploadPort);
    }

    this.mControlBoardsTxtFile.setText(boardFile);
    // if no boards file is selected select the first
    if (this.mControlBoardsTxtFile.getText().isEmpty()) {
      this.mControlBoardsTxtFile.setText(this.mControlBoardsTxtFile.getItem(0));
    }
    int selectedBoardFile = this.mControlBoardsTxtFile.getSelectionIndex();
    this.mcontrolBoardName.setItems(this.mAllBoardsFiles[selectedBoardFile].GetArduinoBoards());
    this.mcontrolBoardName.setText(boardName);
    // BoardModifyListener.handleEvent(null);
    this.mControlUploadPort.setValue(uploadPort);

    // set the options in the combo boxes before setting the value
    Map<String, String> options = InstancePreferences.getLastUsedMenuOption();
    for (LabelCombo curLabelCombo : this.mBoardOptionCombos) {
      curLabelCombo.setItems(
          this.mAllBoardsFiles[selectedBoardFile].getMenuItemNames(
              curLabelCombo.getMenuName(), boardName));
      if (confdesc != null) {
        curLabelCombo.getStoredValue(confdesc);
      } else {
        String value = options.get(curLabelCombo.getMenuName());
        if (value != null) {
          curLabelCombo.setValue(value);
        }
      }
    }
  }
  public void draw(Composite composite) {
    // create the desired layout for this wizard page
    ICConfigurationDescription confdesc = getConfdesc();
    GridLayout theGridLayout = new GridLayout();
    theGridLayout.numColumns = this.ncol;
    composite.setLayout(theGridLayout);

    GridData theGriddata;
    this.mAllBoardsFileNames = Helpers.getBoardsFiles();
    if (this.mAllBoardsFileNames == null) {
      Common.log(
          new Status(
              IStatus.ERROR,
              Const.CORE_PLUGIN_ID,
              "ArduinoHelpers.getBoardsFiles() returns null.\nThis should not happen.\nIt looks like the download of the boards failed.")); //$NON-NLS-1$
    }
    Arrays.sort(this.mAllBoardsFileNames);
    this.mAllBoardsFiles = new Boards[this.mAllBoardsFileNames.length];
    for (int currentBoardFile = 0;
        currentBoardFile < this.mAllBoardsFileNames.length;
        currentBoardFile++) {
      this.mAllBoardsFiles[currentBoardFile] =
          new Boards(new File(this.mAllBoardsFileNames[currentBoardFile]));
    }

    switch (this.mAllBoardsFileNames.length) {
      case 0:
        Common.log(
            new Status(
                IStatus.ERROR, Const.CORE_PLUGIN_ID, Messages.error_no_platform_files_found, null));
        break;
      case 1:
        {
          break;
        }
      default:
        {
          // create a combo to select the boards
          createLabel(composite, this.ncol, "The boards.txt file you want to use"); // $NON-NLS-1$
          new Label(composite, SWT.NONE).setText("Boards.txt file:"); // $NON-NLS-1$
        }
    }

    this.mControlBoardsTxtFile = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
    theGriddata = new GridData();
    theGriddata.horizontalAlignment = SWT.FILL;
    theGriddata.horizontalSpan = (this.ncol - 1);
    this.mControlBoardsTxtFile.setLayoutData(theGriddata);
    this.mControlBoardsTxtFile.setEnabled(false);
    this.mControlBoardsTxtFile.setItems(this.mAllBoardsFileNames);

    createLine(composite, this.ncol);
    // -------

    // ------
    createLabel(composite, this.ncol, "Your Arduino board specifications"); // $NON-NLS-1$
    new Label(composite, SWT.NONE).setText("Board:"); // $NON-NLS-1$
    this.mcontrolBoardName = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
    theGriddata = new GridData();
    theGriddata.horizontalAlignment = SWT.FILL;
    theGriddata.horizontalSpan = (this.ncol - 1);
    this.mcontrolBoardName.setLayoutData(theGriddata);
    this.mcontrolBoardName.setEnabled(false);

    // ----
    this.mControlUploadPort =
        new LabelCombo(
            composite, Messages.ui_port, this.ncol - 1, Const.ENV_KEY_JANTJE_COM_PORT, false);

    this.mControlUploadPort.setItems(
        ArrayUtil.addAll(Activator.bonjourDiscovery.getList(), Common.listComPorts()));

    createLine(composite, this.ncol);

    String[] menuNames = new String[30];
    for (int curBoardsFile = 0; curBoardsFile < this.mAllBoardsFiles.length; curBoardsFile++) {
      ArrayUtil.addAll(menuNames, this.mAllBoardsFiles[curBoardsFile].getMenuNames());
    }
    menuNames = ArrayUtil.removeDuplicates(menuNames);
    this.mBoardOptionCombos = new LabelCombo[menuNames.length];
    for (int currentOption = 0; currentOption < menuNames.length; currentOption++) {
      String menuName = menuNames[currentOption];
      this.mBoardOptionCombos[currentOption] =
          new LabelCombo(
              composite, menuName, this.ncol - 1, Const.ENV_KEY_JANTJE_START + menuName, true);
    }

    // Create the control to alert parents of changes
    this.mFeedbackControl = new Text(composite, SWT.None);
    this.mFeedbackControl.setVisible(false);
    this.mFeedbackControl.setEnabled(false);
    theGriddata = new GridData();
    theGriddata.horizontalSpan = 0;
    this.mFeedbackControl.setLayoutData(theGriddata);
    // End of special controls

    setValues(confdesc);

    // enable the listeners
    childFieldListener controlUploadPortlistener = new childFieldListener();
    controlUploadPortlistener.setInfo(this.mControlUploadPort);
    this.mControlUploadPort.addListener(controlUploadPortlistener);

    this.mcontrolBoardName.addListener(SWT.Modify, this.BoardModifyListener);
    this.mControlBoardsTxtFile.addListener(SWT.Modify, this.boardFileModifyListener);

    for (LabelCombo curLabelCombo : this.mBoardOptionCombos) {
      childFieldListener comboboxModifyListener = new childFieldListener();
      comboboxModifyListener.setInfo(curLabelCombo);
      curLabelCombo.addListener(comboboxModifyListener);
    }

    EnableControls();
    Dialog.applyDialogFont(composite);
  }