public PatchWizard(IStorage patch, IResource target, CompareConfiguration configuration) {
   Assert.isNotNull(configuration);
   this.fConfiguration = configuration;
   setDefaultPageImageDescriptor(
       CompareUIPlugin.getImageDescriptor("wizban/applypatch_wizban.png")); // $NON-NLS-1$
   setWindowTitle(PatchMessages.PatchWizard_title);
   initializeDialogSettings();
   fPatcher = new WorkspacePatcher(target);
   if (patch != null) {
     try {
       fPatcher.parse(patch);
       this.patch = patch;
       patchReadIn = true;
     } catch (IOException e) {
       MessageDialog.openError(
           null,
           PatchMessages.InputPatchPage_PatchErrorDialog_title,
           PatchMessages.InputPatchPage_ParseError_message);
     } catch (CoreException e) {
       ErrorDialog.openError(
           getShell(),
           PatchMessages.InputPatchPage_PatchErrorDialog_title,
           PatchMessages.InputPatchPage_PatchFileNotFound_message,
           e.getStatus());
     }
   }
 }
 public static ImageDescriptor getImageDescriptor(String relativePath) {
   if (fgComparePlugin == null) return null;
   IPath path = Utilities.getIconPath(null).append(relativePath);
   URL url = FileLocator.find(fgComparePlugin.getBundle(), path, null);
   if (url == null) return null;
   return ImageDescriptor.createFromURL(url);
 }
 private void initializeDialogSettings() {
   IDialogSettings workbenchSettings = CompareUIPlugin.getDefault().getDialogSettings();
   IDialogSettings section = workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
   if (section == null) {
     fHasNewDialogSettings = true;
   } else {
     fHasNewDialogSettings = false;
     setDialogSettings(section);
   }
 }
  /* (non-Javadoc)
   * Method declared on IWizard.
   */
  @Override
  public boolean performFinish() {

    IWizardPage currentPage = getContainer().getCurrentPage();
    if (currentPage.getName().equals(PreviewPatchPage2.PREVIEWPATCHPAGE_NAME)) {
      PreviewPatchPage2 previewPage = (PreviewPatchPage2) currentPage;
      previewPage.ensureContentsSaved();
    }

    if (fPatchWizardPage != null) {
      // make sure that the patch has been read
      if (!fPatchWizardPage.isPatchRead()) fPatchWizardPage.readInPatch();
      fPatcher.refresh();

      // make sure that the patch is not invalid
      if (!fPatchWizardPage.checkPageComplete()) return false;
    } else {
      // either we have a patch from the patch input page or one has
      // been specified; double check this
      Assert.isNotNull(patch);
      // make sure that the patch has been read in
      Assert.isTrue(patchReadIn);
    }

    if (!currentPage.getName().equals(PreviewPatchPage2.PREVIEWPATCHPAGE_NAME)
        && fPatcher.hasRejects()) {
      if (!MessageDialog.openConfirm(
          getShell(), PatchMessages.PatchWizard_0, PatchMessages.PatchWizard_1)) {
        return false;
      }
    }

    try {
      // create scheduling rule based on the type of patch - single or workspace
      ISchedulingRule scheduleRule = null;
      if (fPatcher.isWorkspacePatch()) {
        // workspace patch
        ISchedulingRule[] projectRules = fPatcher.getTargetProjects();
        scheduleRule = new MultiRule(projectRules);
      } else {
        // single patch
        IResource resource = getTarget();
        if (resource.getType() == IResource.FILE) {
          // For a file, use the modify rule for the parent since we may need to include a reject
          // file
          resource = resource.getParent();
        }
        scheduleRule = ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(resource);
      }

      WorkspaceModifyOperation op =
          new WorkspaceModifyOperation(scheduleRule) {
            @Override
            protected void execute(IProgressMonitor monitor) throws InvocationTargetException {
              try {
                fPatcher.applyAll(
                    monitor,
                    new Patcher.IFileValidator() {
                      @Override
                      public boolean validateResources(IFile[] resoures) {
                        return Utilities.validateResources(
                            resoures, getShell(), PatchMessages.PatchWizard_title);
                      }
                    });
              } catch (CoreException e) {
                throw new InvocationTargetException(e);
              }
            }
          };
      getContainer().run(true, false, op);

    } catch (InvocationTargetException e) {
      ExceptionHandler.handle(
          e,
          PatchMessages.PatchWizard_title,
          PatchMessages.PatchWizard_unexpectedException_message);
    } catch (InterruptedException e) {
      // cannot happen
      // NeedWork: use assert!
    }

    // Save the dialog settings
    if (fHasNewDialogSettings) {
      IDialogSettings workbenchSettings = CompareUIPlugin.getDefault().getDialogSettings();
      IDialogSettings section = workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
      section = workbenchSettings.addNewSection(DIALOG_SETTINGS_KEY);
      setDialogSettings(section);
    }

    if (fPatchWizardPage != null) fPatchWizardPage.saveWidgetValues();
    fPreviewPage2.saveWidgetValues();
    return true;
  }
 public static IWorkbench getActiveWorkbench() {
   CompareUIPlugin plugin = getDefault();
   if (plugin == null) return null;
   return plugin.getWorkbench();
 }
  protected Object handleOpen() {
    if (mergeViewer.getAncestorTree() == this) {
      return null;
    }
    IStructuredSelection sel = (IStructuredSelection) getSelection();
    if (sel.isEmpty()) {
      return null;
    }
    Object current = sel.iterator().next();
    // if the current selection has an Action_Semantics field
    // the grab the necessary object element to open that, failing
    // that look for a description attribute
    if (current instanceof NonRootModelElementComparable) {
      NonRootModelElement nrme =
          (NonRootModelElement) ((NonRootModelElementComparable) current).getRealElement();
      if (nrme instanceof StateMachineState_c || nrme instanceof Transition_c) {
        // we need to navigate to the Action element
        // for the activity and description attributes
        Object[] children = ((ITreeContentProvider) getContentProvider()).getChildren(current);
        for (Object child : children) {
          if (child instanceof NonRootModelElementComparable) {
            current = child;
            break;
          }
        }
      }
      ObjectElement actionObjEle = null;
      ObjectElement descripObjEle = null;
      Object[] children = ((ITreeContentProvider) getContentProvider()).getChildren(current);
      for (Object child : children) {
        if (child instanceof ObjectElementComparable) {
          ObjectElementComparable comparable = (ObjectElementComparable) child;
          ObjectElement objElement = (ObjectElement) comparable.getRealElement();
          if (objElement.getName().equals("Action_Semantics")) {
            actionObjEle = objElement;
          } else {
            if (objElement.getName().equals("Descrip")) {
              descripObjEle = objElement;
            }
          }
        }
      }
      if (actionObjEle != null) {
        current = ComparableProvider.getComparableTreeObject(actionObjEle);
      } else {
        if (descripObjEle != null) {
          current = ComparableProvider.getComparableTreeObject(descripObjEle);
        }
      }
    }
    if (current instanceof ObjectElementComparable) {
      ObjectElementComparable comparable = (ObjectElementComparable) current;
      ObjectElement objElement = (ObjectElement) comparable.getRealElement();
      if (objElement.getName().equals("Descrip")
          || objElement.getName().equals("Action_Semantics")) {
        Object leftInput = getMergeViewer().getLeftViewer().getInput();
        Object rightInput = getMergeViewer().getRightViewer().getInput();
        TreeItem rightMatch = getMatchingItem(comparable, synchronizedViewers.get(0));
        TreeItem ancestorMatch = null;
        if (synchronizedViewers.size() == 2) {
          ancestorMatch = getMatchingItem(comparable, synchronizedViewers.get(1));
        }
        Object leftElement = comparable.getRealElement();
        Object rightElement = null;
        Object ancestor = null;
        if (ancestorMatch != null) {
          ancestor = ((ComparableTreeObject) ancestorMatch.getData()).getRealElement();
        }
        if (rightMatch != null) {
          rightElement = ((ComparableTreeObject) rightMatch.getData()).getRealElement();
        }
        if (mergeViewer.getLeftViewer() != this) {
          if (rightMatch == null) {
            leftElement = null;
          } else {
            leftElement = rightMatch.getData();
            leftElement = ((ComparableTreeObject) leftElement).getRealElement();
          }
          rightElement = comparable.getRealElement();
        }
        // create a compare dialog, using the text compare
        final CompareConfiguration compareConfiguration = new CompareConfiguration();
        boolean leftEditable =
            leftInput instanceof IEditableContent && ((IEditableContent) leftInput).isEditable();
        boolean rightEditable =
            rightInput instanceof IEditableContent && ((IEditableContent) rightInput).isEditable();
        // if this is a single file compare, do not allow editing
        // at this time as there is no easy way to place the content
        // changes back into the file
        if (ComparePlugin.getDefault()
            .getModelCacheManager()
            .isInputReadonly(ModelCacheManager.getLeftKey(mergeViewer.getInput()))) {
          compareConfiguration.setLeftEditable(false);
          compareConfiguration.setRightEditable(false);
        } else {
          compareConfiguration.setLeftEditable(leftEditable);
          compareConfiguration.setRightEditable(rightEditable);
        }
        final TextualAttributeCompareEditorInput compareInput =
            new TextualAttributeCompareEditorInput(
                compareConfiguration,
                (ObjectElement) leftElement,
                (ObjectElement) rightElement,
                (ObjectElement) ancestor,
                SynchronizedTreeViewer.this);
        if (CompareUIPlugin.getDefault().compareResultOK(compareInput, null)) {
          Runnable runnable =
              new Runnable() {
                public void run() {
                  CompareDialog dialog =
                      new CompareDialog(
                          PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                          compareInput) {

                        @Override
                        protected Button createButton(
                            Composite parent, int id, String label, boolean defaultButton) {
                          if (id == IDialogConstants.CANCEL_ID) {
                            return null;
                          } else {
                            return super.createButton(parent, id, label, defaultButton);
                          }
                        }
                      };
                  dialog.open();
                }
              };
          if (Display.getCurrent() == null) {
            Display.getDefault().syncExec(runnable);
          } else {
            runnable.run();
          }
        }
        return null;
      }
    }

    return null;
  }