View | Details | Raw Unified
Collapse All | Expand All

(-) plugin.xml (-1 / +1 lines)
 Lines 195-201    Link Here 
         <action
         <action
               label="%CompareWithPatchAction.label"
               label="%CompareWithPatchAction.label"
               tooltip="%CompareWithPatchAction.tooltip"
               tooltip="%CompareWithPatchAction.tooltip"
               class="org.eclipse.compare.internal.patch.CompareWithPatchAction"
               class="org.eclipse.compare.CompareWithPatchAction"
               menubarPath="team.main/group1"
               menubarPath="team.main/group1"
               enablesFor="1"
               enablesFor="1"
               id="compareWithPatch">
               id="compareWithPatch">
(-) compare/org/eclipse/compare/internal/CompareEditor.java (-56 / +1 lines)
 Lines 17-23    Link Here 
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.*;
import org.eclipse.jface.util.*;
import org.eclipse.jface.util.Assert;
import org.eclipse.jface.util.Assert;
 Lines 36-42    Link Here 
 * A CompareEditor takes a ICompareEditorInput as input.
 * A CompareEditor takes a ICompareEditorInput as input.
 * Most functionality is delegated to the ICompareEditorInput.
 * Most functionality is delegated to the ICompareEditorInput.
 */
 */
public class CompareEditor extends EditorPart implements IReusableEditor, ISaveableModelSource, ISaveableModel {
public class CompareEditor extends EditorPart implements IReusableEditor {
	
	
	/**
	/**
	 * Internal property change listener for handling changes in the editor's input.
	 * Internal property change listener for handling changes in the editor's input.
 Lines 258-272    Link Here 
	 */
	 */
	public boolean isDirty() {
	public boolean isDirty() {
		IEditorInput input= getEditorInput();
		IEditorInput input= getEditorInput();
		if (input instanceof ISaveableModelSource) {
			ISaveableModelSource sms = (ISaveableModelSource) input;
			ISaveableModel[] models = sms.getModels();
			for (int i = 0; i < models.length; i++) {
				ISaveableModel model = models[i];
				if (model.isDirty())
					return true;
			}
		}
		if (input instanceof CompareEditorInput)
		if (input instanceof CompareEditorInput)
			return ((CompareEditorInput)input).isSaveNeeded();
			return ((CompareEditorInput)input).isSaveNeeded();
		return false;
		return false;
 Lines 278-327    Link Here 
		if (old_value == null || new_value == null || !old_value.equals(new_value))
		if (old_value == null || new_value == null || !old_value.equals(new_value))
			firePropertyChange(PROP_DIRTY);
			firePropertyChange(PROP_DIRTY);
	}
	}
	/* (non-Javadoc)
	 * @see org.eclipse.ui.ISaveableModelSource#getModels()
	 */
	public ISaveableModel[] getModels() {
		IEditorInput input= getEditorInput();
		if (input instanceof ISaveableModelSource) {
			ISaveableModelSource source = (ISaveableModelSource) input;
			return source.getModels();
		}
		return new ISaveableModel[] { this };
	}
	/* (non-Javadoc)
	 * @see org.eclipse.ui.ISaveableModelSource#getActiveModels()
	 */
	public ISaveableModel[] getActiveModels() {
		IEditorInput input= getEditorInput();
		if (input instanceof ISaveableModelSource) {
			ISaveableModelSource source = (ISaveableModelSource) input;
			return source.getActiveModels();
		}
		return new ISaveableModel[] { this };
	}
	/* (non-Javadoc)
	 * @see org.eclipse.ui.ISaveableModel#getName()
	 */
	public String getName() {
		return getPartName();
	}
	/* (non-Javadoc)
	 * @see org.eclipse.ui.ISaveableModel#getToolTipText()
	 */
	public String getToolTipText() {
		return getTitleToolTip();
	}
	/* (non-Javadoc)
	 * @see org.eclipse.ui.ISaveableModel#getImageDescriptor()
	 */
	public ImageDescriptor getImageDescriptor() {
		return ImageDescriptor.createFromImage(getTitleImage());
	}
}
}
(-) compare/org/eclipse/compare/internal/patch/PatchTargetPage.java (-3 / +4 lines)
 Lines 12-17    Link Here 
import java.text.MessageFormat;
import java.text.MessageFormat;
import org.eclipse.compare.PatchReader;
import org.eclipse.compare.internal.ICompareContextIds;
import org.eclipse.compare.internal.ICompareContextIds;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFile;
 Lines 101-115    Link Here 
			InputPatchPage inputPage = (InputPatchPage) fPatchWizard.getPage(InputPatchPage.INPUTPATCHPAGE_NAME);
			InputPatchPage inputPage = (InputPatchPage) fPatchWizard.getPage(InputPatchPage.INPUTPATCHPAGE_NAME);
			String source = ""; //$NON-NLS-1$
			String source = ""; //$NON-NLS-1$
			switch (inputPage.getInputMethod()) {
			switch (inputPage.getInputMethod()) {
				case InputPatchPage.CLIPBOARD :
				case PatchReader.CLIPBOARD :
					source = PatchMessages.InputPatchPage_Clipboard_title;
					source = PatchMessages.InputPatchPage_Clipboard_title;
					break;
					break;
				case InputPatchPage.FILE :
				case PatchReader.FILE :
					source = PatchMessages.InputPatchPage_PatchFile_title;
					source = PatchMessages.InputPatchPage_PatchFile_title;
					break;
					break;
				case InputPatchPage.WORKSPACE :
				case PatchReader.WORKSPACE :
					source = PatchMessages.InputPatchPage_WorkspacePatch_title;
					source = PatchMessages.InputPatchPage_WorkspacePatch_title;
					break;
					break;
			}
			}
