/* (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();
    }
  }
  @Override
  protected void createClient(Section section, FormToolkit toolkit) {

    section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
    GridData data = new GridData(GridData.FILL_BOTH);
    section.setLayoutData(data);

    section.setText(PDEUIMessages.ArgumentsSection_title);
    section.setDescription(PDEUIMessages.ArgumentsSection_desc);

    Composite client = toolkit.createComposite(section);
    client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 1));
    client.setLayoutData(new GridData(GridData.FILL_BOTH));

    fTabFolder = new CTabFolder(client, SWT.FLAT | SWT.TOP);
    toolkit.adapt(fTabFolder, true, true);
    GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    fTabFolder.setLayoutData(gd);
    gd.heightHint = 2;
    toolkit.getColors().initializeSectionToolBarColors();
    Color selectedColor = toolkit.getColors().getColor(IFormColors.TB_BG);
    fTabFolder.setSelectionBackground(
        new Color[] {selectedColor, toolkit.getColors().getBackground()}, new int[] {100}, true);

    fTabFolder.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            if (fProgramArgs.isDirty()) fProgramArgs.commit();
            if (fVMArgs.isDirty()) fVMArgs.commit();
            refresh();
            fArchCombo.select(fLastArch[fLastTab]);
          }
        });
    createTabs();

    Composite archParent = toolkit.createComposite(client);
    archParent.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
    archParent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    toolkit.createLabel(archParent, PDEUIMessages.ArgumentsSection_architecture);
    fArchCombo = new ComboViewerPart();
    fArchCombo.createControl(archParent, toolkit, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
    fArchCombo.getControl().setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
    fArchCombo.setItems(TAB_ARCHLABELS);
    Control archComboControl = fArchCombo.getControl();
    if (archComboControl instanceof Combo) ((Combo) archComboControl).select(fLastArch[fLastTab]);
    else ((CCombo) archComboControl).select(fLastArch[fLastTab]);
    fArchCombo.addSelectionChangedListener(
        new ISelectionChangedListener() {
          @Override
          public void selectionChanged(SelectionChangedEvent event) {
            if (fProgramArgs.isDirty()) fProgramArgs.commit();
            if (fVMArgs.isDirty()) fVMArgs.commit();
            // remember the change in combo for currently selected platform
            Control fArchComboControl = fArchCombo.getControl();
            if (fArchComboControl instanceof Combo)
              fLastArch[fLastTab] = ((Combo) fArchComboControl).getSelectionIndex();
            else fLastArch[fLastTab] = ((CCombo) fArchComboControl).getSelectionIndex();

            refresh();
          }
        });

    IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars();

    fProgramArgs =
        new FormEntry(
            client, toolkit, PDEUIMessages.ArgumentsSection_program, SWT.MULTI | SWT.WRAP);
    fProgramArgs.getText().setLayoutData(new GridData(GridData.FILL_BOTH));
    fProgramArgs.setFormEntryListener(
        new FormEntryAdapter(this, actionBars) {
          @Override
          public void textValueChanged(FormEntry entry) {
            IArgumentsInfo info = getLauncherArguments();
            info.setProgramArguments(entry.getValue().trim(), fLastTab, fLastArch[fLastTab]);
            updateArgumentPreview(info);
          }
        });
    fProgramArgs.setEditable(isEditable());

    fVMArgs =
        new FormEntry(client, toolkit, PDEUIMessages.ArgumentsSection_vm, SWT.MULTI | SWT.WRAP);
    fVMArgs.getText().setLayoutData(new GridData(GridData.FILL_BOTH));
    fVMArgs.setFormEntryListener(
        new FormEntryAdapter(this, actionBars) {
          @Override
          public void textValueChanged(FormEntry entry) {
            IArgumentsInfo info = getLauncherArguments();
            info.setVMArguments(entry.getValue().trim(), fLastTab, fLastArch[fLastTab]);
            updateArgumentPreview(info);
          }
        });
    fVMArgs.setEditable(isEditable());

    fPreviewArgs =
        new FormEntry(
            client, toolkit, PDEUIMessages.ArgumentsSection_preview, SWT.MULTI | SWT.WRAP);
    fPreviewArgs.getText().setLayoutData(new GridData(GridData.FILL_BOTH));
    fPreviewArgs.setEditable(false);

    toolkit.paintBordersFor(client);
    section.setClient(client);
    // Register to be notified when the model changes
    getModel().addModelChangedListener(this);
  }