/*
  * (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();
 }
  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);
    }
  }
 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();
 }
  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;
  }
 private Map getNativeEnvironment() {
   IEnvironment environment = fDialog.getEnvironment();
   IExecutionEnvironment execEnvironment =
       (IExecutionEnvironment) environment.getAdapter(IExecutionEnvironment.class);
   Map stringVars = execEnvironment.getEnvironmentVariables(true);
   HashMap vars = new HashMap();
   for (Iterator i = stringVars.keySet().iterator(); i.hasNext(); ) {
     String key = (String) i.next();
     String value = (String) stringVars.get(key);
     vars.put(key, new EnvironmentVariable(key, value));
   }
   return vars;
 }
  private EnvironmentVariable[] add() {
    MultipleInputDialog dialog =
        new MultipleInputDialog(
            fDialog.getShell(),
            InterpretersMessages.AbstractInterpreterEnvironmentVariablesBlock_addVariable);
    dialog.addTextField(NAME_LABEL, null, false);
    dialog.addVariablesField(VALUE_LABEL, null, true);

    if (dialog.open() != Window.OK) {
      return null;
    }

    String name = dialog.getStringValue(NAME_LABEL);
    String value = dialog.getStringValue(VALUE_LABEL);

    if (name != null && value != null && name.length() > 0 && value.length() > 0) {
      return new EnvironmentVariable[] {new EnvironmentVariable(name.trim(), value.trim())};
    }
    return null;
  }
 protected void setButtonLayoutData(Button button) {
   fDialog.setButtonLayoutData(button);
 }
 protected void updateDialogStatus(IStatus status) {
   fDialog.setSystemLibraryStatus(status);
   fDialog.updateStatusLine();
 }