KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > wizards > GenerateDiffFileOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui.wizards;
12
13
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.PrintStream JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
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 /**
40  * An operation to run the CVS diff operation on a set of resources. The result
41  * of the diff is written to a file. If there are no differences found, the
42  * user is notified and the output file is not created.
43  */

44 public class GenerateDiffFileOperation implements IRunnableWithProgress {
45
46     private File JavaDoc outputFile;
47     private IResource resource;
48     private Shell shell;
49     private LocalOption[] options;
50
51     GenerateDiffFileOperation(IResource resource, File JavaDoc file, LocalOption[] options, Shell shell) {
52         this.resource = resource;
53         this.outputFile = file;
54         this.shell = shell;
55         this.options = options;
56     }
57
58     /**
59      * @see IRunnableWithProgress#run(IProgressMonitor)
60      */

61     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
62         
63         final CVSTeamProvider provider = (CVSTeamProvider)RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId());
64         monitor.beginTask("", 500); //$NON-NLS-1$
65
monitor.setTaskName(CVSUIMessages.GenerateCVSDiff_working); //$NON-NLS-1$
66
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 JavaDoc(e);
74         } finally {
75             monitor.done();
76         }
77     }
78     
79     private void generateDiffToFile(IProgressMonitor monitor, CVSTeamProvider provider, File JavaDoc file) throws TeamException {
80         
81         final FileOutputStream JavaDoc os;
82         try {
83             os= new FileOutputStream JavaDoc(file);
84             try {
85                 provider.diff(resource, options, new PrintStream JavaDoc(os), new SubProgressMonitor(monitor, 500));
86             } finally {
87                 os.close();
88             }
89         } catch (FileNotFoundException JavaDoc e) {
90             throw new TeamException(CVSUIMessages.GenerateDiffFileOperation_0, e); //$NON-NLS-1$
91
} catch (IOException JavaDoc e) {
92             throw new TeamException(CVSUIMessages.GenerateDiffFileOperation_1, e); //$NON-NLS-1$
93
}
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 JavaDoc os = new ByteArrayOutputStream JavaDoc();
103         try {
104             try {
105                 provider.diff(resource, options, new PrintStream JavaDoc(os), new SubProgressMonitor(monitor, 500));
106             } finally {
107                 os.close();
108             }
109         } catch (IOException JavaDoc e) {
110             throw new TeamException(CVSUIMessages.GenerateDiffFileOperation_2, e); //$NON-NLS-1$
111
}
112         if (os.size() == 0) {
113             reportEmptyDiff();
114         } else {
115             copyToClipboard(os);
116         }
117     }
118  
119     private void copyToClipboard(final ByteArrayOutputStream JavaDoc baos) {
120         shell.getDisplay().syncExec(new Runnable JavaDoc() {
121             public void run() {
122                 TextTransfer plainTextTransfer = TextTransfer.getInstance();
123                 Clipboard clipboard = new Clipboard(shell.getDisplay());
124                 clipboard.setContents(
125                     new String JavaDoc[]{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, //$NON-NLS-1$
138
CVSUIMessages.GenerateCVSDiff_noDiffsFoundMsg); //$NON-NLS-1$
139
}
140         }, CVSUIPlugin.PERFORM_SYNC_EXEC);
141     }
142 }
143
Popular Tags