KickJava   Java API By Example, From Geeks To Geeks.

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


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
15 import org.eclipse.compare.IEncodedStreamContentAccessor;
16 import org.eclipse.compare.ITypedElement;
17 import org.eclipse.compare.internal.*;
18 import org.eclipse.compare.patch.IHunk;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.jface.resource.LocalResourceManager;
22 import org.eclipse.swt.graphics.Image;
23
24 public class HunkTypedElement implements ITypedElement, IEncodedStreamContentAccessor, IAdaptable {
25
26     private final HunkResult fHunkResult;
27     private final boolean fIsAfterState;
28     private final boolean fFullContext;
29
30     public HunkTypedElement(HunkResult result, boolean isAfterState, boolean fullContext) {
31         this.fHunkResult = result;
32         this.fIsAfterState = isAfterState;
33         this.fFullContext = fullContext;
34     }
35
36     /* (non-Javadoc)
37      * @see org.eclipse.compare.ITypedElement#getImage()
38      */

39     public Image getImage() {
40         if (!fHunkResult.isOK()) {
41             LocalResourceManager imageCache = PatchCompareEditorInput.getImageCache(fHunkResult.getDiffResult().getConfiguration());
42             return getHunkErrorImage(null, imageCache, false);
43         }
44         return null;
45     }
46
47     public static Image getHunkErrorImage(Image baseImage, LocalResourceManager imageCache, boolean onLeft) {
48         ImageDescriptor desc = new DiffImage(baseImage, CompareUIPlugin.getImageDescriptor(ICompareUIConstants.ERROR_OVERLAY), ICompareUIConstants.COMPARE_IMAGE_WIDTH, onLeft);
49         Image image = imageCache.createImage(desc);
50         return image;
51     }
52
53     public boolean isManuallyMerged() {
54         return getPatcher().isManuallyMerged(getHunkResult().getHunk());
55     }
56
57     private Patcher getPatcher() {
58         return Patcher.getPatcher(fHunkResult.getDiffResult().getConfiguration());
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.compare.ITypedElement#getName()
63      */

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

71     public String JavaDoc getType() {
72         return fHunkResult.getDiffResult().getDiff().getTargetPath(fHunkResult.getDiffResult().getConfiguration()).getFileExtension();
73     }
74
75     /* (non-Javadoc)
76      * @see org.eclipse.compare.IStreamContentAccessor#getContents()
77      */

78     public InputStream getContents() throws CoreException {
79         String JavaDoc contents = fHunkResult.getContents(fIsAfterState, fFullContext);
80         return fHunkResult.asInputStream(contents);
81     }
82
83     public String JavaDoc getCharset() throws CoreException {
84         return fHunkResult.getCharset();
85     }
86
87     public HunkResult getHunkResult() {
88         return fHunkResult;
89     }
90
91     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
92         if (adapter == IHunk.class)
93             return fHunkResult;
94         return Platform.getAdapterManager().getAdapter(this, adapter);
95     }
96
97 }
98
Popular Tags