(-) compare/org/eclipse/compare/internal/patch/PatchWizard.java (-17 / +75 lines)
 Lines 10-17    Link Here 
 *******************************************************************************/
 *******************************************************************************/
package org.eclipse.compare.internal.patch;
package org.eclipse.compare.internal.patch;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.compare.PatchReader;
import org.eclipse.compare.internal.CompareUIPlugin;
import org.eclipse.compare.internal.CompareUIPlugin;
import org.eclipse.compare.internal.ExceptionHandler;
import org.eclipse.compare.internal.ExceptionHandler;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.compare.internal.Utilities;
 Lines 23-36    Link Here 
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
/* package */class PatchWizard extends Wizard {
/* package */public class PatchWizard extends Wizard {
	// dialog store id constants
	// dialog store id constants
	private final static String DIALOG_SETTINGS_KEY= "PatchWizard"; //$NON-NLS-1$
	public final static String DIALOG_SETTINGS_KEY= "PatchWizard"; //$NON-NLS-1$
	private boolean fHasNewDialogSettings;
	private boolean fHasNewDialogSettings;
	private int fPatchSource;
	private String fPatchFilePath;
	
	
	private InputPatchPage fPatchWizardPage;
	private InputPatchPage fPatchWizardPage;
 Lines 39-62    Link Here 
	/*
	/*
	 * Creates a wizard for applying a patch file to the workspace.
	 * Creates a wizard for applying a patch file to the workspace.
	 */
	 */
	/* package */PatchWizard(ISelection selection) {
	/* package */public PatchWizard(ISelection selection) {
		init(selection, PatchReader.UNKNOWN);
	}
	
	/*
	 * Creates a wizard for applying a patch file to the workspace with the option of skipping the
	 * patch input step.
	 */
	public PatchWizard(ISelection selection, int patchSource) {
		init(selection, patchSource);
	}
	
	private void init(ISelection selection, int patchSource) {
		setDefaultPageImageDescriptor(CompareUIPlugin.getImageDescriptor("wizban/applypatch_wizban.gif")); //$NON-NLS-1$
		setDefaultPageImageDescriptor(CompareUIPlugin.getImageDescriptor("wizban/applypatch_wizban.gif")); //$NON-NLS-1$
		setWindowTitle(PatchMessages.PatchWizard_title);
		setWindowTitle(PatchMessages.PatchWizard_title);
		fPatchSource = patchSource;
		
		fPatcher= new WorkspacePatcher();
		fPatcher= new WorkspacePatcher();
		setTarget(Utilities.getFirstResource(selection));
		setTarget(Utilities.getFirstResource(selection));
		
		IDialogSettings workbenchSettings= CompareUIPlugin.getDefault().getDialogSettings();
		if (patchSource == PatchReader.UNKNOWN) {
		IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
			IDialogSettings workbenchSettings= CompareUIPlugin.getDefault().getDialogSettings();
		if (section==null)
			IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
			fHasNewDialogSettings= true;
			if (section==null)
		else {
				fHasNewDialogSettings= true;
			fHasNewDialogSettings= false;
			else {
			setDialogSettings(section);
				fHasNewDialogSettings= false;
				setDialogSettings(section);
			}
		}
		}
	}
	}
	
	WorkspacePatcher getPatcher() {
	WorkspacePatcher getPatcher() {
		return fPatcher;
		return fPatcher;
	}
	}
 Lines 74-81    Link Here 
	 */
	 */
	public void addPages() {
	public void addPages() {
		super.addPages();
		super.addPages();
		
		fPatchWizardPage = new InputPatchPage(this);
		addPage(fPatchWizardPage= new InputPatchPage(this));
		try {
			switch (fPatchSource) {
			case PatchReader.UNKNOWN:
				addPage(fPatchWizardPage);
				break;
	
			case PatchReader.CLIPBOARD:
				Display display = this.getShell().getDisplay();
				if (display != null) {
					Clipboard clipboard = new Clipboard(display);
					PatchReader.readPatchFromClipboard(clipboard, fPatcher);
					clipboard.dispose();
				}
				break;
				
			case PatchReader.FILE:
				PatchReader.readPatchFromFile(fPatchFilePath, fPatcher);
				break;
				
			case PatchReader.WORKSPACE:
				PatchReader.readPatchFromFile(fPatchFilePath, fPatcher);
				break;
			}
		}
		catch (IOException e) {
			/* If we encounter an error reading a patch, add the input patch page */
			addPage(fPatchWizardPage);
		}
		addPage(new PatchTargetPage(this));
		addPage(new PatchTargetPage(this));
		addPage(new PreviewPatchPage(this));
		addPage(new PreviewPatchPage(this));
	}
	}
 Lines 92-98    Link Here 
	 */
	 */
	public boolean performFinish() {
	public boolean performFinish() {
		
		
		fPatcher.setName(fPatchWizardPage.getPatchName());
		if (fPatchSource == PatchReader.UNKNOWN) {
			fPatcher.setName(fPatchWizardPage.getPatchName());
		}
		try {
		try {
			//Create scheduling rule based on the type of patch - single or workspace
			//Create scheduling rule based on the type of patch - single or workspace
 Lines 133-142    Link Here 
			setDialogSettings(section);
			setDialogSettings(section);
		}
		}
		
		
		fPatchWizardPage.saveWidgetValues();
		if (fPatchSource == PatchReader.UNKNOWN) {
			fPatchWizardPage.saveWidgetValues();
		}
		
		//fPreviewPatchPage.saveWidgetValues();
		//fPreviewPatchPage.saveWidgetValues();
		
		
		return true;
		return true;
	}
	}
	public void setPatchFilePath(String filePath) {
		fPatchFilePath = filePath;
	}
}
}
(-) compare/org/eclipse/compare/internal/patch/CompareWithPatchAction.java (-174 lines)
Removed Link Here 
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.internal.patch;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import org.eclipse.compare.internal.BaseCompareAction;
import org.eclipse.compare.internal.ComparePreferencePage;
import org.eclipse.compare.internal.CompareUIPlugin;
import org.eclipse.compare.internal.ExceptionHandler;
import org.eclipse.compare.internal.ListContentProvider;
import org.eclipse.compare.internal.ListDialog;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.util.Assert;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
public class CompareWithPatchAction extends BaseCompareAction {
	static class PatchWizardDialog extends WizardDialog {
	
