1 11 package org.eclipse.ltk.internal.ui.refactoring.model; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.OutputStream ; 16 import java.util.HashSet ; 17 import java.util.Set ; 18 19 import org.eclipse.team.core.mapping.IStorageMerger; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Status; 25 26 import org.eclipse.core.resources.IStorage; 27 28 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 29 import org.eclipse.ltk.core.refactoring.RefactoringSessionDescriptor; 30 31 import org.eclipse.ltk.internal.core.refactoring.IRefactoringSerializationConstants; 32 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryManager; 33 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages; 34 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin; 35 36 import org.eclipse.compare.IStreamMerger; 37 38 43 public final class RefactoringHistoryMerger implements IStreamMerger, IStorageMerger { 44 45 48 public RefactoringHistoryMerger() { 49 } 51 52 55 public boolean canMergeWithoutAncestor() { 56 return true; 57 } 58 59 62 public IStatus merge(final OutputStream output, final String outputEncoding, final InputStream ancestor, final String ancestorEncoding, final InputStream target, final String targetEncoding, final InputStream source, final String sourceEncoding, final IProgressMonitor monitor) { 63 try { 64 performMerge(output, target, source); 65 } catch (CoreException exception) { 66 return new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(), 1, RefactoringUIMessages.RefactoringHistoryMerger_error_auto_merge, exception); 67 } 68 return Status.OK_STATUS; 69 } 70 71 74 public IStatus merge(final OutputStream output, final String encoding, final IStorage ancestor, final IStorage target, final IStorage other, final IProgressMonitor monitor) throws CoreException { 75 InputStream targetStream= null; 76 InputStream sourceStream= null; 77 try { 78 targetStream= target.getContents(); 79 sourceStream= target.getContents(); 80 performMerge(output, targetStream, sourceStream); 81 } catch (CoreException exception) { 82 return new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(), 1, RefactoringUIMessages.RefactoringHistoryMerger_error_auto_merge, exception); 83 } finally { 84 if (targetStream != null) { 85 try { 86 targetStream.close(); 87 } catch (IOException exception) { 88 } 90 } 91 if (sourceStream != null) { 92 try { 93 sourceStream.close(); 94 } catch (IOException exception) { 95 } 97 } 98 } 99 return Status.OK_STATUS; 100 } 101 102 114 private void performMerge(final OutputStream output, final InputStream target, final InputStream source) throws CoreException { 115 final RefactoringDescriptor[] sourceDescriptors= RefactoringHistoryManager.readRefactoringDescriptors(source); 116 final RefactoringDescriptor[] targetDescriptors= RefactoringHistoryManager.readRefactoringDescriptors(target); 117 final Set set= new HashSet (); 118 for (int index= 0; index < sourceDescriptors.length; index++) 119 set.add(sourceDescriptors[index]); 120 for (int index= 0; index < targetDescriptors.length; index++) 121 set.add(targetDescriptors[index]); 122 final RefactoringDescriptor[] outputDescriptors= new RefactoringDescriptor[set.size()]; 123 set.toArray(outputDescriptors); 124 RefactoringHistoryManager.sortRefactoringDescriptorsAscending(outputDescriptors); 125 RefactoringHistoryManager.writeRefactoringSession(output, new RefactoringSessionDescriptor(outputDescriptors, IRefactoringSerializationConstants.CURRENT_VERSION, null), true); 126 } 127 } 128 | Popular Tags |