1 11 package org.eclipse.team.internal.ccvs.ui.wizards; 12 13 14 import java.io.ByteArrayOutputStream ; 15 import java.io.File ; 16 import java.io.FileNotFoundException ; 17 import java.io.FileOutputStream ; 18 import java.io.IOException ; 19 import java.io.PrintStream ; 20 import java.lang.reflect.InvocationTargetException ; 21 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.runtime.IProgressMonitor; 24 import org.eclipse.core.runtime.SubProgressMonitor; 25 import org.eclipse.jface.dialogs.MessageDialog; 26 import org.eclipse.jface.operation.IRunnableWithProgress; 27 import org.eclipse.swt.dnd.Clipboard; 28 import org.eclipse.swt.dnd.TextTransfer; 29 import org.eclipse.swt.dnd.Transfer; 30 import org.eclipse.swt.widgets.Shell; 31 import org.eclipse.team.core.RepositoryProvider; 32 import org.eclipse.team.core.TeamException; 33 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin; 34 import org.eclipse.team.internal.ccvs.core.CVSTeamProvider; 35 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption; 36 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages; 37 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin; 38 39 44 public class GenerateDiffFileOperation implements IRunnableWithProgress { 45 46 private File outputFile; 47 private IResource resource; 48 private Shell shell; 49 private LocalOption[] options; 50 51 GenerateDiffFileOperation(IResource resource, File file, LocalOption[] options, Shell shell) { 52 this.resource = resource; 53 this.outputFile = file; 54 this.shell = shell; 55 this.options = options; 56 } 57 58 61 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 62 63 final CVSTeamProvider provider = (CVSTeamProvider)RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId()); 64 monitor.beginTask("", 500); monitor.setTaskName(CVSUIMessages.GenerateCVSDiff_working); try { 67 if (outputFile != null) { 68 generateDiffToFile(monitor, provider, outputFile); 69 } else { 70 generateDiffToClipboard(monitor, provider); 71 } 72 } catch (TeamException e) { 73 throw new InvocationTargetException (e); 74 } finally { 75 monitor.done(); 76 } 77 } 78 79 private void generateDiffToFile(IProgressMonitor monitor, CVSTeamProvider provider, File file) throws TeamException { 80 81 final FileOutputStream os; 82 try { 83 os= new FileOutputStream (file); 84 try { 85 provider.diff(resource, options, new PrintStream (os), new SubProgressMonitor(monitor, 500)); 86 } finally { 87 os.close(); 88 } 89 } catch (FileNotFoundException e) { 90 throw new TeamException(CVSUIMessages.GenerateDiffFileOperation_0, e); } catch (IOException e) { 92 throw new TeamException(CVSUIMessages.GenerateDiffFileOperation_1, e); } 94 95 if (file.length() == 0) { 96 outputFile.delete(); 97 reportEmptyDiff(); 98 } 99 } 100 101 private void generateDiffToClipboard(IProgressMonitor monitor, CVSTeamProvider provider) throws TeamException { 102 final ByteArrayOutputStream os = new ByteArrayOutputStream (); 103 try { 104 try { 105 provider.diff(resource, options, new PrintStream (os), new SubProgressMonitor(monitor, 500)); 106 } finally { 107 os.close(); 108 } 109 } catch (IOException e) { 110 throw new TeamException(CVSUIMessages.GenerateDiffFileOperation_2, e); } 112 if (os.size() == 0) { 113 reportEmptyDiff(); 114 } else { 115 copyToClipboard(os); 116 } 117 } 118 119 private void copyToClipboard(final ByteArrayOutputStream baos) { 120 shell.getDisplay().syncExec(new Runnable () { 121 public void run() { 122 TextTransfer plainTextTransfer = TextTransfer.getInstance(); 123 Clipboard clipboard = new Clipboard(shell.getDisplay()); 124 clipboard.setContents( 125 new String []{baos.toString()}, 126 new Transfer[]{plainTextTransfer}); 127 clipboard.dispose(); 128 } 129 }); 130 } 131 132 private void reportEmptyDiff() { 133 CVSUIPlugin.openDialog(shell, new CVSUIPlugin.IOpenableInShell() { 134 public void open(Shell shell) { 135 MessageDialog.openInformation( 136 shell, 137 CVSUIMessages.GenerateCVSDiff_noDiffsFoundTitle, CVSUIMessages.GenerateCVSDiff_noDiffsFoundMsg); } 140 }, CVSUIPlugin.PERFORM_SYNC_EXEC); 141 } 142 } 143 | Popular Tags |