KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > operations > FileDiffOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.operations;
12
13 import java.io.*;
14 import java.nio.channels.FileChannel JavaDoc;
15
16 import org.eclipse.core.resources.mapping.ResourceMapping;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.team.internal.ccvs.core.CVSException;
19 import org.eclipse.team.internal.ccvs.core.CVSStatus;
20 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
21 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
22 import org.eclipse.team.internal.core.TeamPlugin;
23 import org.eclipse.ui.IWorkbenchPart;
24
25 public class FileDiffOperation extends DiffOperation {
26
27     FileOutputStream os;
28     PrintStream printStream;
29     File file;
30     File tempFile;
31     
32     public FileDiffOperation(IWorkbenchPart part, ResourceMapping[] mappings, LocalOption[] options, File file, boolean isMultiPatch, boolean includeFullPathInformation, IPath patchRoot) {
33         super(part, mappings, options, isMultiPatch, includeFullPathInformation, patchRoot, file.getAbsolutePath());
34         IPath teamLocation= TeamPlugin.getPlugin().getStateLocation();
35         IPath tempFilePath = teamLocation.append(new Path(IPath.SEPARATOR + "tempDiff" + System.currentTimeMillis())); //$NON-NLS-1$
36
tempFile = tempFilePath.toFile();
37         this.file = file;
38     }
39
40     public void execute(IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
41         super.execute(monitor);
42      
43         if (tempFile.length() == 0) {
44             tempFile.delete();
45             reportEmptyDiff();
46             return;
47         }
48         
49         if (this.isMultiPatch &&
50             (!patchHasContents && !patchHasNewFiles)){
51             tempFile.delete();
52             reportEmptyDiff();
53             return;
54         }
55         
56          copyFile();
57     }
58     
59     protected void copyFile() throws CVSException {
60         FileChannel JavaDoc tempFileChannel=null;
61         FileChannel JavaDoc fileChannel=null;
62         try {
63             tempFileChannel = new FileInputStream(tempFile).getChannel();
64             fileChannel = new FileOutputStream(file).getChannel();
65         
66             long size= tempFileChannel.size();
67             long bytesTransferred = fileChannel.transferFrom(tempFileChannel, 0, size);
68             while (bytesTransferred != size){
69                 //Transfer from point left off until the end of the file
70
bytesTransferred += fileChannel.transferFrom(tempFileChannel, bytesTransferred, size);
71             }
72         } catch (IOException e) {
73             throw CVSException.wrapException(e);
74         } finally {
75             if (tempFileChannel!=null)
76                 try {
77                     tempFileChannel.close();
78                 } catch (IOException e) {
79                     throw CVSException.wrapException(e);
80                 }
81             
82             if (fileChannel!=null)
83                 try {
84                     fileChannel.close();
85                 } catch (IOException e) {
86                     throw CVSException.wrapException(e);
87                 }
88                 
89             if (tempFile != null)
90                 tempFile.delete();
91         }
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.team.internal.ccvs.ui.operations.DiffOperation#openStream()
96      */

97     protected PrintStream openStream() throws CVSException {
98         try {
99             os = new FileOutputStream(tempFile);
100             return new PrintStream(os);
101         } catch (FileNotFoundException e) {
102             IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR, CVSUIMessages.GenerateDiffFileOperation_0, e);
103             throw new CVSException(status);
104         }
105     }
106
107 }
108
Popular Tags