KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > patch > PatchFileTypedElement


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.compare.internal.patch;
12
13 import java.io.*;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.compare.*;
17 import org.eclipse.compare.internal.CompareUIPlugin;
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.resource.LocalResourceManager;
21 import org.eclipse.swt.graphics.Image;
22
23 public class PatchFileTypedElement implements ITypedElement, IEncodedStreamContentAccessor {
24
25     private final FileDiffResult result;
26     private final boolean isAfterState;
27
28     public PatchFileTypedElement(FileDiffResult result, boolean isAfterState) {
29         this.result = result;
30         this.isAfterState = isAfterState;
31     }
32
33     public Image getImage() {
34         IFile file = getPatcher().getTargetFile(result.getDiff());
35         if (file == null) {
36             // We don't get a target file if the file doesn't exist
37
DiffProject project = result.getDiff().getProject();
38             if (project != null) {
39                 file = project.getFile(result.getDiff().getPath(result.getConfiguration().isReversed()));
40             } else {
41                 IResource target = getPatcher().getTarget();
42                 if (target instanceof IFile) {
43                     file = (IFile) target;
44                 } else if (target instanceof IContainer) {
45                     IContainer container = (IContainer) target;
46                     file = container.getFile(result.getTargetPath());
47                 }
48             }
49         }
50         Image image = null;
51         if (file != null) {
52             image = CompareUI.getImage(file);
53         }
54         if (result.containsProblems()) {
55             LocalResourceManager imageCache = PatchCompareEditorInput.getImageCache(result.getConfiguration());
56             image = HunkTypedElement.getHunkErrorImage(image, imageCache, true);
57         }
58         return image;
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.compare.ITypedElement#getName()
63      */

64     public String JavaDoc getName() {
65         return result.getTargetPath().toString();
66     }
67
68     /* (non-Javadoc)
69      * @see org.eclipse.compare.ITypedElement#getType()
70      */

71     public String JavaDoc getType() {
72         return result.getTargetPath().getFileExtension();
73     }
74
75     public String JavaDoc getCharset() throws CoreException {
76         // TODO Auto-generated method stub
77
return null;
78     }
79
80     public InputStream getContents() throws CoreException {
81         // If there are cached contents, use them
82
if (isAfterState && getPatcher().hasCachedContents(result.getDiff()))
83             return new ByteArrayInputStream(getPatcher().getCachedContents(result.getDiff()));
84         // Otherwise, get the lines from the diff result
85
List JavaDoc lines;
86         if (isAfterState) {
87             lines = result.getAfterLines();
88         } else {
89             lines = result.getBeforeLines();
90         }
91         String JavaDoc contents = Patcher.createString(getPatcher().isPreserveLineDelimeters(), lines);
92         String JavaDoc charSet = getCharset();
93         byte[] bytes = null;
94         if (charSet != null) {
95             try {
96                 bytes = contents.getBytes(charSet);
97             } catch (UnsupportedEncodingException e) {
98                 CompareUIPlugin.log(e);
99             }
100         }
101         if (bytes == null) {
102             bytes = contents.getBytes();
103         }
104         return new ByteArrayInputStream(bytes);
105     }
106
107     private Patcher getPatcher() {
108         return Patcher.getPatcher(result.getConfiguration());
109     }
110
111 }
112
Popular Tags