/** Refresh the enable/disable state for the buttons. */
 private void updateButtons() {
   IStructuredSelection selection = (IStructuredSelection) fVariablesViewer.getSelection();
   fRemoveButton.setEnabled(!selection.isEmpty());
   boolean enableUp = true, enableDown = true;
   Object[] libraries = fEnvironmentVariablesContentProvider.getElements(null);
   if (selection.isEmpty() || libraries.length == 0) {
     enableUp = false;
     enableDown = false;
   } else {
     Object first = libraries[0];
     Object last = libraries[libraries.length - 1];
     for (Iterator iter = selection.iterator(); iter.hasNext(); ) {
       Object element = iter.next();
       Object lib;
       lib = element;
       if (lib == first) {
         enableUp = false;
       }
       if (lib == last) {
         enableDown = false;
       }
     }
   }
   // fUpButton.setEnabled(enableUp);
   // fDownButton.setEnabled(enableDown);
 }
  private void edit(IStructuredSelection selection) {
    IStructuredSelection sel = (IStructuredSelection) selection;
    EnvironmentVariable var = (EnvironmentVariable) sel.getFirstElement();
    if (var == null) {
      return;
    }
    String originalName = var.getName();
    String value = var.getValue();
    MultipleInputDialog dialog =
        new MultipleInputDialog(
            fDialog.getShell(),
            InterpretersMessages.AbstractInterpreterEnvironmentVariablesBlock_editVariable);
    dialog.addTextField(NAME_LABEL, originalName, false);
    dialog.addVariablesField(VALUE_LABEL, value, true);

    if (dialog.open() != Window.OK) {
      return;
    }
    String name = dialog.getStringValue(NAME_LABEL);
    value = dialog.getStringValue(VALUE_LABEL);
    if (!originalName.equals(name)) {
      fEnvironmentVariablesContentProvider.add(
          new EnvironmentVariable[] {new EnvironmentVariable(name, value)}, selection);
    } else {
      var.setValue(value);
      fVariablesViewer.refresh(true);
    }
  }
 /*
  * (non-Javadoc)
  *
  * @see
  * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt
  * .events.SelectionEvent)
  */
 public void widgetSelected(SelectionEvent e) {
   Object source = e.getSource();
   /*
    * if (source == fUpButton) { fEnvironmentVariablesContentProvider
    * .up((IStructuredSelection) fLibraryViewer.getSelection()); } else if
    * (source == fDownButton) { fEnvironmentVariablesContentProvider
    * .down((IStructuredSelection) fLibraryViewer.getSelection()); } else
    */ if (source == fRemoveButton) {
     EnvironmentVariable[] old = this.fEnvironmentVariablesContentProvider.getVariables();
     fEnvironmentVariablesContentProvider.remove(
         (IStructuredSelection) fVariablesViewer.getSelection());
     fDialog.updateLibraries(this.fEnvironmentVariablesContentProvider.getVariables(), old);
   } else if (source == fAddExistedButton) {
     addExisted((IStructuredSelection) fVariablesViewer.getSelection());
   } else if (source == fAddButton) {
     add((IStructuredSelection) fVariablesViewer.getSelection());
   } else if (source == fEditButton) {
     EnvironmentVariable[] old = this.fEnvironmentVariablesContentProvider.getVariables();
     edit((IStructuredSelection) fVariablesViewer.getSelection());
     fDialog.updateLibraries(this.fEnvironmentVariablesContentProvider.getVariables(), old);
   } else if (source == fImportButton) {
     performImport();
   } else if (source == fExportButton) {
     performExport();
   }
   update();
 }
  /** The "default" button has been toggled */
  public void restoreDefaultVariables() {
    final EnvironmentVariable[][] libs = new EnvironmentVariable[][] {null};

    libs[0] = new EnvironmentVariable[0];
    if (libs != null) fEnvironmentVariablesContentProvider.setVariables(libs[0]);
    update();
  }
 private void addExisted(IStructuredSelection selection) {
   EnvironmentVariable[] libs = addExisted();
   if (libs == null) return;
   EnvironmentVariable[] old = this.fEnvironmentVariablesContentProvider.getVariables();
   fEnvironmentVariablesContentProvider.add(libs, selection);
   fDialog.updateLibraries(this.fEnvironmentVariablesContentProvider.getVariables(), old);
   update();
 }
 /**
  * Initializes this control based on the settings in the given Interpreter install and type.
  *
  * @param Interpreter Interpreter or <code>null</code> if none
  * @param type type of Interpreter install
  */
 public void initializeFrom(IInterpreterInstall Interpreter, IInterpreterInstallType type) {
   fInterpreterInstall = Interpreter;
   fInterpreterInstallType = type;
   if (Interpreter != null) {
     fEnvironmentVariablesContentProvider.setVariables(
         getInterpreterInstall().getEnvironmentVariables());
   }
   update();
 }
 /** Updates buttons and status based on current libraries */
 public void update() {
   updateButtons();
   IStatus status = Status.OK_STATUS;
   EnvironmentVariable[] standins = fEnvironmentVariablesContentProvider.getStandins();
   for (int i = 0; i < standins.length; i++) {
     IStatus st = standins[i].validate();
     if (!st.isOK()) {
       status = st;
       break;
     }
   }
   updateDialogStatus(status);
 }
  protected EnvironmentVariable[] addExisted() {

    // get Environment Variables from the Environment
    Map envVariables = getNativeEnvironment();
    if (envVariables.size() == 0) {
      MessageBox box = new MessageBox(fDialog.getShell(), SWT.ICON_ERROR);
      box.setMessage(
          MessageFormat.format(
              InterpretersMessages
                  .AbstractInterpreterEnvironmentVariablesBlock_couldNotRetrieveEnvironmentVariablesFrom,
              new Object[] {fDialog.getEnvironment().getName()}));
      box.setText(
          InterpretersMessages
              .AbstractInterpreterEnvironmentVariablesBlock_failedToRetrieveEnvironment);
      box.open();
      return null;
    }

    // get Environment Variables from the table
    EnvironmentVariable[] items = fEnvironmentVariablesContentProvider.getVariables();
    for (int i = 0; i < items.length; i++) {
      EnvironmentVariable var = (EnvironmentVariable) items[i];
      envVariables.remove(var.getName());
    }

    ListSelectionDialog dialog =
        new ListSelectionDialog(
            fDialog.getShell(),
            envVariables,
            createSelectionDialogContentProvider(),
            createSelectionDialogLabelProvider(),
            LaunchConfigurationsMessages.EnvironmentTab_19);
    dialog.setTitle(LaunchConfigurationsMessages.EnvironmentTab_20);

    int button = dialog.open();
    if (button == Window.OK) {
      Object[] selected = dialog.getResult();
      EnvironmentVariable[] vars = new EnvironmentVariable[selected.length];
      for (int i = 0; i < vars.length; i++) {
        vars[i] = (EnvironmentVariable) selected[i];
      }
      return vars;
    }
    return null;
  }
  /**
   * Determines if the present setup is the default location s for this InterpreterEnvironment
   *
   * @return true if the current set of locations are the defaults, false otherwise
   */
  protected boolean isDefaultLocations() {
    EnvironmentVariable[] EnvironmentVariables =
        fEnvironmentVariablesContentProvider.getVariables();
    IInterpreterInstall install = getInterpreterInstall();

    if (install == null || EnvironmentVariables == null) {
      return true;
    }
    IFileHandle installLocation = install.getInstallLocation();
    if (installLocation != null) {
      EnvironmentVariable[] def = new EnvironmentVariable[0];
      if (def.length == EnvironmentVariables.length) {
        for (int i = 0; i < def.length; i++) {
          if (!def[i].equals(EnvironmentVariables[i])) {
            return false;
          }
        }
        return true;
      }
    }
    return false;
  }
 /** Saves settings in the given working copy */
 public void performApply(IInterpreterInstall install) {
   EnvironmentVariable[] libs = fEnvironmentVariablesContentProvider.getVariables();
   install.setEnvironmentVariables(libs);
 }