		private static final String PATCH_WIZARD_SETTINGS_SECTION = "PatchWizard"; //$NON-NLS-1$
		PatchWizardDialog(Shell parent, IWizard wizard) {
			super(parent, wizard);
			
			setShellStyle(getShellStyle() | SWT.RESIZE);
			setMinimumPageSize(700, 500);
		}
		
		protected IDialogSettings getDialogBoundsSettings() {
	        IDialogSettings settings = CompareUIPlugin.getDefault().getDialogSettings();
	        IDialogSettings section = settings.getSection(PATCH_WIZARD_SETTINGS_SECTION);
	        if (section == null) {
	            section = settings.addNewSection(PATCH_WIZARD_SETTINGS_SECTION);
	        } 
	        return section;
		}
	}
	
	protected boolean isEnabled(ISelection selection) {
		return Utilities.getResources(selection).length == 1;
	}
		
	/* (non-Javadoc)
	 * @see org.eclipse.compare.internal.BaseCompareAction#run(org.eclipse.jface.viewers.ISelection)
	 */
	protected void run(ISelection selection) {
		PatchWizard wizard= new PatchWizard(selection);
		
		if (areAllEditorsSaved()) {
			PatchWizardDialog dialog= new PatchWizardDialog(CompareUIPlugin.getShell(), wizard);
			dialog.open();
		}
	}
	private boolean areAllEditorsSaved(){
		if (CompareUIPlugin.getDirtyEditors().length == 0)
			return true;
		if (! saveAllDirtyEditors())
			return false;
		Shell shell= CompareUIPlugin.getShell();
		try {
			// Save isn't cancelable.
			IWorkspace workspace= ResourcesPlugin.getWorkspace();
			IWorkspaceDescription description= workspace.getDescription();
			boolean autoBuild= description.isAutoBuilding();
			description.setAutoBuilding(false);
			workspace.setDescription(description);
			try {
				new ProgressMonitorDialog(shell).run(false, false, createRunnable());
			} finally {
				description.setAutoBuilding(autoBuild);
				workspace.setDescription(description);
			}
			return true;
		} catch (InvocationTargetException e) {
			ExceptionHandler.handle(e, shell, PatchMessages.PatchAction_ExceptionTitle, PatchMessages.PatchAction_Exception);  
			return false;
		} catch (CoreException e) {
			ExceptionHandler.handle(e, shell, PatchMessages.PatchAction_ExceptionTitle, PatchMessages.PatchAction_Exception);  
			return false;			
		} catch (InterruptedException e) {
			Assert.isTrue(false); // Can't happen. Operation isn't cancelable.
			return false;
		}
	}
	private IRunnableWithProgress createRunnable() {
		return new IRunnableWithProgress() {
			public void run(IProgressMonitor pm) {
				IEditorPart[] editorsToSave= CompareUIPlugin.getDirtyEditors();
				pm.beginTask(PatchMessages.PatchAction_SavingDirtyEditorsTask, editorsToSave.length); 
				for (int i= 0; i < editorsToSave.length; i++) {
					editorsToSave[i].doSave(new SubProgressMonitor(pm, 1));
					pm.worked(1);
				}
				pm.done();
			}
		};
	}
	private boolean saveAllDirtyEditors() {
		if (ComparePreferencePage.getSaveAllEditors()) //must save everything
			return true;
		ListDialog dialog= new ListDialog(CompareUIPlugin.getShell()) {
			protected Control createDialogArea(Composite parent) {
				Composite result= (Composite) super.createDialogArea(parent);
				final Button check= new Button(result, SWT.CHECK);
				check.setText(PatchMessages.PatchAction_AlwaysSaveQuestion); 
				check.setSelection(ComparePreferencePage.getSaveAllEditors());
				check.addSelectionListener(
					new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							ComparePreferencePage.setSaveAllEditors(check.getSelection());
						}
					}
				);
				applyDialogFont(result);
				return result;
			}
		};
		dialog.setTitle(PatchMessages.PatchAction_SaveAllQuestion); 
		dialog.setAddCancelButton(true);
		dialog.setLabelProvider(createDialogLabelProvider());
		dialog.setMessage(PatchMessages.PatchAction_SaveAllDescription); 
		dialog.setContentProvider(new ListContentProvider());
		dialog.setInput(Arrays.asList(CompareUIPlugin.getDirtyEditors()));
		return dialog.open() == Window.OK;
	}
	private ILabelProvider createDialogLabelProvider() {
		return new LabelProvider() {
			public Image getImage(Object element) {
				return ((IEditorPart) element).getTitleImage();
			}
			public String getText(Object element) {
				return ((IEditorPart) element).getTitle();
			}
		};
	}
}
(-) compare/org/eclipse/compare/internal/patch/InputPatchPage.java (-60 / +37 lines)
 Lines 19-24    Link Here 
