/**
   * Set the container to display in the tree or <code>null</code> to disable the tree
   *
   * @param input bundle container or <code>null</code>
   */
  public void setInput(ITargetDefinition input) {
    fTargetDefinition = input;

    // Update the cached data
    fFileBundleMapping = null;
    fAllBundles.clear();

    if (input == null || !input.isResolved()) {
      fTree.setInput(Messages.TargetContentsGroup_10);
      setEnabled(false);
      return;
    }

    IResolvedBundle[] allResolvedBundles = input.getAllBundles();
    if (allResolvedBundles == null || allResolvedBundles.length == 0) {
      fTree.setInput(Messages.TargetContentsGroup_11);
      setEnabled(false);
      return;
    }

    for (int i = 0; i < allResolvedBundles.length; i++) {
      fAllBundles.add(allResolvedBundles[i]);
    }

    boolean isFeatureMode =
        ((TargetDefinition) fTargetDefinition).getUIMode() == TargetDefinition.MODE_FEATURE;
    fFeaureModeButton.setSelection(isFeatureMode);
    fPluginModeButton.setSelection(!isFeatureMode);
    fGroupLabel.setEnabled(!isFeatureMode);

    fTree.getControl().setRedraw(false);
    fTree.setInput(fTargetDefinition);
    fTree.expandAll();
    updateCheckState();
    updateButtons();
    setEnabled(true);
    fTree.getControl().setRedraw(true);
  }
 private void updateCheckState() {
   List result = new ArrayList();
   // Checked error statuses
   if (fMissing != null) {
     result.addAll(fMissing);
   }
   if (fFeaureModeButton.getSelection()) {
     // Checked features and plugins
     result.addAll(((TargetDefinition) fTargetDefinition).getFeaturesAndBundles());
   } else {
     // Bundles with errors are already included from fMissing, do not add twice
     IResolvedBundle[] bundles = fTargetDefinition.getBundles();
     for (int i = 0; i < bundles.length; i++) {
       if (bundles[i].getStatus().isOK()) {
         result.add(bundles[i]);
       }
     }
   }
   fTree.setCheckedElements(result.toArray());
 }
  /* (non-Javadoc)
   * @see org.eclipse.pde.internal.ui.wizards.target.TargetDefinitionPage#targetChanged()
   */
  protected void targetChanged(ITargetDefinition definition) {
    super.targetChanged(definition);
    if (definition != null) {
      // When  If the page isn't open yet, try running a UI job so the dialog has time to finish
      // opening
      new UIJob(PDEUIMessages.TargetDefinitionContentPage_0) {
        public IStatus runInUIThread(IProgressMonitor monitor) {
          ITargetDefinition definition = getTargetDefinition();
          if (!definition.isResolved()) {
            try {
              getContainer()
                  .run(
                      true,
                      true,
                      new IRunnableWithProgress() {
                        public void run(IProgressMonitor monitor)
                            throws InvocationTargetException, InterruptedException {
                          getTargetDefinition().resolve(new ResolutionProgressMonitor(monitor));
                          if (monitor.isCanceled()) {
                            throw new InterruptedException();
                          }
                        }
                      });
            } catch (InvocationTargetException e) {
              PDECore.log(e);
            } catch (InterruptedException e) {
              fContentTree.setCancelled();
              return Status.CANCEL_STATUS;
            }
          }
          fContentTree.setInput(definition);
          fLocationTree.setInput(definition);
          if (definition.isResolved() && definition.getStatus().getSeverity() == IStatus.ERROR) {
            fLocationTab.setImage(
                PlatformUI.getWorkbench()
                    .getSharedImages()
                    .getImage(ISharedImages.IMG_OBJS_ERROR_TSK));
          } else {
            fLocationTab.setImage(null);
          }
          return Status.OK_STATUS;
        }
      }.schedule();
      String name = definition.getName();
      if (name == null) {
        name = EMPTY_STRING;
      }

      if (name.trim().length() > 0) fNameText.setText(name);
      else setMessage(PDEUIMessages.TargetDefinitionContentPage_8);

      fLocationTree.setInput(definition);
      fContentTree.setInput(definition);

      String presetValue = (definition.getOS() == null) ? EMPTY_STRING : definition.getOS();
      fOSCombo.setText(presetValue);
      presetValue = (definition.getWS() == null) ? EMPTY_STRING : definition.getWS();
      fWSCombo.setText(presetValue);
      presetValue = (definition.getArch() == null) ? EMPTY_STRING : definition.getArch();
      fArchCombo.setText(presetValue);
      presetValue =
          (definition.getNL() == null)
              ? EMPTY_STRING
              : LocaleUtil.expandLocaleName(definition.getNL());
      fNLCombo.setText(presetValue);

      IPath jrePath = definition.getJREContainer();
      if (jrePath == null || jrePath.equals(JavaRuntime.newDefaultJREContainerPath())) {
        fDefaultJREButton.setSelection(true);
      } else {
        String ee = JavaRuntime.getExecutionEnvironmentId(jrePath);
        if (ee != null) {
          fExecEnvButton.setSelection(true);
          fExecEnvsCombo.select(fExecEnvsCombo.indexOf(ee));
        } else {
          String vm = JavaRuntime.getVMInstallName(jrePath);
          if (vm != null) {
            fNamedJREButton.setSelection(true);
            fNamedJREsCombo.select(fNamedJREsCombo.indexOf(vm));
          }
        }
      }

      if (fExecEnvsCombo.getSelectionIndex() == -1)
        fExecEnvsCombo.setText(fExecEnvChoices.first().toString());

      if (fNamedJREsCombo.getSelectionIndex() == -1)
        fNamedJREsCombo.setText(VMUtil.getDefaultVMInstallName());

      updateJREWidgets();

      presetValue =
          (definition.getProgramArguments() == null)
              ? EMPTY_STRING
              : definition.getProgramArguments();
      fProgramArgs.setText(presetValue);
      presetValue =
          (definition.getVMArguments() == null) ? EMPTY_STRING : definition.getVMArguments();
      fVMArgs.setText(presetValue);

      fElementViewer.refresh();
    }
  }
  private void updateButtons() {
    if (fTargetDefinition != null && !fTree.getSelection().isEmpty()) {
      Object[] selection = ((IStructuredSelection) fTree.getSelection()).toArray();
      boolean hasResolveBundle = false;
      boolean hasParent = false;
      boolean allSelected = true;
      boolean noneSelected = true;
      for (int i = 0; i < selection.length; i++) {
        if (!hasResolveBundle || !hasParent) {
          if (selection[i] instanceof IResolvedBundle) {
            hasResolveBundle = true;
          } else {
            hasParent = true;
          }
        }
        boolean checked = fTree.getChecked(selection[i]);
        if (checked) {
          noneSelected = false;
        } else {
          allSelected = false;
        }
      }
      // Selection is available if not everything is already selected and not both a parent and
      // child item are selected
      fSelectButton.setEnabled(!allSelected && !(hasResolveBundle && hasParent));
      fDeselectButton.setEnabled(!noneSelected && !(hasResolveBundle && hasParent));
    } else {
      fSelectButton.setEnabled(false);
      fDeselectButton.setEnabled(false);
    }

    int total = fAllBundles.size();
    if (fFeaureModeButton.getSelection()) {
      if (fTargetDefinition == null) {
        total = 0;
      } else {
        total = fTargetDefinition.getAllFeatures().length;
        total += ((TargetDefinition) fTargetDefinition).getOtherBundles().length;
      }
    }
    if (fMissing != null) {
      total += fMissing.size();
    }

    fSelectAllButton.setEnabled(fTargetDefinition != null && fTree.getCheckedLeafCount() != total);
    fDeselectAllButton.setEnabled(fTargetDefinition != null && fTree.getCheckedLeafCount() != 0);
    fSelectRequiredButton.setEnabled(
        fTargetDefinition != null
            && fTree.getCheckedLeafCount() > 0
            && fTree.getCheckedLeafCount() != total);

    if (fTargetDefinition != null) {
      fCountLabel.setText(
          MessageFormat.format(
              Messages.TargetContentsGroup_9,
              new String[] {
                Integer.toString(fTree.getCheckedLeafCount()), Integer.toString(total)
              }));
    } else {
      fCountLabel.setText(""); // $NON-NLS-1$
    }
  }
  public void saveIncludedBundleState() {
    if (fFeaureModeButton.getSelection()) {
      // Create a list of checked bundle infos
      List included = new ArrayList();
      int missingCount = 0;
      Object[] checked = fTree.getCheckedLeafElements();
      for (int i = 0; i < checked.length; i++) {
        if (checked[i] instanceof IFeatureModel) {
          included.add(
              new NameVersionDescriptor(
                  ((IFeatureModel) checked[i]).getFeature().getId(),
                  null,
                  NameVersionDescriptor.TYPE_FEATURE));
        }
        if (checked[i] instanceof IResolvedBundle) {
          // Missing features are included as IResolvedBundles, save them as features instead
          if (((IResolvedBundle) checked[i]).getStatus().getCode()
              == IResolvedBundle.STATUS_PLUGIN_DOES_NOT_EXIST) {
            included.add(
                new NameVersionDescriptor(
                    ((IResolvedBundle) checked[i]).getBundleInfo().getSymbolicName(),
                    null,
                    NameVersionDescriptor.TYPE_PLUGIN));
            missingCount++;
          } else if (((IResolvedBundle) checked[i]).getStatus().getCode()
              == IResolvedBundle.STATUS_FEATURE_DOES_NOT_EXIST) {
            included.add(
                new NameVersionDescriptor(
                    ((IResolvedBundle) checked[i]).getBundleInfo().getSymbolicName(),
                    null,
                    NameVersionDescriptor.TYPE_FEATURE));
            missingCount++;
          } else {
            included.add(
                new NameVersionDescriptor(
                    ((IResolvedBundle) checked[i]).getBundleInfo().getSymbolicName(), null));
          }
        }
      }

      if (included.size() == 0) {
        fTargetDefinition.setIncluded(new NameVersionDescriptor[0]);
      } else if (included.size() == 0
          || included.size() - missingCount
              == fTargetDefinition.getAllFeatures().length
                  + ((TargetDefinition) fTargetDefinition).getOtherBundles().length) {
        fTargetDefinition.setIncluded(null);
      } else {
        fTargetDefinition.setIncluded(
            (NameVersionDescriptor[]) included.toArray(new NameVersionDescriptor[included.size()]));
      }
    } else {
      // Figure out if there are multiple bundles sharing the same id
      Set multi = new HashSet(); // BSNs of bundles with multiple versions available
      Set all = new HashSet();
      for (Iterator iterator = fAllBundles.iterator(); iterator.hasNext(); ) {
        IResolvedBundle rb = (IResolvedBundle) iterator.next();
        if (!all.add(rb.getBundleInfo().getSymbolicName())) {
          multi.add(rb.getBundleInfo().getSymbolicName());
        }
      }

      // Create a list of checked bundle infos
      List included = new ArrayList();
      Object[] checked = fTree.getCheckedLeafElements();
      for (int i = 0; i < checked.length; i++) {
        if (checked[i] instanceof IResolvedBundle) {
          // Create the bundle info object
          String bsn = ((IResolvedBundle) checked[i]).getBundleInfo().getSymbolicName();
          NameVersionDescriptor info = null;
          if (multi.contains(bsn)) {
            // include version info
            info =
                new NameVersionDescriptor(
                    bsn, ((IResolvedBundle) checked[i]).getBundleInfo().getVersion());
          } else {
            // don't store version info
            info = new NameVersionDescriptor(bsn, null);
          }
          included.add(info);
        }
      }

      if (included.size() == 0) {
        fTargetDefinition.setIncluded(new NameVersionDescriptor[0]);
      } else if (included.size() == fAllBundles.size() + fMissing.size()) {
        fTargetDefinition.setIncluded(null);
      } else {
        fTargetDefinition.setIncluded(
            (NameVersionDescriptor[]) included.toArray(new NameVersionDescriptor[included.size()]));
      }
    }
  }