import java.io.StringReader;
import java.io.StringReader;
import java.text.MessageFormat;
import java.text.MessageFormat;
import org.eclipse.compare.PatchReader;
import org.eclipse.compare.internal.ICompareContextIds;
import org.eclipse.compare.internal.ICompareContextIds;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFile;
 Lines 72-82    Link Here 
	private final static String STORE_PATCH_FILES_ID= PAGE_NAME+".PATCH_FILES"; //$NON-NLS-1$
	private final static String STORE_PATCH_FILES_ID= PAGE_NAME+".PATCH_FILES"; //$NON-NLS-1$
	private final static String STORE_INPUT_METHOD_ID= PAGE_NAME+".INPUT_METHOD"; //$NON-NLS-1$
	private final static String STORE_INPUT_METHOD_ID= PAGE_NAME+".INPUT_METHOD"; //$NON-NLS-1$
	private final static String STORE_WORKSPACE_PATH_ID= PAGE_NAME+".WORKSPACE_PATH"; //$NON-NLS-1$
	private final static String STORE_WORKSPACE_PATH_ID= PAGE_NAME+".WORKSPACE_PATH"; //$NON-NLS-1$
	//patch input constants
	
	protected final static int CLIPBOARD= 1;
	protected final static int FILE= 2;
	protected final static int WORKSPACE= 3;
	static final char SEPARATOR= System.getProperty("file.separator").charAt(0); //$NON-NLS-1$
	static final char SEPARATOR= System.getProperty("file.separator").charAt(0); //$NON-NLS-1$
	private boolean fShowError= false;
	private boolean fShowError= false;
 Lines 102-112    Link Here 
			//Allow error messages if the selected input actually has something selected in it
			//Allow error messages if the selected input actually has something selected in it
			fShowError=true;
			fShowError=true;
			switch(getInputMethod()){
			switch(getInputMethod()){
				case FILE:
				case PatchReader.FILE:
				fShowError = (fPatchFileNameField.getText() != "");  //$NON-NLS-1$
				fShowError = (fPatchFileNameField.getText() != "");  //$NON-NLS-1$
				break;
				break;
			    
			    
				case WORKSPACE:
				case PatchReader.WORKSPACE:
				fShowError = (!fTreeViewer.getSelection().isEmpty());
				fShowError = (!fTreeViewer.getSelection().isEmpty());
				break;
				break;
				
				
 Lines 131-137    Link Here 
	}
	}
	/* package */ String getPatchName() {
	/* package */ String getPatchName() {
		if (getInputMethod()==CLIPBOARD)
		if (getInputMethod()==PatchReader.CLIPBOARD)
			return PatchMessages.InputPatchPage_Clipboard;
			return PatchMessages.InputPatchPage_Clipboard;
		return getPatchFilePath();
		return getPatchFilePath();
	}
	}
 Lines 176-200    Link Here 
		String source= ""; //$NON-NLS-1$
		String source= ""; //$NON-NLS-1$
		// Create a reader for the input
		Reader reader= null;
		try {
		try {
			int inputMethod= getInputMethod();
			int inputMethod= getInputMethod();
			if (inputMethod==CLIPBOARD) {
			if (inputMethod==PatchReader.CLIPBOARD) {
				Control c= getControl();
				Control c= getControl();
				if (c != null) {
				if (c != null) {
					Clipboard clipboard= new Clipboard(c.getDisplay());
					Clipboard clipboard= new Clipboard(c.getDisplay());
					Object o= clipboard.getContents(TextTransfer.getInstance());
					PatchReader.readPatchFromClipboard(clipboard, patcher);
					clipboard.dispose();
					if (o instanceof String)
						reader= new StringReader((String)o);
				}
				}
				source= PatchMessages.InputPatchPage_Clipboard_title;
				source= PatchMessages.InputPatchPage_Clipboard_title;
			} else if (inputMethod==FILE) {
			} else if (inputMethod==PatchReader.FILE) {
				String patchFilePath= getPatchFilePath();
				String patchFilePath= getPatchFilePath();
				if (patchFilePath != null) {
				if (patchFilePath != null) {
					try {
					try {
						reader= new FileReader(patchFilePath);
						PatchReader.readPatchFromFile(patchFilePath, patcher);
					} catch (FileNotFoundException ex) {
					} catch (FileNotFoundException ex) {
						MessageDialog.openError(null,
						MessageDialog.openError(null,
							PatchMessages.InputPatchPage_PatchErrorDialog_title,	
							PatchMessages.InputPatchPage_PatchErrorDialog_title,	
 Lines 202-214    Link Here 
					}
					}
				}
				}
				source= PatchMessages.InputPatchPage_PatchFile_title;
				source= PatchMessages.InputPatchPage_PatchFile_title;
			} else if (inputMethod==WORKSPACE) {
			} else if (inputMethod==PatchReader.WORKSPACE) {
				//Get the selected patch file (tree will only allow for one selection)
				//Get the selected patch file (tree will only allow for one selection)
				IResource[] resources= Utilities.getResources(fTreeViewer.getSelection());
				IResource[] resources= Utilities.getResources(fTreeViewer.getSelection());
				IResource patchFile= resources[0];
				IResource patchFile= resources[0];
				if (patchFile!=null) {
				if (patchFile!=null) {
					try {
					try {
						reader= new FileReader(patchFile.getRawLocation().toFile());
						PatchReader.readPatchFromFile(patchFile.getRawLocation(), patcher);
					} catch (FileNotFoundException ex) {
					} catch (FileNotFoundException ex) {
						MessageDialog.openError(null, PatchMessages.InputPatchPage_PatchErrorDialog_title, PatchMessages.InputPatchPage_PatchFileNotFound_message);
						MessageDialog.openError(null, PatchMessages.InputPatchPage_PatchErrorDialog_title, PatchMessages.InputPatchPage_PatchFileNotFound_message);
					} catch (NullPointerException nex) {
					} catch (NullPointerException nex) {
 Lines 218-242    Link Here 
				}
				}
				source= PatchMessages.InputPatchPage_WorkspacePatch_title;
				source= PatchMessages.InputPatchPage_WorkspacePatch_title;
			}
			}
			
		} catch (IOException e) {
			// parse the input
			MessageDialog.openError(null,
			if (reader != null) {
					PatchMessages.InputPatchPage_PatchErrorDialog_title, 
				try {
					PatchMessages.InputPatchPage_ParseError_message); 
					patcher.parse(new BufferedReader(reader));
				} catch (IOException ex) {
					MessageDialog.openError(null,
						PatchMessages.InputPatchPage_PatchErrorDialog_title, 
						PatchMessages.InputPatchPage_ParseError_message); 
				}
			}
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException x) {
					// silently ignored
				}
			}
		}
		}
		
		
		Diff[] diffs= patcher.getDiffs();
		Diff[] diffs= patcher.getDiffs();
 Lines 340-347    Link Here 
				clearErrorMessage();
				clearErrorMessage();
				fShowError = (fPatchFileNameField.getText() != ""); //$NON-NLS-1$
				fShowError = (fPatchFileNameField.getText() != ""); //$NON-NLS-1$
				int state= getInputMethod();
				int state= getInputMethod();
				setEnablePatchFile(state==FILE);
				setEnablePatchFile(state==PatchReader.FILE);
				setEnableWorkspacePatch(state==WORKSPACE);
				setEnableWorkspacePatch(state==PatchReader.WORKSPACE);
				updateWidgetEnablements();
				updateWidgetEnablements();
			}
			}
		});
		});
 Lines 374-381    Link Here 
				//If there is anything typed in at all
				//If there is anything typed in at all
				fShowError = (!fTreeViewer.getSelection().isEmpty());
				fShowError = (!fTreeViewer.getSelection().isEmpty());
				int state= getInputMethod();
				int state= getInputMethod();
				setEnablePatchFile(state==FILE);
				setEnablePatchFile(state==PatchReader.FILE);
				setEnableWorkspacePatch(state==WORKSPACE);
				setEnableWorkspacePatch(state==PatchReader.WORKSPACE);
				updateWidgetEnablements();
				updateWidgetEnablements();
			}
			}
		});
		});
 Lines 423-429    Link Here 
		boolean gotPatch= false;
		boolean gotPatch= false;
		int inputMethod= getInputMethod();
		int inputMethod= getInputMethod();
		if (inputMethod==CLIPBOARD) {
		if (inputMethod==PatchReader.CLIPBOARD) {
			Control c= getControl();
			Control c= getControl();
			if (c != null) {
			if (c != null) {
				Clipboard clipboard= new Clipboard(c.getDisplay());
				Clipboard clipboard= new Clipboard(c.getDisplay());
 Lines 439-445    Link Here 
					error= PatchMessages.InputPatchPage_NoTextInClipboard_message;					 
					error= PatchMessages.InputPatchPage_NoTextInClipboard_message;					 
			} else
			} else
				error= PatchMessages.InputPatchPage_CouldNotReadClipboard_message;
				error= PatchMessages.InputPatchPage_CouldNotReadClipboard_message;
		} else if (inputMethod==FILE) {
		} else if (inputMethod==PatchReader.FILE) {
			String path= fPatchFileNameField.getText();
			String path= fPatchFileNameField.getText();
			if (path != null && path.length() > 0) {
			if (path != null && path.length() > 0) {
				File file= new File(path);
				File file= new File(path);
 Lines 449-455    Link Here 
			} else {
			} else {
				error= PatchMessages.InputPatchPage_NoFileName_message; 
				error= PatchMessages.InputPatchPage_NoFileName_message; 
			}
			}
		} else if (inputMethod==WORKSPACE) {
		} else if (inputMethod==PatchReader.WORKSPACE) {
			//Get the selected patch file (tree will only allow for one selection)
			//Get the selected patch file (tree will only allow for one selection)
			IResource[] resources= Utilities.getResources(fTreeViewer.getSelection());
			IResource[] resources= Utilities.getResources(fTreeViewer.getSelection());
			if (resources!=null&&resources.length>0) {
			if (resources!=null&&resources.length>0) {
 Lines 567-573    Link Here 
	 */
	 */
	private void restoreWidgetValues() {
	private void restoreWidgetValues() {
		int inputMethod= FILE;
		int inputMethod= PatchReader.FILE;
		IDialogSettings settings= getDialogSettings();
		IDialogSettings settings= getDialogSettings();
		if (settings != null) {
		if (settings != null) {
 Lines 594-601    Link Here 
			//if there is a valid patch on the clipboard. This will be done in adjustToCurrentTarget()
			//if there is a valid patch on the clipboard. This will be done in adjustToCurrentTarget()
			//so just set it to FILE now and, if there exists a patch on the clipboard, then clipboard
			//so just set it to FILE now and, if there exists a patch on the clipboard, then clipboard
			//will be selected automatically
			//will be selected automatically
			if (inputMethod == CLIPBOARD){
			if (inputMethod == PatchReader.CLIPBOARD){
				inputMethod=FILE;
				inputMethod=PatchReader.FILE;
				fPatchFileNameField.deselectAll();
				fPatchFileNameField.deselectAll();
			}
			}
			
			
 Lines 613-620    Link Here 
				//check to see if the current input is set to workspace - if it is switch it
				//check to see if the current input is set to workspace - if it is switch it
				//back to clipboard since there is no corresponding element to go along with 
				//back to clipboard since there is no corresponding element to go along with 
				//the tree viewer
				//the tree viewer
				if (inputMethod == WORKSPACE)
				if (inputMethod == PatchReader.WORKSPACE)
					inputMethod=FILE;
					inputMethod=PatchReader.FILE;
			}
			}
		}
		}
 Lines 682-688    Link Here 
				if (isPatchFile(reader)) {
				if (isPatchFile(reader)) {
					//Set choice to workspace
					//Set choice to workspace
					setInputButtonState(WORKSPACE);
					setInputButtonState(PatchReader.WORKSPACE);
					if (fTreeViewer != null && patchTarget.exists()) {
					if (fTreeViewer != null && patchTarget.exists()) {
						fTreeViewer.expandToLevel(patchTarget, 0);
						fTreeViewer.expandToLevel(patchTarget, 0);
						fTreeViewer.setSelection(new StructuredSelection(patchTarget));
						fTreeViewer.setSelection(new StructuredSelection(patchTarget));
 Lines 709-715    Link Here 
					if (o instanceof String) {
					if (o instanceof String) {
						reader = new StringReader((String) o);
						reader = new StringReader((String) o);
						if (isPatchFile(reader)) {
						if (isPatchFile(reader)) {
							setInputButtonState(CLIPBOARD);
							setInputButtonState(PatchReader.CLIPBOARD);
							return true;
							return true;
						}
						}
					}
					}
 Lines 753-788    Link Here 
	private void setInputButtonState(int state) {
	private void setInputButtonState(int state) {
		switch (state) {
		switch (state) {
			case CLIPBOARD :
			case PatchReader.CLIPBOARD :
				fUseClipboardButton.setSelection(true);
				fUseClipboardButton.setSelection(true);
				fUsePatchFileButton.setSelection(false);
				fUsePatchFileButton.setSelection(false);
				fUseWorkspaceButton.setSelection(false);
				fUseWorkspaceButton.setSelection(false);
				break;
				break;
			case FILE :
			case PatchReader.FILE :
				fUseClipboardButton.setSelection(false);
				fUseClipboardButton.setSelection(false);
				fUsePatchFileButton.setSelection(true);
				fUsePatchFileButton.setSelection(true);
				fUseWorkspaceButton.setSelection(false);
				fUseWorkspaceButton.setSelection(false);
				break;
				break;
			case WORKSPACE :
			case PatchReader.WORKSPACE :
				fUseClipboardButton.setSelection(false);
				fUseClipboardButton.setSelection(false);
				fUsePatchFileButton.setSelection(false);
				fUsePatchFileButton.setSelection(false);
				fUseWorkspaceButton.setSelection(true);
				fUseWorkspaceButton.setSelection(true);
				break;
				break;
		}
		}
		setEnablePatchFile(state==FILE);
		setEnablePatchFile(state==PatchReader.FILE);
		setEnableWorkspacePatch(state==WORKSPACE);
		setEnableWorkspacePatch(state==PatchReader.WORKSPACE);
	}
	}
	protected int getInputMethod() {
	protected int getInputMethod() {
		if (fUseClipboardButton.getSelection())
		if (fUseClipboardButton.getSelection())
			return CLIPBOARD;
			return PatchReader.CLIPBOARD;
		else if (fUsePatchFileButton.getSelection())
		else if (fUsePatchFileButton.getSelection())
			return FILE;
			return PatchReader.FILE;
		else
		else
			return WORKSPACE;
			return PatchReader.WORKSPACE;
	}
	}
	private String getPatchFilePath() {
	private String getPatchFilePath() {
(-) compare/org/eclipse/compare/PatchReader.java (+109 lines)
Added Link Here 
package org.eclipse.compare;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.eclipse.compare.internal.patch.WorkspacePatcher;
import org.eclipse.core.runtime.IPath;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
public class PatchReader {
	//patch input constants
	public final static int UNKNOWN = -1;
	public final static int CLIPBOARD = 1;
	public final static int FILE = 2;
	public final static int WORKSPACE = 3;	
	
	/**
	 * Populate the given patcher by reading a patch from the clipboard.
	 * 
	 * @param clipboard
	 * @param patcher
	 * @throws IOException 
	 */
	public static void readPatchFromClipboard(Clipboard clipboard, WorkspacePatcher patcher) 
	throws IOException { 
		
		Reader reader = null;
		try {		
			if (clipboard != null) {
				Object o= clipboard.getContents(TextTransfer.getInstance());
				clipboard.dispose();
				if (o instanceof String)
					reader= new StringReader((String)o);
			}
			
			// parse the input
			if (reader != null) {
				patcher.parse(new BufferedReader(reader));
			}
		} 
		finally {
			if (reader != null) {
				reader.close();
			}
		}
	}
	
	/**
	 * Populate the given patcher by reading a patch from the file path.
	 * 
	 * @param filePath
	 * @param patcher
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public static void readPatchFromFile(String filePath, WorkspacePatcher patcher) 
	throws IOException, FileNotFoundException {
		Reader reader = null;
		try {
			reader = new FileReader(filePath);
			
			// parse the input
			if (reader != null) {
				patcher.parse(new BufferedReader(reader));
			}
		}
		finally {
			if (reader != null) {
				reader.close();
			}
		}
	}
	/**
	 * Populate the given patcher by reading a patch from the IPath.
	 * 
	 * @param path
	 * @param patcher
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws NullPointerException 
	 */
	public static void readPatchFromFile(IPath path, WorkspacePatcher patcher) 
	throws IOException, FileNotFoundException, NullPointerException {
		Reader reader = null;
		try {
			reader = new FileReader(path.toFile());
			
			// parse the input
			if (reader != null) {
				patcher.parse(new BufferedReader(reader));
			}
		}
		finally {
			if (reader != null) {
				reader.close();
			}
		}
	}
}
(-) compare/org/eclipse/compare/CompareWithPatchAction.java (+199 lines)
Added Link Here 
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import org.eclipse.compare.internal.BaseCompareAction;
import org.eclipse.compare.internal.ComparePreferencePage;
import org.eclipse.compare.internal.CompareUIPlugin;
import org.eclipse.compare.internal.ExceptionHandler;
import org.eclipse.compare.internal.ListContentProvider;
import org.eclipse.compare.internal.ListDialog;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.compare.internal.patch.PatchMessages;
import org.eclipse.compare.internal.patch.PatchWizard;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.util.Assert;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
public class CompareWithPatchAction extends BaseCompareAction {
	private int patchSource = PatchReader.UNKNOWN;
	private String filePath;
	
	static class PatchWizardDialog extends WizardDialog {
	
		private static final String PATCH_WIZARD_SETTINGS_SECTION = "PatchWizard"; //$NON-NLS-1$
		PatchWizardDialog(Shell parent, IWizard wizard) {
			super(parent, wizard);
			
			setShellStyle(getShellStyle() | SWT.RESIZE);
			setMinimumPageSize(700, 500);
		}
		
		protected IDialogSettings getDialogBoundsSettings() {
	        IDialogSettings settings = CompareUIPlugin.getDefault().getDialogSettings();
	        IDialogSettings section = settings.getSection(PATCH_WIZARD_SETTINGS_SECTION);
	        if (section == null) {
	            section = settings.addNewSection(PATCH_WIZARD_SETTINGS_SECTION);
	        } 
	        return section;
		}
	}
	
	protected boolean isEnabled(ISelection selection) {
		return Utilities.getResources(selection).length == 1;
	}
		
	/* (non-Javadoc)
	 * @see org.eclipse.compare.internal.BaseCompareAction#run(org.eclipse.jface.viewers.ISelection)
	 */
	protected void run(ISelection selection) {
		PatchWizard wizard = null;
		wizard= new PatchWizard(selection, patchSource);
		wizard.setPatchFilePath(filePath);
		
		if (areAllEditorsSaved()) {
			PatchWizardDialog dialog= new PatchWizardDialog(CompareUIPlugin.getShell(), wizard);
			dialog.open();
		}
	}
	private boolean areAllEditorsSaved(){
		if (CompareUIPlugin.getDirtyEditors().length == 0)
			return true;
		if (! saveAllDirtyEditors())
			return false;
		Shell shell= CompareUIPlugin.getShell();
		try {
			// Save isn't cancelable.
			IWorkspace workspace= ResourcesPlugin.getWorkspace();
			IWorkspaceDescription description= workspace.getDescription();
			boolean autoBuild= description.isAutoBuilding();
			description.setAutoBuilding(false);
			workspace.setDescription(description);
			try {
				new ProgressMonitorDialog(shell).run(false, false, createRunnable());
			} finally {
				description.setAutoBuilding(autoBuild);
				workspace.setDescription(description);
			}
			return true;
		} catch (InvocationTargetException e) {
			ExceptionHandler.handle(e, shell, PatchMessages.PatchAction_ExceptionTitle, PatchMessages.PatchAction_Exception);  
			return false;
		} catch (CoreException e) {
			ExceptionHandler.handle(e, shell, PatchMessages.PatchAction_ExceptionTitle, PatchMessages.PatchAction_Exception);  
			return false;			
		} catch (InterruptedException e) {
			Assert.isTrue(false); // Can't happen. Operation isn't cancelable.
			return false;
		}
	}
	private IRunnableWithProgress createRunnable() {
		return new IRunnableWithProgress() {
			public void run(IProgressMonitor pm) {
				IEditorPart[] editorsToSave= CompareUIPlugin.getDirtyEditors();
				pm.beginTask(PatchMessages.PatchAction_SavingDirtyEditorsTask, editorsToSave.length); 
				for (int i= 0; i < editorsToSave.length; i++) {
					editorsToSave[i].doSave(new SubProgressMonitor(pm, 1));
					pm.worked(1);
				}
				pm.done();
			}
		};
	}
	private boolean saveAllDirtyEditors() {
		if (ComparePreferencePage.getSaveAllEditors()) //must save everything
			return true;
		ListDialog dialog= new ListDialog(CompareUIPlugin.getShell()) {
			protected Control createDialogArea(Composite parent) {
				Composite result= (Composite) super.createDialogArea(parent);
				final Button check= new Button(result, SWT.CHECK);
				check.setText(PatchMessages.PatchAction_AlwaysSaveQuestion); 
				check.setSelection(ComparePreferencePage.getSaveAllEditors());
				check.addSelectionListener(
					new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							ComparePreferencePage.setSaveAllEditors(check.getSelection());
						}
					}
				);
				applyDialogFont(result);
				return result;
			}
		};
		dialog.setTitle(PatchMessages.PatchAction_SaveAllQuestion); 
		dialog.setAddCancelButton(true);
		dialog.setLabelProvider(createDialogLabelProvider());
		dialog.setMessage(PatchMessages.PatchAction_SaveAllDescription); 
		dialog.setContentProvider(new ListContentProvider());
		dialog.setInput(Arrays.asList(CompareUIPlugin.getDirtyEditors()));
		return dialog.open() == Window.OK;
	}
	private ILabelProvider createDialogLabelProvider() {
		return new LabelProvider() {
			public Image getImage(Object element) {
				return ((IEditorPart) element).getTitleImage();
			}
			public String getText(Object element) {
				return ((IEditorPart) element).getTitle();
			}
		};
	}
	
	/**
	 * Set the patch dialog settings to be used in the apply patch dialog.
     *
	 * @param source
	 */
	public void setPatchSource(int source) {
		this.patchSource = source;
	}
	
	/**
	 * Set the file path to the patch.
	 * 
	 * @param filePath
	 */
	public void setPatchFilePath(String filePath) {
		this.filePath = filePath;
	}
}